concurrence.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Support for concurrent programing -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file concurrence.h
  22. * This is an internal header file, included by other library headers.
  23. * You should not attempt to use it directly.
  24. */
  25. #ifndef _CONCURRENCE_H
  26. #define _CONCURRENCE_H 1
  27. #include <exception>
  28. #include <bits/gthr.h>
  29. #include <bits/functexcept.h>
  30. _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
  31. // Available locking policies:
  32. // _S_single single-threaded code that doesn't need to be locked.
  33. // _S_mutex multi-threaded code that requires additional support
  34. // from gthr.h or abstraction layers in concurrence.h.
  35. // _S_atomic multi-threaded code using atomic operations.
  36. enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
  37. // Compile time constant that indicates prefered locking policy in
  38. // the current configuration.
  39. static const _Lock_policy __default_lock_policy =
  40. #ifdef __GTHREADS
  41. #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \
  42. && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4))
  43. _S_atomic;
  44. #else
  45. _S_mutex;
  46. #endif
  47. #else
  48. _S_single;
  49. #endif
  50. // NB: As this is used in libsupc++, need to only depend on
  51. // exception. No stdexception classes, no use of std::string.
  52. class __concurrence_lock_error : public std::exception
  53. {
  54. public:
  55. virtual char const*
  56. what() const throw()
  57. { return "__gnu_cxx::__concurrence_lock_error"; }
  58. };
  59. class __concurrence_unlock_error : public std::exception
  60. {
  61. public:
  62. virtual char const*
  63. what() const throw()
  64. { return "__gnu_cxx::__concurrence_unlock_error"; }
  65. };
  66. class __concurrence_broadcast_error : public std::exception
  67. {
  68. public:
  69. virtual char const*
  70. what() const throw()
  71. { return "__gnu_cxx::__concurrence_broadcast_error"; }
  72. };
  73. class __concurrence_wait_error : public std::exception
  74. {
  75. public:
  76. virtual char const*
  77. what() const throw()
  78. { return "__gnu_cxx::__concurrence_wait_error"; }
  79. };
  80. // Substitute for concurrence_error object in the case of -fno-exceptions.
  81. inline void
  82. __throw_concurrence_lock_error()
  83. {
  84. #if __EXCEPTIONS
  85. throw __concurrence_lock_error();
  86. #else
  87. __builtin_abort();
  88. #endif
  89. }
  90. inline void
  91. __throw_concurrence_unlock_error()
  92. {
  93. #if __EXCEPTIONS
  94. throw __concurrence_unlock_error();
  95. #else
  96. __builtin_abort();
  97. #endif
  98. }
  99. #ifdef __GTHREAD_HAS_COND
  100. inline void
  101. __throw_concurrence_broadcast_error()
  102. {
  103. #if __EXCEPTIONS
  104. throw __concurrence_broadcast_error();
  105. #else
  106. __builtin_abort();
  107. #endif
  108. }
  109. inline void
  110. __throw_concurrence_wait_error()
  111. {
  112. #if __EXCEPTIONS
  113. throw __concurrence_wait_error();
  114. #else
  115. __builtin_abort();
  116. #endif
  117. }
  118. #endif
  119. class __mutex
  120. {
  121. private:
  122. __gthread_mutex_t _M_mutex;
  123. __mutex(const __mutex&);
  124. __mutex& operator=(const __mutex&);
  125. public:
  126. __mutex()
  127. {
  128. #if __GTHREADS
  129. if (__gthread_active_p())
  130. {
  131. #if defined __GTHREAD_MUTEX_INIT
  132. __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
  133. _M_mutex = __tmp;
  134. #else
  135. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  136. #endif
  137. }
  138. #endif
  139. }
  140. void lock()
  141. {
  142. #if __GTHREADS
  143. if (__gthread_active_p())
  144. {
  145. if (__gthread_mutex_lock(&_M_mutex) != 0)
  146. __throw_concurrence_lock_error();
  147. }
  148. #endif
  149. }
  150. void unlock()
  151. {
  152. #if __GTHREADS
  153. if (__gthread_active_p())
  154. {
  155. if (__gthread_mutex_unlock(&_M_mutex) != 0)
  156. __throw_concurrence_unlock_error();
  157. }
  158. #endif
  159. }
  160. __gthread_mutex_t* gthread_mutex(void)
  161. { return &_M_mutex; }
  162. };
  163. class __recursive_mutex
  164. {
  165. private:
  166. __gthread_recursive_mutex_t _M_mutex;
  167. __recursive_mutex(const __recursive_mutex&);
  168. __recursive_mutex& operator=(const __recursive_mutex&);
  169. public:
  170. __recursive_mutex()
  171. {
  172. #if __GTHREADS
  173. if (__gthread_active_p())
  174. {
  175. #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
  176. __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
  177. _M_mutex = __tmp;
  178. #else
  179. __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
  180. #endif
  181. }
  182. #endif
  183. }
  184. void lock()
  185. {
  186. #if __GTHREADS
  187. if (__gthread_active_p())
  188. {
  189. if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
  190. __throw_concurrence_lock_error();
  191. }
  192. #endif
  193. }
  194. void unlock()
  195. {
  196. #if __GTHREADS
  197. if (__gthread_active_p())
  198. {
  199. if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
  200. __throw_concurrence_unlock_error();
  201. }
  202. #endif
  203. }
  204. __gthread_recursive_mutex_t* gthread_recursive_mutex(void)
  205. { return &_M_mutex; }
  206. };
  207. /// Scoped lock idiom.
  208. // Acquire the mutex here with a constructor call, then release with
  209. // the destructor call in accordance with RAII style.
  210. class __scoped_lock
  211. {
  212. public:
  213. typedef __mutex __mutex_type;
  214. private:
  215. __mutex_type& _M_device;
  216. __scoped_lock(const __scoped_lock&);
  217. __scoped_lock& operator=(const __scoped_lock&);
  218. public:
  219. explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
  220. { _M_device.lock(); }
  221. ~__scoped_lock() throw()
  222. { _M_device.unlock(); }
  223. };
  224. #ifdef __GTHREAD_HAS_COND
  225. class __cond
  226. {
  227. private:
  228. __gthread_cond_t _M_cond;
  229. __cond(const __cond&);
  230. __cond& operator=(const __cond&);
  231. public:
  232. __cond()
  233. {
  234. #if __GTHREADS
  235. if (__gthread_active_p())
  236. {
  237. #if defined __GTHREAD_COND_INIT
  238. __gthread_cond_t __tmp = __GTHREAD_COND_INIT;
  239. _M_cond = __tmp;
  240. #else
  241. __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
  242. #endif
  243. }
  244. #endif
  245. }
  246. void broadcast()
  247. {
  248. #if __GTHREADS
  249. if (__gthread_active_p())
  250. {
  251. if (__gthread_cond_broadcast(&_M_cond) != 0)
  252. __throw_concurrence_broadcast_error();
  253. }
  254. #endif
  255. }
  256. void wait(__mutex *mutex)
  257. {
  258. #if __GTHREADS
  259. {
  260. if (__gthread_cond_wait(&_M_cond, mutex->gthread_mutex()) != 0)
  261. __throw_concurrence_wait_error();
  262. }
  263. #endif
  264. }
  265. void wait_recursive(__recursive_mutex *mutex)
  266. {
  267. #if __GTHREADS
  268. {
  269. if (__gthread_cond_wait_recursive(&_M_cond,
  270. mutex->gthread_recursive_mutex())
  271. != 0)
  272. __throw_concurrence_wait_error();
  273. }
  274. #endif
  275. }
  276. };
  277. #endif
  278. _GLIBCXX_END_NAMESPACE
  279. #endif