1 | /* |
2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License 2.0 (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 | # if defined(OPENSSL_SYS_UNIX) |
16 | # include <sys/types.h> |
17 | # include <unistd.h> |
18 | # endif |
19 | |
20 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
21 | { |
22 | CRYPTO_RWLOCK *lock; |
23 | |
24 | if ((lock = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) { |
25 | /* Don't set error, to avoid recursion blowup. */ |
26 | return NULL; |
27 | } |
28 | |
29 | *(unsigned int *)lock = 1; |
30 | |
31 | return lock; |
32 | } |
33 | |
34 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) |
35 | { |
36 | if (!ossl_assert(*(unsigned int *)lock == 1)) |
37 | return 0; |
38 | return 1; |
39 | } |
40 | |
41 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) |
42 | { |
43 | if (!ossl_assert(*(unsigned int *)lock == 1)) |
44 | return 0; |
45 | return 1; |
46 | } |
47 | |
48 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) |
49 | { |
50 | if (!ossl_assert(*(unsigned int *)lock == 1)) |
51 | return 0; |
52 | return 1; |
53 | } |
54 | |
55 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) { |
56 | if (lock == NULL) |
57 | return; |
58 | |
59 | *(unsigned int *)lock = 0; |
60 | OPENSSL_free(lock); |
61 | |
62 | return; |
63 | } |
64 | |
65 | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) |
66 | { |
67 | if (*once != 0) |
68 | return 1; |
69 | |
70 | init(); |
71 | *once = 1; |
72 | |
73 | return 1; |
74 | } |
75 | |
76 | #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256 |
77 | |
78 | static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX]; |
79 | |
80 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) |
81 | { |
82 | static unsigned int thread_local_key = 0; |
83 | |
84 | if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) |
85 | return 0; |
86 | |
87 | *key = thread_local_key++; |
88 | |
89 | thread_local_storage[*key] = NULL; |
90 | |
91 | return 1; |
92 | } |
93 | |
94 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) |
95 | { |
96 | if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) |
97 | return NULL; |
98 | |
99 | return thread_local_storage[*key]; |
100 | } |
101 | |
102 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) |
103 | { |
104 | if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX) |
105 | return 0; |
106 | |
107 | thread_local_storage[*key] = val; |
108 | |
109 | return 1; |
110 | } |
111 | |
112 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) |
113 | { |
114 | *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1; |
115 | return 1; |
116 | } |
117 | |
118 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) |
119 | { |
120 | return 0; |
121 | } |
122 | |
123 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) |
124 | { |
125 | return (a == b); |
126 | } |
127 | |
128 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) |
129 | { |
130 | *val += amount; |
131 | *ret = *val; |
132 | |
133 | return 1; |
134 | } |
135 | |
136 | int openssl_init_fork_handlers(void) |
137 | { |
138 | return 0; |
139 | } |
140 | |
141 | int openssl_get_fork_id(void) |
142 | { |
143 | # if defined(OPENSSL_SYS_UNIX) |
144 | return getpid(); |
145 | # else |
146 | return 0; |
147 | # endif |
148 | } |
149 | #endif |
150 | |