if_iwm - Recognize IWM_FW_PAGING_BLOCK_CMD wide cmd response correctly.
[dragonfly.git] / crypto / openssl / crypto / threads / th-lock.c
blobcc8cf2581d6440afe6f58d504780b8103c8fccd4
1 /* crypto/threads/th-lock.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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.
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).
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.
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 :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
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.
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.]
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <errno.h>
63 #ifdef LINUX
64 # include <typedefs.h>
65 #endif
66 #ifdef OPENSSL_SYS_WIN32
67 # include <windows.h>
68 #endif
69 #ifdef SOLARIS
70 # include <synch.h>
71 # include <thread.h>
72 #endif
73 #ifdef IRIX
74 # include <ulocks.h>
75 # include <sys/prctl.h>
76 #endif
77 #ifdef PTHREADS
78 # include <pthread.h>
79 #endif
80 #include <openssl/lhash.h>
81 #include <openssl/crypto.h>
82 #include <openssl/buffer.h>
83 #include "../../e_os.h"
84 #include <openssl/x509.h>
85 #include <openssl/ssl.h>
86 #include <openssl/err.h>
88 void CRYPTO_thread_setup(void);
89 void CRYPTO_thread_cleanup(void);
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);
98 static unsigned long irix_thread_id(void);
99 static unsigned long solaris_thread_id(void);
100 static unsigned long pthreads_thread_id(void);
103 * usage:
104 * CRYPTO_thread_setup();
105 * application code
106 * CRYPTO_thread_cleanup();
109 #define THREAD_STACK_SIZE (16*1024)
111 #ifdef OPENSSL_SYS_WIN32
113 static HANDLE *lock_cs;
115 void CRYPTO_thread_setup(void)
117 int i;
119 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
120 if (!lock_cs) {
121 /* Nothing we can do about this...void function! */
122 return;
124 for (i = 0; i < CRYPTO_num_locks(); i++) {
125 lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
128 CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
129 win32_locking_callback);
130 /* id callback defined */
131 return (1);
134 static void CRYPTO_thread_cleanup(void)
136 int i;
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);
144 void win32_locking_callback(int mode, int type, char *file, int line)
146 if (mode & CRYPTO_LOCK) {
147 WaitForSingleObject(lock_cs[type], INFINITE);
148 } else {
149 ReleaseMutex(lock_cs[type]);
153 #endif /* OPENSSL_SYS_WIN32 */
155 #ifdef SOLARIS
157 # define USE_MUTEX
159 # ifdef USE_MUTEX
160 static mutex_t *lock_cs;
161 # else
162 static rwlock_t *lock_cs;
163 # endif
164 static long *lock_count;
166 void CRYPTO_thread_setup(void)
168 int i;
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
175 if (!lock_cs) {
176 /* Nothing we can do about this...void function! */
177 return;
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
189 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
190 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
193 void CRYPTO_thread_cleanup(void)
195 int i;
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
205 OPENSSL_free(lock_cs);
206 OPENSSL_free(lock_count);
209 void solaris_locking_callback(int mode, int type, char *file, int line)
211 # if 0
212 fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
213 CRYPTO_thread_id(),
214 (mode & CRYPTO_LOCK) ? "l" : "u",
215 (type & CRYPTO_READ) ? "r" : "w", file, line);
216 # endif
218 # if 0
219 if (CRYPTO_LOCK_SSL_CERT == type)
220 fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
221 CRYPTO_thread_id(), mode, file, line);
222 # endif
223 if (mode & CRYPTO_LOCK) {
224 # ifdef USE_MUTEX
225 mutex_lock(&(lock_cs[type]));
226 # else
227 if (mode & CRYPTO_READ)
228 rw_rdlock(&(lock_cs[type]));
229 else
230 rw_wrlock(&(lock_cs[type]));
231 # endif
232 lock_count[type]++;
233 } else {
234 # ifdef USE_MUTEX
235 mutex_unlock(&(lock_cs[type]));
236 # else
237 rw_unlock(&(lock_cs[type]));
238 # endif
242 unsigned long solaris_thread_id(void)
244 unsigned long ret;
246 ret = (unsigned long)thr_self();
247 return (ret);
249 #endif /* SOLARIS */
251 #ifdef IRIX
252 /* I don't think this works..... */
254 static usptr_t *arena;
255 static usema_t **lock_cs;
257 void CRYPTO_thread_setup(void)
259 int i;
260 char filename[20];
262 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
263 if (!lock_cs) {
264 /* Nothing we can do about this...void function! */
265 return;
268 strcpy(filename, "/tmp/mttest.XXXXXX");
269 mktemp(filename);
271 usconfig(CONF_STHREADIOOFF);
272 usconfig(CONF_STHREADMALLOCOFF);
273 usconfig(CONF_INITUSERS, 100);
274 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
275 arena = usinit(filename);
276 unlink(filename);
278 for (i = 0; i < CRYPTO_num_locks(); i++) {
279 lock_cs[i] = usnewsema(arena, 1);
282 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
283 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
286 void CRYPTO_thread_cleanup(void)
288 int i;
290 CRYPTO_set_locking_callback(NULL);
291 for (i = 0; i < CRYPTO_num_locks(); i++) {
292 char buf[10];
294 sprintf(buf, "%2d:", i);
295 usdumpsema(lock_cs[i], stdout, buf);
296 usfreesema(lock_cs[i], arena);
298 OPENSSL_free(lock_cs);
301 void irix_locking_callback(int mode, int type, char *file, int line)
303 if (mode & CRYPTO_LOCK) {
304 uspsema(lock_cs[type]);
305 } else {
306 usvsema(lock_cs[type]);
310 unsigned long irix_thread_id(void)
312 unsigned long ret;
314 ret = (unsigned long)getpid();
315 return (ret);
317 #endif /* IRIX */
319 /* Linux and a few others */
320 #ifdef PTHREADS
322 static pthread_mutex_t *lock_cs;
323 static long *lock_count;
325 void CRYPTO_thread_setup(void)
327 int i;
329 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
330 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
331 if (!lock_cs || !lock_count) {
332 /* Nothing we can do about this...void function! */
333 if (lock_cs)
334 OPENSSL_free(lock_cs);
335 if (lock_count)
336 OPENSSL_free(lock_count);
337 return;
339 for (i = 0; i < CRYPTO_num_locks(); i++) {
340 lock_count[i] = 0;
341 pthread_mutex_init(&(lock_cs[i]), NULL);
344 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
345 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
348 void thread_cleanup(void)
350 int i;
352 CRYPTO_set_locking_callback(NULL);
353 for (i = 0; i < CRYPTO_num_locks(); i++) {
354 pthread_mutex_destroy(&(lock_cs[i]));
356 OPENSSL_free(lock_cs);
357 OPENSSL_free(lock_count);
360 void pthreads_locking_callback(int mode, int type, char *file, int line)
362 # if 0
363 fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
364 CRYPTO_thread_id(),
365 (mode & CRYPTO_LOCK) ? "l" : "u",
366 (type & CRYPTO_READ) ? "r" : "w", file, line);
367 # endif
368 # if 0
369 if (CRYPTO_LOCK_SSL_CERT == type)
370 fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
371 CRYPTO_thread_id(), mode, file, line);
372 # endif
373 if (mode & CRYPTO_LOCK) {
374 pthread_mutex_lock(&(lock_cs[type]));
375 lock_count[type]++;
376 } else {
377 pthread_mutex_unlock(&(lock_cs[type]));
381 unsigned long pthreads_thread_id(void)
383 unsigned long ret;
385 ret = (unsigned long)pthread_self();
386 return (ret);
389 #endif /* PTHREADS */