thread.cpp revision 0bfe8809b4805696036c5801f06a652402088505
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
17thread::~thread()
18{
19    if (__t_ != 0)
20        terminate();
21}
22
23void
24thread::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
32void
33thread::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
46unsigned
47thread::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
62namespace this_thread
63{
64
65void
66sleep_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