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