threads_none.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2016-2024 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. #include "internal/rcu.h"
  12. #include "rcu_internal.h"
  13. #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
  14. # if defined(OPENSSL_SYS_UNIX)
  15. # include <sys/types.h>
  16. # include <unistd.h>
  17. # endif
  18. struct rcu_lock_st {
  19. struct rcu_cb_item *cb_items;
  20. };
  21. CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers)
  22. {
  23. struct rcu_lock_st *lock;
  24. lock = OPENSSL_zalloc(sizeof(*lock));
  25. return lock;
  26. }
  27. void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
  28. {
  29. OPENSSL_free(lock);
  30. }
  31. void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
  32. {
  33. return;
  34. }
  35. void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
  36. {
  37. return;
  38. }
  39. void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
  40. {
  41. return;
  42. }
  43. void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
  44. {
  45. return;
  46. }
  47. void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
  48. {
  49. struct rcu_cb_item *items = lock->cb_items;
  50. struct rcu_cb_item *tmp;
  51. lock->cb_items = NULL;
  52. while (items != NULL) {
  53. tmp = items->next;
  54. items->fn(items->data);
  55. OPENSSL_free(items);
  56. items = tmp;
  57. }
  58. }
  59. int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
  60. {
  61. struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
  62. if (new == NULL)
  63. return 0;
  64. new->fn = cb;
  65. new->data = data;
  66. new->next = lock->cb_items;
  67. lock->cb_items = new;
  68. return 1;
  69. }
  70. void *ossl_rcu_uptr_deref(void **p)
  71. {
  72. return (void *)*p;
  73. }
  74. void ossl_rcu_assign_uptr(void **p, void **v)
  75. {
  76. *(void **)p = *(void **)v;
  77. }
  78. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  79. {
  80. CRYPTO_RWLOCK *lock;
  81. if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
  82. /* Don't set error, to avoid recursion blowup. */
  83. return NULL;
  84. *(unsigned int *)lock = 1;
  85. return lock;
  86. }
  87. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  88. {
  89. if (!ossl_assert(*(unsigned int *)lock == 1))
  90. return 0;
  91. return 1;
  92. }
  93. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  94. {
  95. if (!ossl_assert(*(unsigned int *)lock == 1))
  96. return 0;
  97. return 1;
  98. }
  99. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  100. {
  101. if (!ossl_assert(*(unsigned int *)lock == 1))
  102. return 0;
  103. return 1;
  104. }
  105. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
  106. if (lock == NULL)
  107. return;
  108. *(unsigned int *)lock = 0;
  109. OPENSSL_free(lock);
  110. return;
  111. }
  112. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  113. {
  114. if (*once != 0)
  115. return 1;
  116. init();
  117. *once = 1;
  118. return 1;
  119. }
  120. #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
  121. static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  122. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  123. {
  124. static unsigned int thread_local_key = 0;
  125. if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  126. return 0;
  127. *key = thread_local_key++;
  128. thread_local_storage[*key] = NULL;
  129. return 1;
  130. }
  131. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  132. {
  133. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  134. return NULL;
  135. return thread_local_storage[*key];
  136. }
  137. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  138. {
  139. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  140. return 0;
  141. thread_local_storage[*key] = val;
  142. return 1;
  143. }
  144. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  145. {
  146. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  147. return 1;
  148. }
  149. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  150. {
  151. return 0;
  152. }
  153. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  154. {
  155. return (a == b);
  156. }
  157. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  158. {
  159. *val += amount;
  160. *ret = *val;
  161. return 1;
  162. }
  163. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  164. CRYPTO_RWLOCK *lock)
  165. {
  166. *val |= op;
  167. *ret = *val;
  168. return 1;
  169. }
  170. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  171. {
  172. *ret = *val;
  173. return 1;
  174. }
  175. int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
  176. {
  177. *ret = *val;
  178. return 1;
  179. }
  180. int openssl_init_fork_handlers(void)
  181. {
  182. return 0;
  183. }
  184. int openssl_get_fork_id(void)
  185. {
  186. # if defined(OPENSSL_SYS_UNIX)
  187. return getpid();
  188. # else
  189. return 0;
  190. # endif
  191. }
  192. #endif