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