threads_pthread.c 6.7 KB

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