summaryrefslogtreecommitdiff
path: root/boost/thread/pthread/mutex.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/thread/pthread/mutex.hpp')
-rw-r--r--boost/thread/pthread/mutex.hpp104
1 files changed, 67 insertions, 37 deletions
diff --git a/boost/thread/pthread/mutex.hpp b/boost/thread/pthread/mutex.hpp
index 9ac808b6f4..6180ac5ebd 100644
--- a/boost/thread/pthread/mutex.hpp
+++ b/boost/thread/pthread/mutex.hpp
@@ -16,24 +16,20 @@
#include <boost/thread/lock_types.hpp>
#endif
#include <boost/thread/thread_time.hpp>
+#if defined BOOST_THREAD_USES_DATETIME
#include <boost/thread/xtime.hpp>
+#endif
#include <boost/assert.hpp>
#include <errno.h>
-#include <boost/thread/pthread/timespec.hpp>
+#include <boost/thread/detail/platform_time.hpp>
#include <boost/thread/pthread/pthread_mutex_scoped_lock.hpp>
+#include <boost/thread/pthread/pthread_helpers.hpp>
#ifdef BOOST_THREAD_USES_CHRONO
#include <boost/chrono/system_clocks.hpp>
#include <boost/chrono/ceil.hpp>
#endif
#include <boost/thread/detail/delete.hpp>
-#if (defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS-0)>=200112L) \
- || (defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 21)
-#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
-#define BOOST_PTHREAD_HAS_TIMEDLOCK
-#endif
-#endif
-
#include <boost/config/abi_prefix.hpp>
@@ -165,7 +161,7 @@ namespace boost
{
private:
pthread_mutex_t m;
-#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
+#ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
pthread_cond_t cond;
bool is_locked;
#endif
@@ -178,13 +174,12 @@ namespace boost
{
boost::throw_exception(thread_resource_error(res, "boost:: timed_mutex constructor failed in pthread_mutex_init"));
}
-#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
- int const res2=pthread_cond_init(&cond,NULL);
+#ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
+ int const res2=pthread::cond_init(cond);
if(res2)
{
BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
- //BOOST_VERIFY(!pthread_mutex_destroy(&m));
- boost::throw_exception(thread_resource_error(res2, "boost:: timed_mutex constructor failed in pthread_cond_init"));
+ boost::throw_exception(thread_resource_error(res2, "boost:: timed_mutex constructor failed in pthread::cond_init"));
}
is_locked=false;
#endif
@@ -192,7 +187,7 @@ namespace boost
~timed_mutex()
{
BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
-#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
+#ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
BOOST_VERIFY(!pthread_cond_destroy(&cond));
#endif
}
@@ -201,14 +196,36 @@ namespace boost
template<typename TimeDuration>
bool timed_lock(TimeDuration const & relative_time)
{
- return timed_lock(get_system_time()+relative_time);
+ if (relative_time.is_pos_infinity())
+ {
+ lock();
+ return true;
+ }
+ if (relative_time.is_special())
+ {
+ return true;
+ }
+ detail::platform_duration d(relative_time);
+#if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
+ const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
+ d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
+ while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
+ {
+ d = ts - detail::mono_platform_clock::now();
+ if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
+ d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
+ }
+ return true;
+#else
+ return do_try_lock_until(detail::internal_platform_clock::now() + d);
+#endif
}
bool timed_lock(boost::xtime const & absolute_time)
{
return timed_lock(system_time(absolute_time));
}
#endif
-#ifdef BOOST_PTHREAD_HAS_TIMEDLOCK
+#ifdef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
void lock()
{
int res = posix::pthread_mutex_lock(&m);
@@ -246,9 +263,9 @@ namespace boost
private:
- bool do_try_lock_until(struct timespec const &timeout)
+ bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
{
- int const res=pthread_mutex_timedlock(&m,&timeout);
+ int const res=pthread_mutex_timedlock(&m,&timeout.getTs());
BOOST_ASSERT(!res || res==ETIMEDOUT);
return !res;
}
@@ -284,18 +301,22 @@ namespace boost
}
private:
- bool do_try_lock_until(struct timespec const &timeout)
+ bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
{
boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
while(is_locked)
{
- int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout);
+ int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout.getTs());
if(cond_res==ETIMEDOUT)
{
- return false;
+ break;
}
BOOST_ASSERT(!cond_res);
}
+ if(is_locked)
+ {
+ return false;
+ }
is_locked=true;
return true;
}
@@ -305,8 +326,20 @@ namespace boost
#if defined BOOST_THREAD_USES_DATETIME
bool timed_lock(system_time const & abs_time)
{
- struct timespec const ts=boost::detail::to_timespec(abs_time);
+ const detail::real_platform_timepoint ts(abs_time);
+#if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
+ detail::platform_duration d(ts - detail::real_platform_clock::now());
+ d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
+ while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
+ {
+ d = ts - detail::real_platform_clock::now();
+ if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
+ d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
+ }
+ return true;
+#else
return do_try_lock_until(ts);
+#endif
}
#endif
#ifdef BOOST_THREAD_USES_CHRONO
@@ -318,23 +351,21 @@ namespace boost
template <class Clock, class Duration>
bool try_lock_until(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();
- return try_lock_until(s_now + ceil<nanoseconds>(t - c_now));
+ typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
+ common_duration d(t - Clock::now());
+ d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
+ while ( ! try_lock_until(detail::internal_chrono_clock::now() + d))
+ {
+ d = t - Clock::now();
+ if ( d <= common_duration::zero() ) return false; // timeout occurred
+ d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
+ }
+ return true;
}
template <class Duration>
- bool try_lock_until(const chrono::time_point<chrono::system_clock, Duration>& t)
+ bool try_lock_until(const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
{
- using namespace chrono;
- typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
- return try_lock_until(nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
- }
- bool try_lock_until(const chrono::time_point<chrono::system_clock, chrono::nanoseconds>& tp)
- {
- //using namespace chrono;
- chrono::nanoseconds d = tp.time_since_epoch();
- timespec ts = boost::detail::to_timespec(d);
+ detail::internal_platform_timepoint ts(t);
return do_try_lock_until(ts);
}
#endif
@@ -352,7 +383,6 @@ namespace boost
typedef scoped_timed_lock scoped_lock;
#endif
};
-
}
#include <boost/config/abi_suffix.hpp>