head	1.4;
access;
symbols
	RELENG_9_1_0_RELEASE:1.2.2.2
	RELENG_9_1:1.2.2.2.0.2
	RELENG_9_1_BP:1.2.2.2
	RELENG_9:1.2.0.2;
locks; strict;
comment	@# @;


1.4
date	2013.04.28.00.41.54;	author svnexp;	state Exp;
branches;
next	1.3;

1.3
date	2012.10.22.18.25.04;	author dim;	state Exp;
branches;
next	1.2;

1.2
date	2012.03.14.00.09.36;	author theraven;	state Exp;
branches
	1.2.2.1;
next	1.1;

1.1
date	2011.11.25.20.59.04;	author theraven;	state Exp;
branches;
next	;

1.2.2.1
date	2012.05.22.18.30.14;	author theraven;	state dead;
branches;
next	1.2.2.2;

1.2.2.2
date	2012.05.22.18.30.14;	author theraven;	state Exp;
branches;
next	1.2.2.3;

1.2.2.3
date	2012.11.21.18.41.41;	author svnexp;	state Exp;
branches;
next	1.2.2.4;

1.2.2.4
date	2013.05.11.17.01.44;	author svnexp;	state Exp;
branches;
next	1.2.2.5;

1.2.2.5
date	2014.03.05.20.01.45;	author svnexp;	state Exp;
branches;
next	;


desc
@@


1.4
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/249998
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@// -*- C++ -*-
//===--------------------------- thread -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_THREAD
#define _LIBCPP_THREAD

/*

    thread synopsis

#define __STDCPP_THREADS__ __cplusplus

namespace std
{

class thread
{
public:
    class id;
    typedef pthread_t native_handle_type;

    thread() noexcept;
    template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
    ~thread();

    thread(const thread&) = delete;
    thread(thread&& t) noexcept;

    thread& operator=(const thread&) = delete;
    thread& operator=(thread&& t) noexcept;

    void swap(thread& t) noexcept;

    bool joinable() const noexcept;
    void join();
    void detach();
    id get_id() const noexcept;
    native_handle_type native_handle();

    static unsigned hardware_concurrency() noexcept;
};

void swap(thread& x, thread& y) noexcept;

class thread::id
{
public:
    id() noexcept;
};

bool operator==(thread::id x, thread::id y) noexcept;
bool operator!=(thread::id x, thread::id y) noexcept;
bool operator< (thread::id x, thread::id y) noexcept;
bool operator<=(thread::id x, thread::id y) noexcept;
bool operator> (thread::id x, thread::id y) noexcept;
bool operator>=(thread::id x, thread::id y) noexcept;

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() noexcept;

void yield() noexcept;

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>
#ifndef _LIBCPP_HAS_NO_VARIADICS
#include <tuple>
#endif
#include <pthread.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

#define __STDCPP_THREADS__ __cplusplus

_LIBCPP_BEGIN_NAMESPACE_STD

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();

    _LIBCPP_INLINE_VISIBILITY
    pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
    _LIBCPP_INLINE_VISIBILITY
    pointer operator*() const {return *get();}
    _LIBCPP_INLINE_VISIBILITY
    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)
{
    delete static_cast<pointer>(__p);
}

template <class _Tp>
__thread_specific_ptr<_Tp>::__thread_specific_ptr()
{
    int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__ec)
        throw system_error(error_code(__ec, system_category()),
                           "__thread_specific_ptr construction failed");
#endif
}

template <class _Tp>
__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
{
    pthread_key_delete(__key_);
}

template <class _Tp>
typename __thread_specific_ptr<_Tp>::pointer
__thread_specific_ptr<_Tp>::release()
{
    pointer __p = get();
    pthread_setspecific(__key_, 0);
    return __p;
}

template <class _Tp>
void
__thread_specific_ptr<_Tp>::reset(pointer __p)
{
    pointer __p_old = get();
    pthread_setspecific(__key_, __p);
    delete __p_old;
}

class _LIBCPP_TYPE_VIS thread;
class _LIBCPP_TYPE_VIS __thread_id;

namespace this_thread
{

_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;

}  // this_thread

class _LIBCPP_TYPE_VIS __thread_id;
template<> struct _LIBCPP_TYPE_VIS hash<__thread_id>;

class _LIBCPP_TYPE_VIS __thread_id
{
    // 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.
    pthread_t __id_;

public:
    _LIBCPP_INLINE_VISIBILITY
    __thread_id() _NOEXCEPT : __id_(0) {}

    friend _LIBCPP_INLINE_VISIBILITY
        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
        {return __x.__id_ == __y.__id_;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
        {return !(__x == __y);}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
        {return __x.__id_ < __y.__id_;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
        {return !(__y < __x);}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
        {return   __y < __x ;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
        {return !(__x < __y);}

    template<class _CharT, class _Traits>
    friend
    _LIBCPP_INLINE_VISIBILITY
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
        {return __os << __id.__id_;}

private:
    _LIBCPP_INLINE_VISIBILITY
    __thread_id(pthread_t __id) : __id_(__id) {}

    friend __thread_id this_thread::get_id() _NOEXCEPT;
    friend class _LIBCPP_TYPE_VIS thread;
    friend struct _LIBCPP_TYPE_VIS hash<__thread_id>;
};

template<>
struct _LIBCPP_TYPE_VIS hash<__thread_id>
    : public unary_function<__thread_id, size_t>
{
    _LIBCPP_INLINE_VISIBILITY
    size_t operator()(__thread_id __v) const
    {
        return hash<pthread_t>()(__v.__id_);
    }
};

namespace this_thread
{

inline _LIBCPP_INLINE_VISIBILITY
__thread_id
get_id() _NOEXCEPT
{
    return pthread_self();
}

}  // this_thread

class _LIBCPP_TYPE_VIS thread
{
    pthread_t __t_;

    thread(const thread&);
    thread& operator=(const thread&);
public:
    typedef __thread_id id;
    typedef pthread_t native_handle_type;

    _LIBCPP_INLINE_VISIBILITY
    thread() _NOEXCEPT : __t_(0) {}
#ifndef _LIBCPP_HAS_NO_VARIADICS
    template <class _Fp, class ..._Args,
              class = typename enable_if
              <
                   !is_same<typename decay<_Fp>::type, thread>::value
              >::type
             >
        explicit thread(_Fp&& __f, _Args&&... __args);
#else  // _LIBCPP_HAS_NO_VARIADICS
    template <class _Fp> explicit thread(_Fp __f);
#endif
    ~thread();

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
    thread& operator=(thread&& __t) _NOEXCEPT;
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    _LIBCPP_INLINE_VISIBILITY
    void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}

    _LIBCPP_INLINE_VISIBILITY
    bool joinable() const _NOEXCEPT {return __t_ != 0;}
    void join();
    void detach();
    _LIBCPP_INLINE_VISIBILITY
    id get_id() const _NOEXCEPT {return __t_;}
    _LIBCPP_INLINE_VISIBILITY
    native_handle_type native_handle() _NOEXCEPT {return __t_;}

    static unsigned hardware_concurrency() _NOEXCEPT;
};

class __assoc_sub_state;

class _LIBCPP_HIDDEN __thread_struct_imp;

class __thread_struct
{
    __thread_struct_imp* __p_;

    __thread_struct(const __thread_struct&);
    __thread_struct& operator=(const __thread_struct&);
public:
    __thread_struct();
    ~__thread_struct();

    void notify_all_at_thread_exit(condition_variable*, mutex*);
    void __make_ready_at_thread_exit(__assoc_sub_state*);
};

__thread_specific_ptr<__thread_struct>& __thread_local_data();

#ifndef _LIBCPP_HAS_NO_VARIADICS

template <class _Fp, class ..._Args, size_t ..._Indices>
inline _LIBCPP_INLINE_VISIBILITY
void
__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

template <class _Fp>
void*
__thread_proxy(void* __vp)
{
    __thread_local_data().reset(new __thread_struct);
    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
    __thread_execute(*__p, _Index());
    return nullptr;
}

template <class _Fp, class ..._Args,
          class
         >
thread::thread(_Fp&& __f, _Args&&... __args)
{
    typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
    _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                __decay_copy(_VSTD::forward<_Args>(__args))...));
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
    if (__ec == 0)
        __p.release();
    else
        __throw_system_error(__ec, "thread constructor failed");
}

#else  // _LIBCPP_HAS_NO_VARIADICS

template <class _Fp>
void*
__thread_proxy(void* __vp)
{
    __thread_local_data().reset(new __thread_struct);
    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    (*__p)();
    return nullptr;
}

template <class _Fp>
thread::thread(_Fp __f)
{
    std::unique_ptr<_Fp> __p(new _Fp(__f));
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
    if (__ec == 0)
        __p.release();
    else
        __throw_system_error(__ec, "thread constructor failed");
}

#endif  // _LIBCPP_HAS_NO_VARIADICS

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
thread&
thread::operator=(thread&& __t) _NOEXCEPT
{
    if (__t_ != 0)
        terminate();
    __t_ = __t.__t_;
    __t.__t_ = 0;
    return *this;
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
void swap(thread& __x, thread& __y) _NOEXCEPT {__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;
    if (__d > duration<_Rep, _Period>::zero())
    {
        _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
        nanoseconds __ns;
        if (__d < _Max)
        {
            __ns = duration_cast<nanoseconds>(__d);
            if (__ns < __d)
                ++__ns;
        }
        else
            __ns = nanoseconds::max();
        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>
inline _LIBCPP_INLINE_VISIBILITY
void
sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
{
    using namespace chrono;
    sleep_for(__t - steady_clock::now());
}

inline _LIBCPP_INLINE_VISIBILITY
void yield() _NOEXCEPT {sched_yield();}

}  // this_thread

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_THREAD
@


1.3
log
@SVN rev 241903 on 2012-10-22 18:25:04Z by dim

Import libc++ trunk r165949.  Among other improvements and bug fixes,
this has many visibility problems fixed, which should help with
compiling certain ports that exercise C++11 mode (i.e. Firefox).

Also, belatedly add the LICENSE.TXT and accompanying CREDITS.TXT files,
which are referred to in all the source files.

MFC after:	1 month
@
text
@d147 1
d151 1
d178 2
a179 2
class _LIBCPP_VISIBLE thread;
class _LIBCPP_VISIBLE __thread_id;
d188 2
a189 2
class _LIBCPP_VISIBLE __thread_id;
template<> struct _LIBCPP_VISIBLE hash<__thread_id>;
d191 1
a191 1
class _LIBCPP_VISIBLE __thread_id
d233 2
a234 2
    friend class _LIBCPP_VISIBLE thread;
    friend struct _LIBCPP_VISIBLE hash<__thread_id>;
d238 1
a238 1
struct _LIBCPP_VISIBLE hash<__thread_id>
d260 1
a260 1
class _LIBCPP_VISIBLE thread
d331 1
a331 1
__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
d343 1
a343 1
    __threaad_execute(*__p, _Index());
@


1.2
log
@SVN rev 232950 on 2012-03-14 00:09:36Z by theraven

Import new versions of libcxxrt and libc++.
Please tests any C++ code you care about with -stdlib=libc++!

Approved by:	dim (mentor)
@
text
@d29 1
a29 1
    thread();
d34 1
a34 1
    thread(thread&& t);
d37 1
a37 1
    thread& operator=(thread&& t);
d39 1
a39 1
    void swap(thread& t);
d41 1
a41 1
    bool joinable() const;
d44 1
a44 1
    id get_id() const;
d47 1
a47 1
    static unsigned hardware_concurrency();
d50 1
a50 1
void swap(thread& x, thread& y);
d55 1
a55 1
    id();
d58 6
a63 6
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);
d72 1
a72 1
thread::id get_id();
d74 1
a74 1
void yield();
d176 2
a177 2
class thread;
class __thread_id;
d182 1
a182 1
__thread_id get_id();
d198 1
a198 1
    __thread_id() : __id_(0) {}
d201 1
a201 1
        bool operator==(__thread_id __x, __thread_id __y)
d204 1
a204 1
        bool operator!=(__thread_id __x, __thread_id __y)
d207 1
a207 1
        bool operator< (__thread_id __x, __thread_id __y)
d210 1
a210 1
        bool operator<=(__thread_id __x, __thread_id __y)
d213 1
a213 1
        bool operator> (__thread_id __x, __thread_id __y)
d216 1
a216 1
        bool operator>=(__thread_id __x, __thread_id __y)
d230 1
a230 1
    friend __thread_id this_thread::get_id();
d251 1
a251 1
get_id()
d269 1
a269 1
    thread() : __t_(0) {}
d285 2
a286 2
    thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
    thread& operator=(thread&& __t);
d290 1
a290 1
    void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);}
d293 1
a293 1
    bool joinable() const {return __t_ != 0;}
d297 1
a297 1
    id get_id() const {return __t_;}
d299 1
a299 1
    native_handle_type native_handle() {return __t_;}
d301 1
a301 1
    static unsigned hardware_concurrency();
d389 1
a389 1
thread::operator=(thread&& __t)
d401 1
a401 1
void swap(thread& __x, thread& __y) {__x.swap(__y);}
d413 14
a426 4
    nanoseconds __ns = duration_cast<nanoseconds>(__d);
    if (__ns < __d)
        ++__ns;
    sleep_for(__ns);
d451 1
a451 1
void yield() {sched_yield();}
@


1.2.2.1
log
@file thread was added on branch RELENG_9 on 2012-05-22 18:32:26 +0000
@
text
@d1 447
@


1.2.2.2
log
@SVN rev 235798 on 2012-05-22 18:30:14Z by theraven

Merged libcxxrt and libc++.  Now available for testing on 9-stable with
-stdlib=libc++.  Changes to libstdc++ not yet merged, so it is not yet possible
to mix libstdc++ and libc++ in the same program.

Merged revisions: 226702,226785,227006,227755,227983,227987,228531,228630,228761,229067,230127,232950,233098,234715-234716,234772
@
text
@a0 447
// -*- C++ -*-
//===--------------------------- thread -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_THREAD
#define _LIBCPP_THREAD

/*

    thread synopsis

#define __STDCPP_THREADS__ __cplusplus

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>
#ifndef _LIBCPP_HAS_NO_VARIADICS
#include <tuple>
#endif
#include <pthread.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

#define __STDCPP_THREADS__ __cplusplus

_LIBCPP_BEGIN_NAMESPACE_STD

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();

    _LIBCPP_INLINE_VISIBILITY
    pointer get() const {return static_cast<_Tp*>(pthread_getspecific(__key_));}
    _LIBCPP_INLINE_VISIBILITY
    pointer operator*() const {return *get();}
    _LIBCPP_INLINE_VISIBILITY
    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)
{
    delete static_cast<pointer>(__p);
}

template <class _Tp>
__thread_specific_ptr<_Tp>::__thread_specific_ptr()
{
    int __ec = pthread_key_create(&__key_, &__thread_specific_ptr::__at_thread_exit);
    if (__ec)
        throw system_error(error_code(__ec, system_category()),
                           "__thread_specific_ptr construction failed");
}

template <class _Tp>
__thread_specific_ptr<_Tp>::~__thread_specific_ptr()
{
    pthread_key_delete(__key_);
}

template <class _Tp>
typename __thread_specific_ptr<_Tp>::pointer
__thread_specific_ptr<_Tp>::release()
{
    pointer __p = get();
    pthread_setspecific(__key_, 0);
    return __p;
}

template <class _Tp>
void
__thread_specific_ptr<_Tp>::reset(pointer __p)
{
    pointer __p_old = get();
    pthread_setspecific(__key_, __p);
    delete __p_old;
}

class thread;
class __thread_id;

namespace this_thread
{

__thread_id get_id();

}  // this_thread

class _LIBCPP_VISIBLE __thread_id;
template<> struct _LIBCPP_VISIBLE hash<__thread_id>;

class _LIBCPP_VISIBLE __thread_id
{
    // 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.
    pthread_t __id_;

public:
    _LIBCPP_INLINE_VISIBILITY
    __thread_id() : __id_(0) {}

    friend _LIBCPP_INLINE_VISIBILITY
        bool operator==(__thread_id __x, __thread_id __y)
        {return __x.__id_ == __y.__id_;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator!=(__thread_id __x, __thread_id __y)
        {return !(__x == __y);}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator< (__thread_id __x, __thread_id __y)
        {return __x.__id_ < __y.__id_;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator<=(__thread_id __x, __thread_id __y)
        {return !(__y < __x);}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator> (__thread_id __x, __thread_id __y)
        {return   __y < __x ;}
    friend _LIBCPP_INLINE_VISIBILITY
        bool operator>=(__thread_id __x, __thread_id __y)
        {return !(__x < __y);}

    template<class _CharT, class _Traits>
    friend
    _LIBCPP_INLINE_VISIBILITY
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
        {return __os << __id.__id_;}

private:
    _LIBCPP_INLINE_VISIBILITY
    __thread_id(pthread_t __id) : __id_(__id) {}

    friend __thread_id this_thread::get_id();
    friend class _LIBCPP_VISIBLE thread;
    friend struct _LIBCPP_VISIBLE hash<__thread_id>;
};

template<>
struct _LIBCPP_VISIBLE hash<__thread_id>
    : public unary_function<__thread_id, size_t>
{
    _LIBCPP_INLINE_VISIBILITY
    size_t operator()(__thread_id __v) const
    {
        return hash<pthread_t>()(__v.__id_);
    }
};

namespace this_thread
{

inline _LIBCPP_INLINE_VISIBILITY
__thread_id
get_id()
{
    return pthread_self();
}

}  // this_thread

class _LIBCPP_VISIBLE thread
{
    pthread_t __t_;

    thread(const thread&);
    thread& operator=(const thread&);
public:
    typedef __thread_id id;
    typedef pthread_t native_handle_type;

    _LIBCPP_INLINE_VISIBILITY
    thread() : __t_(0) {}
#ifndef _LIBCPP_HAS_NO_VARIADICS
    template <class _Fp, class ..._Args,
              class = typename enable_if
              <
                   !is_same<typename decay<_Fp>::type, thread>::value
              >::type
             >
        explicit thread(_Fp&& __f, _Args&&... __args);
#else  // _LIBCPP_HAS_NO_VARIADICS
    template <class _Fp> explicit thread(_Fp __f);
#endif
    ~thread();

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    thread(thread&& __t) : __t_(__t.__t_) {__t.__t_ = 0;}
    thread& operator=(thread&& __t);
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    _LIBCPP_INLINE_VISIBILITY
    void swap(thread& __t) {_VSTD::swap(__t_, __t.__t_);}

    _LIBCPP_INLINE_VISIBILITY
    bool joinable() const {return __t_ != 0;}
    void join();
    void detach();
    _LIBCPP_INLINE_VISIBILITY
    id get_id() const {return __t_;}
    _LIBCPP_INLINE_VISIBILITY
    native_handle_type native_handle() {return __t_;}

    static unsigned hardware_concurrency();
};

class __assoc_sub_state;

class _LIBCPP_HIDDEN __thread_struct_imp;

class __thread_struct
{
    __thread_struct_imp* __p_;

    __thread_struct(const __thread_struct&);
    __thread_struct& operator=(const __thread_struct&);
public:
    __thread_struct();
    ~__thread_struct();

    void notify_all_at_thread_exit(condition_variable*, mutex*);
    void __make_ready_at_thread_exit(__assoc_sub_state*);
};

__thread_specific_ptr<__thread_struct>& __thread_local_data();

#ifndef _LIBCPP_HAS_NO_VARIADICS

template <class _Fp, class ..._Args, size_t ..._Indices>
inline _LIBCPP_INLINE_VISIBILITY
void
__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

template <class _Fp>
void*
__thread_proxy(void* __vp)
{
    __thread_local_data().reset(new __thread_struct);
    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
    __threaad_execute(*__p, _Index());
    return nullptr;
}

template <class _Fp, class ..._Args,
          class
         >
thread::thread(_Fp&& __f, _Args&&... __args)
{
    typedef tuple<typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
    _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                __decay_copy(_VSTD::forward<_Args>(__args))...));
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
    if (__ec == 0)
        __p.release();
    else
        __throw_system_error(__ec, "thread constructor failed");
}

#else  // _LIBCPP_HAS_NO_VARIADICS

template <class _Fp>
void*
__thread_proxy(void* __vp)
{
    __thread_local_data().reset(new __thread_struct);
    std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
    (*__p)();
    return nullptr;
}

template <class _Fp>
thread::thread(_Fp __f)
{
    std::unique_ptr<_Fp> __p(new _Fp(__f));
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
    if (__ec == 0)
        __p.release();
    else
        __throw_system_error(__ec, "thread constructor failed");
}

#endif  // _LIBCPP_HAS_NO_VARIADICS

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
thread&
thread::operator=(thread&& __t)
{
    if (__t_ != 0)
        terminate();
    __t_ = __t.__t_;
    __t.__t_ = 0;
    return *this;
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
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>
inline _LIBCPP_INLINE_VISIBILITY
void
sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t)
{
    using namespace chrono;
    sleep_for(__t - steady_clock::now());
}

inline _LIBCPP_INLINE_VISIBILITY
void yield() {sched_yield();}

}  // this_thread

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_THREAD
@


1.2.2.3
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/ 243376
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r243376 | dim | 2012-11-21 18:38:56 +0000 (Wed, 21 Nov 2012) | 14 lines
## SVN ##
## SVN ## MFC r241903:
## SVN ##
## SVN ##   Import libc++ trunk r165949.  Among other improvements and bug fixes,
## SVN ##   this has many visibility problems fixed, which should help with
## SVN ##   compiling certain ports that exercise C++11 mode (i.e. Firefox).
## SVN ##
## SVN ##   Also, belatedly add the LICENSE.TXT and accompanying CREDITS.TXT files,
## SVN ##   which are referred to in all the source files.
## SVN ##
## SVN ## MFC r241907:
## SVN ##
## SVN ##   Fix two -Wsystem-header warnings in libc++ that were exposed by the new
## SVN ##   ATF import.  These have also been sent upstream.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d29 1
a29 1
    thread() noexcept;
d34 1
a34 1
    thread(thread&& t) noexcept;
d37 1
a37 1
    thread& operator=(thread&& t) noexcept;
d39 1
a39 1
    void swap(thread& t) noexcept;
d41 1
a41 1
    bool joinable() const noexcept;
d44 1
a44 1
    id get_id() const noexcept;
d47 1
a47 1
    static unsigned hardware_concurrency() noexcept;
d50 1
a50 1
void swap(thread& x, thread& y) noexcept;
d55 1
a55 1
    id() noexcept;
d58 6
a63 6
bool operator==(thread::id x, thread::id y) noexcept;
bool operator!=(thread::id x, thread::id y) noexcept;
bool operator< (thread::id x, thread::id y) noexcept;
bool operator<=(thread::id x, thread::id y) noexcept;
bool operator> (thread::id x, thread::id y) noexcept;
bool operator>=(thread::id x, thread::id y) noexcept;
d72 1
a72 1
thread::id get_id() noexcept;
d74 1
a74 1
void yield() noexcept;
d176 2
a177 2
class _LIBCPP_VISIBLE thread;
class _LIBCPP_VISIBLE __thread_id;
d182 1
a182 1
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
d198 1
a198 1
    __thread_id() _NOEXCEPT : __id_(0) {}
d201 1
a201 1
        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
d204 1
a204 1
        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
d207 1
a207 1
        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
d210 1
a210 1
        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
d213 1
a213 1
        bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
d216 1
a216 1
        bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
d230 1
a230 1
    friend __thread_id this_thread::get_id() _NOEXCEPT;
d251 1
a251 1
get_id() _NOEXCEPT
d269 1
a269 1
    thread() _NOEXCEPT : __t_(0) {}
d285 2
a286 2
    thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) {__t.__t_ = 0;}
    thread& operator=(thread&& __t) _NOEXCEPT;
d290 1
a290 1
    void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);}
d293 1
a293 1
    bool joinable() const _NOEXCEPT {return __t_ != 0;}
d297 1
a297 1
    id get_id() const _NOEXCEPT {return __t_;}
d299 1
a299 1
    native_handle_type native_handle() _NOEXCEPT {return __t_;}
d301 1
a301 1
    static unsigned hardware_concurrency() _NOEXCEPT;
d389 1
a389 1
thread::operator=(thread&& __t) _NOEXCEPT
d401 1
a401 1
void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);}
d413 4
a416 14
    if (__d > duration<_Rep, _Period>::zero())
    {
        _LIBCPP_CONSTEXPR duration<long double> _Max = nanoseconds::max();
        nanoseconds __ns;
        if (__d < _Max)
        {
            __ns = duration_cast<nanoseconds>(__d);
            if (__ns < __d)
                ++__ns;
        }
        else
            __ns = nanoseconds::max();
        sleep_for(__ns);
    }
d441 1
a441 1
void yield() _NOEXCEPT {sched_yield();}
@


1.2.2.4
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/250514
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a146 1
#ifndef _LIBCPP_NO_EXCEPTIONS
a149 1
#endif
d176 2
a177 2
class _LIBCPP_TYPE_VIS thread;
class _LIBCPP_TYPE_VIS __thread_id;
d186 2
a187 2
class _LIBCPP_TYPE_VIS __thread_id;
template<> struct _LIBCPP_TYPE_VIS hash<__thread_id>;
d189 1
a189 1
class _LIBCPP_TYPE_VIS __thread_id
d231 2
a232 2
    friend class _LIBCPP_TYPE_VIS thread;
    friend struct _LIBCPP_TYPE_VIS hash<__thread_id>;
d236 1
a236 1
struct _LIBCPP_TYPE_VIS hash<__thread_id>
d258 1
a258 1
class _LIBCPP_TYPE_VIS thread
d329 1
a329 1
__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
d341 1
a341 1
    __thread_execute(*__p, _Index());
@


1.2.2.5
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/262801
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d188 2
a189 1
template<> struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
d191 1
a191 1
class _LIBCPP_TYPE_VIS_ONLY __thread_id
d234 1
a234 1
    friend struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>;
d238 1
a238 1
struct _LIBCPP_TYPE_VIS_ONLY hash<__thread_id>
d310 1
a310 1
class _LIBCPP_TYPE_VIS __thread_struct
d324 1
a324 1
_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data();
d408 1
a408 1
_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& ns);
@


1.1
log
@SVN rev 227983 on 2011-11-25 20:59:04Z by theraven

Import libc++ / libcxxrt into base.  Not build by default yet (use
MK_LIBCPLUSPLUS=yes to enable).  This is a work-in-progress.  It works for
me, but is not guaranteed to work for anyone else and may eat your dog.

To build C++ using libc++, add -stdlib=libc++ to your CXX and LD flags.

Bug reports welcome, bug fixes even more welcome...

Approved by:	dim (mentor)
@
text
@d186 3
d232 1
a234 2
template<class _Tp> struct hash;

d242 1
a242 2
        const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
        return *__p;
d271 1
a271 1
    template <class _F, class ..._Args,
d274 1
a274 1
                   !is_same<typename decay<_F>::type, thread>::value
d277 1
a277 1
        explicit thread(_F&& __f, _Args&&... __args);
d279 1
a279 1
    template <class _F> explicit thread(_F __f);
d326 1
a326 1
template <class _F, class ..._Args, size_t ..._Indices>
d329 1
a329 1
__threaad_execute(tuple<_F, _Args...>& __t, __tuple_indices<_Indices...>)
d334 1
a334 1
template <class _F>
d339 2
a340 2
    std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
    typedef typename __make_tuple_indices<tuple_size<_F>::value, 1>::type _Index;
d345 1
a345 1
template <class _F, class ..._Args,
d348 1
a348 1
thread::thread(_F&& __f, _Args&&... __args)
d350 2
a351 2
    typedef tuple<typename decay<_F>::type, typename decay<_Args>::type...> _G;
    _VSTD::unique_ptr<_G> __p(new _G(__decay_copy(_VSTD::forward<_F>(__f)),
d353 1
a353 1
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_G>, __p.get());
d362 1
a362 1
template <class _F>
d367 1
a367 1
    std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
d372 2
a373 2
template <class _F>
thread::thread(_F __f)
d375 2
a376 2
    std::unique_ptr<_F> __p(new _F(__f));
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_F>, __p.get());
@

