concurrence.h 7.4 KB

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