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 /* We need to use the OPENSSL_fork_*() deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
20 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
22 # if defined(OPENSSL_SYS_UNIX)
23 # include <sys/types.h>
29 # ifdef PTHREAD_RWLOCK_INITIALIZER
33 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
38 if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
39 /* Don't set error, to avoid recursion blowup. */
43 if (pthread_rwlock_init(lock, NULL) != 0) {
48 pthread_mutexattr_t attr;
51 if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
52 /* Don't set error, to avoid recursion blowup. */
57 * We don't use recursive mutexes, but try to catch errors if we do.
59 pthread_mutexattr_init(&attr);
60 # if !defined (__TANDEM) && !defined (_SPT_MODEL_)
61 # if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
62 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
64 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
67 /* The SPT Thread Library does not define MUTEX attributes. */
70 if (pthread_mutex_init(lock, &attr) != 0) {
71 pthread_mutexattr_destroy(&attr);
76 pthread_mutexattr_destroy(&attr);
82 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
85 if (pthread_rwlock_rdlock(lock) != 0)
88 if (pthread_mutex_lock(lock) != 0) {
89 assert(errno != EDEADLK && errno != EBUSY);
97 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
100 if (pthread_rwlock_wrlock(lock) != 0)
103 if (pthread_mutex_lock(lock) != 0) {
104 assert(errno != EDEADLK && errno != EBUSY);
112 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
115 if (pthread_rwlock_unlock(lock) != 0)
118 if (pthread_mutex_unlock(lock) != 0) {
119 assert(errno != EPERM);
127 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
133 pthread_rwlock_destroy(lock);
135 pthread_mutex_destroy(lock);
142 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
144 if (pthread_once(once, init) != 0)
150 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
152 if (pthread_key_create(key, cleanup) != 0)
158 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
160 return pthread_getspecific(*key);
163 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
165 if (pthread_setspecific(*key, val) != 0)
171 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
173 if (pthread_key_delete(*key) != 0)
179 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
181 return pthread_self();
184 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
186 return pthread_equal(a, b);
189 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
191 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
192 if (__atomic_is_lock_free(sizeof(*val), val)) {
193 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
196 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
197 /* This will work for all future Solaris versions. */
199 *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
203 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
209 if (!CRYPTO_THREAD_unlock(lock))
215 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
218 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
219 if (__atomic_is_lock_free(sizeof(*val), val)) {
220 *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
223 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
224 /* This will work for all future Solaris versions. */
226 *ret = atomic_or_64_nv(val, op);
230 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
235 if (!CRYPTO_THREAD_unlock(lock))
241 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
243 # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE)
244 if (__atomic_is_lock_free(sizeof(*val), val)) {
245 __atomic_load(val, ret, __ATOMIC_ACQUIRE);
248 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
249 /* This will work for all future Solaris versions. */
251 *ret = atomic_or_64_nv(val, 0);
255 if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
258 if (!CRYPTO_THREAD_unlock(lock))
264 int openssl_init_fork_handlers(void)
268 # endif /* FIPS_MODULE */
270 int openssl_get_fork_id(void)