recursive_mutex.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #ifndef BOOST_THREAD_PTHREAD_RECURSIVE_MUTEX_HPP
  2. #define BOOST_THREAD_PTHREAD_RECURSIVE_MUTEX_HPP
  3. // (C) Copyright 2007-8 Anthony Williams
  4. // (C) Copyright 2011-2012 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 <pthread.h>
  9. #include <boost/throw_exception.hpp>
  10. #include <boost/thread/exceptions.hpp>
  11. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  12. #include <boost/thread/lock_types.hpp>
  13. #endif
  14. #include <boost/thread/thread_time.hpp>
  15. #include <boost/assert.hpp>
  16. #ifndef _WIN32
  17. #include <unistd.h>
  18. #endif
  19. #include <boost/date_time/posix_time/conversion.hpp>
  20. #include <errno.h>
  21. #include <boost/thread/pthread/timespec.hpp>
  22. #include <boost/thread/pthread/pthread_mutex_scoped_lock.hpp>
  23. #ifdef BOOST_THREAD_USES_CHRONO
  24. #include <boost/chrono/system_clocks.hpp>
  25. #include <boost/chrono/ceil.hpp>
  26. #endif
  27. #include <boost/thread/detail/delete.hpp>
  28. #if (defined _POSIX_TIMEOUTS && (_POSIX_TIMEOUTS-0)>=200112L) \
  29. || (defined __ANDROID__ && defined __ANDROID_API__ && __ANDROID_API__ >= 21)
  30. #ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
  31. #define BOOST_PTHREAD_HAS_TIMEDLOCK
  32. #endif
  33. #endif
  34. #if defined BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE \
  35. || defined __ANDROID__
  36. #define BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
  37. #endif
  38. #if defined BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE && defined BOOST_PTHREAD_HAS_TIMEDLOCK
  39. #define BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
  40. #endif
  41. #include <boost/config/abi_prefix.hpp>
  42. namespace boost
  43. {
  44. class recursive_mutex
  45. {
  46. private:
  47. pthread_mutex_t m;
  48. #ifndef BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
  49. pthread_cond_t cond;
  50. bool is_locked;
  51. pthread_t owner;
  52. unsigned count;
  53. #endif
  54. public:
  55. BOOST_THREAD_NO_COPYABLE(recursive_mutex)
  56. recursive_mutex()
  57. {
  58. #ifdef BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
  59. pthread_mutexattr_t attr;
  60. int const init_attr_res=pthread_mutexattr_init(&attr);
  61. if(init_attr_res)
  62. {
  63. boost::throw_exception(thread_resource_error(init_attr_res, "boost:: recursive_mutex constructor failed in pthread_mutexattr_init"));
  64. }
  65. int const set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
  66. if(set_attr_res)
  67. {
  68. BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
  69. boost::throw_exception(thread_resource_error(set_attr_res, "boost:: recursive_mutex constructor failed in pthread_mutexattr_settype"));
  70. }
  71. int const res=pthread_mutex_init(&m,&attr);
  72. if(res)
  73. {
  74. BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
  75. boost::throw_exception(thread_resource_error(res, "boost:: recursive_mutex constructor failed in pthread_mutex_init"));
  76. }
  77. BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
  78. #else
  79. int const res=pthread_mutex_init(&m,NULL);
  80. if(res)
  81. {
  82. boost::throw_exception(thread_resource_error(res, "boost:: recursive_mutex constructor failed in pthread_mutex_init"));
  83. }
  84. int const res2=pthread_cond_init(&cond,NULL);
  85. if(res2)
  86. {
  87. BOOST_VERIFY(!pthread_mutex_destroy(&m));
  88. boost::throw_exception(thread_resource_error(res2, "boost:: recursive_mutex constructor failed in pthread_cond_init"));
  89. }
  90. is_locked=false;
  91. count=0;
  92. #endif
  93. }
  94. ~recursive_mutex()
  95. {
  96. BOOST_VERIFY(!pthread_mutex_destroy(&m));
  97. #ifndef BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
  98. BOOST_VERIFY(!pthread_cond_destroy(&cond));
  99. #endif
  100. }
  101. #ifdef BOOST_THREAD_HAS_PTHREAD_MUTEXATTR_SETTYPE
  102. void lock()
  103. {
  104. BOOST_VERIFY(!pthread_mutex_lock(&m));
  105. }
  106. void unlock()
  107. {
  108. BOOST_VERIFY(!pthread_mutex_unlock(&m));
  109. }
  110. bool try_lock() BOOST_NOEXCEPT
  111. {
  112. int const res=pthread_mutex_trylock(&m);
  113. BOOST_ASSERT(!res || res==EBUSY);
  114. return !res;
  115. }
  116. #define BOOST_THREAD_DEFINES_RECURSIVE_MUTEX_NATIVE_HANDLE
  117. typedef pthread_mutex_t* native_handle_type;
  118. native_handle_type native_handle()
  119. {
  120. return &m;
  121. }
  122. #else
  123. void lock()
  124. {
  125. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  126. if(is_locked && pthread_equal(owner,pthread_self()))
  127. {
  128. ++count;
  129. return;
  130. }
  131. while(is_locked)
  132. {
  133. BOOST_VERIFY(!pthread_cond_wait(&cond,&m));
  134. }
  135. is_locked=true;
  136. ++count;
  137. owner=pthread_self();
  138. }
  139. void unlock()
  140. {
  141. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  142. if(!--count)
  143. {
  144. is_locked=false;
  145. }
  146. BOOST_VERIFY(!pthread_cond_signal(&cond));
  147. }
  148. bool try_lock()
  149. {
  150. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  151. if(is_locked && !pthread_equal(owner,pthread_self()))
  152. {
  153. return false;
  154. }
  155. is_locked=true;
  156. ++count;
  157. owner=pthread_self();
  158. return true;
  159. }
  160. #endif
  161. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  162. typedef unique_lock<recursive_mutex> scoped_lock;
  163. typedef detail::try_lock_wrapper<recursive_mutex> scoped_try_lock;
  164. #endif
  165. };
  166. typedef recursive_mutex recursive_try_mutex;
  167. class recursive_timed_mutex
  168. {
  169. private:
  170. pthread_mutex_t m;
  171. #ifndef BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
  172. pthread_cond_t cond;
  173. bool is_locked;
  174. pthread_t owner;
  175. unsigned count;
  176. #endif
  177. public:
  178. BOOST_THREAD_NO_COPYABLE(recursive_timed_mutex)
  179. recursive_timed_mutex()
  180. {
  181. #ifdef BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
  182. pthread_mutexattr_t attr;
  183. int const init_attr_res=pthread_mutexattr_init(&attr);
  184. if(init_attr_res)
  185. {
  186. boost::throw_exception(thread_resource_error(init_attr_res, "boost:: recursive_timed_mutex constructor failed in pthread_mutexattr_init"));
  187. }
  188. int const set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
  189. if(set_attr_res)
  190. {
  191. boost::throw_exception(thread_resource_error(set_attr_res, "boost:: recursive_timed_mutex constructor failed in pthread_mutexattr_settype"));
  192. }
  193. int const res=pthread_mutex_init(&m,&attr);
  194. if(res)
  195. {
  196. BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
  197. boost::throw_exception(thread_resource_error(res, "boost:: recursive_timed_mutex constructor failed in pthread_mutex_init"));
  198. }
  199. BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
  200. #else
  201. int const res=pthread_mutex_init(&m,NULL);
  202. if(res)
  203. {
  204. boost::throw_exception(thread_resource_error(res, "boost:: recursive_timed_mutex constructor failed in pthread_mutex_init"));
  205. }
  206. int const res2=pthread_cond_init(&cond,NULL);
  207. if(res2)
  208. {
  209. BOOST_VERIFY(!pthread_mutex_destroy(&m));
  210. boost::throw_exception(thread_resource_error(res2, "boost:: recursive_timed_mutex constructor failed in pthread_cond_init"));
  211. }
  212. is_locked=false;
  213. count=0;
  214. #endif
  215. }
  216. ~recursive_timed_mutex()
  217. {
  218. BOOST_VERIFY(!pthread_mutex_destroy(&m));
  219. #ifndef BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
  220. BOOST_VERIFY(!pthread_cond_destroy(&cond));
  221. #endif
  222. }
  223. #if defined BOOST_THREAD_USES_DATETIME
  224. template<typename TimeDuration>
  225. bool timed_lock(TimeDuration const & relative_time)
  226. {
  227. return timed_lock(get_system_time()+relative_time);
  228. }
  229. #endif
  230. #ifdef BOOST_USE_PTHREAD_RECURSIVE_TIMEDLOCK
  231. void lock()
  232. {
  233. BOOST_VERIFY(!pthread_mutex_lock(&m));
  234. }
  235. void unlock()
  236. {
  237. BOOST_VERIFY(!pthread_mutex_unlock(&m));
  238. }
  239. bool try_lock()
  240. {
  241. int const res=pthread_mutex_trylock(&m);
  242. BOOST_ASSERT(!res || res==EBUSY);
  243. return !res;
  244. }
  245. private:
  246. bool do_try_lock_until(struct timespec const &timeout)
  247. {
  248. int const res=pthread_mutex_timedlock(&m,&timeout);
  249. BOOST_ASSERT(!res || res==ETIMEDOUT);
  250. return !res;
  251. }
  252. public:
  253. #else
  254. void lock()
  255. {
  256. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  257. if(is_locked && pthread_equal(owner,pthread_self()))
  258. {
  259. ++count;
  260. return;
  261. }
  262. while(is_locked)
  263. {
  264. BOOST_VERIFY(!pthread_cond_wait(&cond,&m));
  265. }
  266. is_locked=true;
  267. ++count;
  268. owner=pthread_self();
  269. }
  270. void unlock()
  271. {
  272. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  273. if(!--count)
  274. {
  275. is_locked=false;
  276. }
  277. BOOST_VERIFY(!pthread_cond_signal(&cond));
  278. }
  279. bool try_lock() BOOST_NOEXCEPT
  280. {
  281. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  282. if(is_locked && !pthread_equal(owner,pthread_self()))
  283. {
  284. return false;
  285. }
  286. is_locked=true;
  287. ++count;
  288. owner=pthread_self();
  289. return true;
  290. }
  291. private:
  292. bool do_try_lock_until(struct timespec const &timeout)
  293. {
  294. boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
  295. if(is_locked && pthread_equal(owner,pthread_self()))
  296. {
  297. ++count;
  298. return true;
  299. }
  300. while(is_locked)
  301. {
  302. int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout);
  303. if(cond_res==ETIMEDOUT)
  304. {
  305. return false;
  306. }
  307. BOOST_ASSERT(!cond_res);
  308. }
  309. is_locked=true;
  310. ++count;
  311. owner=pthread_self();
  312. return true;
  313. }
  314. public:
  315. #endif
  316. #if defined BOOST_THREAD_USES_DATETIME
  317. bool timed_lock(system_time const & abs_time)
  318. {
  319. struct timespec const ts=detail::to_timespec(abs_time);
  320. return do_try_lock_until(ts);
  321. }
  322. #endif
  323. #ifdef BOOST_THREAD_USES_CHRONO
  324. template <class Rep, class Period>
  325. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  326. {
  327. return try_lock_until(chrono::steady_clock::now() + rel_time);
  328. }
  329. template <class Clock, class Duration>
  330. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  331. {
  332. using namespace chrono;
  333. system_clock::time_point s_now = system_clock::now();
  334. typename Clock::time_point c_now = Clock::now();
  335. return try_lock_until(s_now + ceil<nanoseconds>(t - c_now));
  336. }
  337. template <class Duration>
  338. bool try_lock_until(const chrono::time_point<chrono::system_clock, Duration>& t)
  339. {
  340. using namespace chrono;
  341. typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
  342. return try_lock_until(nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
  343. }
  344. bool try_lock_until(const chrono::time_point<chrono::system_clock, chrono::nanoseconds>& tp)
  345. {
  346. //using namespace chrono;
  347. chrono::nanoseconds d = tp.time_since_epoch();
  348. timespec ts = boost::detail::to_timespec(d);
  349. return do_try_lock_until(ts);
  350. }
  351. #endif
  352. #define BOOST_THREAD_DEFINES_RECURSIVE_TIMED_MUTEX_NATIVE_HANDLE
  353. typedef pthread_mutex_t* native_handle_type;
  354. native_handle_type native_handle()
  355. {
  356. return &m;
  357. }
  358. #if defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
  359. typedef unique_lock<recursive_timed_mutex> scoped_timed_lock;
  360. typedef detail::try_lock_wrapper<recursive_timed_mutex> scoped_try_lock;
  361. typedef scoped_timed_lock scoped_lock;
  362. #endif
  363. };
  364. }
  365. #include <boost/config/abi_suffix.hpp>
  366. #endif