Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- thread.cpp----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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/sysctl.h> |
| 13 | |
| 14 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 15 | |
| 16 | thread::~thread() |
| 17 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 18 | if (__t_ != 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | terminate(); |
| 20 | } |
| 21 | |
| 22 | void |
| 23 | thread::join() |
| 24 | { |
| 25 | int ec = pthread_join(__t_, 0); |
| 26 | if (ec) |
| 27 | throw system_error(error_code(ec, system_category()), "thread::join failed"); |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 28 | __t_ = 0; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
| 32 | thread::detach() |
| 33 | { |
| 34 | int ec = EINVAL; |
| 35 | if (__t_ != 0) |
| 36 | { |
| 37 | ec = pthread_detach(__t_); |
| 38 | if (ec == 0) |
| 39 | __t_ = 0; |
| 40 | } |
| 41 | if (ec) |
| 42 | throw system_error(error_code(ec, system_category()), "thread::detach failed"); |
| 43 | } |
| 44 | |
| 45 | unsigned |
| 46 | thread::hardware_concurrency() |
| 47 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 48 | #if defined(CTL_HW) && defined(HW_NCPU) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | int n; |
| 50 | int mib[2] = {CTL_HW, HW_NCPU}; |
| 51 | std::size_t s = sizeof(n); |
| 52 | sysctl(mib, 2, &n, &s, 0, 0); |
| 53 | return n; |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame^] | 54 | #else // !defined(CTL_HW && HW_NCPU) |
| 55 | // TODO: grovel through /proc or check cpuid on x86 and similar |
| 56 | // instructions on other architectures. |
| 57 | return 0; // Means not computable [thread.thread.static] |
| 58 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | namespace this_thread |
| 62 | { |
| 63 | |
| 64 | void |
| 65 | sleep_for(const chrono::nanoseconds& ns) |
| 66 | { |
| 67 | using namespace chrono; |
| 68 | if (ns >= nanoseconds::zero()) |
| 69 | { |
| 70 | timespec ts; |
| 71 | ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count()); |
| 72 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count()); |
| 73 | nanosleep(&ts, 0); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } // this_thread |
| 78 | |
| 79 | _LIBCPP_END_NAMESPACE_STD |