summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/threads
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/threads')
-rw-r--r--src/lib/libcrypto/threads/README14
-rw-r--r--src/lib/libcrypto/threads/mttest.c1101
-rw-r--r--src/lib/libcrypto/threads/profile.sh4
-rw-r--r--src/lib/libcrypto/threads/ptest.bat4
-rw-r--r--src/lib/libcrypto/threads/pthread.sh9
-rw-r--r--src/lib/libcrypto/threads/pthread2.sh7
-rw-r--r--src/lib/libcrypto/threads/pthreads-vms.com9
-rw-r--r--src/lib/libcrypto/threads/purify.sh4
-rw-r--r--src/lib/libcrypto/threads/solaris.sh4
-rw-r--r--src/lib/libcrypto/threads/th-lock.c387
-rw-r--r--src/lib/libcrypto/threads/win32.bat4
11 files changed, 1547 insertions, 0 deletions
diff --git a/src/lib/libcrypto/threads/README b/src/lib/libcrypto/threads/README
new file mode 100644
index 0000000000..df6b26e146
--- /dev/null
+++ b/src/lib/libcrypto/threads/README
@@ -0,0 +1,14 @@
1Mutithreading testing area.
2
3Since this stuff is very very platorm specific, this is not part of the
4normal build. Have a read of doc/threads.doc.
5
6mttest will do some testing and will currently build under Windows NT/95,
7Solaris and Linux. The IRIX stuff is not finished.
8
9I have tested this program on a 12 CPU ultra sparc box (solaris 2.5.1)
10and things seem to work ok.
11
12The Linux pthreads package can be retrieved from
13http://www.mit.edu:8001/people/proven/pthreads.html
14
diff --git a/src/lib/libcrypto/threads/mttest.c b/src/lib/libcrypto/threads/mttest.c
new file mode 100644
index 0000000000..c474a63c74
--- /dev/null
+++ b/src/lib/libcrypto/threads/mttest.c
@@ -0,0 +1,1101 @@
1/* crypto/threads/mttest.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#include <openssl/rand.h>
88
89#ifdef OPENSSL_NO_FP_API
90#define APPS_WIN16
91#include "../buffer/bss_file.c"
92#endif
93
94#define TEST_SERVER_CERT "../../apps/server.pem"
95#define TEST_CLIENT_CERT "../../apps/client.pem"
96
97#define MAX_THREAD_NUMBER 100
98
99int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
100void thread_setup(void);
101void thread_cleanup(void);
102void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
103
104void irix_locking_callback(int mode,int type,char *file,int line);
105void solaris_locking_callback(int mode,int type,char *file,int line);
106void win32_locking_callback(int mode,int type,char *file,int line);
107void pthreads_locking_callback(int mode,int type,char *file,int line);
108
109unsigned long irix_thread_id(void );
110unsigned long solaris_thread_id(void );
111unsigned long pthreads_thread_id(void );
112
113BIO *bio_err=NULL;
114BIO *bio_stdout=NULL;
115
116static char *cipher=NULL;
117int verbose=0;
118#ifdef FIONBIO
119static int s_nbio=0;
120#endif
121
122int thread_number=10;
123int number_of_loops=10;
124int reconnect=0;
125int cache_stats=0;
126
127static const char rnd_seed[] = "string to make the random number generator think it has entropy";
128
129int doit(char *ctx[4]);
130static void print_stats(FILE *fp, SSL_CTX *ctx)
131{
132 fprintf(fp,"%4ld items in the session cache\n",
133 SSL_CTX_sess_number(ctx));
134 fprintf(fp,"%4d client connects (SSL_connect())\n",
135 SSL_CTX_sess_connect(ctx));
136 fprintf(fp,"%4d client connects that finished\n",
137 SSL_CTX_sess_connect_good(ctx));
138 fprintf(fp,"%4d server connects (SSL_accept())\n",
139 SSL_CTX_sess_accept(ctx));
140 fprintf(fp,"%4d server connects that finished\n",
141 SSL_CTX_sess_accept_good(ctx));
142 fprintf(fp,"%4d session cache hits\n",SSL_CTX_sess_hits(ctx));
143 fprintf(fp,"%4d session cache misses\n",SSL_CTX_sess_misses(ctx));
144 fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx));
145 }
146
147static void sv_usage(void)
148 {
149 fprintf(stderr,"usage: ssltest [args ...]\n");
150 fprintf(stderr,"\n");
151 fprintf(stderr," -server_auth - check server certificate\n");
152 fprintf(stderr," -client_auth - do client authentication\n");
153 fprintf(stderr," -v - more output\n");
154 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
155 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
156 fprintf(stderr," -threads arg - number of threads\n");
157 fprintf(stderr," -loops arg - number of 'connections', per thread\n");
158 fprintf(stderr," -reconnect - reuse session-id's\n");
159 fprintf(stderr," -stats - server session-id cache stats\n");
160 fprintf(stderr," -cert arg - server certificate/key\n");
161 fprintf(stderr," -ccert arg - client certificate/key\n");
162 fprintf(stderr," -ssl3 - just SSLv3n\n");
163 }
164
165int main(int argc, char *argv[])
166 {
167 char *CApath=NULL,*CAfile=NULL;
168 int badop=0;
169 int ret=1;
170 int client_auth=0;
171 int server_auth=0;
172 SSL_CTX *s_ctx=NULL;
173 SSL_CTX *c_ctx=NULL;
174 char *scert=TEST_SERVER_CERT;
175 char *ccert=TEST_CLIENT_CERT;
176 SSL_METHOD *ssl_method=SSLv23_method();
177
178 RAND_seed(rnd_seed, sizeof rnd_seed);
179
180 if (bio_err == NULL)
181 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
182 if (bio_stdout == NULL)
183 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
184 argc--;
185 argv++;
186
187 while (argc >= 1)
188 {
189 if (strcmp(*argv,"-server_auth") == 0)
190 server_auth=1;
191 else if (strcmp(*argv,"-client_auth") == 0)
192 client_auth=1;
193 else if (strcmp(*argv,"-reconnect") == 0)
194 reconnect=1;
195 else if (strcmp(*argv,"-stats") == 0)
196 cache_stats=1;
197 else if (strcmp(*argv,"-ssl3") == 0)
198 ssl_method=SSLv3_method();
199 else if (strcmp(*argv,"-ssl2") == 0)
200 ssl_method=SSLv2_method();
201 else if (strcmp(*argv,"-CApath") == 0)
202 {
203 if (--argc < 1) goto bad;
204 CApath= *(++argv);
205 }
206 else if (strcmp(*argv,"-CAfile") == 0)
207 {
208 if (--argc < 1) goto bad;
209 CAfile= *(++argv);
210 }
211 else if (strcmp(*argv,"-cert") == 0)
212 {
213 if (--argc < 1) goto bad;
214 scert= *(++argv);
215 }
216 else if (strcmp(*argv,"-ccert") == 0)
217 {
218 if (--argc < 1) goto bad;
219 ccert= *(++argv);
220 }
221 else if (strcmp(*argv,"-threads") == 0)
222 {
223 if (--argc < 1) goto bad;
224 thread_number= atoi(*(++argv));
225 if (thread_number == 0) thread_number=1;
226 if (thread_number > MAX_THREAD_NUMBER)
227 thread_number=MAX_THREAD_NUMBER;
228 }
229 else if (strcmp(*argv,"-loops") == 0)
230 {
231 if (--argc < 1) goto bad;
232 number_of_loops= atoi(*(++argv));
233 if (number_of_loops == 0) number_of_loops=1;
234 }
235 else
236 {
237 fprintf(stderr,"unknown option %s\n",*argv);
238 badop=1;
239 break;
240 }
241 argc--;
242 argv++;
243 }
244 if (badop)
245 {
246bad:
247 sv_usage();
248 goto end;
249 }
250
251 if (cipher == NULL && issetugid() == 0)
252 cipher=getenv("SSL_CIPHER");
253
254 SSL_load_error_strings();
255 OpenSSL_add_ssl_algorithms();
256
257 c_ctx=SSL_CTX_new(ssl_method);
258 s_ctx=SSL_CTX_new(ssl_method);
259 if ((c_ctx == NULL) || (s_ctx == NULL))
260 {
261 ERR_print_errors(bio_err);
262 goto end;
263 }
264
265 SSL_CTX_set_session_cache_mode(s_ctx,
266 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
267 SSL_CTX_set_session_cache_mode(c_ctx,
268 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
269
270 if (!SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM))
271 {
272 ERR_print_errors(bio_err);
273 }
274 else if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM))
275 {
276 ERR_print_errors(bio_err);
277 goto end;
278 }
279
280 if (client_auth)
281 {
282 SSL_CTX_use_certificate_file(c_ctx,ccert,
283 SSL_FILETYPE_PEM);
284 SSL_CTX_use_RSAPrivateKey_file(c_ctx,ccert,
285 SSL_FILETYPE_PEM);
286 }
287
288 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
289 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
290 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
291 (!SSL_CTX_set_default_verify_paths(c_ctx)))
292 {
293 fprintf(stderr,"SSL_load_verify_locations\n");
294 ERR_print_errors(bio_err);
295 goto end;
296 }
297
298 if (client_auth)
299 {
300 fprintf(stderr,"client authentication\n");
301 SSL_CTX_set_verify(s_ctx,
302 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
303 verify_callback);
304 }
305 if (server_auth)
306 {
307 fprintf(stderr,"server authentication\n");
308 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
309 verify_callback);
310 }
311
312 thread_setup();
313 do_threads(s_ctx,c_ctx);
314 thread_cleanup();
315end:
316
317 if (c_ctx != NULL)
318 {
319 fprintf(stderr,"Client SSL_CTX stats then free it\n");
320 print_stats(stderr,c_ctx);
321 SSL_CTX_free(c_ctx);
322 }
323 if (s_ctx != NULL)
324 {
325 fprintf(stderr,"Server SSL_CTX stats then free it\n");
326 print_stats(stderr,s_ctx);
327 if (cache_stats)
328 {
329 fprintf(stderr,"-----\n");
330 lh_stats(SSL_CTX_sessions(s_ctx),stderr);
331 fprintf(stderr,"-----\n");
332 /* lh_node_stats(SSL_CTX_sessions(s_ctx),stderr);
333 fprintf(stderr,"-----\n"); */
334 lh_node_usage_stats(SSL_CTX_sessions(s_ctx),stderr);
335 fprintf(stderr,"-----\n");
336 }
337 SSL_CTX_free(s_ctx);
338 fprintf(stderr,"done free\n");
339 }
340 exit(ret);
341 return(0);
342 }
343
344#define W_READ 1
345#define W_WRITE 2
346#define C_DONE 1
347#define S_DONE 2
348
349int ndoit(SSL_CTX *ssl_ctx[2])
350 {
351 int i;
352 int ret;
353 char *ctx[4];
354
355 ctx[0]=(char *)ssl_ctx[0];
356 ctx[1]=(char *)ssl_ctx[1];
357
358 if (reconnect)
359 {
360 ctx[2]=(char *)SSL_new(ssl_ctx[0]);
361 ctx[3]=(char *)SSL_new(ssl_ctx[1]);
362 }
363 else
364 {
365 ctx[2]=NULL;
366 ctx[3]=NULL;
367 }
368
369 fprintf(stdout,"started thread %lu\n",CRYPTO_thread_id());
370 for (i=0; i<number_of_loops; i++)
371 {
372/* fprintf(stderr,"%4d %2d ctx->ref (%3d,%3d)\n",
373 CRYPTO_thread_id(),i,
374 ssl_ctx[0]->references,
375 ssl_ctx[1]->references); */
376 /* pthread_delay_np(&tm);*/
377
378 ret=doit(ctx);
379 if (ret != 0)
380 {
381 fprintf(stdout,"error[%d] %lu - %d\n",
382 i,CRYPTO_thread_id(),ret);
383 return(ret);
384 }
385 }
386 fprintf(stdout,"DONE %lu\n",CRYPTO_thread_id());
387 if (reconnect)
388 {
389 SSL_free((SSL *)ctx[2]);
390 SSL_free((SSL *)ctx[3]);
391 }
392 return(0);
393 }
394
395int doit(char *ctx[4])
396 {
397 SSL_CTX *s_ctx,*c_ctx;
398 static char cbuf[200],sbuf[200];
399 SSL *c_ssl=NULL;
400 SSL *s_ssl=NULL;
401 BIO *c_to_s=NULL;
402 BIO *s_to_c=NULL;
403 BIO *c_bio=NULL;
404 BIO *s_bio=NULL;
405 int c_r,c_w,s_r,s_w;
406 int c_want,s_want;
407 int i;
408 int done=0;
409 int c_write,s_write;
410 int do_server=0,do_client=0;
411
412 s_ctx=(SSL_CTX *)ctx[0];
413 c_ctx=(SSL_CTX *)ctx[1];
414
415 if (ctx[2] != NULL)
416 s_ssl=(SSL *)ctx[2];
417 else
418 s_ssl=SSL_new(s_ctx);
419
420 if (ctx[3] != NULL)
421 c_ssl=(SSL *)ctx[3];
422 else
423 c_ssl=SSL_new(c_ctx);
424
425 if ((s_ssl == NULL) || (c_ssl == NULL)) goto err;
426
427 c_to_s=BIO_new(BIO_s_mem());
428 s_to_c=BIO_new(BIO_s_mem());
429 if ((s_to_c == NULL) || (c_to_s == NULL)) goto err;
430
431 c_bio=BIO_new(BIO_f_ssl());
432 s_bio=BIO_new(BIO_f_ssl());
433 if ((c_bio == NULL) || (s_bio == NULL)) goto err;
434
435 SSL_set_connect_state(c_ssl);
436 SSL_set_bio(c_ssl,s_to_c,c_to_s);
437 BIO_set_ssl(c_bio,c_ssl,(ctx[2] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
438
439 SSL_set_accept_state(s_ssl);
440 SSL_set_bio(s_ssl,c_to_s,s_to_c);
441 BIO_set_ssl(s_bio,s_ssl,(ctx[3] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
442
443 c_r=0; s_r=1;
444 c_w=1; s_w=0;
445 c_want=W_WRITE;
446 s_want=0;
447 c_write=1,s_write=0;
448
449 /* We can always do writes */
450 for (;;)
451 {
452 do_server=0;
453 do_client=0;
454
455 i=(int)BIO_pending(s_bio);
456 if ((i && s_r) || s_w) do_server=1;
457
458 i=(int)BIO_pending(c_bio);
459 if ((i && c_r) || c_w) do_client=1;
460
461 if (do_server && verbose)
462 {
463 if (SSL_in_init(s_ssl))
464 printf("server waiting in SSL_accept - %s\n",
465 SSL_state_string_long(s_ssl));
466 else if (s_write)
467 printf("server:SSL_write()\n");
468 else
469 printf("server:SSL_read()\n");
470 }
471
472 if (do_client && verbose)
473 {
474 if (SSL_in_init(c_ssl))
475 printf("client waiting in SSL_connect - %s\n",
476 SSL_state_string_long(c_ssl));
477 else if (c_write)
478 printf("client:SSL_write()\n");
479 else
480 printf("client:SSL_read()\n");
481 }
482
483 if (!do_client && !do_server)
484 {
485 fprintf(stdout,"ERROR IN STARTUP\n");
486 break;
487 }
488 if (do_client && !(done & C_DONE))
489 {
490 if (c_write)
491 {
492 i=BIO_write(c_bio,"hello from client\n",18);
493 if (i < 0)
494 {
495 c_r=0;
496 c_w=0;
497 if (BIO_should_retry(c_bio))
498 {
499 if (BIO_should_read(c_bio))
500 c_r=1;
501 if (BIO_should_write(c_bio))
502 c_w=1;
503 }
504 else
505 {
506 fprintf(stderr,"ERROR in CLIENT\n");
507 ERR_print_errors_fp(stderr);
508 return(1);
509 }
510 }
511 else if (i == 0)
512 {
513 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
514 return(1);
515 }
516 else
517 {
518 /* ok */
519 c_write=0;
520 }
521 }
522 else
523 {
524 i=BIO_read(c_bio,cbuf,100);
525 if (i < 0)
526 {
527 c_r=0;
528 c_w=0;
529 if (BIO_should_retry(c_bio))
530 {
531 if (BIO_should_read(c_bio))
532 c_r=1;
533 if (BIO_should_write(c_bio))
534 c_w=1;
535 }
536 else
537 {
538 fprintf(stderr,"ERROR in CLIENT\n");
539 ERR_print_errors_fp(stderr);
540 return(1);
541 }
542 }
543 else if (i == 0)
544 {
545 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
546 return(1);
547 }
548 else
549 {
550 done|=C_DONE;
551#ifdef undef
552 fprintf(stdout,"CLIENT:from server:");
553 fwrite(cbuf,1,i,stdout);
554 fflush(stdout);
555#endif
556 }
557 }
558 }
559
560 if (do_server && !(done & S_DONE))
561 {
562 if (!s_write)
563 {
564 i=BIO_read(s_bio,sbuf,100);
565 if (i < 0)
566 {
567 s_r=0;
568 s_w=0;
569 if (BIO_should_retry(s_bio))
570 {
571 if (BIO_should_read(s_bio))
572 s_r=1;
573 if (BIO_should_write(s_bio))
574 s_w=1;
575 }
576 else
577 {
578 fprintf(stderr,"ERROR in SERVER\n");
579 ERR_print_errors_fp(stderr);
580 return(1);
581 }
582 }
583 else if (i == 0)
584 {
585 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
586 return(1);
587 }
588 else
589 {
590 s_write=1;
591 s_w=1;
592#ifdef undef
593 fprintf(stdout,"SERVER:from client:");
594 fwrite(sbuf,1,i,stdout);
595 fflush(stdout);
596#endif
597 }
598 }
599 else
600 {
601 i=BIO_write(s_bio,"hello from server\n",18);
602 if (i < 0)
603 {
604 s_r=0;
605 s_w=0;
606 if (BIO_should_retry(s_bio))
607 {
608 if (BIO_should_read(s_bio))
609 s_r=1;
610 if (BIO_should_write(s_bio))
611 s_w=1;
612 }
613 else
614 {
615 fprintf(stderr,"ERROR in SERVER\n");
616 ERR_print_errors_fp(stderr);
617 return(1);
618 }
619 }
620 else if (i == 0)
621 {
622 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
623 return(1);
624 }
625 else
626 {
627 s_write=0;
628 s_r=1;
629 done|=S_DONE;
630 }
631 }
632 }
633
634 if ((done & S_DONE) && (done & C_DONE)) break;
635 }
636
637 SSL_set_shutdown(c_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
638 SSL_set_shutdown(s_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
639
640#ifdef undef
641 fprintf(stdout,"DONE\n");
642#endif
643err:
644 /* We have to set the BIO's to NULL otherwise they will be
645 * free()ed twice. Once when th s_ssl is SSL_free()ed and
646 * again when c_ssl is SSL_free()ed.
647 * This is a hack required because s_ssl and c_ssl are sharing the same
648 * BIO structure and SSL_set_bio() and SSL_free() automatically
649 * BIO_free non NULL entries.
650 * You should not normally do this or be required to do this */
651
652 if (s_ssl != NULL)
653 {
654 s_ssl->rbio=NULL;
655 s_ssl->wbio=NULL;
656 }
657 if (c_ssl != NULL)
658 {
659 c_ssl->rbio=NULL;
660 c_ssl->wbio=NULL;
661 }
662
663 /* The SSL's are optionally freed in the following calls */
664 if (c_to_s != NULL) BIO_free(c_to_s);
665 if (s_to_c != NULL) BIO_free(s_to_c);
666
667 if (c_bio != NULL) BIO_free(c_bio);
668 if (s_bio != NULL) BIO_free(s_bio);
669 return(0);
670 }
671
672int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
673 {
674 char *s, buf[256];
675
676 if (verbose)
677 {
678 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
679 buf,256);
680 if (s != NULL)
681 {
682 if (ok)
683 fprintf(stderr,"depth=%d %s\n",
684 ctx->error_depth,buf);
685 else
686 fprintf(stderr,"depth=%d error=%d %s\n",
687 ctx->error_depth,ctx->error,buf);
688 }
689 }
690 return(ok);
691 }
692
693#define THREAD_STACK_SIZE (16*1024)
694
695#ifdef OPENSSL_SYS_WIN32
696
697static HANDLE *lock_cs;
698
699void thread_setup(void)
700 {
701 int i;
702
703 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
704 for (i=0; i<CRYPTO_num_locks(); i++)
705 {
706 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
707 }
708
709 CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
710 /* id callback defined */
711 }
712
713void thread_cleanup(void)
714 {
715 int i;
716
717 CRYPTO_set_locking_callback(NULL);
718 for (i=0; i<CRYPTO_num_locks(); i++)
719 CloseHandle(lock_cs[i]);
720 OPENSSL_free(lock_cs);
721 }
722
723void win32_locking_callback(int mode, int type, char *file, int line)
724 {
725 if (mode & CRYPTO_LOCK)
726 {
727 WaitForSingleObject(lock_cs[type],INFINITE);
728 }
729 else
730 {
731 ReleaseMutex(lock_cs[type]);
732 }
733 }
734
735void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
736 {
737 double ret;
738 SSL_CTX *ssl_ctx[2];
739 DWORD thread_id[MAX_THREAD_NUMBER];
740 HANDLE thread_handle[MAX_THREAD_NUMBER];
741 int i;
742 SYSTEMTIME start,end;
743
744 ssl_ctx[0]=s_ctx;
745 ssl_ctx[1]=c_ctx;
746
747 GetSystemTime(&start);
748 for (i=0; i<thread_number; i++)
749 {
750 thread_handle[i]=CreateThread(NULL,
751 THREAD_STACK_SIZE,
752 (LPTHREAD_START_ROUTINE)ndoit,
753 (void *)ssl_ctx,
754 0L,
755 &(thread_id[i]));
756 }
757
758 printf("reaping\n");
759 for (i=0; i<thread_number; i+=50)
760 {
761 int j;
762
763 j=(thread_number < (i+50))?(thread_number-i):50;
764
765 if (WaitForMultipleObjects(j,
766 (CONST HANDLE *)&(thread_handle[i]),TRUE,INFINITE)
767 == WAIT_FAILED)
768 {
769 fprintf(stderr,"WaitForMultipleObjects failed:%d\n",GetLastError());
770 exit(1);
771 }
772 }
773 GetSystemTime(&end);
774
775 if (start.wDayOfWeek > end.wDayOfWeek) end.wDayOfWeek+=7;
776 ret=(end.wDayOfWeek-start.wDayOfWeek)*24;
777
778 ret=(ret+end.wHour-start.wHour)*60;
779 ret=(ret+end.wMinute-start.wMinute)*60;
780 ret=(ret+end.wSecond-start.wSecond);
781 ret+=(end.wMilliseconds-start.wMilliseconds)/1000.0;
782
783 printf("win32 threads done - %.3f seconds\n",ret);
784 }
785
786#endif /* OPENSSL_SYS_WIN32 */
787
788#ifdef SOLARIS
789
790static mutex_t *lock_cs;
791/*static rwlock_t *lock_cs; */
792static long *lock_count;
793
794void thread_setup(void)
795 {
796 int i;
797
798 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
799 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
800 for (i=0; i<CRYPTO_num_locks(); i++)
801 {
802 lock_count[i]=0;
803 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
804 mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
805 }
806
807 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
808 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
809 }
810
811void thread_cleanup(void)
812 {
813 int i;
814
815 CRYPTO_set_locking_callback(NULL);
816
817 fprintf(stderr,"cleanup\n");
818
819 for (i=0; i<CRYPTO_num_locks(); i++)
820 {
821 /* rwlock_destroy(&(lock_cs[i])); */
822 mutex_destroy(&(lock_cs[i]));
823 fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
824 }
825 OPENSSL_free(lock_cs);
826 OPENSSL_free(lock_count);
827
828 fprintf(stderr,"done cleanup\n");
829
830 }
831
832void solaris_locking_callback(int mode, int type, char *file, int line)
833 {
834#ifdef undef
835 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
836 CRYPTO_thread_id(),
837 (mode&CRYPTO_LOCK)?"l":"u",
838 (type&CRYPTO_READ)?"r":"w",file,line);
839#endif
840
841 /*
842 if (CRYPTO_LOCK_SSL_CERT == type)
843 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
844 CRYPTO_thread_id(),
845 mode,file,line);
846 */
847 if (mode & CRYPTO_LOCK)
848 {
849 /* if (mode & CRYPTO_READ)
850 rw_rdlock(&(lock_cs[type]));
851 else
852 rw_wrlock(&(lock_cs[type])); */
853
854 mutex_lock(&(lock_cs[type]));
855 lock_count[type]++;
856 }
857 else
858 {
859/* rw_unlock(&(lock_cs[type])); */
860 mutex_unlock(&(lock_cs[type]));
861 }
862 }
863
864void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
865 {
866 SSL_CTX *ssl_ctx[2];
867 thread_t thread_ctx[MAX_THREAD_NUMBER];
868 int i;
869
870 ssl_ctx[0]=s_ctx;
871 ssl_ctx[1]=c_ctx;
872
873 thr_setconcurrency(thread_number);
874 for (i=0; i<thread_number; i++)
875 {
876 thr_create(NULL, THREAD_STACK_SIZE,
877 (void *(*)())ndoit,
878 (void *)ssl_ctx,
879 0L,
880 &(thread_ctx[i]));
881 }
882
883 printf("reaping\n");
884 for (i=0; i<thread_number; i++)
885 {
886 thr_join(thread_ctx[i],NULL,NULL);
887 }
888
889 printf("solaris threads done (%d,%d)\n",
890 s_ctx->references,c_ctx->references);
891 }
892
893unsigned long solaris_thread_id(void)
894 {
895 unsigned long ret;
896
897 ret=(unsigned long)thr_self();
898 return(ret);
899 }
900#endif /* SOLARIS */
901
902#ifdef IRIX
903
904
905static usptr_t *arena;
906static usema_t **lock_cs;
907
908void thread_setup(void)
909 {
910 int i;
911 char filename[20];
912
913 strcpy(filename,"/tmp/mttest.XXXXXX");
914 mktemp(filename);
915
916 usconfig(CONF_STHREADIOOFF);
917 usconfig(CONF_STHREADMALLOCOFF);
918 usconfig(CONF_INITUSERS,100);
919 usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
920 arena=usinit(filename);
921 unlink(filename);
922
923 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
924 for (i=0; i<CRYPTO_num_locks(); i++)
925 {
926 lock_cs[i]=usnewsema(arena,1);
927 }
928
929 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
930 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
931 }
932
933void thread_cleanup(void)
934 {
935 int i;
936
937 CRYPTO_set_locking_callback(NULL);
938 for (i=0; i<CRYPTO_num_locks(); i++)
939 {
940 char buf[10];
941
942 sprintf(buf,"%2d:",i);
943 usdumpsema(lock_cs[i],stdout,buf);
944 usfreesema(lock_cs[i],arena);
945 }
946 OPENSSL_free(lock_cs);
947 }
948
949void irix_locking_callback(int mode, int type, char *file, int line)
950 {
951 if (mode & CRYPTO_LOCK)
952 {
953 printf("lock %d\n",type);
954 uspsema(lock_cs[type]);
955 }
956 else
957 {
958 printf("unlock %d\n",type);
959 usvsema(lock_cs[type]);
960 }
961 }
962
963void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
964 {
965 SSL_CTX *ssl_ctx[2];
966 int thread_ctx[MAX_THREAD_NUMBER];
967 int i;
968
969 ssl_ctx[0]=s_ctx;
970 ssl_ctx[1]=c_ctx;
971
972 for (i=0; i<thread_number; i++)
973 {
974 thread_ctx[i]=sproc((void (*)())ndoit,
975 PR_SADDR|PR_SFDS,(void *)ssl_ctx);
976 }
977
978 printf("reaping\n");
979 for (i=0; i<thread_number; i++)
980 {
981 wait(NULL);
982 }
983
984 printf("irix threads done (%d,%d)\n",
985 s_ctx->references,c_ctx->references);
986 }
987
988unsigned long irix_thread_id(void)
989 {
990 unsigned long ret;
991
992 ret=(unsigned long)getpid();
993 return(ret);
994 }
995#endif /* IRIX */
996
997#ifdef PTHREADS
998
999static pthread_mutex_t *lock_cs;
1000static long *lock_count;
1001
1002void thread_setup(void)
1003 {
1004 int i;
1005
1006 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
1007 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1008 for (i=0; i<CRYPTO_num_locks(); i++)
1009 {
1010 lock_count[i]=0;
1011 pthread_mutex_init(&(lock_cs[i]),NULL);
1012 }
1013
1014 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
1015 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
1016 }
1017
1018void thread_cleanup(void)
1019 {
1020 int i;
1021
1022 CRYPTO_set_locking_callback(NULL);
1023 fprintf(stderr,"cleanup\n");
1024 for (i=0; i<CRYPTO_num_locks(); i++)
1025 {
1026 pthread_mutex_destroy(&(lock_cs[i]));
1027 fprintf(stderr,"%8ld:%s\n",lock_count[i],
1028 CRYPTO_get_lock_name(i));
1029 }
1030 OPENSSL_free(lock_cs);
1031 OPENSSL_free(lock_count);
1032
1033 fprintf(stderr,"done cleanup\n");
1034 }
1035
1036void pthreads_locking_callback(int mode, int type, char *file,
1037 int line)
1038 {
1039#ifdef undef
1040 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
1041 CRYPTO_thread_id(),
1042 (mode&CRYPTO_LOCK)?"l":"u",
1043 (type&CRYPTO_READ)?"r":"w",file,line);
1044#endif
1045/*
1046 if (CRYPTO_LOCK_SSL_CERT == type)
1047 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
1048 CRYPTO_thread_id(),
1049 mode,file,line);
1050*/
1051 if (mode & CRYPTO_LOCK)
1052 {
1053 pthread_mutex_lock(&(lock_cs[type]));
1054 lock_count[type]++;
1055 }
1056 else
1057 {
1058 pthread_mutex_unlock(&(lock_cs[type]));
1059 }
1060 }
1061
1062void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1063 {
1064 SSL_CTX *ssl_ctx[2];
1065 pthread_t thread_ctx[MAX_THREAD_NUMBER];
1066 int i;
1067
1068 ssl_ctx[0]=s_ctx;
1069 ssl_ctx[1]=c_ctx;
1070
1071 /*
1072 thr_setconcurrency(thread_number);
1073 */
1074 for (i=0; i<thread_number; i++)
1075 {
1076 pthread_create(&(thread_ctx[i]), NULL,
1077 (void *(*)())ndoit, (void *)ssl_ctx);
1078 }
1079
1080 printf("reaping\n");
1081 for (i=0; i<thread_number; i++)
1082 {
1083 pthread_join(thread_ctx[i],NULL);
1084 }
1085
1086 printf("pthreads threads done (%d,%d)\n",
1087 s_ctx->references,c_ctx->references);
1088 }
1089
1090unsigned long pthreads_thread_id(void)
1091 {
1092 unsigned long ret;
1093
1094 ret=(unsigned long)pthread_self();
1095 return(ret);
1096 }
1097
1098#endif /* PTHREADS */
1099
1100
1101
diff --git a/src/lib/libcrypto/threads/profile.sh b/src/lib/libcrypto/threads/profile.sh
new file mode 100644
index 0000000000..6e3e342fc0
--- /dev/null
+++ b/src/lib/libcrypto/threads/profile.sh
@@ -0,0 +1,4 @@
1#!/bin/sh
2/bin/rm -f mttest
3cc -p -DSOLARIS -I../../include -g mttest.c -o mttest -L/usr/lib/libc -ldl -L../.. -lthread -lssl -lcrypto -lnsl -lsocket
4
diff --git a/src/lib/libcrypto/threads/ptest.bat b/src/lib/libcrypto/threads/ptest.bat
new file mode 100644
index 0000000000..4071b5ffea
--- /dev/null
+++ b/src/lib/libcrypto/threads/ptest.bat
@@ -0,0 +1,4 @@
1del mttest.exe
2
3purify cl /O2 -DWIN32 /MD -I..\..\out mttest.c /Femttest ..\..\out\ssl32.lib ..\..\out\crypt32.lib
4
diff --git a/src/lib/libcrypto/threads/pthread.sh b/src/lib/libcrypto/threads/pthread.sh
new file mode 100644
index 0000000000..f1c49821d2
--- /dev/null
+++ b/src/lib/libcrypto/threads/pthread.sh
@@ -0,0 +1,9 @@
1#!/bin/sh
2#
3# build using pthreads
4#
5# http://www.mit.edu:8001/people/proven/pthreads.html
6#
7/bin/rm -f mttest
8pgcc -DPTHREADS -I../../include -g mttest.c -o mttest -L../.. -lssl -lcrypto
9
diff --git a/src/lib/libcrypto/threads/pthread2.sh b/src/lib/libcrypto/threads/pthread2.sh
new file mode 100644
index 0000000000..41264c6a50
--- /dev/null
+++ b/src/lib/libcrypto/threads/pthread2.sh
@@ -0,0 +1,7 @@
1#!/bin/sh
2#
3# build using pthreads where it's already built into the system
4#
5/bin/rm -f mttest
6gcc -DPTHREADS -I../../include -g mttest.c -o mttest -L../.. -lssl -lcrypto -lpthread
7
diff --git a/src/lib/libcrypto/threads/pthreads-vms.com b/src/lib/libcrypto/threads/pthreads-vms.com
new file mode 100644
index 0000000000..63f5b8cc2e
--- /dev/null
+++ b/src/lib/libcrypto/threads/pthreads-vms.com
@@ -0,0 +1,9 @@
1$! To compile mttest on VMS.
2$!
3$! WARNING: only tested with DEC C so far.
4$
5$ arch := vax
6$ if f$getsyi("CPU") .ge. 128 then arch := axp
7$ define/user openssl [--.include.openssl]
8$ cc/def=PTHREADS mttest.c
9$ link mttest,[--.'arch'.exe.ssl]libssl/lib,[--.'arch'.exe.crypto]libcrypto/lib
diff --git a/src/lib/libcrypto/threads/purify.sh b/src/lib/libcrypto/threads/purify.sh
new file mode 100644
index 0000000000..6d44fe26b7
--- /dev/null
+++ b/src/lib/libcrypto/threads/purify.sh
@@ -0,0 +1,4 @@
1#!/bin/sh
2/bin/rm -f mttest
3purify cc -DSOLARIS -I../../include -g mttest.c -o mttest -L../.. -lthread -lssl -lcrypto -lnsl -lsocket
4
diff --git a/src/lib/libcrypto/threads/solaris.sh b/src/lib/libcrypto/threads/solaris.sh
new file mode 100644
index 0000000000..bc93094a27
--- /dev/null
+++ b/src/lib/libcrypto/threads/solaris.sh
@@ -0,0 +1,4 @@
1#!/bin/sh
2/bin/rm -f mttest
3cc -DSOLARIS -I../../include -g mttest.c -o mttest -L../.. -lthread -lssl -lcrypto -lnsl -lsocket
4
diff --git a/src/lib/libcrypto/threads/th-lock.c b/src/lib/libcrypto/threads/th-lock.c
new file mode 100644
index 0000000000..a6a79b9f45
--- /dev/null
+++ b/src/lib/libcrypto/threads/th-lock.c
@@ -0,0 +1,387 @@
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 <openssl/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
diff --git a/src/lib/libcrypto/threads/win32.bat b/src/lib/libcrypto/threads/win32.bat
new file mode 100644
index 0000000000..ee6da80a07
--- /dev/null
+++ b/src/lib/libcrypto/threads/win32.bat
@@ -0,0 +1,4 @@
1del mttest.exe
2
3cl /O2 -DWIN32 /MD -I..\..\out mttest.c /Femttest ..\..\out\ssleay32.lib ..\..\out\libeay32.lib
4