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)
14 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
16 CRYPTO_RWLOCK
*lock
= OPENSSL_zalloc(sizeof(unsigned int));
20 *(unsigned int *)lock
= 1;
25 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
27 OPENSSL_assert(*(unsigned int *)lock
== 1);
31 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
33 OPENSSL_assert(*(unsigned int *)lock
== 1);
37 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
39 OPENSSL_assert(*(unsigned int *)lock
== 1);
43 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
) {
47 *(unsigned int *)lock
= 0;
53 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
64 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
66 static void *thread_local_storage
[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
];
68 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
70 static unsigned int thread_local_key
= 0;
72 if (thread_local_key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
75 *key
= thread_local_key
++;
77 thread_local_storage
[*key
] = NULL
;
82 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
84 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
87 return thread_local_storage
[*key
];
90 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
92 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
95 thread_local_storage
[*key
] = val
;
100 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
102 *key
= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
+ 1;
106 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
111 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
116 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)