Fix a few if(, for(, while( inside code.
[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
12 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
13
14 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
15 {
16     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(unsigned int));
17     if (lock == NULL)
18         return NULL;
19
20     *(unsigned int *)lock = 1;
21
22     return lock;
23 }
24
25 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
26 {
27     OPENSSL_assert(*(unsigned int *)lock == 1);
28     return 1;
29 }
30
31 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
32 {
33     OPENSSL_assert(*(unsigned int *)lock == 1);
34     return 1;
35 }
36
37 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
38 {
39     OPENSSL_assert(*(unsigned int *)lock == 1);
40     return 1;
41 }
42
43 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
44     if (lock == NULL)
45         return;
46
47     *(unsigned int *)lock = 0;
48     OPENSSL_free(lock);
49
50     return;
51 }
52
53 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
54 {
55     if (*once != 0)
56         return 1;
57
58     init();
59     *once = 1;
60
61     return 1;
62 }
63
64 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
65
66 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
67
68 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
69 {
70     static unsigned int thread_local_key = 0;
71
72     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
73         return 0;
74
75     *key = thread_local_key++;
76
77     thread_local_storage[*key] = NULL;
78
79     return 1;
80 }
81
82 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
83 {
84     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
85         return NULL;
86
87     return thread_local_storage[*key];
88 }
89
90 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
91 {
92     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
93         return 0;
94
95     thread_local_storage[*key] = val;
96
97     return 1;
98 }
99
100 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
101 {
102     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
103     return 1;
104 }
105
106 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
107 {
108     return 0;
109 }
110
111 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
112 {
113     return (a == b);
114 }
115
116 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
117 {
118     *val += amount;
119     *ret  = *val;
120
121     return 1;
122 }
123
124 #endif