2 * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
11 #include "internal/cryptlib.h"
12 #include "internal/rcu.h"
13 #include "rcu_internal.h"
15 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
17 # if defined(OPENSSL_SYS_UNIX)
18 # include <sys/types.h>
23 struct rcu_cb_item *cb_items;
26 CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
28 struct rcu_lock_st *lock;
30 lock = OPENSSL_zalloc(sizeof(*lock));
34 void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
39 void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
44 void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
49 void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
54 void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
59 void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
61 struct rcu_cb_item *items = lock->cb_items;
62 struct rcu_cb_item *tmp;
64 lock->cb_items = NULL;
66 while (items != NULL) {
68 items->fn(items->data);
74 int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
76 struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
83 new->next = lock->cb_items;
88 void *ossl_rcu_uptr_deref(void **p)
93 void ossl_rcu_assign_uptr(void **p, void **v)
95 *(void **)p = *(void **)v;
98 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
102 if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
103 /* Don't set error, to avoid recursion blowup. */
106 *(unsigned int *)lock = 1;
111 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
113 if (!ossl_assert(*(unsigned int *)lock == 1))
118 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
120 if (!ossl_assert(*(unsigned int *)lock == 1))
125 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
127 if (!ossl_assert(*(unsigned int *)lock == 1))
132 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
136 *(unsigned int *)lock = 0;
142 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
153 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
155 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
157 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
159 static unsigned int thread_local_key = 0;
161 if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
164 *key = thread_local_key++;
166 thread_local_storage[*key] = NULL;
171 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
173 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
176 return thread_local_storage[*key];
179 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
181 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
184 thread_local_storage[*key] = val;
189 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
191 *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
195 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
200 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
205 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
213 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
222 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
229 int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
236 int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
243 int openssl_init_fork_handlers(void)
248 int openssl_get_fork_id(void)
250 # if defined(OPENSSL_SYS_UNIX)