threads_pthread.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <openssl/crypto.h>
  10. #include "internal/cryptlib.h"
  11. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
  12. # ifdef PTHREAD_RWLOCK_INITIALIZER
  13. # define USE_RWLOCK
  14. # endif
  15. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  16. {
  17. # ifdef USE_RWLOCK
  18. CRYPTO_RWLOCK *lock;
  19. if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
  20. /* Don't set error, to avoid recursion blowup. */
  21. return NULL;
  22. }
  23. if (pthread_rwlock_init(lock, NULL) != 0) {
  24. OPENSSL_free(lock);
  25. return NULL;
  26. }
  27. # else
  28. pthread_mutexattr_t attr;
  29. CRYPTO_RWLOCK *lock;
  30. if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
  31. /* Don't set error, to avoid recursion blowup. */
  32. return NULL;
  33. }
  34. pthread_mutexattr_init(&attr);
  35. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  36. if (pthread_mutex_init(lock, &attr) != 0) {
  37. pthread_mutexattr_destroy(&attr);
  38. OPENSSL_free(lock);
  39. return NULL;
  40. }
  41. pthread_mutexattr_destroy(&attr);
  42. # endif
  43. return lock;
  44. }
  45. int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  46. {
  47. # ifdef USE_RWLOCK
  48. if (pthread_rwlock_rdlock(lock) != 0)
  49. return 0;
  50. # else
  51. if (pthread_mutex_lock(lock) != 0)
  52. return 0;
  53. # endif
  54. return 1;
  55. }
  56. int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  57. {
  58. # ifdef USE_RWLOCK
  59. if (pthread_rwlock_wrlock(lock) != 0)
  60. return 0;
  61. # else
  62. if (pthread_mutex_lock(lock) != 0)
  63. return 0;
  64. # endif
  65. return 1;
  66. }
  67. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  68. {
  69. # ifdef USE_RWLOCK
  70. if (pthread_rwlock_unlock(lock) != 0)
  71. return 0;
  72. # else
  73. if (pthread_mutex_unlock(lock) != 0)
  74. return 0;
  75. # endif
  76. return 1;
  77. }
  78. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  79. {
  80. if (lock == NULL)
  81. return;
  82. # ifdef USE_RWLOCK
  83. pthread_rwlock_destroy(lock);
  84. # else
  85. pthread_mutex_destroy(lock);
  86. # endif
  87. OPENSSL_free(lock);
  88. return;
  89. }
  90. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  91. {
  92. if (pthread_once(once, init) != 0)
  93. return 0;
  94. return 1;
  95. }
  96. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  97. {
  98. if (pthread_key_create(key, cleanup) != 0)
  99. return 0;
  100. return 1;
  101. }
  102. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  103. {
  104. return pthread_getspecific(*key);
  105. }
  106. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  107. {
  108. if (pthread_setspecific(*key, val) != 0)
  109. return 0;
  110. return 1;
  111. }
  112. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  113. {
  114. if (pthread_key_delete(*key) != 0)
  115. return 0;
  116. return 1;
  117. }
  118. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  119. {
  120. return pthread_self();
  121. }
  122. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  123. {
  124. return pthread_equal(a, b);
  125. }
  126. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  127. {
  128. # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
  129. if (__atomic_is_lock_free(sizeof(*val), val)) {
  130. *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
  131. return 1;
  132. }
  133. # endif
  134. if (!CRYPTO_THREAD_write_lock(lock))
  135. return 0;
  136. *val += amount;
  137. *ret = *val;
  138. if (!CRYPTO_THREAD_unlock(lock))
  139. return 0;
  140. return 1;
  141. }
  142. int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock)
  143. {
  144. # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE)
  145. if (__atomic_is_lock_free(sizeof(*val), val)) {
  146. __atomic_load(val, ret, __ATOMIC_ACQUIRE);
  147. return 1;
  148. }
  149. # endif
  150. if (!CRYPTO_THREAD_read_lock(lock))
  151. return 0;
  152. *ret = *val;
  153. if (!CRYPTO_THREAD_unlock(lock))
  154. return 0;
  155. return 1;
  156. }
  157. int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock)
  158. {
  159. # if defined(__GNUC__) && defined(__ATOMIC_RELEASE)
  160. if (__atomic_is_lock_free(sizeof(*val), val)) {
  161. __atomic_store(val, &n, __ATOMIC_RELEASE);
  162. return 1;
  163. }
  164. # endif
  165. if (!CRYPTO_THREAD_write_lock(lock))
  166. return 0;
  167. *val = n;
  168. if (!CRYPTO_THREAD_unlock(lock))
  169. return 0;
  170. return 1;
  171. }
  172. # ifdef OPENSSL_SYS_UNIX
  173. static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
  174. static void fork_once_func(void)
  175. {
  176. pthread_atfork(OPENSSL_fork_prepare,
  177. OPENSSL_fork_parent, OPENSSL_fork_child);
  178. }
  179. # endif
  180. int openssl_init_fork_handlers(void)
  181. {
  182. # ifdef OPENSSL_SYS_UNIX
  183. if (pthread_once(&fork_once_control, fork_once_func) == 0)
  184. return 1;
  185. # endif
  186. return 0;
  187. }
  188. #endif