summaryrefslogtreecommitdiff
path: root/boost/thread/pthread/recursive_mutex.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/thread/pthread/recursive_mutex.hpp')
-rw-r--r--boost/thread/pthread/recursive_mutex.hpp98
1 files changed, 65 insertions, 33 deletions
diff --git a/boost/thread/pthread/recursive_mutex.hpp b/boost/thread/pthread/recursive_mutex.hpp
index 4caae0b5df..ce9a8ce44b 100644
--- a/boost/thread/pthread/recursive_mutex.hpp
+++ b/boost/thread/pthread/recursive_mutex.hpp
@@ -19,27 +19,22 @@
#endif
#include <boost/date_time/posix_time/conversion.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
#if defined BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE \
|| defined __ANDROID__
#define BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
#endif
-#if defined BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE && defined BOOST_PTHREAD_HAS_TIMEDLOCK
+#if defined BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE && defined BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
#define BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
#endif
@@ -89,11 +84,11 @@ namespace boost
{
boost::throw_exception(thread_resource_error(res, "boost:: recursive_mutex constructor failed in pthread_mutex_init"));
}
- int const res2=pthread_cond_init(&cond,NULL);
+ int const res2=pthread::cond_init(cond);
if(res2)
{
BOOST_VERIFY(!pthread_mutex_destroy(&m));
- boost::throw_exception(thread_resource_error(res2, "boost:: recursive_mutex constructor failed in pthread_cond_init"));
+ boost::throw_exception(thread_resource_error(res2, "boost:: recursive_mutex constructor failed in pthread::cond_init"));
}
is_locked=false;
count=0;
@@ -224,11 +219,11 @@ namespace boost
{
boost::throw_exception(thread_resource_error(res, "boost:: recursive_timed_mutex constructor failed in pthread_mutex_init"));
}
- int const res2=pthread_cond_init(&cond,NULL);
+ int const res2=pthread::cond_init(cond);
if(res2)
{
BOOST_VERIFY(!pthread_mutex_destroy(&m));
- boost::throw_exception(thread_resource_error(res2, "boost:: recursive_timed_mutex constructor failed in pthread_cond_init"));
+ boost::throw_exception(thread_resource_error(res2, "boost:: recursive_timed_mutex constructor failed in pthread::cond_init"));
}
is_locked=false;
count=0;
@@ -246,7 +241,29 @@ 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
}
#endif
@@ -268,9 +285,9 @@ namespace boost
return !res;
}
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;
}
@@ -320,7 +337,7 @@ 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);
if(is_locked && pthread_equal(owner,pthread_self()))
@@ -330,13 +347,17 @@ namespace boost
}
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;
++count;
owner=pthread_self();
@@ -349,8 +370,20 @@ namespace boost
#if defined BOOST_THREAD_USES_DATETIME
bool timed_lock(system_time const & abs_time)
{
- struct timespec const ts=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
@@ -362,23 +395,22 @@ 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)
- {
- 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)
+ bool try_lock_until(const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
{
- //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