recursive_mutex.hpp 12 KB

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