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