2 * Copyright 2016-2021 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"
13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
15 # if defined(OPENSSL_SYS_UNIX)
16 # include <sys/types.h>
20 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
24 if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
25 /* Don't set error, to avoid recursion blowup. */
28 *(unsigned int *)lock = 1;
33 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
35 if (!ossl_assert(*(unsigned int *)lock == 1))
40 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
42 if (!ossl_assert(*(unsigned int *)lock == 1))
47 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
49 if (!ossl_assert(*(unsigned int *)lock == 1))
54 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
58 *(unsigned int *)lock = 0;
64 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
75 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
77 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
79 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
81 static unsigned int thread_local_key = 0;
83 if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
86 *key = thread_local_key++;
88 thread_local_storage[*key] = NULL;
93 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
95 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
98 return thread_local_storage[*key];
101 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
103 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
106 thread_local_storage[*key] = val;
111 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
113 *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
117 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
122 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
127 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
135 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
144 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
151 int openssl_init_fork_handlers(void)
156 int openssl_get_fork_id(void)
158 # if defined(OPENSSL_SYS_UNIX)