2010-05-11 19:42:16 +00:00
|
|
|
//===------------------------- thread.cpp----------------------------------===//
|
|
|
|
//
|
2010-05-11 21:36:01 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "thread"
|
|
|
|
#include "exception"
|
2010-05-25 17:25:25 +00:00
|
|
|
#include <sys/types.h>
|
2010-05-11 19:42:16 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
thread::~thread()
|
|
|
|
{
|
2010-05-24 17:49:41 +00:00
|
|
|
if (__t_ != 0)
|
2010-05-11 19:42:16 +00:00
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
thread::join()
|
|
|
|
{
|
|
|
|
int ec = pthread_join(__t_, 0);
|
2010-08-11 17:04:31 +00:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-11 19:42:16 +00:00
|
|
|
if (ec)
|
|
|
|
throw system_error(error_code(ec, system_category()), "thread::join failed");
|
2010-08-22 00:03:27 +00:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2010-05-24 17:49:41 +00:00
|
|
|
__t_ = 0;
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
thread::detach()
|
|
|
|
{
|
|
|
|
int ec = EINVAL;
|
|
|
|
if (__t_ != 0)
|
|
|
|
{
|
|
|
|
ec = pthread_detach(__t_);
|
|
|
|
if (ec == 0)
|
|
|
|
__t_ = 0;
|
|
|
|
}
|
2010-08-11 17:04:31 +00:00
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
2010-05-11 19:42:16 +00:00
|
|
|
if (ec)
|
|
|
|
throw system_error(error_code(ec, system_category()), "thread::detach failed");
|
2010-08-22 00:03:27 +00:00
|
|
|
#endif // _LIBCPP_NO_EXCEPTIONS
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
thread::hardware_concurrency()
|
|
|
|
{
|
2010-05-24 17:49:41 +00:00
|
|
|
#if defined(CTL_HW) && defined(HW_NCPU)
|
2010-05-11 19:42:16 +00:00
|
|
|
int n;
|
|
|
|
int mib[2] = {CTL_HW, HW_NCPU};
|
|
|
|
std::size_t s = sizeof(n);
|
|
|
|
sysctl(mib, 2, &n, &s, 0, 0);
|
|
|
|
return n;
|
2010-08-22 00:03:27 +00:00
|
|
|
#else // defined(CTL_HW) && defined(HW_NCPU)
|
2010-05-24 17:49:41 +00:00
|
|
|
// TODO: grovel through /proc or check cpuid on x86 and similar
|
|
|
|
// instructions on other architectures.
|
|
|
|
return 0; // Means not computable [thread.thread.static]
|
2010-08-22 00:03:27 +00:00
|
|
|
#endif // defined(CTL_HW) && defined(HW_NCPU)
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace this_thread
|
|
|
|
{
|
|
|
|
|
|
|
|
void
|
|
|
|
sleep_for(const chrono::nanoseconds& ns)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
if (ns >= nanoseconds::zero())
|
|
|
|
{
|
|
|
|
timespec ts;
|
|
|
|
ts.tv_sec = static_cast<decltype(ts.tv_sec)>(duration_cast<seconds>(ns).count());
|
|
|
|
ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns - seconds(ts.tv_sec)).count());
|
|
|
|
nanosleep(&ts, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // this_thread
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|