head	1.5;
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.5
date	2013.04.28.00.41.54;	author svnexp;	state Exp;
branches;
next	1.4;

1.4
date	2012.11.17.03.36.33;	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.40;	author svnexp;	state Exp;
branches;
next	1.2.2.4;

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

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

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


desc
@@


1.5
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/249998
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
//                     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___MUTEX_BASE
#define _LIBCPP___MUTEX_BASE

#include <__config>
#include <chrono>
#include <system_error>
#include <pthread.h>

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

#ifdef _LIBCPP_SHARED_LOCK

namespace ting {
template <class _Mutex> class shared_lock;
template <class _Mutex> class upgrade_lock;
}

#endif  // _LIBCPP_SHARED_LOCK


_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_TYPE_VIS mutex
{
    pthread_mutex_t __m_;

public:
    _LIBCPP_INLINE_VISIBILITY
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
     constexpr mutex() _NOEXCEPT : __m_(PTHREAD_MUTEX_INITIALIZER) {}
#else
     mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
#endif
     ~mutex();

private:
    mutex(const mutex&);// = delete;
    mutex& operator=(const mutex&);// = delete;

public:
    void lock();
    bool try_lock() _NOEXCEPT;
    void unlock() _NOEXCEPT;

    typedef pthread_mutex_t* native_handle_type;
    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
};

struct _LIBCPP_TYPE_VIS defer_lock_t {};
struct _LIBCPP_TYPE_VIS try_to_lock_t {};
struct _LIBCPP_TYPE_VIS adopt_lock_t {};

#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)

extern const defer_lock_t  defer_lock;
extern const try_to_lock_t try_to_lock;
extern const adopt_lock_t  adopt_lock;

#else

constexpr defer_lock_t  defer_lock  = defer_lock_t();
constexpr try_to_lock_t try_to_lock = try_to_lock_t();
constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();

#endif

template <class _Mutex>
class _LIBCPP_TYPE_VIS lock_guard
{
public:
    typedef _Mutex mutex_type;

private:
    mutex_type& __m_;
public:

    _LIBCPP_INLINE_VISIBILITY
    explicit lock_guard(mutex_type& __m)
        : __m_(__m) {__m_.lock();}
    _LIBCPP_INLINE_VISIBILITY
    lock_guard(mutex_type& __m, adopt_lock_t)
        : __m_(__m) {}
    _LIBCPP_INLINE_VISIBILITY
    ~lock_guard() {__m_.unlock();}

private:
    lock_guard(lock_guard const&);// = delete;
    lock_guard& operator=(lock_guard const&);// = delete;
};

template <class _Mutex>
class _LIBCPP_TYPE_VIS unique_lock
{
public:
    typedef _Mutex mutex_type;

private:
    mutex_type* __m_;
    bool __owns_;

public:
    _LIBCPP_INLINE_VISIBILITY
    unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
    _LIBCPP_INLINE_VISIBILITY
    explicit unique_lock(mutex_type& __m)
        : __m_(&__m), __owns_(true) {__m_->lock();}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
        : __m_(&__m), __owns_(false) {}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, try_to_lock_t)
        : __m_(&__m), __owns_(__m.try_lock()) {}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, adopt_lock_t)
        : __m_(&__m), __owns_(true) {}
    template <class _Clock, class _Duration>
    _LIBCPP_INLINE_VISIBILITY
        unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
            : __m_(&__m), __owns_(__m.try_lock_until(__t)) {}
    template <class _Rep, class _Period>
    _LIBCPP_INLINE_VISIBILITY
        unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
            : __m_(&__m), __owns_(__m.try_lock_for(__d)) {}
    _LIBCPP_INLINE_VISIBILITY
    ~unique_lock()
    {
        if (__owns_)
            __m_->unlock();
    }

private:
    unique_lock(unique_lock const&); // = delete;
    unique_lock& operator=(unique_lock const&); // = delete;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(unique_lock&& __u) _NOEXCEPT
        : __m_(__u.__m_), __owns_(__u.__owns_)
        {__u.__m_ = nullptr; __u.__owns_ = false;}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
        {
            if (__owns_)
                __m_->unlock();
            __m_ = __u.__m_;
            __owns_ = __u.__owns_;
            __u.__m_ = nullptr;
            __u.__owns_ = false;
            return *this;
        }

#ifdef _LIBCPP_SHARED_LOCK

    unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t);
    template <class _Clock, class _Duration>
        unique_lock(ting::shared_lock<mutex_type>&&,
                    const chrono::time_point<_Clock, _Duration>&);
    template <class _Rep, class _Period>
        unique_lock(ting::shared_lock<mutex_type>&&,
                    const chrono::duration<_Rep, _Period>&);

    explicit unique_lock(ting::upgrade_lock<mutex_type>&&);
    unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t);
    template <class _Clock, class _Duration>
        unique_lock(ting::upgrade_lock<mutex_type>&&,
                    const chrono::time_point<_Clock, _Duration>&);
    template <class _Rep, class _Period>
        unique_lock(ting::upgrade_lock<mutex_type>&&,
                    const chrono::duration<_Rep, _Period>&);

#endif  // _LIBCPP_SHARED_LOCK

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    void lock();
    bool try_lock();

    template <class _Rep, class _Period>
        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
    template <class _Clock, class _Duration>
        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);

    void unlock();

    _LIBCPP_INLINE_VISIBILITY
    void swap(unique_lock& __u) _NOEXCEPT
    {
        _VSTD::swap(__m_, __u.__m_);
        _VSTD::swap(__owns_, __u.__owns_);
    }
    _LIBCPP_INLINE_VISIBILITY
    mutex_type* release() _NOEXCEPT
    {
        mutex_type* __m = __m_;
        __m_ = nullptr;
        __owns_ = false;
        return __m;
    }

    _LIBCPP_INLINE_VISIBILITY
    bool owns_lock() const _NOEXCEPT {return __owns_;}
    _LIBCPP_INLINE_VISIBILITY
    _LIBCPP_EXPLICIT
        operator bool () const _NOEXCEPT {return __owns_;}
    _LIBCPP_INLINE_VISIBILITY
    mutex_type* mutex() const _NOEXCEPT {return __m_;}
};

template <class _Mutex>
void
unique_lock<_Mutex>::lock()
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
    __m_->lock();
    __owns_ = true;
}

template <class _Mutex>
bool
unique_lock<_Mutex>::try_lock()
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
    __owns_ = __m_->try_lock();
    return __owns_;
}

template <class _Mutex>
template <class _Rep, class _Period>
bool
unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
    __owns_ = __m_->try_lock_for(__d);
    return __owns_;
}

template <class _Mutex>
template <class _Clock, class _Duration>
bool
unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
    __owns_ = __m_->try_lock_until(__t);
    return __owns_;
}

template <class _Mutex>
void
unique_lock<_Mutex>::unlock()
{
    if (!__owns_)
        __throw_system_error(EPERM, "unique_lock::unlock: not locked");
    __m_->unlock();
    __owns_ = false;
}

template <class _Mutex>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
    {__x.swap(__y);}

struct _LIBCPP_TYPE_VIS cv_status
{
    enum __lx {
        no_timeout,
        timeout
    };

    __lx __v_;

    _LIBCPP_INLINE_VISIBILITY cv_status(__lx __v) : __v_(__v) {}
    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}

};

class _LIBCPP_TYPE_VIS condition_variable
{
    pthread_cond_t __cv_;
public:
    _LIBCPP_INLINE_VISIBILITY
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
    constexpr condition_variable() : __cv_(PTHREAD_COND_INITIALIZER) {}
#else
    condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
#endif
    ~condition_variable();

private:
    condition_variable(const condition_variable&); // = delete;
    condition_variable& operator=(const condition_variable&); // = delete;

public:
    void notify_one() _NOEXCEPT;
    void notify_all() _NOEXCEPT;

    void wait(unique_lock<mutex>& __lk);
    template <class _Predicate>
        void wait(unique_lock<mutex>& __lk, _Predicate __pred);

    template <class _Clock, class _Duration>
        cv_status
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t);

    template <class _Clock, class _Duration, class _Predicate>
        bool
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t,
                   _Predicate __pred);

    template <class _Rep, class _Period>
        cv_status
        wait_for(unique_lock<mutex>& __lk,
                 const chrono::duration<_Rep, _Period>& __d);

    template <class _Rep, class _Period, class _Predicate>
        bool
        wait_for(unique_lock<mutex>& __lk,
                 const chrono::duration<_Rep, _Period>& __d,
                 _Predicate __pred);

    typedef pthread_cond_t* native_handle_type;
    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}

private:
    void __do_timed_wait(unique_lock<mutex>& __lk,
                 chrono::time_point<chrono::system_clock, chrono::nanoseconds>);
};

template <class _To, class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
typename enable_if
<
    chrono::__is_duration<_To>::value,
    _To
>::type
__ceil(chrono::duration<_Rep, _Period> __d)
{
    using namespace chrono;
    _To __r = duration_cast<_To>(__d);
    if (__r < __d)
        ++__r;
    return __r;
}

template <class _Predicate>
void
condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
{
    while (!__pred())
        wait(__lk);
}

template <class _Clock, class _Duration>
cv_status
condition_variable::wait_until(unique_lock<mutex>& __lk,
                               const chrono::time_point<_Clock, _Duration>& __t)
{
    using namespace chrono;
    wait_for(__lk, __t - _Clock::now());
    return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
}

template <class _Clock, class _Duration, class _Predicate>
bool
condition_variable::wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t,
                   _Predicate __pred)
{
    while (!__pred())
    {
        if (wait_until(__lk, __t) == cv_status::timeout)
            return __pred();
    }
    return true;
}

template <class _Rep, class _Period>
cv_status
condition_variable::wait_for(unique_lock<mutex>& __lk,
                             const chrono::duration<_Rep, _Period>& __d)
{
    using namespace chrono;
    if (__d <= __d.zero())
        return cv_status::timeout;
    typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
    typedef time_point<system_clock, nanoseconds> __sys_tpi;
    __sys_tpf _Max = __sys_tpi::max();
    system_clock::time_point __s_now = system_clock::now();
    steady_clock::time_point __c_now = steady_clock::now();
    if (_Max - __d > __s_now)
        __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
    else
        __do_timed_wait(__lk, __sys_tpi::max());
    return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
                                                 cv_status::timeout;
}

template <class _Rep, class _Period, class _Predicate>
inline _LIBCPP_INLINE_VISIBILITY
bool
condition_variable::wait_for(unique_lock<mutex>& __lk,
                             const chrono::duration<_Rep, _Period>& __d,
                             _Predicate __pred)
{
    return wait_until(__lk, chrono::steady_clock::now() + __d,
                      _VSTD::move(__pred));
}

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP___MUTEX_BASE
@


1.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/242945
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r242945 | theraven | 2012-11-13 03:27:43 +0000 (Tue, 13 Nov 2012) | 2 lines
## SVN ##
## SVN ## Import new version of libc++ into base.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d35 1
a35 1
class _LIBCPP_VISIBLE mutex
d61 3
a63 3
struct _LIBCPP_VISIBLE defer_lock_t {};
struct _LIBCPP_VISIBLE try_to_lock_t {};
struct _LIBCPP_VISIBLE adopt_lock_t {};
d80 1
a80 1
class _LIBCPP_VISIBLE lock_guard
d104 1
a104 1
class _LIBCPP_VISIBLE unique_lock
d288 1
a288 1
struct _LIBCPP_VISIBLE cv_status
d302 1
a302 1
class _LIBCPP_VISIBLE condition_variable
@


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
@d290 1
a290 1
    enum _ {
d295 1
a295 1
    _ __v_;
d297 1
a297 1
    _LIBCPP_INLINE_VISIBILITY cv_status(_ __v) : __v_(__v) {}
@


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
@d41 5
a45 1
     mutex() {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
d54 2
a55 2
    bool try_lock();
    void unlock();
d65 13
a77 11
//constexpr
extern const
defer_lock_t  defer_lock;

//constexpr
extern const
try_to_lock_t try_to_lock;

//constexpr
extern const
adopt_lock_t  adopt_lock;
d115 1
a115 1
    unique_lock() : __m_(nullptr), __owns_(false) {}
d120 1
a120 1
    unique_lock(mutex_type& __m, defer_lock_t)
d150 1
a150 1
    unique_lock(unique_lock&& __u)
d154 1
a154 1
    unique_lock& operator=(unique_lock&& __u)
d199 1
a199 1
    void swap(unique_lock& __u)
d205 1
a205 1
    mutex_type* release()
d214 1
a214 1
    bool owns_lock() const {return __owns_;}
d217 1
a217 1
        operator bool () const {return __owns_;}
d219 1
a219 1
    mutex_type* mutex() const {return __m_;}
d285 2
a286 1
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) {__x.swap(__y);}
d307 3
d311 1
d319 2
a320 2
    void notify_one();
    void notify_all();
a325 5
    template <class _Duration>
        cv_status
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<chrono::system_clock, _Duration>& __t);

a379 13
template <class _Duration>
cv_status
condition_variable::wait_until(unique_lock<mutex>& __lk,
                 const chrono::time_point<chrono::system_clock, _Duration>& __t)
{
    using namespace chrono;
    typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt;
    __do_timed_wait(__lk,
                  __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch())));
    return system_clock::now() < __t ? cv_status::no_timeout :
                                       cv_status::timeout;
}

d386 1
a386 3
    system_clock::time_point     __s_now = system_clock::now();
    typename _Clock::time_point  __c_now = _Clock::now();
    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now));
d410 5
d417 4
a420 1
    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
@


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


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 439
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
//                     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___MUTEX_BASE
#define _LIBCPP___MUTEX_BASE

#include <__config>
#include <chrono>
#include <system_error>
#include <pthread.h>

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

#ifdef _LIBCPP_SHARED_LOCK

namespace ting {
template <class _Mutex> class shared_lock;
template <class _Mutex> class upgrade_lock;
}

#endif  // _LIBCPP_SHARED_LOCK


_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_VISIBLE mutex
{
    pthread_mutex_t __m_;

public:
    _LIBCPP_INLINE_VISIBILITY
     mutex() {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
     ~mutex();

private:
    mutex(const mutex&);// = delete;
    mutex& operator=(const mutex&);// = delete;

public:
    void lock();
    bool try_lock();
    void unlock();

    typedef pthread_mutex_t* native_handle_type;
    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
};

struct _LIBCPP_VISIBLE defer_lock_t {};
struct _LIBCPP_VISIBLE try_to_lock_t {};
struct _LIBCPP_VISIBLE adopt_lock_t {};

//constexpr
extern const
defer_lock_t  defer_lock;

//constexpr
extern const
try_to_lock_t try_to_lock;

//constexpr
extern const
adopt_lock_t  adopt_lock;

template <class _Mutex>
class _LIBCPP_VISIBLE lock_guard
{
public:
    typedef _Mutex mutex_type;

private:
    mutex_type& __m_;
public:

    _LIBCPP_INLINE_VISIBILITY
    explicit lock_guard(mutex_type& __m)
        : __m_(__m) {__m_.lock();}
    _LIBCPP_INLINE_VISIBILITY
    lock_guard(mutex_type& __m, adopt_lock_t)
        : __m_(__m) {}
    _LIBCPP_INLINE_VISIBILITY
    ~lock_guard() {__m_.unlock();}

private:
    lock_guard(lock_guard const&);// = delete;
    lock_guard& operator=(lock_guard const&);// = delete;
};

template <class _Mutex>
class _LIBCPP_VISIBLE unique_lock
{
public:
    typedef _Mutex mutex_type;

private:
    mutex_type* __m_;
    bool __owns_;

public:
    _LIBCPP_INLINE_VISIBILITY
    unique_lock() : __m_(nullptr), __owns_(false) {}
    _LIBCPP_INLINE_VISIBILITY
    explicit unique_lock(mutex_type& __m)
        : __m_(&__m), __owns_(true) {__m_->lock();}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, defer_lock_t)
        : __m_(&__m), __owns_(false) {}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, try_to_lock_t)
        : __m_(&__m), __owns_(__m.try_lock()) {}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(mutex_type& __m, adopt_lock_t)
        : __m_(&__m), __owns_(true) {}
    template <class _Clock, class _Duration>
    _LIBCPP_INLINE_VISIBILITY
        unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
            : __m_(&__m), __owns_(__m.try_lock_until(__t)) {}
    template <class _Rep, class _Period>
    _LIBCPP_INLINE_VISIBILITY
        unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
            : __m_(&__m), __owns_(__m.try_lock_for(__d)) {}
    _LIBCPP_INLINE_VISIBILITY
    ~unique_lock()
    {
        if (__owns_)
            __m_->unlock();
    }

private:
    unique_lock(unique_lock const&); // = delete;
    unique_lock& operator=(unique_lock const&); // = delete;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    unique_lock(unique_lock&& __u)
        : __m_(__u.__m_), __owns_(__u.__owns_)
        {__u.__m_ = nullptr; __u.__owns_ = false;}
    _LIBCPP_INLINE_VISIBILITY
    unique_lock& operator=(unique_lock&& __u)
        {
            if (__owns_)
                __m_->unlock();
            __m_ = __u.__m_;
            __owns_ = __u.__owns_;
            __u.__m_ = nullptr;
            __u.__owns_ = false;
            return *this;
        }

#ifdef _LIBCPP_SHARED_LOCK

    unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t);
    template <class _Clock, class _Duration>
        unique_lock(ting::shared_lock<mutex_type>&&,
                    const chrono::time_point<_Clock, _Duration>&);
    template <class _Rep, class _Period>
        unique_lock(ting::shared_lock<mutex_type>&&,
                    const chrono::duration<_Rep, _Period>&);

    explicit unique_lock(ting::upgrade_lock<mutex_type>&&);
    unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t);
    template <class _Clock, class _Duration>
        unique_lock(ting::upgrade_lock<mutex_type>&&,
                    const chrono::time_point<_Clock, _Duration>&);
    template <class _Rep, class _Period>
        unique_lock(ting::upgrade_lock<mutex_type>&&,
                    const chrono::duration<_Rep, _Period>&);

#endif  // _LIBCPP_SHARED_LOCK

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    void lock();
    bool try_lock();

    template <class _Rep, class _Period>
        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
    template <class _Clock, class _Duration>
        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);

    void unlock();

    _LIBCPP_INLINE_VISIBILITY
    void swap(unique_lock& __u)
    {
        _VSTD::swap(__m_, __u.__m_);
        _VSTD::swap(__owns_, __u.__owns_);
    }
    _LIBCPP_INLINE_VISIBILITY
    mutex_type* release()
    {
        mutex_type* __m = __m_;
        __m_ = nullptr;
        __owns_ = false;
        return __m;
    }

    _LIBCPP_INLINE_VISIBILITY
    bool owns_lock() const {return __owns_;}
    _LIBCPP_INLINE_VISIBILITY
    _LIBCPP_EXPLICIT
        operator bool () const {return __owns_;}
    _LIBCPP_INLINE_VISIBILITY
    mutex_type* mutex() const {return __m_;}
};

template <class _Mutex>
void
unique_lock<_Mutex>::lock()
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
    __m_->lock();
    __owns_ = true;
}

template <class _Mutex>
bool
unique_lock<_Mutex>::try_lock()
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
    __owns_ = __m_->try_lock();
    return __owns_;
}

template <class _Mutex>
template <class _Rep, class _Period>
bool
unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
    __owns_ = __m_->try_lock_for(__d);
    return __owns_;
}

template <class _Mutex>
template <class _Clock, class _Duration>
bool
unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
{
    if (__m_ == nullptr)
        __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
    if (__owns_)
        __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
    __owns_ = __m_->try_lock_until(__t);
    return __owns_;
}

template <class _Mutex>
void
unique_lock<_Mutex>::unlock()
{
    if (!__owns_)
        __throw_system_error(EPERM, "unique_lock::unlock: not locked");
    __m_->unlock();
    __owns_ = false;
}

template <class _Mutex>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) {__x.swap(__y);}

struct _LIBCPP_VISIBLE cv_status
{
    enum _ {
        no_timeout,
        timeout
    };

    _ __v_;

    _LIBCPP_INLINE_VISIBILITY cv_status(_ __v) : __v_(__v) {}
    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}

};

class _LIBCPP_VISIBLE condition_variable
{
    pthread_cond_t __cv_;
public:
    _LIBCPP_INLINE_VISIBILITY
    condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
    ~condition_variable();

private:
    condition_variable(const condition_variable&); // = delete;
    condition_variable& operator=(const condition_variable&); // = delete;

public:
    void notify_one();
    void notify_all();

    void wait(unique_lock<mutex>& __lk);
    template <class _Predicate>
        void wait(unique_lock<mutex>& __lk, _Predicate __pred);

    template <class _Duration>
        cv_status
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<chrono::system_clock, _Duration>& __t);

    template <class _Clock, class _Duration>
        cv_status
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t);

    template <class _Clock, class _Duration, class _Predicate>
        bool
        wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t,
                   _Predicate __pred);

    template <class _Rep, class _Period>
        cv_status
        wait_for(unique_lock<mutex>& __lk,
                 const chrono::duration<_Rep, _Period>& __d);

    template <class _Rep, class _Period, class _Predicate>
        bool
        wait_for(unique_lock<mutex>& __lk,
                 const chrono::duration<_Rep, _Period>& __d,
                 _Predicate __pred);

    typedef pthread_cond_t* native_handle_type;
    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}

private:
    void __do_timed_wait(unique_lock<mutex>& __lk,
                 chrono::time_point<chrono::system_clock, chrono::nanoseconds>);
};

template <class _To, class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
typename enable_if
<
    chrono::__is_duration<_To>::value,
    _To
>::type
__ceil(chrono::duration<_Rep, _Period> __d)
{
    using namespace chrono;
    _To __r = duration_cast<_To>(__d);
    if (__r < __d)
        ++__r;
    return __r;
}

template <class _Predicate>
void
condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
{
    while (!__pred())
        wait(__lk);
}

template <class _Duration>
cv_status
condition_variable::wait_until(unique_lock<mutex>& __lk,
                 const chrono::time_point<chrono::system_clock, _Duration>& __t)
{
    using namespace chrono;
    typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt;
    __do_timed_wait(__lk,
                  __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch())));
    return system_clock::now() < __t ? cv_status::no_timeout :
                                       cv_status::timeout;
}

template <class _Clock, class _Duration>
cv_status
condition_variable::wait_until(unique_lock<mutex>& __lk,
                               const chrono::time_point<_Clock, _Duration>& __t)
{
    using namespace chrono;
    system_clock::time_point     __s_now = system_clock::now();
    typename _Clock::time_point  __c_now = _Clock::now();
    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now));
    return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
}

template <class _Clock, class _Duration, class _Predicate>
bool
condition_variable::wait_until(unique_lock<mutex>& __lk,
                   const chrono::time_point<_Clock, _Duration>& __t,
                   _Predicate __pred)
{
    while (!__pred())
    {
        if (wait_until(__lk, __t) == cv_status::timeout)
            return __pred();
    }
    return true;
}

template <class _Rep, class _Period>
cv_status
condition_variable::wait_for(unique_lock<mutex>& __lk,
                             const chrono::duration<_Rep, _Period>& __d)
{
    using namespace chrono;
    system_clock::time_point __s_now = system_clock::now();
    steady_clock::time_point __c_now = steady_clock::now();
    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
    return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
                                                 cv_status::timeout;
}

template <class _Rep, class _Period, class _Predicate>
inline _LIBCPP_INLINE_VISIBILITY
bool
condition_variable::wait_for(unique_lock<mutex>& __lk,
                             const chrono::duration<_Rep, _Period>& __d,
                             _Predicate __pred)
{
    return wait_until(__lk, chrono::steady_clock::now() + __d,
                      _VSTD::move(__pred));
}

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP___MUTEX_BASE
@


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
@d41 1
a41 5
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
     constexpr mutex() _NOEXCEPT : __m_(PTHREAD_MUTEX_INITIALIZER) {}
#else
     mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
#endif
d50 2
a51 2
    bool try_lock() _NOEXCEPT;
    void unlock() _NOEXCEPT;
d61 11
a71 13
#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)

extern const defer_lock_t  defer_lock;
extern const try_to_lock_t try_to_lock;
extern const adopt_lock_t  adopt_lock;

#else

constexpr defer_lock_t  defer_lock  = defer_lock_t();
constexpr try_to_lock_t try_to_lock = try_to_lock_t();
constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();

#endif
d109 1
a109 1
    unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
d114 1
a114 1
    unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
d144 1
a144 1
    unique_lock(unique_lock&& __u) _NOEXCEPT
d148 1
a148 1
    unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
d193 1
a193 1
    void swap(unique_lock& __u) _NOEXCEPT
d199 1
a199 1
    mutex_type* release() _NOEXCEPT
d208 1
a208 1
    bool owns_lock() const _NOEXCEPT {return __owns_;}
d211 1
a211 1
        operator bool () const _NOEXCEPT {return __owns_;}
d213 1
a213 1
    mutex_type* mutex() const _NOEXCEPT {return __m_;}
d279 1
a279 2
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
    {__x.swap(__y);}
a299 3
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
    constexpr condition_variable() : __cv_(PTHREAD_COND_INITIALIZER) {}
#else
a300 1
#endif
d308 2
a309 2
    void notify_one() _NOEXCEPT;
    void notify_all() _NOEXCEPT;
d315 5
d374 13
d393 3
a395 1
    wait_for(__lk, __t - _Clock::now());
a418 5
    if (__d <= __d.zero())
        return cv_status::timeout;
    typedef time_point<system_clock, duration<long double, nano> > __sys_tpf;
    typedef time_point<system_clock, nanoseconds> __sys_tpi;
    __sys_tpf _Max = __sys_tpi::max();
d421 1
a421 4
    if (_Max - __d > __s_now)
        __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
    else
        __do_timed_wait(__lk, __sys_tpi::max());
@


1.2.2.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/243683
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r243683 | dim | 2012-11-29 21:26:57 +0000 (Thu, 29 Nov 2012) | 4 lines
## SVN ##
## SVN ## MFC r242945 (by theraven):
## SVN ##
## SVN ##   Import new version of libc++ into base.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d290 1
a290 1
    enum __lx {
d295 1
a295 1
    __lx __v_;
d297 1
a297 1
    _LIBCPP_INLINE_VISIBILITY cv_status(__lx __v) : __v_(__v) {}
@


1.2.2.5
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/250514
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d35 1
a35 1
class _LIBCPP_TYPE_VIS mutex
d61 3
a63 3
struct _LIBCPP_TYPE_VIS defer_lock_t {};
struct _LIBCPP_TYPE_VIS try_to_lock_t {};
struct _LIBCPP_TYPE_VIS adopt_lock_t {};
d80 1
a80 1
class _LIBCPP_TYPE_VIS lock_guard
d104 1
a104 1
class _LIBCPP_TYPE_VIS unique_lock
d288 1
a288 1
struct _LIBCPP_TYPE_VIS cv_status
d302 1
a302 1
class _LIBCPP_TYPE_VIS condition_variable
@


1.2.2.6
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/262801
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d23 10
d80 1
a80 1
class _LIBCPP_TYPE_VIS_ONLY lock_guard
d104 1
a104 1
class _LIBCPP_TYPE_VIS_ONLY unique_lock
d165 21
@


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
@d210 1
a210 1
//    explicit
@

