Add a CRYPTO_atomic_read call which allows an int variable to be read
[openssl.git] / crypto / threads_none.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <openssl/crypto.h>
11 #include "internal/cryptlib.h"
12
13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
14
15 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
16 {
17     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(unsigned int));
18     if (lock == NULL)
19         return NULL;
20
21     *(unsigned int *)lock = 1;
22
23     return lock;
24 }
25
26 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
27 {
28     if (!ossl_assert(*(unsigned int *)lock == 1))
29         return 0;
30     return 1;
31 }
32
33 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
34 {
35     if (!ossl_assert(*(unsigned int *)lock == 1))
36         return 0;
37     return 1;
38 }
39
40 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
41 {
42     if (!ossl_assert(*(unsigned int *)lock == 1))
43         return 0;
44     return 1;
45 }
46
47 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
48     if (lock == NULL)
49         return;
50
51     *(unsigned int *)lock = 0;
52     OPENSSL_free(lock);
53
54     return;
55 }
56
57 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
58 {
59     if (*once != 0)
60         return 1;
61
62     init();
63     *once = 1;
64
65     return 1;
66 }
67
68 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
69
70 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
71
72 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
73 {
74     static unsigned int thread_local_key = 0;
75
76     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
77         return 0;
78
79     *key = thread_local_key++;
80
81     thread_local_storage[*key] = NULL;
82
83     return 1;
84 }
85
86 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
87 {
88     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
89         return NULL;
90
91     return thread_local_storage[*key];
92 }
93
94 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
95 {
96     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
97         return 0;
98
99     thread_local_storage[*key] = val;
100
101     return 1;
102 }
103
104 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
105 {
106     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
107     return 1;
108 }
109
110 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
111 {
112     return 0;
113 }
114
115 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
116 {
117     return (a == b);
118 }
119
120 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
121 {
122     *val += amount;
123     *ret  = *val;
124
125     return 1;
126 }
127
128 int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock)
129 {
130     *ret  = *val;
131     return 1;
132 }
133
134 int openssl_init_fork_handlers(void)
135 {
136     return 0;
137 }
138
139 #endif