diff options
Diffstat (limited to 'src/lib/libcrypto/threads/th-lock.c')
-rw-r--r-- | src/lib/libcrypto/threads/th-lock.c | 138 |
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> |
86 | int CRYPTO_thread_setup(void); | 86 | #include <openssl/err.h> |
87 | |||
88 | void CRYPTO_thread_setup(void); | ||
87 | void CRYPTO_thread_cleanup(void); | 89 | void CRYPTO_thread_cleanup(void); |
88 | 90 | ||
89 | static void irix_locking_callback(int mode,int type,char *file,int line); | 91 | static void irix_locking_callback(int mode,int type,char *file,int line); |
@@ -95,38 +97,24 @@ static unsigned long irix_thread_id(void ); | |||
95 | static unsigned long solaris_thread_id(void ); | 97 | static unsigned long solaris_thread_id(void ); |
96 | static unsigned long pthreads_thread_id(void ); | 98 | static unsigned long pthreads_thread_id(void ); |
97 | 99 | ||
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: | 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 | ||
123 | static HANDLE lock_cs[CRYPTO_NUM_LOCKS]; | 110 | static HANDLE *lock_cs; |
124 | 111 | ||
125 | int CRYPTO_thread_setup() | 112 | void 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 | ||
139 | static void CRYPTO_thread_cleanup() | 127 | static 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 | ||
148 | void win32_locking_callback(mode,type,file,line) | 137 | void win32_locking_callback(int mode, int type, char *file, int line) |
149 | int mode; | ||
150 | int type; | ||
151 | char *file; | ||
152 | int 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 | ||
170 | static mutex_t lock_cs[CRYPTO_NUM_LOCKS]; | ||
171 | #ifdef USE_MUTEX | 155 | #ifdef USE_MUTEX |
172 | static long lock_count[CRYPTO_NUM_LOCKS]; | 156 | static mutex_t *lock_cs; |
173 | #else | 157 | #else |
174 | static rwlock_t lock_cs[CRYPTO_NUM_LOCKS]; | 158 | static rwlock_t *lock_cs; |
175 | #endif | 159 | #endif |
160 | static long *lock_count; | ||
176 | 161 | ||
177 | void CRYPTO_thread_setup() | 162 | void 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 | ||
195 | void CRYPTO_thread_cleanup() | 186 | void 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 | ||
210 | void solaris_locking_callback(mode,type,file,line) | 203 | void solaris_locking_callback(int mode, int type, char *file, int line) |
211 | int mode; | ||
212 | int type; | ||
213 | char *file; | ||
214 | int 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 | ||
251 | unsigned long solaris_thread_id() | 240 | unsigned 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 | ||
263 | static usptr_t *arena; | 252 | static usptr_t *arena; |
264 | static usema_t *lock_cs[CRYPTO_NUM_LOCKS]; | 253 | static usema_t **lock_cs; |
265 | 254 | ||
266 | void CRYPTO_thread_setup() | 255 | void 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 | ||
290 | void CRYPTO_thread_cleanup() | 280 | void 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 | ||
305 | void irix_locking_callback(mode,type,file,line) | 296 | void irix_locking_callback(int mode, int type, char *file, int line) |
306 | int mode; | ||
307 | int type; | ||
308 | char *file; | ||
309 | int 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 | ||
321 | unsigned long irix_thread_id() | 308 | unsigned 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 | ||
333 | static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS]; | 320 | static pthread_mutex_t *lock_cs; |
334 | static long lock_count[CRYPTO_NUM_LOCKS]; | 321 | static long *lock_count; |
335 | 322 | ||
336 | void CRYPTO_thread_setup() | 323 | void 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 | ||
350 | void thread_cleanup() | 339 | void 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 | ||
361 | void pthreads_locking_callback(mode,type,file,line) | 352 | void pthreads_locking_callback(int mode, int type, char *file, |
362 | int mode; | 353 | int line) |
363 | int type; | ||
364 | char *file; | ||
365 | int 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 | ||
390 | unsigned long pthreads_thread_id() | 378 | unsigned long pthreads_thread_id(void) |
391 | { | 379 | { |
392 | unsigned long ret; | 380 | unsigned long ret; |
393 | 381 | ||