pthread_helpers.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
  2. #define BOOST_THREAD_PTHREAD_PTHREAD_HELPERS_HPP
  3. // Copyright (C) 2017
  4. // Vicente J. Botet Escriba
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/throw_exception.hpp>
  11. #include <pthread.h>
  12. #include <errno.h>
  13. #include <boost/config/abi_prefix.hpp>
  14. #ifndef BOOST_THREAD_HAS_NO_EINTR_BUG
  15. #define BOOST_THREAD_HAS_EINTR_BUG
  16. #endif
  17. namespace boost
  18. {
  19. namespace posix
  20. {
  21. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  22. int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t* attr = NULL)
  23. {
  24. return ::pthread_mutex_init(m, attr);
  25. }
  26. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  27. int pthread_cond_init(pthread_cond_t* c)
  28. {
  29. #ifdef BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  30. pthread_condattr_t attr;
  31. int res = pthread_condattr_init(&attr);
  32. if (res)
  33. {
  34. return res;
  35. }
  36. BOOST_VERIFY(!pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
  37. res = ::pthread_cond_init(c, &attr);
  38. BOOST_VERIFY(!pthread_condattr_destroy(&attr));
  39. return res;
  40. #else
  41. return ::pthread_cond_init(c, NULL);
  42. #endif
  43. }
  44. #ifdef BOOST_THREAD_HAS_EINTR_BUG
  45. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  46. int pthread_mutex_destroy(pthread_mutex_t* m)
  47. {
  48. int ret;
  49. do
  50. {
  51. ret = ::pthread_mutex_destroy(m);
  52. } while (ret == EINTR);
  53. return ret;
  54. }
  55. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  56. int pthread_cond_destroy(pthread_cond_t* c)
  57. {
  58. int ret;
  59. do
  60. {
  61. ret = ::pthread_cond_destroy(c);
  62. } while (ret == EINTR);
  63. return ret;
  64. }
  65. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  66. int pthread_mutex_lock(pthread_mutex_t* m)
  67. {
  68. int ret;
  69. do
  70. {
  71. ret = ::pthread_mutex_lock(m);
  72. } while (ret == EINTR);
  73. return ret;
  74. }
  75. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  76. int pthread_mutex_trylock(pthread_mutex_t* m)
  77. {
  78. int ret;
  79. do
  80. {
  81. ret = ::pthread_mutex_trylock(m);
  82. } while (ret == EINTR);
  83. return ret;
  84. }
  85. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  86. int pthread_mutex_unlock(pthread_mutex_t* m)
  87. {
  88. int ret;
  89. do
  90. {
  91. ret = ::pthread_mutex_unlock(m);
  92. } while (ret == EINTR);
  93. return ret;
  94. }
  95. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  96. int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
  97. {
  98. int ret;
  99. do
  100. {
  101. ret = ::pthread_cond_wait(c, m);
  102. } while (ret == EINTR);
  103. return ret;
  104. }
  105. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  106. int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
  107. {
  108. int ret;
  109. do
  110. {
  111. ret = ::pthread_cond_timedwait(c, m, t);
  112. } while (ret == EINTR);
  113. return ret;
  114. }
  115. #else
  116. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  117. int pthread_mutex_destroy(pthread_mutex_t* m)
  118. {
  119. return ::pthread_mutex_destroy(m);
  120. }
  121. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  122. int pthread_cond_destroy(pthread_cond_t* c)
  123. {
  124. return ::pthread_cond_destroy(c);
  125. }
  126. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  127. int pthread_mutex_lock(pthread_mutex_t* m)
  128. {
  129. return ::pthread_mutex_lock(m);
  130. }
  131. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  132. int pthread_mutex_trylock(pthread_mutex_t* m)
  133. {
  134. return ::pthread_mutex_trylock(m);
  135. }
  136. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  137. int pthread_mutex_unlock(pthread_mutex_t* m)
  138. {
  139. return ::pthread_mutex_unlock(m);
  140. }
  141. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  142. int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m)
  143. {
  144. return ::pthread_cond_wait(c, m);
  145. }
  146. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  147. int pthread_cond_timedwait(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t)
  148. {
  149. return ::pthread_cond_timedwait(c, m, t);
  150. }
  151. #endif
  152. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  153. int pthread_cond_signal(pthread_cond_t* c)
  154. {
  155. return ::pthread_cond_signal(c);
  156. }
  157. BOOST_FORCEINLINE BOOST_THREAD_DISABLE_THREAD_SAFETY_ANALYSIS
  158. int pthread_cond_broadcast(pthread_cond_t* c)
  159. {
  160. return ::pthread_cond_broadcast(c);
  161. }
  162. }
  163. }
  164. #include <boost/config/abi_suffix.hpp>
  165. #endif