xref: /openbsd-src/gnu/llvm/lldb/source/Host/posix/HostThreadPosix.cpp (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1dda28197Spatrick //===-- HostThreadPosix.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Host/posix/HostThreadPosix.h"
10061da546Spatrick #include "lldb/Utility/Status.h"
11061da546Spatrick 
12*be691f3bSpatrick #include <cerrno>
13061da546Spatrick #include <pthread.h>
14061da546Spatrick 
15061da546Spatrick using namespace lldb;
16061da546Spatrick using namespace lldb_private;
17061da546Spatrick 
18*be691f3bSpatrick HostThreadPosix::HostThreadPosix() = default;
19061da546Spatrick 
HostThreadPosix(lldb::thread_t thread)20061da546Spatrick HostThreadPosix::HostThreadPosix(lldb::thread_t thread)
21061da546Spatrick     : HostNativeThreadBase(thread) {}
22061da546Spatrick 
23*be691f3bSpatrick HostThreadPosix::~HostThreadPosix() = default;
24061da546Spatrick 
Join(lldb::thread_result_t * result)25061da546Spatrick Status HostThreadPosix::Join(lldb::thread_result_t *result) {
26061da546Spatrick   Status error;
27061da546Spatrick   if (IsJoinable()) {
28061da546Spatrick     int err = ::pthread_join(m_thread, result);
29061da546Spatrick     error.SetError(err, lldb::eErrorTypePOSIX);
30061da546Spatrick   } else {
31061da546Spatrick     if (result)
32061da546Spatrick       *result = nullptr;
33061da546Spatrick     error.SetError(EINVAL, eErrorTypePOSIX);
34061da546Spatrick   }
35061da546Spatrick 
36061da546Spatrick   Reset();
37061da546Spatrick   return error;
38061da546Spatrick }
39061da546Spatrick 
Cancel()40061da546Spatrick Status HostThreadPosix::Cancel() {
41061da546Spatrick   Status error;
42061da546Spatrick   if (IsJoinable()) {
43061da546Spatrick #ifndef __FreeBSD__
44061da546Spatrick     llvm_unreachable("someone is calling HostThread::Cancel()");
45061da546Spatrick #else
46061da546Spatrick     int err = ::pthread_cancel(m_thread);
47061da546Spatrick     error.SetError(err, eErrorTypePOSIX);
48061da546Spatrick #endif
49061da546Spatrick   }
50061da546Spatrick   return error;
51061da546Spatrick }
52061da546Spatrick 
Detach()53061da546Spatrick Status HostThreadPosix::Detach() {
54061da546Spatrick   Status error;
55061da546Spatrick   if (IsJoinable()) {
56061da546Spatrick     int err = ::pthread_detach(m_thread);
57061da546Spatrick     error.SetError(err, eErrorTypePOSIX);
58061da546Spatrick   }
59061da546Spatrick   Reset();
60061da546Spatrick   return error;
61061da546Spatrick }
62