threads_pthread.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(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. # ifndef FIPS_MODE
  143. /* TODO(3.0): No fork protection in FIPS module yet! */
  144. # ifdef OPENSSL_SYS_UNIX
  145. static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
  146. static void fork_once_func(void)
  147. {
  148. pthread_atfork(OPENSSL_fork_prepare,
  149. OPENSSL_fork_parent, OPENSSL_fork_child);
  150. }
  151. # endif
  152. int openssl_init_fork_handlers(void)
  153. {
  154. # ifdef OPENSSL_SYS_UNIX
  155. if (pthread_once(&fork_once_control, fork_once_func) == 0)
  156. return 1;
  157. # endif
  158. return 0;
  159. }
  160. # endif /* FIPS_MODE */
  161. #endif