1 /* crypto/threads/mttest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
66 #ifdef OPENSSL_SYS_WIN32
75 #include <sys/prctl.h>
80 #ifdef OPENSSL_SYS_NETWARE
82 # define __int64 long long
86 #include <openssl/lhash.h>
87 #include <openssl/crypto.h>
88 #include <openssl/buffer.h>
89 #include "../../e_os.h"
90 #include <openssl/x509.h>
91 #include <openssl/ssl.h>
92 #include <openssl/err.h>
93 #include <openssl/rand.h>
95 #ifdef OPENSSL_NO_STDIO
96 #include "../buffer/bss_file.c"
99 #ifdef OPENSSL_SYS_NETWARE
100 #define TEST_SERVER_CERT "/openssl/apps/server.pem"
101 #define TEST_CLIENT_CERT "/openssl/apps/client.pem"
103 #define TEST_SERVER_CERT "../../apps/server.pem"
104 #define TEST_CLIENT_CERT "../../apps/client.pem"
107 #define MAX_THREAD_NUMBER 100
109 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
110 void thread_setup(void);
111 void thread_cleanup(void);
112 void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
114 void irix_locking_callback(int mode,int type,char *file,int line);
115 void solaris_locking_callback(int mode,int type,char *file,int line);
116 void win32_locking_callback(int mode,int type,char *file,int line);
117 void pthreads_locking_callback(int mode,int type,char *file,int line);
118 void netware_locking_callback(int mode,int type,char *file,int line);
119 void beos_locking_callback(int mode,int type,const char *file,int line);
121 unsigned long irix_thread_id(void );
122 unsigned long solaris_thread_id(void );
123 unsigned long pthreads_thread_id(void );
124 unsigned long netware_thread_id(void );
125 unsigned long beos_thread_id(void );
127 #if defined(OPENSSL_SYS_NETWARE)
128 static MPKMutex *lock_cs;
129 static MPKSema ThreadSem;
130 static long *lock_count;
134 BIO *bio_stdout=NULL;
136 static char *cipher=NULL;
142 int thread_number=10;
143 int number_of_loops=10;
147 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
149 int doit(char *ctx[4]);
150 static void print_stats(FILE *fp, SSL_CTX *ctx)
152 fprintf(fp,"%4ld items in the session cache\n",
153 SSL_CTX_sess_number(ctx));
154 fprintf(fp,"%4d client connects (SSL_connect())\n",
155 SSL_CTX_sess_connect(ctx));
156 fprintf(fp,"%4d client connects that finished\n",
157 SSL_CTX_sess_connect_good(ctx));
158 fprintf(fp,"%4d server connects (SSL_accept())\n",
159 SSL_CTX_sess_accept(ctx));
160 fprintf(fp,"%4d server connects that finished\n",
161 SSL_CTX_sess_accept_good(ctx));
162 fprintf(fp,"%4d session cache hits\n",SSL_CTX_sess_hits(ctx));
163 fprintf(fp,"%4d session cache misses\n",SSL_CTX_sess_misses(ctx));
164 fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx));
167 static void sv_usage(void)
169 fprintf(stderr,"usage: ssltest [args ...]\n");
170 fprintf(stderr,"\n");
171 fprintf(stderr," -server_auth - check server certificate\n");
172 fprintf(stderr," -client_auth - do client authentication\n");
173 fprintf(stderr," -v - more output\n");
174 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
175 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
176 fprintf(stderr," -threads arg - number of threads\n");
177 fprintf(stderr," -loops arg - number of 'connections', per thread\n");
178 fprintf(stderr," -reconnect - reuse session-id's\n");
179 fprintf(stderr," -stats - server session-id cache stats\n");
180 fprintf(stderr," -cert arg - server certificate/key\n");
181 fprintf(stderr," -ccert arg - client certificate/key\n");
182 fprintf(stderr," -ssl3 - just SSLv3n\n");
185 int main(int argc, char *argv[])
187 char *CApath=NULL,*CAfile=NULL;
194 char *scert=TEST_SERVER_CERT;
195 char *ccert=TEST_CLIENT_CERT;
196 SSL_METHOD *ssl_method=SSLv23_method();
198 RAND_seed(rnd_seed, sizeof rnd_seed);
201 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
202 if (bio_stdout == NULL)
203 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
209 if (strcmp(*argv,"-server_auth") == 0)
211 else if (strcmp(*argv,"-client_auth") == 0)
213 else if (strcmp(*argv,"-reconnect") == 0)
215 else if (strcmp(*argv,"-stats") == 0)
217 else if (strcmp(*argv,"-ssl3") == 0)
218 ssl_method=SSLv3_method();
219 else if (strcmp(*argv,"-CApath") == 0)
221 if (--argc < 1) goto bad;
224 else if (strcmp(*argv,"-CAfile") == 0)
226 if (--argc < 1) goto bad;
229 else if (strcmp(*argv,"-cert") == 0)
231 if (--argc < 1) goto bad;
234 else if (strcmp(*argv,"-ccert") == 0)
236 if (--argc < 1) goto bad;
239 else if (strcmp(*argv,"-threads") == 0)
241 if (--argc < 1) goto bad;
242 thread_number= atoi(*(++argv));
243 if (thread_number == 0) thread_number=1;
244 if (thread_number > MAX_THREAD_NUMBER)
245 thread_number=MAX_THREAD_NUMBER;
247 else if (strcmp(*argv,"-loops") == 0)
249 if (--argc < 1) goto bad;
250 number_of_loops= atoi(*(++argv));
251 if (number_of_loops == 0) number_of_loops=1;
255 fprintf(stderr,"unknown option %s\n",*argv);
269 if (cipher == NULL && OPENSSL_issetugid() == 0)
270 cipher=getenv("SSL_CIPHER");
272 SSL_load_error_strings();
273 OpenSSL_add_ssl_algorithms();
275 c_ctx=SSL_CTX_new(ssl_method);
276 s_ctx=SSL_CTX_new(ssl_method);
277 if ((c_ctx == NULL) || (s_ctx == NULL))
279 ERR_print_errors(bio_err);
283 SSL_CTX_set_session_cache_mode(s_ctx,
284 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
285 SSL_CTX_set_session_cache_mode(c_ctx,
286 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
288 if (!SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM))
290 ERR_print_errors(bio_err);
292 else if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM))
294 ERR_print_errors(bio_err);
300 SSL_CTX_use_certificate_file(c_ctx,ccert,
302 SSL_CTX_use_RSAPrivateKey_file(c_ctx,ccert,
306 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
307 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
308 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
309 (!SSL_CTX_set_default_verify_paths(c_ctx)))
311 fprintf(stderr,"SSL_load_verify_locations\n");
312 ERR_print_errors(bio_err);
318 fprintf(stderr,"client authentication\n");
319 SSL_CTX_set_verify(s_ctx,
320 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
325 fprintf(stderr,"server authentication\n");
326 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
331 do_threads(s_ctx,c_ctx);
337 fprintf(stderr,"Client SSL_CTX stats then free it\n");
338 print_stats(stderr,c_ctx);
343 fprintf(stderr,"Server SSL_CTX stats then free it\n");
344 print_stats(stderr,s_ctx);
347 fprintf(stderr,"-----\n");
348 lh_stats(SSL_CTX_sessions(s_ctx),stderr);
349 fprintf(stderr,"-----\n");
350 /*- lh_node_stats(SSL_CTX_sessions(s_ctx),stderr);
351 fprintf(stderr,"-----\n"); */
352 lh_node_usage_stats(SSL_CTX_sessions(s_ctx),stderr);
353 fprintf(stderr,"-----\n");
356 fprintf(stderr,"done free\n");
367 int ndoit(SSL_CTX *ssl_ctx[2])
373 ctx[0]=(char *)ssl_ctx[0];
374 ctx[1]=(char *)ssl_ctx[1];
378 ctx[2]=(char *)SSL_new(ssl_ctx[0]);
379 ctx[3]=(char *)SSL_new(ssl_ctx[1]);
387 fprintf(stdout,"started thread %lu\n",CRYPTO_thread_id());
388 for (i=0; i<number_of_loops; i++)
390 /*- fprintf(stderr,"%4d %2d ctx->ref (%3d,%3d)\n",
391 CRYPTO_thread_id(),i,
392 ssl_ctx[0]->references,
393 ssl_ctx[1]->references); */
394 /* pthread_delay_np(&tm);*/
399 fprintf(stdout,"error[%d] %lu - %d\n",
400 i,CRYPTO_thread_id(),ret);
404 fprintf(stdout,"DONE %lu\n",CRYPTO_thread_id());
407 SSL_free((SSL *)ctx[2]);
408 SSL_free((SSL *)ctx[3]);
410 # ifdef OPENSSL_SYS_NETWARE
411 MPKSemaphoreSignal(ThreadSem);
416 int doit(char *ctx[4])
418 SSL_CTX *s_ctx,*c_ctx;
419 static char cbuf[200],sbuf[200];
431 int do_server=0,do_client=0;
433 s_ctx=(SSL_CTX *)ctx[0];
434 c_ctx=(SSL_CTX *)ctx[1];
439 s_ssl=SSL_new(s_ctx);
444 c_ssl=SSL_new(c_ctx);
446 if ((s_ssl == NULL) || (c_ssl == NULL)) goto err;
448 c_to_s=BIO_new(BIO_s_mem());
449 s_to_c=BIO_new(BIO_s_mem());
450 if ((s_to_c == NULL) || (c_to_s == NULL)) goto err;
452 c_bio=BIO_new(BIO_f_ssl());
453 s_bio=BIO_new(BIO_f_ssl());
454 if ((c_bio == NULL) || (s_bio == NULL)) goto err;
456 SSL_set_connect_state(c_ssl);
457 SSL_set_bio(c_ssl,s_to_c,c_to_s);
458 BIO_set_ssl(c_bio,c_ssl,(ctx[2] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
460 SSL_set_accept_state(s_ssl);
461 SSL_set_bio(s_ssl,c_to_s,s_to_c);
462 BIO_set_ssl(s_bio,s_ssl,(ctx[3] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
470 /* We can always do writes */
476 i=(int)BIO_pending(s_bio);
477 if ((i && s_r) || s_w) do_server=1;
479 i=(int)BIO_pending(c_bio);
480 if ((i && c_r) || c_w) do_client=1;
482 if (do_server && verbose)
484 if (SSL_in_init(s_ssl))
485 printf("server waiting in SSL_accept - %s\n",
486 SSL_state_string_long(s_ssl));
488 printf("server:SSL_write()\n");
490 printf("server:SSL_read()\n");
493 if (do_client && verbose)
495 if (SSL_in_init(c_ssl))
496 printf("client waiting in SSL_connect - %s\n",
497 SSL_state_string_long(c_ssl));
499 printf("client:SSL_write()\n");
501 printf("client:SSL_read()\n");
504 if (!do_client && !do_server)
506 fprintf(stdout,"ERROR IN STARTUP\n");
509 if (do_client && !(done & C_DONE))
513 i=BIO_write(c_bio,"hello from client\n",18);
518 if (BIO_should_retry(c_bio))
520 if (BIO_should_read(c_bio))
522 if (BIO_should_write(c_bio))
527 fprintf(stderr,"ERROR in CLIENT\n");
528 ERR_print_errors_fp(stderr);
534 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
545 i=BIO_read(c_bio,cbuf,100);
550 if (BIO_should_retry(c_bio))
552 if (BIO_should_read(c_bio))
554 if (BIO_should_write(c_bio))
559 fprintf(stderr,"ERROR in CLIENT\n");
560 ERR_print_errors_fp(stderr);
566 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
573 fprintf(stdout,"CLIENT:from server:");
574 fwrite(cbuf,1,i,stdout);
581 if (do_server && !(done & S_DONE))
585 i=BIO_read(s_bio,sbuf,100);
590 if (BIO_should_retry(s_bio))
592 if (BIO_should_read(s_bio))
594 if (BIO_should_write(s_bio))
599 fprintf(stderr,"ERROR in SERVER\n");
600 ERR_print_errors_fp(stderr);
606 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
614 fprintf(stdout,"SERVER:from client:");
615 fwrite(sbuf,1,i,stdout);
622 i=BIO_write(s_bio,"hello from server\n",18);
627 if (BIO_should_retry(s_bio))
629 if (BIO_should_read(s_bio))
631 if (BIO_should_write(s_bio))
636 fprintf(stderr,"ERROR in SERVER\n");
637 ERR_print_errors_fp(stderr);
643 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
655 if ((done & S_DONE) && (done & C_DONE)) break;
656 # if defined(OPENSSL_SYS_NETWARE)
657 ThreadSwitchWithDelay();
661 SSL_set_shutdown(c_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
662 SSL_set_shutdown(s_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
665 fprintf(stdout,"DONE\n");
668 /* We have to set the BIO's to NULL otherwise they will be
669 * free()ed twice. Once when th s_ssl is SSL_free()ed and
670 * again when c_ssl is SSL_free()ed.
671 * This is a hack required because s_ssl and c_ssl are sharing the same
672 * BIO structure and SSL_set_bio() and SSL_free() automatically
673 * BIO_free non NULL entries.
674 * You should not normally do this or be required to do this */
687 /* The SSL's are optionally freed in the following calls */
688 if (c_to_s != NULL) BIO_free(c_to_s);
689 if (s_to_c != NULL) BIO_free(s_to_c);
691 if (c_bio != NULL) BIO_free(c_bio);
692 if (s_bio != NULL) BIO_free(s_bio);
696 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
702 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
707 fprintf(stderr,"depth=%d %s\n",
708 ctx->error_depth,buf);
710 fprintf(stderr,"depth=%d error=%d %s\n",
711 ctx->error_depth,ctx->error,buf);
717 #define THREAD_STACK_SIZE (16*1024)
719 #ifdef OPENSSL_SYS_WIN32
721 static HANDLE *lock_cs;
723 void thread_setup(void)
727 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
728 for (i=0; i<CRYPTO_num_locks(); i++)
730 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
733 CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
734 /* id callback defined */
737 void thread_cleanup(void)
741 CRYPTO_set_locking_callback(NULL);
742 for (i=0; i<CRYPTO_num_locks(); i++)
743 CloseHandle(lock_cs[i]);
744 OPENSSL_free(lock_cs);
747 void win32_locking_callback(int mode, int type, char *file, int line)
749 if (mode & CRYPTO_LOCK)
751 WaitForSingleObject(lock_cs[type],INFINITE);
755 ReleaseMutex(lock_cs[type]);
759 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
763 DWORD thread_id[MAX_THREAD_NUMBER];
764 HANDLE thread_handle[MAX_THREAD_NUMBER];
766 SYSTEMTIME start,end;
771 GetSystemTime(&start);
772 for (i=0; i<thread_number; i++)
774 thread_handle[i]=CreateThread(NULL,
776 (LPTHREAD_START_ROUTINE)ndoit,
783 for (i=0; i<thread_number; i+=50)
787 j=(thread_number < (i+50))?(thread_number-i):50;
789 if (WaitForMultipleObjects(j,
790 (CONST HANDLE *)&(thread_handle[i]),TRUE,INFINITE)
793 fprintf(stderr,"WaitForMultipleObjects failed:%d\n",GetLastError());
799 if (start.wDayOfWeek > end.wDayOfWeek) end.wDayOfWeek+=7;
800 ret=(end.wDayOfWeek-start.wDayOfWeek)*24;
802 ret=(ret+end.wHour-start.wHour)*60;
803 ret=(ret+end.wMinute-start.wMinute)*60;
804 ret=(ret+end.wSecond-start.wSecond);
805 ret+=(end.wMilliseconds-start.wMilliseconds)/1000.0;
807 printf("win32 threads done - %.3f seconds\n",ret);
810 #endif /* OPENSSL_SYS_WIN32 */
814 static mutex_t *lock_cs;
815 /*static rwlock_t *lock_cs; */
816 static long *lock_count;
818 void thread_setup(void)
822 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
823 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
824 for (i=0; i<CRYPTO_num_locks(); i++)
827 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
828 mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
831 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
832 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
835 void thread_cleanup(void)
839 CRYPTO_set_locking_callback(NULL);
841 fprintf(stderr,"cleanup\n");
843 for (i=0; i<CRYPTO_num_locks(); i++)
845 /* rwlock_destroy(&(lock_cs[i])); */
846 mutex_destroy(&(lock_cs[i]));
847 fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
849 OPENSSL_free(lock_cs);
850 OPENSSL_free(lock_count);
852 fprintf(stderr,"done cleanup\n");
856 void solaris_locking_callback(int mode, int type, char *file, int line)
859 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
861 (mode&CRYPTO_LOCK)?"l":"u",
862 (type&CRYPTO_READ)?"r":"w",file,line);
866 if (CRYPTO_LOCK_SSL_CERT == type)
867 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
871 if (mode & CRYPTO_LOCK)
874 if (mode & CRYPTO_READ)
875 rw_rdlock(&(lock_cs[type]));
877 rw_wrlock(&(lock_cs[type])); */
879 mutex_lock(&(lock_cs[type]));
884 /* rw_unlock(&(lock_cs[type])); */
885 mutex_unlock(&(lock_cs[type]));
889 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
892 thread_t thread_ctx[MAX_THREAD_NUMBER];
898 thr_setconcurrency(thread_number);
899 for (i=0; i<thread_number; i++)
901 thr_create(NULL, THREAD_STACK_SIZE,
909 for (i=0; i<thread_number; i++)
911 thr_join(thread_ctx[i],NULL,NULL);
914 printf("solaris threads done (%d,%d)\n",
915 s_ctx->references,c_ctx->references);
918 unsigned long solaris_thread_id(void)
922 ret=(unsigned long)thr_self();
930 static usptr_t *arena;
931 static usema_t **lock_cs;
933 void thread_setup(void)
938 strcpy(filename,"/tmp/mttest.XXXXXX");
941 usconfig(CONF_STHREADIOOFF);
942 usconfig(CONF_STHREADMALLOCOFF);
943 usconfig(CONF_INITUSERS,100);
944 usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
945 arena=usinit(filename);
948 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
949 for (i=0; i<CRYPTO_num_locks(); i++)
951 lock_cs[i]=usnewsema(arena,1);
954 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
955 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
958 void thread_cleanup(void)
962 CRYPTO_set_locking_callback(NULL);
963 for (i=0; i<CRYPTO_num_locks(); i++)
967 sprintf(buf,"%2d:",i);
968 usdumpsema(lock_cs[i],stdout,buf);
969 usfreesema(lock_cs[i],arena);
971 OPENSSL_free(lock_cs);
974 void irix_locking_callback(int mode, int type, char *file, int line)
976 if (mode & CRYPTO_LOCK)
978 printf("lock %d\n",type);
979 uspsema(lock_cs[type]);
983 printf("unlock %d\n",type);
984 usvsema(lock_cs[type]);
988 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
991 int thread_ctx[MAX_THREAD_NUMBER];
997 for (i=0; i<thread_number; i++)
999 thread_ctx[i]=sproc((void (*)())ndoit,
1000 PR_SADDR|PR_SFDS,(void *)ssl_ctx);
1003 printf("reaping\n");
1004 for (i=0; i<thread_number; i++)
1009 printf("irix threads done (%d,%d)\n",
1010 s_ctx->references,c_ctx->references);
1013 unsigned long irix_thread_id(void)
1017 ret=(unsigned long)getpid();
1024 static pthread_mutex_t *lock_cs;
1025 static long *lock_count;
1027 void thread_setup(void)
1031 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
1032 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1033 for (i=0; i<CRYPTO_num_locks(); i++)
1036 pthread_mutex_init(&(lock_cs[i]),NULL);
1039 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
1040 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
1043 void thread_cleanup(void)
1047 CRYPTO_set_locking_callback(NULL);
1048 fprintf(stderr,"cleanup\n");
1049 for (i=0; i<CRYPTO_num_locks(); i++)
1051 pthread_mutex_destroy(&(lock_cs[i]));
1052 fprintf(stderr,"%8ld:%s\n",lock_count[i],
1053 CRYPTO_get_lock_name(i));
1055 OPENSSL_free(lock_cs);
1056 OPENSSL_free(lock_count);
1058 fprintf(stderr,"done cleanup\n");
1061 void pthreads_locking_callback(int mode, int type, char *file,
1065 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
1067 (mode&CRYPTO_LOCK)?"l":"u",
1068 (type&CRYPTO_READ)?"r":"w",file,line);
1071 if (CRYPTO_LOCK_SSL_CERT == type)
1072 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
1076 if (mode & CRYPTO_LOCK)
1078 pthread_mutex_lock(&(lock_cs[type]));
1083 pthread_mutex_unlock(&(lock_cs[type]));
1087 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1089 SSL_CTX *ssl_ctx[2];
1090 pthread_t thread_ctx[MAX_THREAD_NUMBER];
1097 thr_setconcurrency(thread_number);
1099 for (i=0; i<thread_number; i++)
1101 pthread_create(&(thread_ctx[i]), NULL,
1102 (void *(*)())ndoit, (void *)ssl_ctx);
1105 printf("reaping\n");
1106 for (i=0; i<thread_number; i++)
1108 pthread_join(thread_ctx[i],NULL);
1111 printf("pthreads threads done (%d,%d)\n",
1112 s_ctx->references,c_ctx->references);
1115 unsigned long pthreads_thread_id(void)
1119 ret=(unsigned long)pthread_self();
1123 #endif /* PTHREADS */
1127 #ifdef OPENSSL_SYS_NETWARE
1129 void thread_setup(void)
1133 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex));
1134 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1135 for (i=0; i<CRYPTO_num_locks(); i++)
1138 lock_cs[i]=MPKMutexAlloc("OpenSSL mutex");
1141 ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0 );
1143 CRYPTO_set_id_callback((unsigned long (*)())netware_thread_id);
1144 CRYPTO_set_locking_callback((void (*)())netware_locking_callback);
1147 void thread_cleanup(void)
1151 CRYPTO_set_locking_callback(NULL);
1153 fprintf(stdout,"thread_cleanup\n");
1155 for (i=0; i<CRYPTO_num_locks(); i++)
1157 MPKMutexFree(lock_cs[i]);
1158 fprintf(stdout,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
1160 OPENSSL_free(lock_cs);
1161 OPENSSL_free(lock_count);
1163 MPKSemaphoreFree(ThreadSem);
1165 fprintf(stdout,"done cleanup\n");
1168 void netware_locking_callback(int mode, int type, char *file, int line)
1170 if (mode & CRYPTO_LOCK)
1172 MPKMutexLock(lock_cs[type]);
1176 MPKMutexUnlock(lock_cs[type]);
1179 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1181 SSL_CTX *ssl_ctx[2];
1186 for (i=0; i<thread_number; i++)
1188 BeginThread( (void(*)(void*))ndoit, NULL, THREAD_STACK_SIZE,
1190 ThreadSwitchWithDelay();
1193 printf("reaping\n");
1195 /* loop until all threads have signaled the semaphore */
1196 for (i=0; i<thread_number; i++)
1198 MPKSemaphoreWait(ThreadSem);
1200 printf("netware threads done (%d,%d)\n",
1201 s_ctx->references,c_ctx->references);
1204 unsigned long netware_thread_id(void)
1208 ret=(unsigned long)GetThreadID();
1211 #endif /* NETWARE */