threads_pthread.c 6.3 KB

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