threads_pthread.c 4.3 KB

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