threads_none.c 5.0 KB

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