2 * Copyright 2016-2019 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 # if defined(OPENSSL_SYS_UNIX)
16 # include <sys/types.h>
20 # ifdef PTHREAD_RWLOCK_INITIALIZER
24 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
29 if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
30 /* Don't set error, to avoid recursion blowup. */
34 if (pthread_rwlock_init(lock, NULL) != 0) {
39 pthread_mutexattr_t attr;
42 if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
43 /* Don't set error, to avoid recursion blowup. */
47 pthread_mutexattr_init(&attr);
48 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
50 if (pthread_mutex_init(lock, &attr) != 0) {
51 pthread_mutexattr_destroy(&attr);
56 pthread_mutexattr_destroy(&attr);
62 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
65 if (pthread_rwlock_rdlock(lock) != 0)
68 if (pthread_mutex_lock(lock) != 0)
75 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
78 if (pthread_rwlock_wrlock(lock) != 0)
81 if (pthread_mutex_lock(lock) != 0)
88 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
91 if (pthread_rwlock_unlock(lock) != 0)
94 if (pthread_mutex_unlock(lock) != 0)
101 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
107 pthread_rwlock_destroy(lock);
109 pthread_mutex_destroy(lock);
116 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
118 if (pthread_once(once, init) != 0)
124 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
126 if (pthread_key_create(key, cleanup) != 0)
132 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
134 return pthread_getspecific(*key);
137 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
139 if (pthread_setspecific(*key, val) != 0)
145 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
147 if (pthread_key_delete(*key) != 0)
153 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
155 return pthread_self();
158 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
160 return pthread_equal(a, b);
163 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
165 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
166 if (__atomic_is_lock_free(sizeof(*val), val)) {
167 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
171 if (!CRYPTO_THREAD_write_lock(lock))
177 if (!CRYPTO_THREAD_unlock(lock))
183 # ifdef OPENSSL_SYS_UNIX
184 static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
186 static void fork_once_func(void)
188 pthread_atfork(OPENSSL_fork_prepare,
189 OPENSSL_fork_parent, OPENSSL_fork_child);
193 int openssl_init_fork_handlers(void)
195 # ifdef OPENSSL_SYS_UNIX
196 if (pthread_once(&fork_once_control, fork_once_func) == 0)
202 int openssl_get_fork_id(void)