1 /* crypto/threads/th-lock.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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
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
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.]
64 # include <typedefs.h>
66 #ifdef OPENSSL_SYS_WIN32
75 # include <sys/prctl.h>
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,
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,
98 static unsigned long irix_thread_id(void);
99 static unsigned long solaris_thread_id(void);
100 static unsigned long pthreads_thread_id(void);
104 * CRYPTO_thread_setup();
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)
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);
124 CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
125 win32_locking_callback);
126 /* id callback defined */
130 static void CRYPTO_thread_cleanup(void)
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);
140 void win32_locking_callback(int mode, int type, char *file, int line)
142 if (mode & CRYPTO_LOCK) {
143 WaitForSingleObject(lock_cs[type], INFINITE);
145 ReleaseMutex(lock_cs[type]);
149 #endif /* OPENSSL_SYS_WIN32 */
156 static mutex_t *lock_cs;
158 static rwlock_t *lock_cs;
160 static long *lock_count;
162 void CRYPTO_thread_setup(void)
167 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
169 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t));
171 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
172 for (i = 0; i < CRYPTO_num_locks(); i++) {
175 mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
177 rwlock_init(&(lock_cs[i]), USYNC_THREAD, NULL);
181 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
182 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
185 void CRYPTO_thread_cleanup(void)
189 CRYPTO_set_locking_callback(NULL);
190 for (i = 0; i < CRYPTO_num_locks(); i++) {
192 mutex_destroy(&(lock_cs[i]));
194 rwlock_destroy(&(lock_cs[i]));
197 OPENSSL_free(lock_cs);
198 OPENSSL_free(lock_count);
201 void solaris_locking_callback(int mode, int type, char *file, int line)
203 if (mode & CRYPTO_LOCK) {
205 mutex_lock(&(lock_cs[type]));
207 if (mode & CRYPTO_READ)
208 rw_rdlock(&(lock_cs[type]));
210 rw_wrlock(&(lock_cs[type]));
215 mutex_unlock(&(lock_cs[type]));
217 rw_unlock(&(lock_cs[type]));
222 unsigned long solaris_thread_id(void)
226 ret = (unsigned long)thr_self();
232 /* I don't think this works..... */
234 static usptr_t *arena;
235 static usema_t **lock_cs;
237 void CRYPTO_thread_setup(void)
242 strcpy(filename, "/tmp/mttest.XXXXXX");
245 usconfig(CONF_STHREADIOOFF);
246 usconfig(CONF_STHREADMALLOCOFF);
247 usconfig(CONF_INITUSERS, 100);
248 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
249 arena = usinit(filename);
252 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
253 for (i = 0; i < CRYPTO_num_locks(); i++) {
254 lock_cs[i] = usnewsema(arena, 1);
257 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
258 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
261 void CRYPTO_thread_cleanup(void)
265 CRYPTO_set_locking_callback(NULL);
266 for (i = 0; i < CRYPTO_num_locks(); i++) {
269 sprintf(buf, "%2d:", i);
270 usdumpsema(lock_cs[i], stdout, buf);
271 usfreesema(lock_cs[i], arena);
273 OPENSSL_free(lock_cs);
276 void irix_locking_callback(int mode, int type, char *file, int line)
278 if (mode & CRYPTO_LOCK) {
279 uspsema(lock_cs[type]);
281 usvsema(lock_cs[type]);
285 unsigned long irix_thread_id(void)
289 ret = (unsigned long)getpid();
294 /* Linux and a few others */
297 static pthread_mutex_t *lock_cs;
298 static long *lock_count;
300 void CRYPTO_thread_setup(void)
304 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
305 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
306 for (i = 0; i < CRYPTO_num_locks(); i++) {
308 pthread_mutex_init(&(lock_cs[i]), NULL);
311 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
312 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
315 void thread_cleanup(void)
319 CRYPTO_set_locking_callback(NULL);
320 for (i = 0; i < CRYPTO_num_locks(); i++) {
321 pthread_mutex_destroy(&(lock_cs[i]));
323 OPENSSL_free(lock_cs);
324 OPENSSL_free(lock_count);
327 void pthreads_locking_callback(int mode, int type, char *file, int line)
329 if (mode & CRYPTO_LOCK) {
330 pthread_mutex_lock(&(lock_cs[type]));
333 pthread_mutex_unlock(&(lock_cs[type]));
337 unsigned long pthreads_thread_id(void)
341 ret = (unsigned long)pthread_self();
345 #endif /* PTHREADS */