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.]
75 #include <sys/prctl.h>
86 int CRYPTO_thread_setup(void);
87 void CRYPTO_thread_cleanup(void);
89 static void irix_locking_callback(int mode,int type,char *file,int line);
90 static void solaris_locking_callback(int mode,int type,char *file,int line);
91 static void win32_locking_callback(int mode,int type,char *file,int line);
92 static void pthreads_locking_callback(int mode,int type,char *file,int line);
94 static unsigned long irix_thread_id(void );
95 static unsigned long solaris_thread_id(void );
96 static unsigned long pthreads_thread_id(void );
99 int CRYPOTO_thread_setup();
100 void CRYPTO_cleanup();
102 static void irix_locking_callback();
103 static void solaris_locking_callback();
104 static void win32_locking_callback();
105 static void pthreads_locking_callback();
107 static unsigned long irix_thread_id();
108 static unsigned long solaris_thread_id();
109 static unsigned long pthreads_thread_id();
114 * CRYPTO_thread_setup();
116 * CRYPTO_thread_cleanup();
119 #define THREAD_STACK_SIZE (16*1024)
123 static HANDLE lock_cs[CRYPTO_NUM_LOCKS];
125 int CRYPTO_thread_setup()
129 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
131 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
134 CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
135 /* id callback defined */
139 static void CRYPTO_thread_cleanup()
143 CRYPTO_set_locking_callback(NULL);
144 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
145 CloseHandle(lock_cs[i]);
148 void win32_locking_callback(mode,type,file,line)
154 if (mode & CRYPTO_LOCK)
156 WaitForSingleObject(lock_cs[type],INFINITE);
160 ReleaseMutex(lock_cs[type]);
170 static mutex_t lock_cs[CRYPTO_NUM_LOCKS];
172 static long lock_count[CRYPTO_NUM_LOCKS];
174 static rwlock_t lock_cs[CRYPTO_NUM_LOCKS];
177 void CRYPTO_thread_setup()
181 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
185 mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
187 rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL);
191 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
192 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
195 void CRYPTO_thread_cleanup()
199 CRYPTO_set_locking_callback(NULL);
200 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
203 mutex_destroy(&(lock_cs[i]));
205 rwlock_destroy(&(lock_cs[i]));
210 void solaris_locking_callback(mode,type,file,line)
217 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
219 (mode&CRYPTO_LOCK)?"l":"u",
220 (type&CRYPTO_READ)?"r":"w",file,line);
224 if (CRYPTO_LOCK_SSL_CERT == type)
225 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
229 if (mode & CRYPTO_LOCK)
232 mutex_lock(&(lock_cs[type]));
234 if (mode & CRYPTO_READ)
235 rw_rdlock(&(lock_cs[type]));
237 rw_wrlock(&(lock_cs[type]));
244 mutex_unlock(&(lock_cs[type]));
246 rw_unlock(&(lock_cs[type]));
251 unsigned long solaris_thread_id()
255 ret=(unsigned long)thr_self();
261 /* I don't think this works..... */
263 static usptr_t *arena;
264 static usema_t *lock_cs[CRYPTO_NUM_LOCKS];
266 void CRYPTO_thread_setup()
271 strcpy(filename,"/tmp/mttest.XXXXXX");
274 usconfig(CONF_STHREADIOOFF);
275 usconfig(CONF_STHREADMALLOCOFF);
276 usconfig(CONF_INITUSERS,100);
277 usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
278 arena=usinit(filename);
281 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
283 lock_cs[i]=usnewsema(arena,1);
286 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
287 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
290 void CRYPTO_thread_cleanup()
294 CRYPTO_set_locking_callback(NULL);
295 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
299 sprintf(buf,"%2d:",i);
300 usdumpsema(lock_cs[i],stdout,buf);
301 usfreesema(lock_cs[i],arena);
305 void irix_locking_callback(mode,type,file,line)
311 if (mode & CRYPTO_LOCK)
313 uspsema(lock_cs[type]);
317 usvsema(lock_cs[type]);
321 unsigned long irix_thread_id()
325 ret=(unsigned long)getpid();
330 /* Linux and a few others */
333 static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS];
334 static long lock_count[CRYPTO_NUM_LOCKS];
336 void CRYPTO_thread_setup()
340 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
343 pthread_mutex_init(&(lock_cs[i]),NULL);
346 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
347 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
350 void thread_cleanup()
354 CRYPTO_set_locking_callback(NULL);
355 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
357 pthread_mutex_destroy(&(lock_cs[i]));
361 void pthreads_locking_callback(mode,type,file,line)
368 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
370 (mode&CRYPTO_LOCK)?"l":"u",
371 (type&CRYPTO_READ)?"r":"w",file,line);
374 if (CRYPTO_LOCK_SSL_CERT == type)
375 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
379 if (mode & CRYPTO_LOCK)
381 pthread_mutex_lock(&(lock_cs[type]));
386 pthread_mutex_unlock(&(lock_cs[type]));
390 unsigned long pthreads_thread_id()
394 ret=(unsigned long)pthread_self();
398 #endif /* PTHREADS */