mutex.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef BOOST_THREAD_PTHREAD_MUTEX_HPP
  2. #define BOOST_THREAD_PTHREAD_MUTEX_HPP
  3. // (C) Copyright 2007-8 Anthony Williams
  4. // (C) Copyright 2011,2012,2015 Vicente J. Botet Escriba
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/assert.hpp>
  10. #include <pthread.h>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/core/ignore_unused.hpp>
  13. #include <boost/thread/exceptions.hpp>
  14. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  15. #include <boost/thread/lock_types.hpp>
  16. #endif
  17. #include <boost/thread/thread_time.hpp>
  18. #if defined BOOST_THREAD_USES_DATETIME
  19. #include <boost/thread/xtime.hpp>
  20. #endif
  21. #include <boost/assert.hpp>
  22. #include <errno.h>
  23. #include <boost/thread/detail/platform_time.hpp>
  24. #include <boost/thread/pthread/pthread_mutex_scoped_lock.hpp>
  25. #include <boost/thread/pthread/pthread_helpers.hpp>
  26. #ifdef BOOST_THREAD_USES_CHRONO
  27. #include <boost/chrono/system_clocks.hpp>
  28. #include <boost/chrono/ceil.hpp>
  29. #endif
  30. #include <boost/thread/detail/delete.hpp>
  31. #include <boost/config/abi_prefix.hpp>
  32. namespace boost
  33. {
  34. class BOOST_THREAD_CAPABILITY("mutex") mutex
  35. {
  36. private:
  37. pthread_mutex_t m;
  38. public:
  39. BOOST_THREAD_NO_COPYABLE(mutex)
  40. mutex()
  41. {
  42. int const res=posix::pthread_mutex_init(&m);
  43. if(res)
  44. {
  45. boost::throw_exception(thread_resource_error(res, "boost:: mutex constructor failed in pthread_mutex_init"));
  46. }
  47. }
  48. ~mutex()
  49. {
  50. BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
  51. }
  52. void lock() BOOST_THREAD_ACQUIRE()
  53. {
  54. int res = posix::pthread_mutex_lock(&m);
  55. if (res)
  56. {
  57. boost::throw_exception(lock_error(res,"boost: mutex lock failed in pthread_mutex_lock"));
  58. }
  59. }
  60. void unlock() BOOST_THREAD_RELEASE()
  61. {
  62. BOOST_VERIFY(!posix::pthread_mutex_unlock(&m));
  63. }
  64. bool try_lock() BOOST_THREAD_TRY_ACQUIRE(true)
  65. {
  66. int res = posix::pthread_mutex_trylock(&m);
  67. if (res==EBUSY)
  68. {
  69. return false;
  70. }
  71. return !res;
  72. }
  73. #define BOOST_THREAD_DEFINES_MUTEX_NATIVE_HANDLE
  74. typedef pthread_mutex_t* native_handle_type;
  75. native_handle_type native_handle()
  76. {
  77. return &m;
  78. }
  79. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  80. typedef unique_lock<mutex> scoped_lock;
  81. typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
  82. #endif
  83. };
  84. typedef mutex try_mutex;
  85. class timed_mutex
  86. {
  87. private:
  88. pthread_mutex_t m;
  89. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  90. pthread_cond_t cond;
  91. bool is_locked;
  92. #endif
  93. public:
  94. BOOST_THREAD_NO_COPYABLE(timed_mutex)
  95. timed_mutex()
  96. {
  97. int const res=posix::pthread_mutex_init(&m);
  98. if(res)
  99. {
  100. boost::throw_exception(thread_resource_error(res, "boost:: timed_mutex constructor failed in pthread_mutex_init"));
  101. }
  102. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  103. int const res2=posix::pthread_cond_init(&cond);
  104. if(res2)
  105. {
  106. BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
  107. boost::throw_exception(thread_resource_error(res2, "boost:: timed_mutex constructor failed in pthread_cond_init"));
  108. }
  109. is_locked=false;
  110. #endif
  111. }
  112. ~timed_mutex()
  113. {
  114. BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
  115. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  116. BOOST_VERIFY(!posix::pthread_cond_destroy(&cond));
  117. #endif
  118. }
  119. #if defined BOOST_THREAD_USES_DATETIME
  120. template<typename TimeDuration>
  121. bool timed_lock(TimeDuration const & relative_time)
  122. {
  123. if (relative_time.is_pos_infinity())
  124. {
  125. lock();
  126. return true;
  127. }
  128. if (relative_time.is_special())
  129. {
  130. return true;
  131. }
  132. detail::platform_duration d(relative_time);
  133. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  134. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  135. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  136. while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
  137. {
  138. d = ts - detail::mono_platform_clock::now();
  139. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  140. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  141. }
  142. return true;
  143. #else
  144. return do_try_lock_until(detail::internal_platform_clock::now() + d);
  145. #endif
  146. }
  147. bool timed_lock(boost::xtime const & absolute_time)
  148. {
  149. return timed_lock(system_time(absolute_time));
  150. }
  151. #endif
  152. #ifdef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  153. void lock()
  154. {
  155. int res = posix::pthread_mutex_lock(&m);
  156. if (res)
  157. {
  158. boost::throw_exception(lock_error(res,"boost: mutex lock failed in pthread_mutex_lock"));
  159. }
  160. }
  161. void unlock()
  162. {
  163. BOOST_VERIFY(!posix::pthread_mutex_unlock(&m));
  164. }
  165. bool try_lock()
  166. {
  167. int res = posix::pthread_mutex_trylock(&m);
  168. if (res==EBUSY)
  169. {
  170. return false;
  171. }
  172. return !res;
  173. }
  174. private:
  175. bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
  176. {
  177. int const res=pthread_mutex_timedlock(&m,&timeout.getTs());
  178. BOOST_ASSERT(!res || res==ETIMEDOUT);
  179. return !res;
  180. }
  181. public:
  182. #else
  183. void lock()
  184. {
  185. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  186. while(is_locked)
  187. {
  188. BOOST_VERIFY(!posix::pthread_cond_wait(&cond,&m));
  189. }
  190. is_locked=true;
  191. }
  192. void unlock()
  193. {
  194. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  195. is_locked=false;
  196. BOOST_VERIFY(!posix::pthread_cond_signal(&cond));
  197. }
  198. bool try_lock()
  199. {
  200. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  201. if(is_locked)
  202. {
  203. return false;
  204. }
  205. is_locked=true;
  206. return true;
  207. }
  208. private:
  209. bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
  210. {
  211. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  212. while(is_locked)
  213. {
  214. int const cond_res=posix::pthread_cond_timedwait(&cond,&m,&timeout.getTs());
  215. if(cond_res==ETIMEDOUT)
  216. {
  217. break;
  218. }
  219. BOOST_ASSERT(!cond_res);
  220. }
  221. if(is_locked)
  222. {
  223. return false;
  224. }
  225. is_locked=true;
  226. return true;
  227. }
  228. public:
  229. #endif
  230. #if defined BOOST_THREAD_USES_DATETIME
  231. bool timed_lock(system_time const & abs_time)
  232. {
  233. const detail::real_platform_timepoint ts(abs_time);
  234. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  235. detail::platform_duration d(ts - detail::real_platform_clock::now());
  236. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  237. while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
  238. {
  239. d = ts - detail::real_platform_clock::now();
  240. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  241. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  242. }
  243. return true;
  244. #else
  245. return do_try_lock_until(ts);
  246. #endif
  247. }
  248. #endif
  249. #ifdef BOOST_THREAD_USES_CHRONO
  250. template <class Rep, class Period>
  251. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  252. {
  253. return try_lock_until(chrono::steady_clock::now() + rel_time);
  254. }
  255. template <class Clock, class Duration>
  256. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  257. {
  258. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  259. common_duration d(t - Clock::now());
  260. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  261. while ( ! try_lock_until(detail::internal_chrono_clock::now() + d))
  262. {
  263. d = t - Clock::now();
  264. if ( d <= common_duration::zero() ) return false; // timeout occurred
  265. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  266. }
  267. return true;
  268. }
  269. template <class Duration>
  270. bool try_lock_until(const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  271. {
  272. detail::internal_platform_timepoint ts(t);
  273. return do_try_lock_until(ts);
  274. }
  275. #endif
  276. #define BOOST_THREAD_DEFINES_TIMED_MUTEX_NATIVE_HANDLE
  277. typedef pthread_mutex_t* native_handle_type;
  278. native_handle_type native_handle()
  279. {
  280. return &m;
  281. }
  282. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  283. typedef unique_lock<timed_mutex> scoped_timed_lock;
  284. typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
  285. typedef scoped_timed_lock scoped_lock;
  286. #endif
  287. };
  288. }
  289. #include <boost/config/abi_suffix.hpp>
  290. #endif