28721eaccd73dd43812d6dc4eadccda890759dc6
[openssl.git] / crypto / threads / th-lock.c
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.
8  * 
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).
15  * 
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.
22  * 
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)"
40  * 
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.
52  * 
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
64 #include <typedefs.h>
65 #endif
66 #ifdef 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 #include <openssl/lhash.h>
78 #include <openssl/crypto.h>
79 #include <openssl/buffer.h>
80 #include <openssl/e_os.h>
81 #include <openssl/x509.h>
82 #include <openssl/ssl.h>
83 #include <openssl/err.h>
84
85 #ifndef NOPROTO
86 int CRYPTO_thread_setup(void);
87 void CRYPTO_thread_cleanup(void);
88
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);
93
94 static unsigned long irix_thread_id(void );
95 static unsigned long solaris_thread_id(void );
96 static unsigned long pthreads_thread_id(void );
97
98 #else
99 int CRYPOTO_thread_setup();
100 void CRYPTO_cleanup();
101
102 static void irix_locking_callback();
103 static void solaris_locking_callback();
104 static void win32_locking_callback();
105 static void pthreads_locking_callback();
106
107 static unsigned long irix_thread_id();
108 static unsigned long solaris_thread_id();
109 static unsigned long pthreads_thread_id();
110
111 #endif
112
113 /* usage:
114  * CRYPTO_thread_setup();
115  * applicaion code
116  * CRYPTO_thread_cleanup();
117  */
118
119 #define THREAD_STACK_SIZE (16*1024)
120
121 #ifdef WIN32
122
123 static HANDLE lock_cs[CRYPTO_NUM_LOCKS];
124
125 int CRYPTO_thread_setup(void)
126         {
127         int i;
128
129         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
130                 {
131                 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
132                 }
133
134         CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
135         /* id callback defined */
136         return(1);
137         }
138
139 static void CRYPTO_thread_cleanup(void)
140         {
141         int i;
142
143         CRYPTO_set_locking_callback(NULL);
144         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
145                 CloseHandle(lock_cs[i]);
146         }
147
148 void win32_locking_callback(int mode, int type, char *file, int line)
149         {
150         if (mode & CRYPTO_LOCK)
151                 {
152                 WaitForSingleObject(lock_cs[type],INFINITE);
153                 }
154         else
155                 {
156                 ReleaseMutex(lock_cs[type]);
157                 }
158         }
159
160 #endif /* WIN32 */
161
162 #ifdef SOLARIS
163
164 #define USE_MUTEX
165
166 static mutex_t lock_cs[CRYPTO_NUM_LOCKS];
167 #ifdef USE_MUTEX
168 static long lock_count[CRYPTO_NUM_LOCKS];
169 #else
170 static rwlock_t lock_cs[CRYPTO_NUM_LOCKS];
171 #endif
172
173 void CRYPTO_thread_setup(void)
174         {
175         int i;
176
177         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
178                 {
179                 lock_count[i]=0;
180 #ifdef USE_MUTEX
181                 mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
182 #else
183                 rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL);
184 #endif
185                 }
186
187         CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
188         CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
189         }
190
191 void CRYPTO_thread_cleanup(void)
192         {
193         int i;
194
195         CRYPTO_set_locking_callback(NULL);
196         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
197                 {
198 #ifdef USE_MUTEX
199                 mutex_destroy(&(lock_cs[i]));
200 #else
201                 rwlock_destroy(&(lock_cs[i]));
202 #endif
203                 }
204         }
205
206 void solaris_locking_callback(int mode, int type, char *file, int line)
207         {
208 #if 0
209         fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
210                 CRYPTO_thread_id(),
211                 (mode&CRYPTO_LOCK)?"l":"u",
212                 (type&CRYPTO_READ)?"r":"w",file,line);
213 #endif
214
215 #if 0
216         if (CRYPTO_LOCK_SSL_CERT == type)
217                 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
218                         CRYPTO_thread_id(),
219                         mode,file,line);
220 #endif
221         if (mode & CRYPTO_LOCK)
222                 {
223 #ifdef USE_MUTEX
224                 mutex_lock(&(lock_cs[type]));
225 #else
226                 if (mode & CRYPTO_READ)
227                         rw_rdlock(&(lock_cs[type]));
228                 else
229                         rw_wrlock(&(lock_cs[type]));
230 #endif
231                 lock_count[type]++;
232                 }
233         else
234                 {
235 #ifdef USE_MUTEX
236                 mutex_unlock(&(lock_cs[type]));
237 #else
238                 rw_unlock(&(lock_cs[type]));
239 #endif
240                 }
241         }
242
243 unsigned long solaris_thread_id(void)
244         {
245         unsigned long ret;
246
247         ret=(unsigned long)thr_self();
248         return(ret);
249         }
250 #endif /* SOLARIS */
251
252 #ifdef IRIX
253 /* I don't think this works..... */
254
255 static usptr_t *arena;
256 static usema_t *lock_cs[CRYPTO_NUM_LOCKS];
257
258 void CRYPTO_thread_setup(void)
259         {
260         int i;
261         char filename[20];
262
263         strcpy(filename,"/tmp/mttest.XXXXXX");
264         mktemp(filename);
265
266         usconfig(CONF_STHREADIOOFF);
267         usconfig(CONF_STHREADMALLOCOFF);
268         usconfig(CONF_INITUSERS,100);
269         usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
270         arena=usinit(filename);
271         unlink(filename);
272
273         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
274                 {
275                 lock_cs[i]=usnewsema(arena,1);
276                 }
277
278         CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
279         CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
280         }
281
282 void CRYPTO_thread_cleanup(void)
283         {
284         int i;
285
286         CRYPTO_set_locking_callback(NULL);
287         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
288                 {
289                 char buf[10];
290
291                 sprintf(buf,"%2d:",i);
292                 usdumpsema(lock_cs[i],stdout,buf);
293                 usfreesema(lock_cs[i],arena);
294                 }
295         }
296
297 void irix_locking_callback(int mode, int type, char *file, int line)
298         {
299         if (mode & CRYPTO_LOCK)
300                 {
301                 uspsema(lock_cs[type]);
302                 }
303         else
304                 {
305                 usvsema(lock_cs[type]);
306                 }
307         }
308
309 unsigned long irix_thread_id(void)
310         {
311         unsigned long ret;
312
313         ret=(unsigned long)getpid();
314         return(ret);
315         }
316 #endif /* IRIX */
317
318 /* Linux and a few others */
319 #ifdef PTHREADS
320
321 static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS];
322 static long lock_count[CRYPTO_NUM_LOCKS];
323
324 void CRYPTO_thread_setup(void)
325         {
326         int i;
327
328         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
329                 {
330                 lock_count[i]=0;
331                 pthread_mutex_init(&(lock_cs[i]),NULL);
332                 }
333
334         CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
335         CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
336         }
337
338 void thread_cleanup(void)
339         {
340         int i;
341
342         CRYPTO_set_locking_callback(NULL);
343         for (i=0; i<CRYPTO_NUM_LOCKS; i++)
344                 {
345                 pthread_mutex_destroy(&(lock_cs[i]));
346                 }
347         }
348
349 void pthreads_locking_callback(int mode, int type, char *file,
350              int line)
351       {
352 #if 0
353         fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
354                 CRYPTO_thread_id(),
355                 (mode&CRYPTO_LOCK)?"l":"u",
356                 (type&CRYPTO_READ)?"r":"w",file,line);
357 #endif
358 #if 0
359         if (CRYPTO_LOCK_SSL_CERT == type)
360                 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
361                 CRYPTO_thread_id(),
362                 mode,file,line);
363 #endif
364         if (mode & CRYPTO_LOCK)
365                 {
366                 pthread_mutex_lock(&(lock_cs[type]));
367                 lock_count[type]++;
368                 }
369         else
370                 {
371                 pthread_mutex_unlock(&(lock_cs[type]));
372                 }
373         }
374
375 unsigned long pthreads_thread_id(void)
376         {
377         unsigned long ret;
378
379         ret=(unsigned long)pthread_self();
380         return(ret);
381         }
382
383 #endif /* PTHREADS */
384