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>
12 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
14 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
16 CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));
20 if (pthread_rwlock_init(lock, NULL) != 0) {
28 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
30 if (pthread_rwlock_rdlock(lock) != 0)
36 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
38 if (pthread_rwlock_wrlock(lock) != 0)
44 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
46 if (pthread_rwlock_unlock(lock) != 0)
52 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
57 pthread_rwlock_destroy(lock);
63 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
65 if (pthread_once(once, init) != 0)
71 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
73 if (pthread_key_create(key, cleanup) != 0)
79 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
81 return pthread_getspecific(*key);
84 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
86 if (pthread_setspecific(*key, val) != 0)
92 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
94 if (pthread_key_delete(*key) != 0)
100 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
102 return pthread_self();
105 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
107 return pthread_equal(a, b);
110 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
112 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
113 if (__atomic_is_lock_free(sizeof(*val), val)) {
114 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
118 if (!CRYPTO_THREAD_write_lock(lock))
124 if (!CRYPTO_THREAD_unlock(lock))