threads_pthread.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright 2016-2022 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 = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
  41. /* Don't set error, to avoid recursion blowup. */
  42. return NULL;
  43. }
  44. if (pthread_rwlock_init(lock, NULL) != 0) {
  45. OPENSSL_free(lock);
  46. return NULL;
  47. }
  48. # else
  49. pthread_mutexattr_t attr;
  50. CRYPTO_RWLOCK *lock;
  51. if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
  52. /* Don't set error, to avoid recursion blowup. */
  53. return NULL;
  54. }
  55. /*
  56. * We don't use recursive mutexes, but try to catch errors if we do.
  57. */
  58. pthread_mutexattr_init(&attr);
  59. # if !defined (__TANDEM) && !defined (_SPT_MODEL_)
  60. # if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
  61. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  62. # else
  63. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  64. # endif
  65. # else
  66. /* The SPT Thread Library does not define MUTEX attributes. */
  67. # endif
  68. if (pthread_mutex_init(lock, &attr) != 0) {
  69. pthread_mutexattr_destroy(&attr);
  70. OPENSSL_free(lock);
  71. return NULL;
  72. }
  73. pthread_mutexattr_destroy(&attr);
  74. # endif
  75. return lock;
  76. }
  77. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  78. {
  79. # ifdef USE_RWLOCK
  80. if (pthread_rwlock_rdlock(lock) != 0)
  81. return 0;
  82. # else
  83. if (pthread_mutex_lock(lock) != 0) {
  84. assert(errno != EDEADLK && errno != EBUSY);
  85. return 0;
  86. }
  87. # endif
  88. return 1;
  89. }
  90. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  91. {
  92. # ifdef USE_RWLOCK
  93. if (pthread_rwlock_wrlock(lock) != 0)
  94. return 0;
  95. # else
  96. if (pthread_mutex_lock(lock) != 0) {
  97. assert(errno != EDEADLK && errno != EBUSY);
  98. return 0;
  99. }
  100. # endif
  101. return 1;
  102. }
  103. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  104. {
  105. # ifdef USE_RWLOCK
  106. if (pthread_rwlock_unlock(lock) != 0)
  107. return 0;
  108. # else
  109. if (pthread_mutex_unlock(lock) != 0) {
  110. assert(errno != EPERM);
  111. return 0;
  112. }
  113. # endif
  114. return 1;
  115. }
  116. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  117. {
  118. if (lock == NULL)
  119. return;
  120. # ifdef USE_RWLOCK
  121. pthread_rwlock_destroy(lock);
  122. # else
  123. pthread_mutex_destroy(lock);
  124. # endif
  125. OPENSSL_free(lock);
  126. return;
  127. }
  128. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  129. {
  130. if (pthread_once(once, init) != 0)
  131. return 0;
  132. return 1;
  133. }
  134. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  135. {
  136. if (pthread_key_create(key, cleanup) != 0)
  137. return 0;
  138. return 1;
  139. }
  140. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  141. {
  142. return pthread_getspecific(*key);
  143. }
  144. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  145. {
  146. if (pthread_setspecific(*key, val) != 0)
  147. return 0;
  148. return 1;
  149. }
  150. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  151. {
  152. if (pthread_key_delete(*key) != 0)
  153. return 0;
  154. return 1;
  155. }
  156. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  157. {
  158. return pthread_self();
  159. }
  160. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  161. {
  162. return pthread_equal(a, b);
  163. }
  164. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  165. {
  166. # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
  167. if (__atomic_is_lock_free(sizeof(*val), val)) {
  168. *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
  169. return 1;
  170. }
  171. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  172. /* This will work for all future Solaris versions. */
  173. if (ret != NULL) {
  174. *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
  175. return 1;
  176. }
  177. # endif
  178. if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
  179. return 0;
  180. *val += amount;
  181. *ret = *val;
  182. if (!CRYPTO_THREAD_unlock(lock))
  183. return 0;
  184. return 1;
  185. }
  186. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  187. CRYPTO_RWLOCK *lock)
  188. {
  189. # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
  190. if (__atomic_is_lock_free(sizeof(*val), val)) {
  191. *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
  192. return 1;
  193. }
  194. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  195. /* This will work for all future Solaris versions. */
  196. if (ret != NULL) {
  197. *ret = atomic_or_64_nv(val, op);
  198. return 1;
  199. }
  200. # endif
  201. if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
  202. return 0;
  203. *val |= op;
  204. *ret = *val;
  205. if (!CRYPTO_THREAD_unlock(lock))
  206. return 0;
  207. return 1;
  208. }
  209. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  210. {
  211. # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
  212. if (__atomic_is_lock_free(sizeof(*val), val)) {
  213. __atomic_load(val, ret, __ATOMIC_ACQUIRE);
  214. return 1;
  215. }
  216. # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
  217. /* This will work for all future Solaris versions. */
  218. if (ret != NULL) {
  219. *ret = atomic_or_64_nv(val, 0);
  220. return 1;
  221. }
  222. # endif
  223. if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
  224. return 0;
  225. *ret = *val;
  226. if (!CRYPTO_THREAD_unlock(lock))
  227. return 0;
  228. return 1;
  229. }
  230. # ifndef FIPS_MODULE
  231. int openssl_init_fork_handlers(void)
  232. {
  233. return 1;
  234. }
  235. # endif /* FIPS_MODULE */
  236. int openssl_get_fork_id(void)
  237. {
  238. return getpid();
  239. }
  240. #endif