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)
15 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
17 CRYPTO_RWLOCK
*lock
= OPENSSL_zalloc(sizeof(unsigned int));
21 *(unsigned int *)lock
= 1;
26 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
28 if (!ossl_assert(*(unsigned int *)lock
== 1))
33 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
35 if (!ossl_assert(*(unsigned int *)lock
== 1))
40 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
42 if (!ossl_assert(*(unsigned int *)lock
== 1))
47 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
) {
51 *(unsigned int *)lock
= 0;
57 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
68 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
70 static void *thread_local_storage
[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
];
72 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
74 static unsigned int thread_local_key
= 0;
76 if (thread_local_key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
79 *key
= thread_local_key
++;
81 thread_local_storage
[*key
] = NULL
;
86 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
88 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
91 return thread_local_storage
[*key
];
94 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
96 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
99 thread_local_storage
[*key
] = val
;
104 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
106 *key
= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
+ 1;
110 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
115 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
120 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
128 int openssl_init_fork_handlers(void)