1 //===------------------------- thread.cpp----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "thread" 11 #include "exception" 12 #include <sys/types.h> 13 #include <sys/sysctl.h> 14 15 _LIBCPP_BEGIN_NAMESPACE_STD 16 17 thread::~thread() 18 { 19 if (__t_ != 0) 20 terminate(); 21 } 22 23 void 24 thread::join() 25 { 26 int ec = pthread_join(__t_, 0); 27 if (ec) 28 throw system_error(error_code(ec, system_category()), "thread::join failed"); 29 __t_ = 0; 30 } 31 32 void 33 thread::detach() 34 { 35 int ec = EINVAL; 36 if (__t_ != 0) 37 { 38 ec = pthread_detach(__t_); 39 if (ec == 0) 40 __t_ = 0; 41 } 42 if (ec) 43 throw system_error(error_code(ec, system_category()), "thread::detach failed"); 44 } 45 46 unsigned 47 thread::hardware_concurrency() 48 { 49 #if defined(CTL_HW) && defined(HW_NCPU) 50 int n; 51 int mib[2] = {CTL_HW, HW_NCPU}; 52 std::size_t s = sizeof(n); 53 sysctl(mib, 2, &n, &s, 0, 0); 54 return n; 55 #else // !defined(CTL_HW && HW_NCPU) 56 // TODO: grovel through /proc or check cpuid on x86 and similar 57 // instructions on other architectures. 58 return 0; // Means not computable [thread.thread.static] 59 #endif 60 } 61 62 namespace this_thread 63 { 64 65 void 66 sleep_for(const chrono::nanoseconds& ns) 67 { 68 using namespace chrono; 69 if (ns >= nanoseconds::zero()) 70 { 71 timespec ts; 72 ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count()); 73 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count()); 74 nanosleep(&ts, 0); 75 } 76 } 77 78 } // this_thread 79 80 _LIBCPP_END_NAMESPACE_STD 81