]>
Commit | Line | Data |
---|---|---|
58964a49 RE |
1 | /* crypto/threads/th-lock.c */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | |
3 | * All rights reserved. | |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 8 | * |
58964a49 RE |
9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 15 | * |
58964a49 RE |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 22 | * |
58964a49 RE |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
58964a49 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
58964a49 RE |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
0f113f3e | 52 | * |
58964a49 RE |
53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <string.h> | |
62 | #include <errno.h> | |
63 | #ifdef LINUX | |
0f113f3e | 64 | # include <typedefs.h> |
58964a49 | 65 | #endif |
bc36ee62 | 66 | #ifdef OPENSSL_SYS_WIN32 |
0f113f3e | 67 | # include <windows.h> |
58964a49 RE |
68 | #endif |
69 | #ifdef SOLARIS | |
0f113f3e MC |
70 | # include <synch.h> |
71 | # include <thread.h> | |
58964a49 RE |
72 | #endif |
73 | #ifdef IRIX | |
0f113f3e MC |
74 | # include <ulocks.h> |
75 | # include <sys/prctl.h> | |
58964a49 | 76 | #endif |
2d2d3139 | 77 | #ifdef PTHREADS |
0f113f3e | 78 | # include <pthread.h> |
2d2d3139 | 79 | #endif |
ec577822 BM |
80 | #include <openssl/lhash.h> |
81 | #include <openssl/crypto.h> | |
82 | #include <openssl/buffer.h> | |
59ade205 | 83 | #include "../../e_os.h" |
ec577822 BM |
84 | #include <openssl/x509.h> |
85 | #include <openssl/ssl.h> | |
86 | #include <openssl/err.h> | |
58964a49 | 87 | |
2d2d3139 | 88 | void CRYPTO_thread_setup(void); |
58964a49 RE |
89 | void CRYPTO_thread_cleanup(void); |
90 | ||
0f113f3e MC |
91 | static void irix_locking_callback(int mode, int type, char *file, int line); |
92 | static void solaris_locking_callback(int mode, int type, char *file, | |
93 | int line); | |
94 | static void win32_locking_callback(int mode, int type, char *file, int line); | |
95 | static void pthreads_locking_callback(int mode, int type, char *file, | |
96 | int line); | |
58964a49 | 97 | |
0f113f3e MC |
98 | static unsigned long irix_thread_id(void); |
99 | static unsigned long solaris_thread_id(void); | |
100 | static unsigned long pthreads_thread_id(void); | |
58964a49 | 101 | |
3a83462d MC |
102 | /*- |
103 | * usage: | |
58964a49 | 104 | * CRYPTO_thread_setup(); |
657e60fa | 105 | * application code |
58964a49 RE |
106 | * CRYPTO_thread_cleanup(); |
107 | */ | |
108 | ||
109 | #define THREAD_STACK_SIZE (16*1024) | |
110 | ||
bc36ee62 | 111 | #ifdef OPENSSL_SYS_WIN32 |
58964a49 | 112 | |
2d2d3139 | 113 | static HANDLE *lock_cs; |
58964a49 | 114 | |
2d2d3139 | 115 | void CRYPTO_thread_setup(void) |
0f113f3e MC |
116 | { |
117 | int i; | |
58964a49 | 118 | |
0f113f3e | 119 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); |
61986d32 | 120 | if (!lock_cs) { |
918bb865 MC |
121 | /* Nothing we can do about this...void function! */ |
122 | return; | |
123 | } | |
0f113f3e MC |
124 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
125 | lock_cs[i] = CreateMutex(NULL, FALSE, NULL); | |
126 | } | |
58964a49 | 127 | |
0f113f3e MC |
128 | CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) |
129 | win32_locking_callback); | |
130 | /* id callback defined */ | |
131 | return (1); | |
132 | } | |
58964a49 | 133 | |
6b691a5c | 134 | static void CRYPTO_thread_cleanup(void) |
0f113f3e MC |
135 | { |
136 | int i; | |
58964a49 | 137 | |
0f113f3e MC |
138 | CRYPTO_set_locking_callback(NULL); |
139 | for (i = 0; i < CRYPTO_num_locks(); i++) | |
140 | CloseHandle(lock_cs[i]); | |
141 | OPENSSL_free(lock_cs); | |
142 | } | |
58964a49 | 143 | |
6b691a5c | 144 | void win32_locking_callback(int mode, int type, char *file, int line) |
0f113f3e MC |
145 | { |
146 | if (mode & CRYPTO_LOCK) { | |
147 | WaitForSingleObject(lock_cs[type], INFINITE); | |
148 | } else { | |
149 | ReleaseMutex(lock_cs[type]); | |
150 | } | |
151 | } | |
152 | ||
153 | #endif /* OPENSSL_SYS_WIN32 */ | |
58964a49 RE |
154 | |
155 | #ifdef SOLARIS | |
156 | ||
0f113f3e | 157 | # define USE_MUTEX |
58964a49 | 158 | |
0f113f3e | 159 | # ifdef USE_MUTEX |
2d2d3139 | 160 | static mutex_t *lock_cs; |
0f113f3e | 161 | # else |
2d2d3139 | 162 | static rwlock_t *lock_cs; |
0f113f3e | 163 | # endif |
2d2d3139 | 164 | static long *lock_count; |
58964a49 | 165 | |
6b691a5c | 166 | void CRYPTO_thread_setup(void) |
0f113f3e MC |
167 | { |
168 | int i; | |
169 | ||
170 | # ifdef USE_MUTEX | |
171 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); | |
172 | # else | |
173 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t)); | |
174 | # endif | |
61986d32 | 175 | if (!lock_cs) { |
918bb865 MC |
176 | /* Nothing we can do about this...void function! */ |
177 | return; | |
178 | } | |
0f113f3e MC |
179 | lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); |
180 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
181 | lock_count[i] = 0; | |
182 | # ifdef USE_MUTEX | |
183 | mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL); | |
184 | # else | |
185 | rwlock_init(&(lock_cs[i]), USYNC_THREAD, NULL); | |
186 | # endif | |
187 | } | |
188 | ||
189 | CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id); | |
190 | CRYPTO_set_locking_callback((void (*)())solaris_locking_callback); | |
191 | } | |
58964a49 | 192 | |
6b691a5c | 193 | void CRYPTO_thread_cleanup(void) |
0f113f3e MC |
194 | { |
195 | int i; | |
196 | ||
197 | CRYPTO_set_locking_callback(NULL); | |
198 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
199 | # ifdef USE_MUTEX | |
200 | mutex_destroy(&(lock_cs[i])); | |
201 | # else | |
202 | rwlock_destroy(&(lock_cs[i])); | |
203 | # endif | |
204 | } | |
205 | OPENSSL_free(lock_cs); | |
206 | OPENSSL_free(lock_count); | |
207 | } | |
58964a49 | 208 | |
6b691a5c | 209 | void solaris_locking_callback(int mode, int type, char *file, int line) |
0f113f3e | 210 | { |
0f113f3e MC |
211 | if (mode & CRYPTO_LOCK) { |
212 | # ifdef USE_MUTEX | |
213 | mutex_lock(&(lock_cs[type])); | |
214 | # else | |
215 | if (mode & CRYPTO_READ) | |
216 | rw_rdlock(&(lock_cs[type])); | |
217 | else | |
218 | rw_wrlock(&(lock_cs[type])); | |
219 | # endif | |
220 | lock_count[type]++; | |
221 | } else { | |
222 | # ifdef USE_MUTEX | |
223 | mutex_unlock(&(lock_cs[type])); | |
224 | # else | |
225 | rw_unlock(&(lock_cs[type])); | |
226 | # endif | |
227 | } | |
228 | } | |
58964a49 | 229 | |
6b691a5c | 230 | unsigned long solaris_thread_id(void) |
0f113f3e MC |
231 | { |
232 | unsigned long ret; | |
58964a49 | 233 | |
0f113f3e MC |
234 | ret = (unsigned long)thr_self(); |
235 | return (ret); | |
236 | } | |
237 | #endif /* SOLARIS */ | |
58964a49 RE |
238 | |
239 | #ifdef IRIX | |
240 | /* I don't think this works..... */ | |
241 | ||
242 | static usptr_t *arena; | |
2d2d3139 | 243 | static usema_t **lock_cs; |
58964a49 | 244 | |
6b691a5c | 245 | void CRYPTO_thread_setup(void) |
0f113f3e MC |
246 | { |
247 | int i; | |
248 | char filename[20]; | |
249 | ||
918bb865 | 250 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); |
61986d32 | 251 | if (!lock_cs) { |
918bb865 MC |
252 | /* Nothing we can do about this...void function! */ |
253 | return; | |
254 | } | |
255 | ||
0f113f3e MC |
256 | strcpy(filename, "/tmp/mttest.XXXXXX"); |
257 | mktemp(filename); | |
258 | ||
259 | usconfig(CONF_STHREADIOOFF); | |
260 | usconfig(CONF_STHREADMALLOCOFF); | |
261 | usconfig(CONF_INITUSERS, 100); | |
262 | usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); | |
263 | arena = usinit(filename); | |
264 | unlink(filename); | |
265 | ||
0f113f3e MC |
266 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
267 | lock_cs[i] = usnewsema(arena, 1); | |
268 | } | |
269 | ||
270 | CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id); | |
271 | CRYPTO_set_locking_callback((void (*)())irix_locking_callback); | |
272 | } | |
58964a49 | 273 | |
6b691a5c | 274 | void CRYPTO_thread_cleanup(void) |
0f113f3e MC |
275 | { |
276 | int i; | |
58964a49 | 277 | |
0f113f3e MC |
278 | CRYPTO_set_locking_callback(NULL); |
279 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
280 | char buf[10]; | |
58964a49 | 281 | |
0f113f3e MC |
282 | sprintf(buf, "%2d:", i); |
283 | usdumpsema(lock_cs[i], stdout, buf); | |
284 | usfreesema(lock_cs[i], arena); | |
285 | } | |
286 | OPENSSL_free(lock_cs); | |
287 | } | |
58964a49 | 288 | |
6b691a5c | 289 | void irix_locking_callback(int mode, int type, char *file, int line) |
0f113f3e MC |
290 | { |
291 | if (mode & CRYPTO_LOCK) { | |
292 | uspsema(lock_cs[type]); | |
293 | } else { | |
294 | usvsema(lock_cs[type]); | |
295 | } | |
296 | } | |
58964a49 | 297 | |
6b691a5c | 298 | unsigned long irix_thread_id(void) |
0f113f3e MC |
299 | { |
300 | unsigned long ret; | |
58964a49 | 301 | |
0f113f3e MC |
302 | ret = (unsigned long)getpid(); |
303 | return (ret); | |
304 | } | |
305 | #endif /* IRIX */ | |
58964a49 RE |
306 | |
307 | /* Linux and a few others */ | |
308 | #ifdef PTHREADS | |
309 | ||
2d2d3139 RL |
310 | static pthread_mutex_t *lock_cs; |
311 | static long *lock_count; | |
58964a49 | 312 | |
6b691a5c | 313 | void CRYPTO_thread_setup(void) |
0f113f3e MC |
314 | { |
315 | int i; | |
58964a49 | 316 | |
0f113f3e MC |
317 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); |
318 | lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); | |
61986d32 | 319 | if (!lock_cs || !lock_count) { |
918bb865 | 320 | /* Nothing we can do about this...void function! */ |
61986d32 | 321 | if (lock_cs) |
918bb865 | 322 | OPENSSL_free(lock_cs); |
61986d32 | 323 | if (lock_count) |
918bb865 MC |
324 | OPENSSL_free(lock_count); |
325 | return; | |
326 | } | |
0f113f3e MC |
327 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
328 | lock_count[i] = 0; | |
329 | pthread_mutex_init(&(lock_cs[i]), NULL); | |
330 | } | |
58964a49 | 331 | |
0f113f3e MC |
332 | CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); |
333 | CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); | |
334 | } | |
58964a49 | 335 | |
6b691a5c | 336 | void thread_cleanup(void) |
0f113f3e MC |
337 | { |
338 | int i; | |
339 | ||
340 | CRYPTO_set_locking_callback(NULL); | |
341 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
342 | pthread_mutex_destroy(&(lock_cs[i])); | |
343 | } | |
344 | OPENSSL_free(lock_cs); | |
345 | OPENSSL_free(lock_count); | |
346 | } | |
347 | ||
348 | void pthreads_locking_callback(int mode, int type, char *file, int line) | |
349 | { | |
0f113f3e MC |
350 | if (mode & CRYPTO_LOCK) { |
351 | pthread_mutex_lock(&(lock_cs[type])); | |
352 | lock_count[type]++; | |
353 | } else { | |
354 | pthread_mutex_unlock(&(lock_cs[type])); | |
355 | } | |
356 | } | |
58964a49 | 357 | |
6b691a5c | 358 | unsigned long pthreads_thread_id(void) |
0f113f3e MC |
359 | { |
360 | unsigned long ret; | |
58964a49 | 361 | |
0f113f3e MC |
362 | ret = (unsigned long)pthread_self(); |
363 | return (ret); | |
364 | } | |
58964a49 | 365 | |
0f113f3e | 366 | #endif /* PTHREADS */ |