2 * Copyright 2016-2018 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"
17 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
19 # ifdef PTHREAD_RWLOCK_INITIALIZER
23 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
28 if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
29 /* Don't set error, to avoid recursion blowup. */
33 if (pthread_rwlock_init(lock, NULL) != 0) {
38 pthread_mutexattr_t attr;
41 if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
42 /* Don't set error, to avoid recursion blowup. */
46 pthread_mutexattr_init(&attr);
47 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
49 if (pthread_mutex_init(lock, &attr) != 0) {
50 pthread_mutexattr_destroy(&attr);
55 pthread_mutexattr_destroy(&attr);
61 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
64 if (pthread_rwlock_rdlock(lock) != 0)
67 if (pthread_mutex_lock(lock) != 0)
74 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
77 if (pthread_rwlock_wrlock(lock) != 0)
80 if (pthread_mutex_lock(lock) != 0)
87 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
90 if (pthread_rwlock_unlock(lock) != 0)
93 if (pthread_mutex_unlock(lock) != 0)
100 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
106 pthread_rwlock_destroy(lock);
108 pthread_mutex_destroy(lock);
115 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
117 if (pthread_once(once, init) != 0)
123 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
125 if (pthread_key_create(key, cleanup) != 0)
131 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
133 return pthread_getspecific(*key);
136 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
138 if (pthread_setspecific(*key, val) != 0)
144 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
146 if (pthread_key_delete(*key) != 0)
152 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
154 return pthread_self();
157 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
159 return pthread_equal(a, b);
162 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
164 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
165 if (__atomic_is_lock_free(sizeof(*val), val)) {
166 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
169 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
170 /* This will work for all future Solaris versions. */
172 *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
176 if (!CRYPTO_THREAD_write_lock(lock))
182 if (!CRYPTO_THREAD_unlock(lock))
189 /* TODO(3.0): No fork protection in FIPS module yet! */
191 # ifdef OPENSSL_SYS_UNIX
192 static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
194 static void fork_once_func(void)
196 pthread_atfork(OPENSSL_fork_prepare,
197 OPENSSL_fork_parent, OPENSSL_fork_child);
201 int openssl_init_fork_handlers(void)
203 # ifdef OPENSSL_SYS_UNIX
204 if (pthread_once(&fork_once_control, fork_once_func) == 0)
209 # endif /* FIPS_MODE */