NonStop port updates for 3.0.0.
[openssl.git] / crypto / threads_pthread.c
1 /*
2  * Copyright 2016-2020 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(__sun)
14 # include <atomic.h>
15 #endif
16
17 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
18
19 # if defined(OPENSSL_SYS_UNIX)
20 #  include <sys/types.h>
21 #  include <unistd.h>
22 #endif
23
24 # ifdef PTHREAD_RWLOCK_INITIALIZER
25 #  define USE_RWLOCK
26 # endif
27
28 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
29 {
30 # ifdef USE_RWLOCK
31     CRYPTO_RWLOCK *lock;
32
33     if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
34         /* Don't set error, to avoid recursion blowup. */
35         return NULL;
36     }
37
38     if (pthread_rwlock_init(lock, NULL) != 0) {
39         OPENSSL_free(lock);
40         return NULL;
41     }
42 # else
43     pthread_mutexattr_t attr;
44     CRYPTO_RWLOCK *lock;
45
46     if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
47         /* Don't set error, to avoid recursion blowup. */
48         return NULL;
49     }
50
51     pthread_mutexattr_init(&attr);
52     #if defined(__TANDEM) && defined(_SPT_MODEL_)
53       pthread_mutexattr_setkind_np(&attr,MUTEX_RECURSIVE_NP);
54     #else
55       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
56     #endif
57
58     if (pthread_mutex_init(lock, &attr) != 0) {
59         pthread_mutexattr_destroy(&attr);
60         OPENSSL_free(lock);
61         return NULL;
62     }
63
64     pthread_mutexattr_destroy(&attr);
65 # endif
66
67     return lock;
68 }
69
70 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
71 {
72 # ifdef USE_RWLOCK
73     if (pthread_rwlock_rdlock(lock) != 0)
74         return 0;
75 # else
76     if (pthread_mutex_lock(lock) != 0)
77         return 0;
78 # endif
79
80     return 1;
81 }
82
83 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
84 {
85 # ifdef USE_RWLOCK
86     if (pthread_rwlock_wrlock(lock) != 0)
87         return 0;
88 # else
89     if (pthread_mutex_lock(lock) != 0)
90         return 0;
91 # endif
92
93     return 1;
94 }
95
96 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
97 {
98 # ifdef USE_RWLOCK
99     if (pthread_rwlock_unlock(lock) != 0)
100         return 0;
101 # else
102     if (pthread_mutex_unlock(lock) != 0)
103         return 0;
104 # endif
105
106     return 1;
107 }
108
109 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
110 {
111     if (lock == NULL)
112         return;
113
114 # ifdef USE_RWLOCK
115     pthread_rwlock_destroy(lock);
116 # else
117     pthread_mutex_destroy(lock);
118 # endif
119     OPENSSL_free(lock);
120
121     return;
122 }
123
124 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
125 {
126     if (pthread_once(once, init) != 0)
127         return 0;
128
129     return 1;
130 }
131
132 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
133 {
134     if (pthread_key_create(key, cleanup) != 0)
135         return 0;
136
137     return 1;
138 }
139
140 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
141 {
142     return pthread_getspecific(*key);
143 }
144
145 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
146 {
147     if (pthread_setspecific(*key, val) != 0)
148         return 0;
149
150     return 1;
151 }
152
153 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
154 {
155     if (pthread_key_delete(*key) != 0)
156         return 0;
157
158     return 1;
159 }
160
161 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
162 {
163     return pthread_self();
164 }
165
166 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
167 {
168     return pthread_equal(a, b);
169 }
170
171 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
172 {
173 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
174     if (__atomic_is_lock_free(sizeof(*val), val)) {
175         *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
176         return 1;
177     }
178 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
179     /* This will work for all future Solaris versions. */
180     if (ret != NULL) {
181         *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
182         return 1;
183     }
184 # endif
185     if (!CRYPTO_THREAD_write_lock(lock))
186         return 0;
187
188     *val += amount;
189     *ret  = *val;
190
191     if (!CRYPTO_THREAD_unlock(lock))
192         return 0;
193
194     return 1;
195 }
196
197 # ifndef FIPS_MODULE
198 /* TODO(3.0): No fork protection in FIPS module yet! */
199
200 #  ifdef OPENSSL_SYS_UNIX
201 static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
202
203 static void fork_once_func(void)
204 {
205     pthread_atfork(OPENSSL_fork_prepare,
206                    OPENSSL_fork_parent, OPENSSL_fork_child);
207 }
208 #  endif
209
210 int openssl_init_fork_handlers(void)
211 {
212 #  ifdef OPENSSL_SYS_UNIX
213     if (pthread_once(&fork_once_control, fork_once_func) == 0)
214         return 1;
215 #  endif
216     return 0;
217 }
218 # endif /* FIPS_MODULE */
219
220 int openssl_get_fork_id(void)
221 {
222     return getpid();
223 }
224 #endif