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
= OPENSSL_zalloc(sizeof(unsigned int))) == NULL
) {
25 /* Don't set error, to avoid recursion blowup. */
29 *(unsigned int *)lock
= 1;
34 __owur
int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
36 if (!ossl_assert(*(unsigned int *)lock
== 1))
41 __owur
int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
43 if (!ossl_assert(*(unsigned int *)lock
== 1))
48 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
50 if (!ossl_assert(*(unsigned int *)lock
== 1))
55 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
) {
59 *(unsigned int *)lock
= 0;
65 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
76 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
78 static void *thread_local_storage
[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
];
80 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
82 static unsigned int thread_local_key
= 0;
84 if (thread_local_key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
87 *key
= thread_local_key
++;
89 thread_local_storage
[*key
] = NULL
;
94 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
96 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
99 return thread_local_storage
[*key
];
102 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
104 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
107 thread_local_storage
[*key
] = val
;
112 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
114 *key
= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
+ 1;
118 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
123 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
128 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
136 int CRYPTO_atomic_or(uint64_t *val
, uint64_t op
, uint64_t *ret
,
145 int CRYPTO_atomic_load(uint64_t *val
, uint64_t *ret
, CRYPTO_RWLOCK
*lock
)
152 int openssl_init_fork_handlers(void)
157 int openssl_get_fork_id(void)
159 # if defined(OPENSSL_SYS_UNIX)