mutexes.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. [/
  2. (C) Copyright 2007-8 Anthony Williams.
  3. (C) Copyright 2013 Oliver Kowalke.
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt).
  7. ]
  8. [section:mutex_types Mutex Types]
  9. [class_heading mutex]
  10. #include <boost/fiber/mutex.hpp>
  11. namespace boost {
  12. namespace fibers {
  13. class mutex {
  14. public:
  15. mutex();
  16. ~mutex();
  17. mutex( mutex const& other) = delete;
  18. mutex & operator=( mutex const& other) = delete;
  19. void lock();
  20. bool try_lock();
  21. void unlock();
  22. };
  23. }}
  24. __mutex__ provides an exclusive-ownership mutex. At most one fiber can own the
  25. lock on a given instance of __mutex__ at any time. Multiple concurrent calls to
  26. __lock__, __try_lock__ and __unlock__ shall be permitted.
  27. Any fiber blocked in __lock__ is suspended until the owning fiber releases the
  28. lock by calling __unlock__.
  29. [member_heading mutex..lock]
  30. void lock();
  31. [variablelist
  32. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  33. [[Effects:] [The current fiber blocks until ownership can be obtained.]]
  34. [[Throws:] [`lock_error`]]
  35. [[Error Conditions:] [
  36. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  37. ]
  38. [member_heading mutex..try_lock]
  39. bool try_lock();
  40. [variablelist
  41. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  42. [[Effects:] [Attempt to obtain ownership for the current fiber without
  43. blocking.]]
  44. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  45. otherwise.]]
  46. [[Throws:] [`lock_error`]]
  47. [[Error Conditions:] [
  48. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  49. ]
  50. [member_heading mutex..unlock]
  51. void unlock();
  52. [variablelist
  53. [[Precondition:] [The current fiber owns `*this`.]]
  54. [[Effects:] [Releases a lock on `*this` by the current fiber.]]
  55. [[Throws:] [`lock_error`]]
  56. [[Error Conditions:] [
  57. [*operation_not_permitted]: if `boost::this_fiber::get_id()` does not own the mutex.]]
  58. ]
  59. [class_heading timed_mutex]
  60. #include <boost/fiber/timed_mutex.hpp>
  61. namespace boost {
  62. namespace fibers {
  63. class timed_mutex {
  64. public:
  65. timed_mutex();
  66. ~timed_mutex();
  67. timed_mutex( timed_mutex const& other) = delete;
  68. timed_mutex & operator=( timed_mutex const& other) = delete;
  69. void lock();
  70. bool try_lock();
  71. void unlock();
  72. template< typename Clock, typename Duration >
  73. bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time);
  74. template< typename Rep, typename Period >
  75. bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration);
  76. };
  77. }}
  78. __timed_mutex__ provides an exclusive-ownership mutex. At most one fiber can own
  79. the lock on a given instance of __timed_mutex__ at any time. Multiple concurrent
  80. calls to __lock__, __try_lock__, __try_lock_until__, __try_lock_for__ and
  81. __unlock__ shall be permitted.
  82. [member_heading timed_mutex..lock]
  83. void lock();
  84. [variablelist
  85. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  86. [[Effects:] [The current fiber blocks until ownership can be obtained.]]
  87. [[Throws:] [`lock_error`]]
  88. [[Error Conditions:] [
  89. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  90. ]
  91. [member_heading timed_mutex..try_lock]
  92. bool try_lock();
  93. [variablelist
  94. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  95. [[Effects:] [Attempt to obtain ownership for the current fiber without
  96. blocking.]]
  97. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  98. otherwise.]]
  99. [[Throws:] [`lock_error`]]
  100. [[Error Conditions:] [
  101. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  102. ]
  103. [member_heading timed_mutex..unlock]
  104. void unlock();
  105. [variablelist
  106. [[Precondition:] [The current fiber owns `*this`.]]
  107. [[Effects:] [Releases a lock on `*this` by the current fiber.]]
  108. [[Throws:] [`lock_error`]]
  109. [[Error Conditions:] [
  110. [*operation_not_permitted]: if `boost::this_fiber::get_id()` does not own the mutex.]]
  111. ]
  112. [template_member_heading timed_mutex..try_lock_until]
  113. template< typename Clock, typename Duration >
  114. bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time);
  115. [variablelist
  116. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  117. [[Effects:] [Attempt to obtain ownership for the current fiber. Blocks until
  118. ownership can be obtained, or the specified time is reached. If the specified
  119. time has already passed, behaves as [member_link timed_mutex..try_lock].]]
  120. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  121. otherwise.]]
  122. [[Throws:] [`lock_error`, timeout-related exceptions.]]
  123. [[Error Conditions:] [
  124. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  125. ]
  126. [template_member_heading timed_mutex..try_lock_for]
  127. template< typename Rep, typename Period >
  128. bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration);
  129. [variablelist
  130. [[Precondition:] [The calling fiber doesn't own the mutex.]]
  131. [[Effects:] [Attempt to obtain ownership for the current fiber. Blocks until
  132. ownership can be obtained, or the specified time is reached. If the specified
  133. time has already passed, behaves as [member_link timed_mutex..try_lock].]]
  134. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  135. otherwise.]]
  136. [[Throws:] [`lock_error`, timeout-related exceptions.]]
  137. [[Error Conditions:] [
  138. [*resource_deadlock_would_occur]: if `boost::this_fiber::get_id()` already owns the mutex.]]
  139. ]
  140. [class_heading recursive_mutex]
  141. #include <boost/fiber/recursive_mutex.hpp>
  142. namespace boost {
  143. namespace fibers {
  144. class recursive_mutex {
  145. public:
  146. recursive_mutex();
  147. ~recursive_mutex();
  148. recursive_mutex( recursive_mutex const& other) = delete;
  149. recursive_mutex & operator=( recursive_mutex const& other) = delete;
  150. void lock();
  151. bool try_lock() noexcept;
  152. void unlock();
  153. };
  154. }}
  155. __recursive_mutex__ provides an exclusive-ownership recursive mutex. At most one
  156. fiber can own the lock on a given instance of __recursive_mutex__ at any time.
  157. Multiple concurrent calls to __lock__, __try_lock__ and __unlock__ shall be
  158. permitted. A fiber that already has exclusive ownership of a given
  159. __recursive_mutex__ instance can call __lock__ or __try_lock__ to acquire an
  160. additional level of ownership of the mutex. __unlock__ must be called once for
  161. each level of ownership acquired by a single fiber before ownership can be
  162. acquired by another fiber.
  163. [member_heading recursive_mutex..lock]
  164. void lock();
  165. [variablelist
  166. [[Effects:] [The current fiber blocks until ownership can be obtained.]]
  167. [[Throws:] [Nothing]]
  168. ]
  169. [member_heading recursive_mutex..try_lock]
  170. bool try_lock() noexcept;
  171. [variablelist
  172. [[Effects:] [Attempt to obtain ownership for the current fiber without
  173. blocking.]]
  174. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  175. otherwise.]]
  176. [[Throws:] [Nothing.]]
  177. ]
  178. [member_heading recursive_mutex..unlock]
  179. void unlock();
  180. [variablelist
  181. [[Effects:] [Releases a lock on `*this` by the current fiber.]]
  182. [[Throws:] [`lock_error`]]
  183. [[Error Conditions:] [
  184. [*operation_not_permitted]: if `boost::this_fiber::get_id()` does not own the mutex.]]
  185. ]
  186. [class_heading recursive_timed_mutex]
  187. #include <boost/fiber/recursive_timed_mutex.hpp>
  188. namespace boost {
  189. namespace fibers {
  190. class recursive_timed_mutex {
  191. public:
  192. recursive_timed_mutex();
  193. ~recursive_timed_mutex();
  194. recursive_timed_mutex( recursive_timed_mutex const& other) = delete;
  195. recursive_timed_mutex & operator=( recursive_timed_mutex const& other) = delete;
  196. void lock();
  197. bool try_lock() noexcept;
  198. void unlock();
  199. template< typename Clock, typename Duration >
  200. bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time);
  201. template< typename Rep, typename Period >
  202. bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration);
  203. };
  204. }}
  205. __recursive_timed_mutex__ provides an exclusive-ownership recursive mutex. At
  206. most one fiber can own the lock on a given instance of
  207. __recursive_timed_mutex__ at any time. Multiple concurrent calls to __lock__,
  208. __try_lock__, __try_lock_for__, __try_lock_until__ and __unlock__ shall be
  209. permitted. A fiber that already has exclusive ownership of a given
  210. __recursive_timed_mutex__ instance can call __lock__, __try_lock__,
  211. __try_lock_for__ or __try_lock_until__ to acquire an additional level of
  212. ownership of the mutex. __unlock__ must be called once for each level of
  213. ownership acquired by a single fiber before ownership can be acquired by another
  214. fiber.
  215. [member_heading recursive_timed_mutex..lock]
  216. void lock();
  217. [variablelist
  218. [[Effects:] [The current fiber blocks until ownership can be obtained.]]
  219. [[Throws:] [Nothing]]
  220. ]
  221. [member_heading recursive_timed_mutex..try_lock]
  222. bool try_lock() noexcept;
  223. [variablelist
  224. [[Effects:] [Attempt to obtain ownership for the current fiber without
  225. blocking.]]
  226. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  227. otherwise.]]
  228. [[Throws:] [Nothing.]]
  229. ]
  230. [member_heading recursive_timed_mutex..unlock]
  231. void unlock();
  232. [variablelist
  233. [[Effects:] [Releases a lock on `*this` by the current fiber.]]
  234. [[Throws:] [`lock_error`]]
  235. [[Error Conditions:] [
  236. [*operation_not_permitted]: if `boost::this_fiber::get_id()` does not own the mutex.]]
  237. ]
  238. [template_member_heading recursive_timed_mutex..try_lock_until]
  239. template< typename Clock, typename Duration >
  240. bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time);
  241. [variablelist
  242. [[Effects:] [Attempt to obtain ownership for the current fiber. Blocks until
  243. ownership can be obtained, or the specified time is reached. If the specified
  244. time has already passed, behaves as [member_link recursive_timed_mutex..try_lock].]]
  245. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  246. otherwise.]]
  247. [[Throws:] [Timeout-related exceptions.]]
  248. ]
  249. [template_member_heading recursive_timed_mutex..try_lock_for]
  250. template< typename Rep, typename Period >
  251. bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration);
  252. [variablelist
  253. [[Effects:] [Attempt to obtain ownership for the current fiber. Blocks until
  254. ownership can be obtained, or the specified time is reached. If the specified
  255. time has already passed, behaves as [member_link recursive_timed_mutex..try_lock].]]
  256. [[Returns:] [`true` if ownership was obtained for the current fiber, `false`
  257. otherwise.]]
  258. [[Throws:] [Timeout-related exceptions.]]
  259. ]
  260. [endsect]