2010-05-11 21:42:16 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===--------------------------- thread -----------------------------------===//
|
|
|
|
//
|
2010-05-11 23:36:01 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-11 21:42:16 +02:00
|
|
|
//
|
2010-11-16 23:09:02 +01:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-11 21:42:16 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_THREAD
|
|
|
|
#define _LIBCPP_THREAD
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
thread synopsis
|
|
|
|
|
2010-08-21 23:01:59 +02:00
|
|
|
#define __STDCPP_THREADS__ __cplusplus
|
2010-05-11 21:42:16 +02:00
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class id;
|
|
|
|
typedef pthread_t native_handle_type;
|
|
|
|
|
|
|
|
thread();
|
|
|
|
template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
|
|
|
|
~thread();
|
|
|
|
|
|
|
|
thread(const thread&) = delete;
|
|
|
|
thread(thread&& t);
|
|
|
|
|
|
|
|
thread& operator=(const thread&) = delete;
|
|
|
|
thread& operator=(thread&& t);
|
|
|
|
|
|
|
|
void swap(thread& t);
|
|
|
|
|
|
|
|
bool joinable() const;
|
|
|
|
void join();
|
|
|
|
void detach();
|
|
|
|
id get_id() const;
|
|
|
|
native_handle_type native_handle();
|
|
|
|
|
|
|
|
static unsigned hardware_concurrency();
|
|
|
|
};
|
|
|
|
|
|
|
|
void swap(thread& x, thread& y);
|
|
|
|
|
|
|
|
class thread::id
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
id();
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(thread::id x, thread::id y);
|
|
|
|
bool operator!=(thread::id x, thread::id y);
|
|
|
|
bool operator< (thread::id x, thread::id y);
|
|
|
|
bool operator<=(thread::id x, thread::id y);
|
|
|
|
bool operator> (thread::id x, thread::id y);
|
|
|
|
bool operator>=(thread::id x, thread::id y);
|
|
|
|
|
|
|
|
template<class charT, class traits>
|
|
|
|
basic_ostream<charT, traits>&
|
|
|
|
operator<<(basic_ostream<charT, traits>& out, thread::id id);
|
|
|
|
|
|
|
|
namespace this_thread
|
|
|
|
{
|
|
|
|
|
|
|
|
thread::id get_id();
|
|
|
|
|
|
|
|
void yield();
|
|
|
|
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
|
|
|
|
|
|
|
|
template <class Rep, class Period>
|
|
|
|
void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
|
|
|
|
|
|
|
} // this_thread
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <__config>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <__functional_base>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <system_error>
|
|
|
|
#include <chrono>
|
|
|
|
#include <__mutex_base>
|
2011-05-16 20:40:35 +02:00
|
|
|
#ifndef _LIBCPP_HAS_NO_VARIADICS
|
|
|
|
#include <tuple>
|
|
|
|
#endif
|
2010-05-11 21:42:16 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2011-10-17 22:05:10 +02:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2010-05-11 21:42:16 +02:00
|
|
|
#pragma GCC system_header
|
2011-10-17 22:05:10 +02:00
|
|
|
#endif
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-08-21 23:01:59 +02:00
|
|
|
#define __STDCPP_THREADS__ __cplusplus
|
2010-05-11 21:42:16 +02:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2010-08-27 22:10:19 +02:00
|
|
|
template <class _Tp>
|
|
|
|
class __thread_specific_ptr
|
|
|
|
{
|
|
|
|
pthread_key_t __key_;
|
|
|
|
|
|
|
|
__thread_specific_ptr(const __thread_specific_ptr&);
|
|
|
|
__thread_specific_ptr& operator=(const __thread_specific_ptr&);
|
|
|
|
|
|
|
|
static void __at_thread_exit(void*);
|
|
|
|
public:
|
|
|
|
typedef _Tp* pointer;
|
|
|
|
|
|
|
|
__thread_specific_ptr();
|
|
|
|
~__thread_specific_ptr();
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-08-27 22:10:19 +02:00
|
|
|
pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-08-27 22:10:19 +02:00
|
|
|
pointer operator*() const {return *get();}
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-08-27 22:10:19 +02:00
|
|
|
pointer operator->() const {return get();}
|
|
|
|
pointer release();
|
|
|
|
void reset(pointer __p = nullptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
void
|
|
|
|
__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
|
|
|
|
{
|
2011-10-17 22:08:59 +02:00
|
|
|
delete static_cast<pointer>(__p);
|
2010-08-27 22:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
__thread_specific_ptr<_Tp>::__thread_specific_ptr()
|
|
|
|
{
|
|
|
|
int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
|
2011-10-17 22:08:59 +02:00
|
|
|
if (__ec)
|
|
|
|
throw system_error(error_code(__ec, system_category()),
|
|
|
|
"__thread_specific_ptr construction failed");
|
2010-08-27 22:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
|
|
|
|
{
|
2011-10-17 22:08:59 +02:00
|
|
|
pthread_key_delete(__key_);
|
2010-08-27 22:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
typename __thread_specific_ptr<_Tp>::pointer
|
|
|
|
__thread_specific_ptr<_Tp>::release()
|
|
|
|
{
|
2011-10-17 22:08:59 +02:00
|
|
|
pointer __p = get();
|
|
|
|
pthread_setspecific(__key_, 0);
|
|
|
|
return __p;
|
2010-08-27 22:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
void
|
|
|
|
__thread_specific_ptr<_Tp>::reset(pointer __p)
|
|
|
|
{
|
2011-10-17 22:08:59 +02:00
|
|
|
pointer __p_old = get();
|
|
|
|
pthread_setspecific(__key_, __p);
|
|
|
|
delete __p_old;
|
2010-08-27 22:10:19 +02:00
|
|
|
}
|
|
|
|
|
2010-05-11 21:42:16 +02:00
|
|
|
class thread;
|
|
|
|
class __thread_id;
|
|
|
|
|
|
|
|
namespace this_thread
|
|
|
|
{
|
|
|
|
|
|
|
|
__thread_id get_id();
|
|
|
|
|
|
|
|
} // this_thread
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
class _LIBCPP_VISIBLE __thread_id
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
2010-05-24 19:49:41 +02:00
|
|
|
// FIXME: pthread_t is a pointer on Darwin but a long on Linux.
|
|
|
|
// NULL is the no-thread value on Darwin. Someone needs to check
|
|
|
|
// on other platforms. We assume 0 works everywhere for now.
|
2010-05-11 21:42:16 +02:00
|
|
|
pthread_t __id_;
|
|
|
|
|
|
|
|
public:
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
__thread_id() : __id_(0) {}
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator==(__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return __x.__id_ == __y.__id_;}
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator!=(__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return !(__x == __y);}
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator< (__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return __x.__id_ < __y.__id_;}
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator<=(__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return !(__y < __x);}
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator> (__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return __y < __x ;}
|
2010-09-23 19:31:07 +02:00
|
|
|
friend _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator>=(__thread_id __x, __thread_id __y)
|
2010-05-11 21:42:16 +02:00
|
|
|
{return !(__x < __y);}
|
|
|
|
|
|
|
|
template<class _CharT, class _Traits>
|
|
|
|
friend
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
basic_ostream<_CharT, _Traits>&
|
|
|
|
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
|
|
|
|
{return __os << __id.__id_;}
|
|
|
|
|
|
|
|
private:
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
__thread_id(pthread_t __id) : __id_(__id) {}
|
|
|
|
|
|
|
|
friend __thread_id this_thread::get_id();
|
2010-09-23 19:31:07 +02:00
|
|
|
friend class _LIBCPP_VISIBLE thread;
|
2010-05-11 21:42:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class _Tp> struct hash;
|
|
|
|
|
|
|
|
template<>
|
2010-09-23 19:31:07 +02:00
|
|
|
struct _LIBCPP_VISIBLE hash<__thread_id>
|
2010-05-11 21:42:16 +02:00
|
|
|
: public unary_function<__thread_id, size_t>
|
|
|
|
{
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
size_t operator()(__thread_id __v) const
|
|
|
|
{
|
|
|
|
const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
|
|
|
|
return *__p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace this_thread
|
|
|
|
{
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
__thread_id
|
|
|
|
get_id()
|
|
|
|
{
|
|
|
|
return pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // this_thread
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
class _LIBCPP_VISIBLE thread
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
pthread_t __t_;
|
|
|
|
|
2010-08-10 22:48:29 +02:00
|
|
|
thread(const thread&);
|
|
|
|
thread& operator=(const thread&);
|
2010-05-11 21:42:16 +02:00
|
|
|
public:
|
|
|
|
typedef __thread_id id;
|
|
|
|
typedef pthread_t native_handle_type;
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
thread() : __t_(0) {}
|
|
|
|
#ifndef _LIBCPP_HAS_NO_VARIADICS
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp, class ..._Args,
|
2010-05-11 21:42:16 +02:00
|
|
|
class = typename enable_if
|
|
|
|
<
|
2011-11-29 19:15:50 +01:00
|
|
|
!is_same<typename decay<_Fp>::type, thread>::value
|
2010-05-11 21:42:16 +02:00
|
|
|
>::type
|
|
|
|
>
|
2011-11-29 19:15:50 +01:00
|
|
|
explicit thread(_Fp&& __f, _Args&&... __args);
|
2010-08-22 02:02:43 +02:00
|
|
|
#else // _LIBCPP_HAS_NO_VARIADICS
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp> explicit thread(_Fp __f);
|
2010-05-11 21:42:16 +02:00
|
|
|
#endif
|
|
|
|
~thread();
|
|
|
|
|
2010-09-05 01:28:19 +02:00
|
|
|
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
|
|
|
|
thread& operator=(thread&& __t);
|
2010-09-05 01:28:19 +02:00
|
|
|
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2011-06-30 23:18:19 +02:00
|
|
|
void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);}
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-24 19:49:41 +02:00
|
|
|
bool joinable() const {return __t_ != 0;}
|
2010-05-11 21:42:16 +02:00
|
|
|
void join();
|
|
|
|
void detach();
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
id get_id() const {return __t_;}
|
2010-09-23 19:31:07 +02:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
native_handle_type native_handle() {return __t_;}
|
|
|
|
|
|
|
|
static unsigned hardware_concurrency();
|
|
|
|
};
|
|
|
|
|
2010-08-27 22:10:19 +02:00
|
|
|
class __assoc_sub_state;
|
|
|
|
|
2010-12-17 15:46:43 +01:00
|
|
|
class _LIBCPP_HIDDEN __thread_struct_imp;
|
2010-08-27 22:10:19 +02:00
|
|
|
|
|
|
|
class __thread_struct
|
|
|
|
{
|
|
|
|
__thread_struct_imp* __p_;
|
|
|
|
|
|
|
|
__thread_struct(const __thread_struct&);
|
|
|
|
__thread_struct& operator=(const __thread_struct&);
|
|
|
|
public:
|
|
|
|
__thread_struct();
|
|
|
|
~__thread_struct();
|
|
|
|
|
2010-09-03 23:46:37 +02:00
|
|
|
void notify_all_at_thread_exit(condition_variable*, mutex*);
|
2010-08-27 22:10:19 +02:00
|
|
|
void __make_ready_at_thread_exit(__assoc_sub_state*);
|
|
|
|
};
|
|
|
|
|
2010-10-14 21:18:04 +02:00
|
|
|
__thread_specific_ptr<__thread_struct>& __thread_local_data();
|
2010-08-27 22:10:19 +02:00
|
|
|
|
2011-05-16 20:40:35 +02:00
|
|
|
#ifndef _LIBCPP_HAS_NO_VARIADICS
|
|
|
|
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp, class ..._Args, size_t ..._Indices>
|
2011-05-16 20:40:35 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void
|
2011-11-29 19:15:50 +01:00
|
|
|
__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
|
2011-05-16 20:40:35 +02:00
|
|
|
{
|
2011-06-30 23:18:19 +02:00
|
|
|
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
|
2011-05-16 20:40:35 +02:00
|
|
|
}
|
|
|
|
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp>
|
2010-05-11 21:42:16 +02:00
|
|
|
void*
|
|
|
|
__thread_proxy(void* __vp)
|
|
|
|
{
|
2010-10-14 21:18:04 +02:00
|
|
|
__thread_local_data().reset(new __thread_struct);
|
2011-11-29 19:15:50 +01:00
|
|
|
std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
|
|
|
|
typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
|
2011-05-16 20:40:35 +02:00
|
|
|
__threaad_execute(*__p, _Index());
|
2010-05-11 21:42:16 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp, class ..._Args,
|
2010-05-11 21:42:16 +02:00
|
|
|
class
|
|
|
|
>
|
2011-11-29 19:15:50 +01:00
|
|
|
thread::thread(_Fp&& __f, _Args&&... __args)
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
2011-11-29 19:15:50 +01:00
|
|
|
typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
|
|
|
|
_VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
|
2011-06-30 23:18:19 +02:00
|
|
|
__decay_copy(_VSTD::forward<_Args>(__args))...));
|
2011-11-29 19:15:50 +01:00
|
|
|
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
|
2010-05-11 21:42:16 +02:00
|
|
|
if (__ec == 0)
|
|
|
|
__p.release();
|
|
|
|
else
|
|
|
|
__throw_system_error(__ec, "thread constructor failed");
|
|
|
|
}
|
|
|
|
|
2010-08-22 02:02:43 +02:00
|
|
|
#else // _LIBCPP_HAS_NO_VARIADICS
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp>
|
2011-05-16 20:40:35 +02:00
|
|
|
void*
|
|
|
|
__thread_proxy(void* __vp)
|
|
|
|
{
|
|
|
|
__thread_local_data().reset(new __thread_struct);
|
2011-11-29 19:15:50 +01:00
|
|
|
std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
|
2011-05-16 20:40:35 +02:00
|
|
|
(*__p)();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2011-11-29 19:15:50 +01:00
|
|
|
template <class _Fp>
|
|
|
|
thread::thread(_Fp __f)
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
2011-11-29 19:15:50 +01:00
|
|
|
std::unique_ptr<_Fp> __p(new _Fp(__f));
|
|
|
|
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
|
2010-05-11 21:42:16 +02:00
|
|
|
if (__ec == 0)
|
|
|
|
__p.release();
|
|
|
|
else
|
|
|
|
__throw_system_error(__ec, "thread constructor failed");
|
|
|
|
}
|
|
|
|
|
2010-08-22 02:02:43 +02:00
|
|
|
#endif // _LIBCPP_HAS_NO_VARIADICS
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-09-05 01:28:19 +02:00
|
|
|
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
thread&
|
|
|
|
thread::operator=(thread&& __t)
|
|
|
|
{
|
2010-06-02 20:20:39 +02:00
|
|
|
if (__t_ != 0)
|
2010-05-11 21:42:16 +02:00
|
|
|
terminate();
|
|
|
|
__t_ = __t.__t_;
|
2010-06-02 20:20:39 +02:00
|
|
|
__t.__t_ = 0;
|
2010-05-11 21:42:16 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-09-05 01:28:19 +02:00
|
|
|
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
void swap(thread& __x, thread& __y) {__x.swap(__y);}
|
|
|
|
|
|
|
|
namespace this_thread
|
|
|
|
{
|
|
|
|
|
|
|
|
void sleep_for(const chrono::nanoseconds& ns);
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
void
|
|
|
|
sleep_for(const chrono::duration<_Rep, _Period>& __d)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
nanoseconds __ns = duration_cast<nanoseconds>(__d);
|
|
|
|
if (__ns < __d)
|
|
|
|
++__ns;
|
|
|
|
sleep_for(__ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
void
|
|
|
|
sleep_until(const chrono::time_point<_Clock, _Duration>& __t)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
mutex __mut;
|
|
|
|
condition_variable __cv;
|
|
|
|
unique_lock<mutex> __lk(__mut);
|
|
|
|
while (_Clock::now() < __t)
|
|
|
|
__cv.wait_until(__lk, __t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Duration>
|
2010-09-23 19:31:07 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
void
|
2010-11-20 20:16:30 +01:00
|
|
|
sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
using namespace chrono;
|
2010-11-20 20:16:30 +01:00
|
|
|
sleep_for(__t - steady_clock::now());
|
2010-05-11 21:42:16 +02:00
|
|
|
}
|
|
|
|
|
2010-09-23 19:31:07 +02:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-11 21:42:16 +02:00
|
|
|
void yield() {sched_yield();}
|
|
|
|
|
|
|
|
} // this_thread
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP_THREAD
|