]>
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. | |
ae5c8664 | 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). | |
ae5c8664 | 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. | |
ae5c8664 | 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 :-). | |
ae5c8664 | 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)" | |
ae5c8664 | 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. | |
ae5c8664 | 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 | |
ae5c8664 | 64 | # include <typedefs.h> |
58964a49 | 65 | #endif |
bc36ee62 | 66 | #ifdef OPENSSL_SYS_WIN32 |
ae5c8664 | 67 | # include <windows.h> |
58964a49 RE |
68 | #endif |
69 | #ifdef SOLARIS | |
ae5c8664 MC |
70 | # include <synch.h> |
71 | # include <thread.h> | |
58964a49 RE |
72 | #endif |
73 | #ifdef IRIX | |
ae5c8664 MC |
74 | # include <ulocks.h> |
75 | # include <sys/prctl.h> | |
58964a49 | 76 | #endif |
2d2d3139 | 77 | #ifdef PTHREADS |
ae5c8664 | 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 | ||
ae5c8664 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 | |
ae5c8664 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 | |
e19d4a99 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) |
ae5c8664 MC |
116 | { |
117 | int i; | |
58964a49 | 118 | |
ae5c8664 MC |
119 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); |
120 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
121 | lock_cs[i] = CreateMutex(NULL, FALSE, NULL); | |
122 | } | |
58964a49 | 123 | |
ae5c8664 MC |
124 | CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) |
125 | win32_locking_callback); | |
126 | /* id callback defined */ | |
127 | return (1); | |
128 | } | |
58964a49 | 129 | |
6b691a5c | 130 | static void CRYPTO_thread_cleanup(void) |
ae5c8664 MC |
131 | { |
132 | int i; | |
58964a49 | 133 | |
ae5c8664 MC |
134 | CRYPTO_set_locking_callback(NULL); |
135 | for (i = 0; i < CRYPTO_num_locks(); i++) | |
136 | CloseHandle(lock_cs[i]); | |
137 | OPENSSL_free(lock_cs); | |
138 | } | |
58964a49 | 139 | |
6b691a5c | 140 | void win32_locking_callback(int mode, int type, char *file, int line) |
ae5c8664 MC |
141 | { |
142 | if (mode & CRYPTO_LOCK) { | |
143 | WaitForSingleObject(lock_cs[type], INFINITE); | |
144 | } else { | |
145 | ReleaseMutex(lock_cs[type]); | |
146 | } | |
147 | } | |
148 | ||
149 | #endif /* OPENSSL_SYS_WIN32 */ | |
58964a49 RE |
150 | |
151 | #ifdef SOLARIS | |
152 | ||
ae5c8664 | 153 | # define USE_MUTEX |
58964a49 | 154 | |
ae5c8664 | 155 | # ifdef USE_MUTEX |
2d2d3139 | 156 | static mutex_t *lock_cs; |
ae5c8664 | 157 | # else |
2d2d3139 | 158 | static rwlock_t *lock_cs; |
ae5c8664 | 159 | # endif |
2d2d3139 | 160 | static long *lock_count; |
58964a49 | 161 | |
6b691a5c | 162 | void CRYPTO_thread_setup(void) |
ae5c8664 MC |
163 | { |
164 | int i; | |
165 | ||
166 | # ifdef USE_MUTEX | |
167 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); | |
168 | # else | |
169 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t)); | |
170 | # endif | |
171 | lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); | |
172 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
173 | lock_count[i] = 0; | |
174 | # ifdef USE_MUTEX | |
175 | mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL); | |
176 | # else | |
177 | rwlock_init(&(lock_cs[i]), USYNC_THREAD, NULL); | |
178 | # endif | |
179 | } | |
180 | ||
181 | CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id); | |
182 | CRYPTO_set_locking_callback((void (*)())solaris_locking_callback); | |
183 | } | |
58964a49 | 184 | |
6b691a5c | 185 | void CRYPTO_thread_cleanup(void) |
ae5c8664 MC |
186 | { |
187 | int i; | |
188 | ||
189 | CRYPTO_set_locking_callback(NULL); | |
190 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
191 | # ifdef USE_MUTEX | |
192 | mutex_destroy(&(lock_cs[i])); | |
193 | # else | |
194 | rwlock_destroy(&(lock_cs[i])); | |
195 | # endif | |
196 | } | |
197 | OPENSSL_free(lock_cs); | |
198 | OPENSSL_free(lock_count); | |
199 | } | |
58964a49 | 200 | |
6b691a5c | 201 | void solaris_locking_callback(int mode, int type, char *file, int line) |
ae5c8664 MC |
202 | { |
203 | # if 0 | |
204 | fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n", | |
205 | CRYPTO_thread_id(), | |
206 | (mode & CRYPTO_LOCK) ? "l" : "u", | |
207 | (type & CRYPTO_READ) ? "r" : "w", file, line); | |
208 | # endif | |
209 | ||
210 | # if 0 | |
211 | if (CRYPTO_LOCK_SSL_CERT == type) | |
212 | fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n", | |
213 | CRYPTO_thread_id(), mode, file, line); | |
214 | # endif | |
215 | if (mode & CRYPTO_LOCK) { | |
216 | # ifdef USE_MUTEX | |
217 | mutex_lock(&(lock_cs[type])); | |
218 | # else | |
219 | if (mode & CRYPTO_READ) | |
220 | rw_rdlock(&(lock_cs[type])); | |
221 | else | |
222 | rw_wrlock(&(lock_cs[type])); | |
223 | # endif | |
224 | lock_count[type]++; | |
225 | } else { | |
226 | # ifdef USE_MUTEX | |
227 | mutex_unlock(&(lock_cs[type])); | |
228 | # else | |
229 | rw_unlock(&(lock_cs[type])); | |
230 | # endif | |
231 | } | |
232 | } | |
58964a49 | 233 | |
6b691a5c | 234 | unsigned long solaris_thread_id(void) |
ae5c8664 MC |
235 | { |
236 | unsigned long ret; | |
58964a49 | 237 | |
ae5c8664 MC |
238 | ret = (unsigned long)thr_self(); |
239 | return (ret); | |
240 | } | |
241 | #endif /* SOLARIS */ | |
58964a49 RE |
242 | |
243 | #ifdef IRIX | |
244 | /* I don't think this works..... */ | |
245 | ||
246 | static usptr_t *arena; | |
2d2d3139 | 247 | static usema_t **lock_cs; |
58964a49 | 248 | |
6b691a5c | 249 | void CRYPTO_thread_setup(void) |
ae5c8664 MC |
250 | { |
251 | int i; | |
252 | char filename[20]; | |
253 | ||
254 | strcpy(filename, "/tmp/mttest.XXXXXX"); | |
255 | mktemp(filename); | |
256 | ||
257 | usconfig(CONF_STHREADIOOFF); | |
258 | usconfig(CONF_STHREADMALLOCOFF); | |
259 | usconfig(CONF_INITUSERS, 100); | |
260 | usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); | |
261 | arena = usinit(filename); | |
262 | unlink(filename); | |
263 | ||
264 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); | |
265 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
266 | lock_cs[i] = usnewsema(arena, 1); | |
267 | } | |
268 | ||
269 | CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id); | |
270 | CRYPTO_set_locking_callback((void (*)())irix_locking_callback); | |
271 | } | |
58964a49 | 272 | |
6b691a5c | 273 | void CRYPTO_thread_cleanup(void) |
ae5c8664 MC |
274 | { |
275 | int i; | |
58964a49 | 276 | |
ae5c8664 MC |
277 | CRYPTO_set_locking_callback(NULL); |
278 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
279 | char buf[10]; | |
58964a49 | 280 | |
ae5c8664 MC |
281 | sprintf(buf, "%2d:", i); |
282 | usdumpsema(lock_cs[i], stdout, buf); | |
283 | usfreesema(lock_cs[i], arena); | |
284 | } | |
285 | OPENSSL_free(lock_cs); | |
286 | } | |
58964a49 | 287 | |
6b691a5c | 288 | void irix_locking_callback(int mode, int type, char *file, int line) |
ae5c8664 MC |
289 | { |
290 | if (mode & CRYPTO_LOCK) { | |
291 | uspsema(lock_cs[type]); | |
292 | } else { | |
293 | usvsema(lock_cs[type]); | |
294 | } | |
295 | } | |
58964a49 | 296 | |
6b691a5c | 297 | unsigned long irix_thread_id(void) |
ae5c8664 MC |
298 | { |
299 | unsigned long ret; | |
58964a49 | 300 | |
ae5c8664 MC |
301 | ret = (unsigned long)getpid(); |
302 | return (ret); | |
303 | } | |
304 | #endif /* IRIX */ | |
58964a49 RE |
305 | |
306 | /* Linux and a few others */ | |
307 | #ifdef PTHREADS | |
308 | ||
2d2d3139 RL |
309 | static pthread_mutex_t *lock_cs; |
310 | static long *lock_count; | |
58964a49 | 311 | |
6b691a5c | 312 | void CRYPTO_thread_setup(void) |
ae5c8664 MC |
313 | { |
314 | int i; | |
58964a49 | 315 | |
ae5c8664 MC |
316 | lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); |
317 | lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); | |
318 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
319 | lock_count[i] = 0; | |
320 | pthread_mutex_init(&(lock_cs[i]), NULL); | |
321 | } | |
58964a49 | 322 | |
ae5c8664 MC |
323 | CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); |
324 | CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); | |
325 | } | |
58964a49 | 326 | |
6b691a5c | 327 | void thread_cleanup(void) |
ae5c8664 MC |
328 | { |
329 | int i; | |
330 | ||
331 | CRYPTO_set_locking_callback(NULL); | |
332 | for (i = 0; i < CRYPTO_num_locks(); i++) { | |
333 | pthread_mutex_destroy(&(lock_cs[i])); | |
334 | } | |
335 | OPENSSL_free(lock_cs); | |
336 | OPENSSL_free(lock_count); | |
337 | } | |
338 | ||
339 | void pthreads_locking_callback(int mode, int type, char *file, int line) | |
340 | { | |
341 | # if 0 | |
342 | fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n", | |
343 | CRYPTO_thread_id(), | |
344 | (mode & CRYPTO_LOCK) ? "l" : "u", | |
345 | (type & CRYPTO_READ) ? "r" : "w", file, line); | |
346 | # endif | |
347 | # if 0 | |
348 | if (CRYPTO_LOCK_SSL_CERT == type) | |
349 | fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n", | |
350 | CRYPTO_thread_id(), mode, file, line); | |
351 | # endif | |
352 | if (mode & CRYPTO_LOCK) { | |
353 | pthread_mutex_lock(&(lock_cs[type])); | |
354 | lock_count[type]++; | |
355 | } else { | |
356 | pthread_mutex_unlock(&(lock_cs[type])); | |
357 | } | |
358 | } | |
58964a49 | 359 | |
6b691a5c | 360 | unsigned long pthreads_thread_id(void) |
ae5c8664 MC |
361 | { |
362 | unsigned long ret; | |
58964a49 | 363 | |
ae5c8664 MC |
364 | ret = (unsigned long)pthread_self(); |
365 | return (ret); | |
366 | } | |
58964a49 | 367 | |
ae5c8664 | 368 | #endif /* PTHREADS */ |