xref: /freebsd-src/contrib/llvm-project/lldb/source/Host/posix/HostThreadPosix.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
15ffd83dbSDimitry Andric //===-- HostThreadPosix.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Host/posix/HostThreadPosix.h"
100b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
110b57cec5SDimitry Andric 
12*fe6060f1SDimitry Andric #include <cerrno>
130b57cec5SDimitry Andric #include <pthread.h>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric using namespace lldb;
160b57cec5SDimitry Andric using namespace lldb_private;
170b57cec5SDimitry Andric 
18*fe6060f1SDimitry Andric HostThreadPosix::HostThreadPosix() = default;
190b57cec5SDimitry Andric 
HostThreadPosix(lldb::thread_t thread)200b57cec5SDimitry Andric HostThreadPosix::HostThreadPosix(lldb::thread_t thread)
210b57cec5SDimitry Andric     : HostNativeThreadBase(thread) {}
220b57cec5SDimitry Andric 
23*fe6060f1SDimitry Andric HostThreadPosix::~HostThreadPosix() = default;
240b57cec5SDimitry Andric 
Join(lldb::thread_result_t * result)250b57cec5SDimitry Andric Status HostThreadPosix::Join(lldb::thread_result_t *result) {
260b57cec5SDimitry Andric   Status error;
270b57cec5SDimitry Andric   if (IsJoinable()) {
280b57cec5SDimitry Andric     int err = ::pthread_join(m_thread, result);
290b57cec5SDimitry Andric     error.SetError(err, lldb::eErrorTypePOSIX);
300b57cec5SDimitry Andric   } else {
310b57cec5SDimitry Andric     if (result)
320b57cec5SDimitry Andric       *result = nullptr;
330b57cec5SDimitry Andric     error.SetError(EINVAL, eErrorTypePOSIX);
340b57cec5SDimitry Andric   }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   Reset();
370b57cec5SDimitry Andric   return error;
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
Cancel()400b57cec5SDimitry Andric Status HostThreadPosix::Cancel() {
410b57cec5SDimitry Andric   Status error;
420b57cec5SDimitry Andric   if (IsJoinable()) {
430b57cec5SDimitry Andric #ifndef __FreeBSD__
440b57cec5SDimitry Andric     llvm_unreachable("someone is calling HostThread::Cancel()");
450b57cec5SDimitry Andric #else
460b57cec5SDimitry Andric     int err = ::pthread_cancel(m_thread);
470b57cec5SDimitry Andric     error.SetError(err, eErrorTypePOSIX);
480b57cec5SDimitry Andric #endif
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric   return error;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
Detach()530b57cec5SDimitry Andric Status HostThreadPosix::Detach() {
540b57cec5SDimitry Andric   Status error;
550b57cec5SDimitry Andric   if (IsJoinable()) {
560b57cec5SDimitry Andric     int err = ::pthread_detach(m_thread);
570b57cec5SDimitry Andric     error.SetError(err, eErrorTypePOSIX);
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric   Reset();
600b57cec5SDimitry Andric   return error;
610b57cec5SDimitry Andric }
62