2
0

threads_pthread.c 4.5 KB

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