2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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) && !defined(OPENSSL_SYS_WINDOWS)
15 # ifdef PTHREAD_RWLOCK_INITIALIZER
19 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
22 CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));
26 if (pthread_rwlock_init(lock, NULL) != 0) {
31 pthread_mutexattr_t attr;
32 CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_mutex_t));
36 pthread_mutexattr_init(&attr);
37 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
39 if (pthread_mutex_init(lock, &attr) != 0) {
40 pthread_mutexattr_destroy(&attr);
45 pthread_mutexattr_destroy(&attr);
51 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
54 if (pthread_rwlock_rdlock(lock) != 0)
57 if (pthread_mutex_lock(lock) != 0)
64 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
67 if (pthread_rwlock_wrlock(lock) != 0)
70 if (pthread_mutex_lock(lock) != 0)
77 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
80 if (pthread_rwlock_unlock(lock) != 0)
83 if (pthread_mutex_unlock(lock) != 0)
90 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
96 pthread_rwlock_destroy(lock);
98 pthread_mutex_destroy(lock);
105 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
107 if (pthread_once(once, init) != 0)
113 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
115 if (pthread_key_create(key, cleanup) != 0)
121 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
123 return pthread_getspecific(*key);
126 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
128 if (pthread_setspecific(*key, val) != 0)
134 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
136 if (pthread_key_delete(*key) != 0)
142 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
144 return pthread_self();
147 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
149 return pthread_equal(a, b);
152 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
154 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
155 if (__atomic_is_lock_free(sizeof(*val), val)) {
156 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
160 if (!CRYPTO_THREAD_write_lock(lock))
166 if (!CRYPTO_THREAD_unlock(lock))
172 int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock)
174 # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE)
175 if (__atomic_is_lock_free(sizeof(*val), val)) {
176 __atomic_load(val, ret, __ATOMIC_ACQUIRE);
180 if (!CRYPTO_THREAD_read_lock(lock))
185 if (!CRYPTO_THREAD_unlock(lock))
191 int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock)
193 # if defined(__GNUC__) && defined(__ATOMIC_RELEASE)
194 if (__atomic_is_lock_free(sizeof(*val), val)) {
195 __atomic_store(val, &n, __ATOMIC_RELEASE);
199 if (!CRYPTO_THREAD_write_lock(lock))
204 if (!CRYPTO_THREAD_unlock(lock))
210 # ifdef OPENSSL_SYS_UNIX
211 static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
213 static void fork_once_func(void)
215 pthread_atfork(OPENSSL_fork_prepare,
216 OPENSSL_fork_parent, OPENSSL_fork_child);
220 int openssl_init_fork_handlers(void)
222 # ifdef OPENSSL_SYS_UNIX
223 if (pthread_once(&fork_once_control, fork_once_func) == 0)