mutex.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #ifndef BOOST_THREAD_HAS_NO_EINTR_BUG
  33. #define BOOST_THREAD_HAS_EINTR_BUG
  34. #endif
  35. namespace boost
  36. {
  37. class BOOST_THREAD_CAPABILITY("mutex") mutex
  38. {
  39. private:
  40. pthread_mutex_t m;
  41. public:
  42. BOOST_THREAD_NO_COPYABLE(mutex)
  43. mutex()
  44. {
  45. int const res=pthread_mutex_init(&m,NULL);
  46. if(res)
  47. {
  48. boost::throw_exception(thread_resource_error(res, "boost:: mutex constructor failed in pthread_mutex_init"));
  49. }
  50. }
  51. ~mutex()
  52. {
  53. int const res = posix::pthread_mutex_destroy(&m);
  54. boost::ignore_unused(res);
  55. BOOST_ASSERT(!res);
  56. }
  57. void lock() BOOST_THREAD_ACQUIRE()
  58. {
  59. int res = posix::pthread_mutex_lock(&m);
  60. if (res)
  61. {
  62. boost::throw_exception(lock_error(res,"boost: mutex lock failed in pthread_mutex_lock"));
  63. }
  64. }
  65. void unlock() BOOST_THREAD_RELEASE()
  66. {
  67. int res = posix::pthread_mutex_unlock(&m);
  68. (void)res;
  69. BOOST_ASSERT(res == 0);
  70. // if (res)
  71. // {
  72. // boost::throw_exception(lock_error(res,"boost: mutex unlock failed in pthread_mutex_unlock"));
  73. // }
  74. }
  75. bool try_lock() BOOST_THREAD_TRY_ACQUIRE(true)
  76. {
  77. int res;
  78. do
  79. {
  80. res = posix::pthread_mutex_trylock(&m);
  81. } while (res == EINTR);
  82. if (res==EBUSY)
  83. {
  84. return false;
  85. }
  86. return !res;
  87. }
  88. #define BOOST_THREAD_DEFINES_MUTEX_NATIVE_HANDLE
  89. typedef pthread_mutex_t* native_handle_type;
  90. native_handle_type native_handle()
  91. {
  92. return &m;
  93. }
  94. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  95. typedef unique_lock<mutex> scoped_lock;
  96. typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
  97. #endif
  98. };
  99. typedef mutex try_mutex;
  100. class timed_mutex
  101. {
  102. private:
  103. pthread_mutex_t m;
  104. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  105. pthread_cond_t cond;
  106. bool is_locked;
  107. #endif
  108. public:
  109. BOOST_THREAD_NO_COPYABLE(timed_mutex)
  110. timed_mutex()
  111. {
  112. int const res=pthread_mutex_init(&m,NULL);
  113. if(res)
  114. {
  115. boost::throw_exception(thread_resource_error(res, "boost:: timed_mutex constructor failed in pthread_mutex_init"));
  116. }
  117. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  118. int const res2=pthread::cond_init(cond);
  119. if(res2)
  120. {
  121. BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
  122. boost::throw_exception(thread_resource_error(res2, "boost:: timed_mutex constructor failed in pthread::cond_init"));
  123. }
  124. is_locked=false;
  125. #endif
  126. }
  127. ~timed_mutex()
  128. {
  129. BOOST_VERIFY(!posix::pthread_mutex_destroy(&m));
  130. #ifndef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  131. BOOST_VERIFY(!pthread_cond_destroy(&cond));
  132. #endif
  133. }
  134. #if defined BOOST_THREAD_USES_DATETIME
  135. template<typename TimeDuration>
  136. bool timed_lock(TimeDuration const & relative_time)
  137. {
  138. if (relative_time.is_pos_infinity())
  139. {
  140. lock();
  141. return true;
  142. }
  143. if (relative_time.is_special())
  144. {
  145. return true;
  146. }
  147. detail::platform_duration d(relative_time);
  148. #if defined(BOOST_THREAD_HAS_MONO_CLOCK) && !defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  149. const detail::mono_platform_timepoint ts(detail::mono_platform_clock::now() + d);
  150. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  151. while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
  152. {
  153. d = ts - detail::mono_platform_clock::now();
  154. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  155. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  156. }
  157. return true;
  158. #else
  159. return do_try_lock_until(detail::internal_platform_clock::now() + d);
  160. #endif
  161. }
  162. bool timed_lock(boost::xtime const & absolute_time)
  163. {
  164. return timed_lock(system_time(absolute_time));
  165. }
  166. #endif
  167. #ifdef BOOST_THREAD_USES_PTHREAD_TIMEDLOCK
  168. void lock()
  169. {
  170. int res = posix::pthread_mutex_lock(&m);
  171. if (res)
  172. {
  173. boost::throw_exception(lock_error(res,"boost: mutex lock failed in pthread_mutex_lock"));
  174. }
  175. }
  176. void unlock()
  177. {
  178. int res = posix::pthread_mutex_unlock(&m);
  179. (void)res;
  180. BOOST_ASSERT(res == 0);
  181. // if (res)
  182. // {
  183. // boost::throw_exception(lock_error(res,"boost: mutex unlock failed in pthread_mutex_unlock"));
  184. // }
  185. }
  186. bool try_lock()
  187. {
  188. int res;
  189. do
  190. {
  191. res = posix::pthread_mutex_trylock(&m);
  192. } while (res == EINTR);
  193. if (res==EBUSY)
  194. {
  195. return false;
  196. }
  197. return !res;
  198. }
  199. private:
  200. bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
  201. {
  202. int const res=pthread_mutex_timedlock(&m,&timeout.getTs());
  203. BOOST_ASSERT(!res || res==ETIMEDOUT);
  204. return !res;
  205. }
  206. public:
  207. #else
  208. void lock()
  209. {
  210. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  211. while(is_locked)
  212. {
  213. BOOST_VERIFY(!posix::pthread_cond_wait(&cond,&m));
  214. }
  215. is_locked=true;
  216. }
  217. void unlock()
  218. {
  219. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  220. is_locked=false;
  221. BOOST_VERIFY(!posix::pthread_cond_signal(&cond));
  222. }
  223. bool try_lock()
  224. {
  225. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  226. if(is_locked)
  227. {
  228. return false;
  229. }
  230. is_locked=true;
  231. return true;
  232. }
  233. private:
  234. bool do_try_lock_until(detail::internal_platform_timepoint const &timeout)
  235. {
  236. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  237. while(is_locked)
  238. {
  239. int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout.getTs());
  240. if(cond_res==ETIMEDOUT)
  241. {
  242. break;
  243. }
  244. BOOST_ASSERT(!cond_res);
  245. }
  246. if(is_locked)
  247. {
  248. return false;
  249. }
  250. is_locked=true;
  251. return true;
  252. }
  253. public:
  254. #endif
  255. #if defined BOOST_THREAD_USES_DATETIME
  256. bool timed_lock(system_time const & abs_time)
  257. {
  258. const detail::real_platform_timepoint ts(abs_time);
  259. #if defined BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  260. detail::platform_duration d(ts - detail::real_platform_clock::now());
  261. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  262. while ( ! do_try_lock_until(detail::internal_platform_clock::now() + d) )
  263. {
  264. d = ts - detail::real_platform_clock::now();
  265. if ( d <= detail::platform_duration::zero() ) return false; // timeout occurred
  266. d = (std::min)(d, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  267. }
  268. return true;
  269. #else
  270. return do_try_lock_until(ts);
  271. #endif
  272. }
  273. #endif
  274. #ifdef BOOST_THREAD_USES_CHRONO
  275. template <class Rep, class Period>
  276. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  277. {
  278. return try_lock_until(chrono::steady_clock::now() + rel_time);
  279. }
  280. template <class Clock, class Duration>
  281. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  282. {
  283. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  284. common_duration d(t - Clock::now());
  285. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  286. while ( ! try_lock_until(detail::internal_chrono_clock::now() + d))
  287. {
  288. d = t - Clock::now();
  289. if ( d <= common_duration::zero() ) return false; // timeout occurred
  290. d = (std::min)(d, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  291. }
  292. return true;
  293. }
  294. template <class Duration>
  295. bool try_lock_until(const chrono::time_point<detail::internal_chrono_clock, Duration>& t)
  296. {
  297. detail::internal_platform_timepoint ts(t);
  298. return do_try_lock_until(ts);
  299. }
  300. #endif
  301. #define BOOST_THREAD_DEFINES_TIMED_MUTEX_NATIVE_HANDLE
  302. typedef pthread_mutex_t* native_handle_type;
  303. native_handle_type native_handle()
  304. {
  305. return &m;
  306. }
  307. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  308. typedef unique_lock<timed_mutex> scoped_timed_lock;
  309. typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
  310. typedef scoped_timed_lock scoped_lock;
  311. #endif
  312. };
  313. }
  314. #include <boost/config/abi_suffix.hpp>
  315. #endif