threads_pthread.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use the OPENSSL_fork_*() deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/crypto.h>
  12. #include "internal/cryptlib.h"
  13. #if defined(__sun)
  14. # include <atomic.h>
  15. #endif
  16. #if defined(__apple_build_version__) && __apple_build_version__ < 6000000
  17. /*
  18. * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
  19. * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
  20. * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
  21. * All of this makes impossible to use __atomic_is_lock_free here.
  22. *
  23. * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
  24. */
  25. #define BROKEN_CLANG_ATOMICS
  26. #endif
  27. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
  28. # if defined(OPENSSL_SYS_UNIX)
  29. # include <sys/types.h>
  30. # include <unistd.h>
  31. #endif
  32. # include <assert.h>
  33. # ifdef PTHREAD_RWLOCK_INITIALIZER
  34. # define USE_RWLOCK
  35. # endif
  36. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  37. {
  38. # ifdef USE_RWLOCK
  39. CRYPTO_RWLOCK *lock;
  40. if ((lock = CRYPTO_zalloc(sizeof(pthread_rwlock_t), NULL, 0)) == NULL)
  41. /* Don't set error, to avoid recursion blowup. */
  42. return NULL;
  43. if (pthread_rwlock_init(lock, NULL) != 0) {
  44. OPENSSL_free(lock);
  45. return NULL;
  46. }
  47. # else
  48. pthread_mutexattr_t attr;
  49. CRYPTO_RWLOCK *lock;
  50. if ((lock = CRYPTO_zalloc(sizeof(pthread_mutex_t), NULL, 0)) == NULL)
  51. /* Don't set error, to avoid recursion blowup. */
  52. return NULL;
  53. /*
  54. * We don't use recursive mutexes, but try to catch errors if we do.
  55. */
  56. pthread_mutexattr_init(&attr);
  57. # if !defined (__TANDEM) && !defined (_SPT_MODEL_)
  58. # if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
  59. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  60. # endif
  61. # else
  62. /* The SPT Thread Library does not define MUTEX attributes. */
  63. # endif
  64. if (pthread_mutex_init(lock, &attr) != 0) {
  65. pthread_mutexattr_destroy(&attr);
  66. OPENSSL_free(lock);
  67. return NULL;
  68. }
  69. pthread_mutexattr_destroy(&attr);
  70. # endif
  71. return lock;
  72. }
  73. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  74. {
  75. # ifdef USE_RWLOCK
  76. if (pthread_rwlock_rdlock(lock) != 0)
  77. return 0;
  78. # else
  79. if (pthread_mutex_lock(lock) != 0) {
  80. assert(errno != EDEADLK && errno != EBUSY);
  81. return 0;
  82. }
  83. # endif
  84. return 1;
  85. }
  86. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  87. {
  88. # ifdef USE_RWLOCK
  89. if (pthread_rwlock_wrlock(lock) != 0)
  90. return 0;
  91. # else
  92. if (pthread_mutex_lock(lock) != 0) {
  93. assert(errno != EDEADLK && errno != EBUSY);
  94. return 0;
  95. }
  96. # endif
  97. return 1;
  98. }
  99. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  100. {
  101. # ifdef USE_RWLOCK
  102. if (pthread_rwlock_unlock(lock) != 0)
  103. return 0;
  104. # else
  105. if (pthread_mutex_unlock(lock) != 0) {
  106. assert(errno != EPERM);
  107. return 0;
  108. }
  109. # endif
  110. return 1;
  111. }
  112. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  113. {
  114. if (lock == NULL)
  115. return;
  116. # ifdef USE_RWLOCK
  117. pthread_rwlock_destroy(lock);
  118. # else
  119. pthread_mutex_destroy(lock);
  120. # endif
  121. OPENSSL_free(lock);
  122. return;
  123. }
  124. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  125. {
  126. if (pthread_once(once, init) != 0)
  127. return 0;
  128. return 1;
  129. }
  130. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  131. {
  132. if (pthread_key_create(key, cleanup) != 0)
  133. return 0;
  134. return 1;
  135. }
  136. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  137. {
  138. return pthread_getspecific(*key);
  139. }
  140. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  141. {
  142. if (pthread_setspecific(*key, val) != 0)
  143. return 0;
  144. return 1;
  145. }
  146. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  147. {
  148. if (pthread_key_delete(*key) != 0)
  149. return 0;
  150. return 1;
  151. }
  152. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  153. {
  154. return pthread_self();
  155. }
  156. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  157. {
  158. return pthread_equal(a, b);
  159. }
  160. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  161. {
  162. # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
  163. if (__atomic_is_lock_free(sizeof(*val), val)) {
  164. *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
  165. return 1;
  166. }
  167. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  168. /* This will work for all future Solaris versions. */
  169. if (ret != NULL) {
  170. *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
  171. return 1;
  172. }
  173. # endif
  174. if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
  175. return 0;
  176. *val += amount;
  177. *ret = *val;
  178. if (!CRYPTO_THREAD_unlock(lock))
  179. return 0;
  180. return 1;
  181. }
  182. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  183. CRYPTO_RWLOCK *lock)
  184. {
  185. # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
  186. if (__atomic_is_lock_free(sizeof(*val), val)) {
  187. *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
  188. return 1;
  189. }
  190. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  191. /* This will work for all future Solaris versions. */
  192. if (ret != NULL) {
  193. *ret = atomic_or_64_nv(val, op);
  194. return 1;
  195. }
  196. # endif
  197. if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
  198. return 0;
  199. *val |= op;
  200. *ret = *val;
  201. if (!CRYPTO_THREAD_unlock(lock))
  202. return 0;
  203. return 1;
  204. }
  205. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  206. {
  207. # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
  208. if (__atomic_is_lock_free(sizeof(*val), val)) {
  209. __atomic_load(val, ret, __ATOMIC_ACQUIRE);
  210. return 1;
  211. }
  212. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  213. /* This will work for all future Solaris versions. */
  214. if (ret != NULL) {
  215. *ret = atomic_or_64_nv(val, 0);
  216. return 1;
  217. }
  218. # endif
  219. if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
  220. return 0;
  221. *ret = *val;
  222. if (!CRYPTO_THREAD_unlock(lock))
  223. return 0;
  224. return 1;
  225. }
  226. int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
  227. {
  228. # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
  229. if (__atomic_is_lock_free(sizeof(*val), val)) {
  230. __atomic_load(val, ret, __ATOMIC_ACQUIRE);
  231. return 1;
  232. }
  233. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  234. /* This will work for all future Solaris versions. */
  235. if (ret != NULL) {
  236. *ret = (int *)atomic_or_uint_nv((unsigned int *)val, 0);
  237. return 1;
  238. }
  239. # endif
  240. if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
  241. return 0;
  242. *ret = *val;
  243. if (!CRYPTO_THREAD_unlock(lock))
  244. return 0;
  245. return 1;
  246. }
  247. # ifndef FIPS_MODULE
  248. int openssl_init_fork_handlers(void)
  249. {
  250. return 1;
  251. }
  252. # endif /* FIPS_MODULE */
  253. int openssl_get_fork_id(void)
  254. {
  255. return getpid();
  256. }
  257. #endif