summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/threads/th-lock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/threads/th-lock.c')
-rw-r--r--src/lib/libcrypto/threads/th-lock.c138
1 files changed, 63 insertions, 75 deletions
diff --git a/src/lib/libcrypto/threads/th-lock.c b/src/lib/libcrypto/threads/th-lock.c
index 039022446d..a6a79b9f45 100644
--- a/src/lib/libcrypto/threads/th-lock.c
+++ b/src/lib/libcrypto/threads/th-lock.c
@@ -63,7 +63,7 @@
63#ifdef LINUX 63#ifdef LINUX
64#include <typedefs.h> 64#include <typedefs.h>
65#endif 65#endif
66#ifdef WIN32 66#ifdef OPENSSL_SYS_WIN32
67#include <windows.h> 67#include <windows.h>
68#endif 68#endif
69#ifdef SOLARIS 69#ifdef SOLARIS
@@ -74,16 +74,18 @@
74#include <ulocks.h> 74#include <ulocks.h>
75#include <sys/prctl.h> 75#include <sys/prctl.h>
76#endif 76#endif
77#include "lhash.h" 77#ifdef PTHREADS
78#include "crypto.h" 78#include <pthread.h>
79#include "buffer.h" 79#endif
80#include "e_os.h" 80#include <openssl/lhash.h>
81#include "x509.h" 81#include <openssl/crypto.h>
82#include "ssl.h" 82#include <openssl/buffer.h>
83#include "err.h" 83#include <openssl/e_os.h>
84 84#include <openssl/x509.h>
85#ifndef NOPROTO 85#include <openssl/ssl.h>
86int CRYPTO_thread_setup(void); 86#include <openssl/err.h>
87
88void CRYPTO_thread_setup(void);
87void CRYPTO_thread_cleanup(void); 89void CRYPTO_thread_cleanup(void);
88 90
89static void irix_locking_callback(int mode,int type,char *file,int line); 91static void irix_locking_callback(int mode,int type,char *file,int line);
@@ -95,38 +97,24 @@ static unsigned long irix_thread_id(void );
95static unsigned long solaris_thread_id(void ); 97static unsigned long solaris_thread_id(void );
96static unsigned long pthreads_thread_id(void ); 98static unsigned long pthreads_thread_id(void );
97 99
98#else
99int CRYPOTO_thread_setup();
100void CRYPTO_cleanup();
101
102static void irix_locking_callback();
103static void solaris_locking_callback();
104static void win32_locking_callback();
105static void pthreads_locking_callback();
106
107static unsigned long irix_thread_id();
108static unsigned long solaris_thread_id();
109static unsigned long pthreads_thread_id();
110
111#endif
112
113/* usage: 100/* usage:
114 * CRYPTO_thread_setup(); 101 * CRYPTO_thread_setup();
115 * applicaion code 102 * application code
116 * CRYPTO_thread_cleanup(); 103 * CRYPTO_thread_cleanup();
117 */ 104 */
118 105
119#define THREAD_STACK_SIZE (16*1024) 106#define THREAD_STACK_SIZE (16*1024)
120 107
121#ifdef WIN32 108#ifdef OPENSSL_SYS_WIN32
122 109
123static HANDLE lock_cs[CRYPTO_NUM_LOCKS]; 110static HANDLE *lock_cs;
124 111
125int CRYPTO_thread_setup() 112void CRYPTO_thread_setup(void)
126 { 113 {
127 int i; 114 int i;
128 115
129 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 116 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
117 for (i=0; i<CRYPTO_num_locks(); i++)
130 { 118 {
131 lock_cs[i]=CreateMutex(NULL,FALSE,NULL); 119 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
132 } 120 }
@@ -136,20 +124,17 @@ int CRYPTO_thread_setup()
136 return(1); 124 return(1);
137 } 125 }
138 126
139static void CRYPTO_thread_cleanup() 127static void CRYPTO_thread_cleanup(void)
140 { 128 {
141 int i; 129 int i;
142 130
143 CRYPTO_set_locking_callback(NULL); 131 CRYPTO_set_locking_callback(NULL);
144 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 132 for (i=0; i<CRYPTO_num_locks(); i++)
145 CloseHandle(lock_cs[i]); 133 CloseHandle(lock_cs[i]);
134 OPENSSL_free(lock_cs);
146 } 135 }
147 136
148void win32_locking_callback(mode,type,file,line) 137void win32_locking_callback(int mode, int type, char *file, int line)
149int mode;
150int type;
151char *file;
152int line;
153 { 138 {
154 if (mode & CRYPTO_LOCK) 139 if (mode & CRYPTO_LOCK)
155 { 140 {
@@ -161,24 +146,30 @@ int line;
161 } 146 }
162 } 147 }
163 148
164#endif /* WIN32 */ 149#endif /* OPENSSL_SYS_WIN32 */
165 150
166#ifdef SOLARIS 151#ifdef SOLARIS
167 152
168#define USE_MUTEX 153#define USE_MUTEX
169 154
170static mutex_t lock_cs[CRYPTO_NUM_LOCKS];
171#ifdef USE_MUTEX 155#ifdef USE_MUTEX
172static long lock_count[CRYPTO_NUM_LOCKS]; 156static mutex_t *lock_cs;
173#else 157#else
174static rwlock_t lock_cs[CRYPTO_NUM_LOCKS]; 158static rwlock_t *lock_cs;
175#endif 159#endif
160static long *lock_count;
176 161
177void CRYPTO_thread_setup() 162void CRYPTO_thread_setup(void)
178 { 163 {
179 int i; 164 int i;
180 165
181 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 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++)
182 { 173 {
183 lock_count[i]=0; 174 lock_count[i]=0;
184#ifdef USE_MUTEX 175#ifdef USE_MUTEX
@@ -192,12 +183,12 @@ void CRYPTO_thread_setup()
192 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback); 183 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
193 } 184 }
194 185
195void CRYPTO_thread_cleanup() 186void CRYPTO_thread_cleanup(void)
196 { 187 {
197 int i; 188 int i;
198 189
199 CRYPTO_set_locking_callback(NULL); 190 CRYPTO_set_locking_callback(NULL);
200 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 191 for (i=0; i<CRYPTO_num_locks(); i++)
201 { 192 {
202#ifdef USE_MUTEX 193#ifdef USE_MUTEX
203 mutex_destroy(&(lock_cs[i])); 194 mutex_destroy(&(lock_cs[i]));
@@ -205,13 +196,11 @@ void CRYPTO_thread_cleanup()
205 rwlock_destroy(&(lock_cs[i])); 196 rwlock_destroy(&(lock_cs[i]));
206#endif 197#endif
207 } 198 }
199 OPENSSL_free(lock_cs);
200 OPENSSL_free(lock_count);
208 } 201 }
209 202
210void solaris_locking_callback(mode,type,file,line) 203void solaris_locking_callback(int mode, int type, char *file, int line)
211int mode;
212int type;
213char *file;
214int line;
215 { 204 {
216#if 0 205#if 0
217 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", 206 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
@@ -248,7 +237,7 @@ int line;
248 } 237 }
249 } 238 }
250 239
251unsigned long solaris_thread_id() 240unsigned long solaris_thread_id(void)
252 { 241 {
253 unsigned long ret; 242 unsigned long ret;
254 243
@@ -261,9 +250,9 @@ unsigned long solaris_thread_id()
261/* I don't think this works..... */ 250/* I don't think this works..... */
262 251
263static usptr_t *arena; 252static usptr_t *arena;
264static usema_t *lock_cs[CRYPTO_NUM_LOCKS]; 253static usema_t **lock_cs;
265 254
266void CRYPTO_thread_setup() 255void CRYPTO_thread_setup(void)
267 { 256 {
268 int i; 257 int i;
269 char filename[20]; 258 char filename[20];
@@ -278,7 +267,8 @@ void CRYPTO_thread_setup()
278 arena=usinit(filename); 267 arena=usinit(filename);
279 unlink(filename); 268 unlink(filename);
280 269
281 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 270 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
271 for (i=0; i<CRYPTO_num_locks(); i++)
282 { 272 {
283 lock_cs[i]=usnewsema(arena,1); 273 lock_cs[i]=usnewsema(arena,1);
284 } 274 }
@@ -287,12 +277,12 @@ void CRYPTO_thread_setup()
287 CRYPTO_set_locking_callback((void (*)())irix_locking_callback); 277 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
288 } 278 }
289 279
290void CRYPTO_thread_cleanup() 280void CRYPTO_thread_cleanup(void)
291 { 281 {
292 int i; 282 int i;
293 283
294 CRYPTO_set_locking_callback(NULL); 284 CRYPTO_set_locking_callback(NULL);
295 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 285 for (i=0; i<CRYPTO_num_locks(); i++)
296 { 286 {
297 char buf[10]; 287 char buf[10];
298 288
@@ -300,13 +290,10 @@ void CRYPTO_thread_cleanup()
300 usdumpsema(lock_cs[i],stdout,buf); 290 usdumpsema(lock_cs[i],stdout,buf);
301 usfreesema(lock_cs[i],arena); 291 usfreesema(lock_cs[i],arena);
302 } 292 }
293 OPENSSL_free(lock_cs);
303 } 294 }
304 295
305void irix_locking_callback(mode,type,file,line) 296void irix_locking_callback(int mode, int type, char *file, int line)
306int mode;
307int type;
308char *file;
309int line;
310 { 297 {
311 if (mode & CRYPTO_LOCK) 298 if (mode & CRYPTO_LOCK)
312 { 299 {
@@ -318,7 +305,7 @@ int line;
318 } 305 }
319 } 306 }
320 307
321unsigned long irix_thread_id() 308unsigned long irix_thread_id(void)
322 { 309 {
323 unsigned long ret; 310 unsigned long ret;
324 311
@@ -330,14 +317,16 @@ unsigned long irix_thread_id()
330/* Linux and a few others */ 317/* Linux and a few others */
331#ifdef PTHREADS 318#ifdef PTHREADS
332 319
333static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS]; 320static pthread_mutex_t *lock_cs;
334static long lock_count[CRYPTO_NUM_LOCKS]; 321static long *lock_count;
335 322
336void CRYPTO_thread_setup() 323void CRYPTO_thread_setup(void)
337 { 324 {
338 int i; 325 int i;
339 326
340 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 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++)
341 { 330 {
342 lock_count[i]=0; 331 lock_count[i]=0;
343 pthread_mutex_init(&(lock_cs[i]),NULL); 332 pthread_mutex_init(&(lock_cs[i]),NULL);
@@ -347,22 +336,21 @@ void CRYPTO_thread_setup()
347 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); 336 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
348 } 337 }
349 338
350void thread_cleanup() 339void thread_cleanup(void)
351 { 340 {
352 int i; 341 int i;
353 342
354 CRYPTO_set_locking_callback(NULL); 343 CRYPTO_set_locking_callback(NULL);
355 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 344 for (i=0; i<CRYPTO_num_locks(); i++)
356 { 345 {
357 pthread_mutex_destroy(&(lock_cs[i])); 346 pthread_mutex_destroy(&(lock_cs[i]));
358 } 347 }
348 OPENSSL_free(lock_cs);
349 OPENSSL_free(lock_count);
359 } 350 }
360 351
361void pthreads_locking_callback(mode,type,file,line) 352void pthreads_locking_callback(int mode, int type, char *file,
362int mode; 353 int line)
363int type;
364char *file;
365int line;
366 { 354 {
367#if 0 355#if 0
368 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", 356 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
@@ -387,7 +375,7 @@ int line;
387 } 375 }
388 } 376 }
389 377
390unsigned long pthreads_thread_id() 378unsigned long pthreads_thread_id(void)
391 { 379 {
392 unsigned long ret; 380 unsigned long ret;
393 381