__threading_support 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_THREADING_SUPPORT
  11. #define _LIBCPP_THREADING_SUPPORT
  12. #include <__config>
  13. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  14. #pragma GCC system_header
  15. #endif
  16. #ifndef _LIBCPP_HAS_NO_THREADS
  17. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  18. #include <pthread.h>
  19. #include <sched.h>
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  23. // Mutex
  24. #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  25. typedef pthread_mutex_t __libcpp_mutex_t;
  26. inline _LIBCPP_ALWAYS_INLINE
  27. int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m)
  28. {
  29. pthread_mutexattr_t attr;
  30. int __ec = pthread_mutexattr_init(&attr);
  31. if (__ec) return __ec;
  32. __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  33. if (__ec)
  34. {
  35. pthread_mutexattr_destroy(&attr);
  36. return __ec;
  37. }
  38. __ec = pthread_mutex_init(__m, &attr);
  39. if (__ec)
  40. {
  41. pthread_mutexattr_destroy(&attr);
  42. return __ec;
  43. }
  44. __ec = pthread_mutexattr_destroy(&attr);
  45. if (__ec)
  46. {
  47. pthread_mutex_destroy(__m);
  48. return __ec;
  49. }
  50. return 0;
  51. }
  52. inline _LIBCPP_ALWAYS_INLINE
  53. int __libcpp_mutex_lock(__libcpp_mutex_t* __m)
  54. {
  55. return pthread_mutex_lock(__m);
  56. }
  57. inline _LIBCPP_ALWAYS_INLINE
  58. int __libcpp_mutex_trylock(__libcpp_mutex_t* __m)
  59. {
  60. return pthread_mutex_trylock(__m);
  61. }
  62. inline _LIBCPP_ALWAYS_INLINE
  63. int __libcpp_mutex_unlock(__libcpp_mutex_t* __m)
  64. {
  65. return pthread_mutex_unlock(__m);
  66. }
  67. inline _LIBCPP_ALWAYS_INLINE
  68. int __libcpp_mutex_destroy(__libcpp_mutex_t* __m)
  69. {
  70. return pthread_mutex_destroy(__m);
  71. }
  72. // Condition variable
  73. #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
  74. typedef pthread_cond_t __libcpp_condvar_t;
  75. inline _LIBCPP_ALWAYS_INLINE
  76. int __libcpp_condvar_signal(__libcpp_condvar_t* __cv)
  77. {
  78. return pthread_cond_signal(__cv);
  79. }
  80. inline _LIBCPP_ALWAYS_INLINE
  81. int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv)
  82. {
  83. return pthread_cond_broadcast(__cv);
  84. }
  85. inline _LIBCPP_ALWAYS_INLINE
  86. int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m)
  87. {
  88. return pthread_cond_wait(__cv, __m);
  89. }
  90. inline _LIBCPP_ALWAYS_INLINE
  91. int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts)
  92. {
  93. return pthread_cond_timedwait(__cv, __m, __ts);
  94. }
  95. inline _LIBCPP_ALWAYS_INLINE
  96. int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv)
  97. {
  98. return pthread_cond_destroy(__cv);
  99. }
  100. // Thread id
  101. typedef pthread_t __libcpp_thread_id;
  102. // Returns non-zero if the thread ids are equal, otherwise 0
  103. inline _LIBCPP_ALWAYS_INLINE
  104. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
  105. {
  106. return pthread_equal(t1, t2) != 0;
  107. }
  108. // Returns non-zero if t1 < t2, otherwise 0
  109. inline _LIBCPP_ALWAYS_INLINE
  110. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
  111. {
  112. return t1 < t2;
  113. }
  114. // Thread
  115. typedef pthread_t __libcpp_thread_t;
  116. inline _LIBCPP_ALWAYS_INLINE
  117. int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg)
  118. {
  119. return pthread_create(__t, 0, __func, __arg);
  120. }
  121. inline _LIBCPP_ALWAYS_INLINE
  122. __libcpp_thread_id __libcpp_thread_get_current_id()
  123. {
  124. return pthread_self();
  125. }
  126. inline _LIBCPP_ALWAYS_INLINE
  127. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t)
  128. {
  129. return *__t;
  130. }
  131. inline _LIBCPP_ALWAYS_INLINE
  132. int __libcpp_thread_join(__libcpp_thread_t* __t)
  133. {
  134. return pthread_join(*__t, 0);
  135. }
  136. inline _LIBCPP_ALWAYS_INLINE
  137. int __libcpp_thread_detach(__libcpp_thread_t* __t)
  138. {
  139. return pthread_detach(*__t);
  140. }
  141. inline _LIBCPP_ALWAYS_INLINE
  142. void __libcpp_thread_yield()
  143. {
  144. sched_yield();
  145. }
  146. // Thread local storage
  147. typedef pthread_key_t __libcpp_tl_key;
  148. inline _LIBCPP_ALWAYS_INLINE
  149. int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*))
  150. {
  151. return pthread_key_create(__key, __at_exit);
  152. }
  153. inline _LIBCPP_ALWAYS_INLINE
  154. void* __libcpp_tl_get(__libcpp_tl_key __key)
  155. {
  156. return pthread_getspecific(__key);
  157. }
  158. inline _LIBCPP_ALWAYS_INLINE
  159. void __libcpp_tl_set(__libcpp_tl_key __key, void* __p)
  160. {
  161. pthread_setspecific(__key, __p);
  162. }
  163. #else // !_LIBCPP_HAS_THREAD_API_PTHREAD
  164. #error "No thread API selected."
  165. #endif
  166. _LIBCPP_END_NAMESPACE_STD
  167. #endif // _LIBCPP_HAS_NO_THREADS
  168. #endif // _LIBCPP_THREADING_SUPPORT