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/mttest.c247
-rw-r--r--src/lib/libcrypto/threads/th-lock.c138
2 files changed, 179 insertions, 206 deletions
diff --git a/src/lib/libcrypto/threads/mttest.c b/src/lib/libcrypto/threads/mttest.c
index be395f2bc4..7142e4edc7 100644
--- a/src/lib/libcrypto/threads/mttest.c
+++ b/src/lib/libcrypto/threads/mttest.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,27 +74,29 @@
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 "../../e_os.h"
84 84#include <openssl/x509.h>
85#ifdef NO_FP_API 85#include <openssl/ssl.h>
86#include <openssl/err.h>
87#include <openssl/rand.h>
88
89#ifdef OPENSSL_NO_FP_API
86#define APPS_WIN16 90#define APPS_WIN16
87#include "../crypto/buffer/bss_file.c" 91#include "../buffer/bss_file.c"
88#endif 92#endif
89 93
90#define TEST_SERVER_CERT "../apps/server.pem" 94#define TEST_SERVER_CERT "../../apps/server.pem"
91#define TEST_CLIENT_CERT "../apps/client.pem" 95#define TEST_CLIENT_CERT "../../apps/client.pem"
92 96
93#define MAX_THREAD_NUMBER 100 97#define MAX_THREAD_NUMBER 100
94 98
95#ifndef NOPROTO 99int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
96int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth,
97 int error,char *arg);
98void thread_setup(void); 100void thread_setup(void);
99void thread_cleanup(void); 101void thread_cleanup(void);
100void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx); 102void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
@@ -108,23 +110,6 @@ unsigned long irix_thread_id(void );
108unsigned long solaris_thread_id(void ); 110unsigned long solaris_thread_id(void );
109unsigned long pthreads_thread_id(void ); 111unsigned long pthreads_thread_id(void );
110 112
111#else
112int MS_CALLBACK verify_callback();
113void thread_setup();
114void thread_cleanup();
115void do_threads();
116
117void irix_locking_callback();
118void solaris_locking_callback();
119void win32_locking_callback();
120void pthreads_locking_callback();
121
122unsigned long irix_thread_id();
123unsigned long solaris_thread_id();
124unsigned long pthreads_thread_id();
125
126#endif
127
128BIO *bio_err=NULL; 113BIO *bio_err=NULL;
129BIO *bio_stdout=NULL; 114BIO *bio_stdout=NULL;
130 115
@@ -139,15 +124,10 @@ int number_of_loops=10;
139int reconnect=0; 124int reconnect=0;
140int cache_stats=0; 125int cache_stats=0;
141 126
142#ifndef NOPROTO 127static const char rnd_seed[] = "string to make the random number generator think it has entropy";
143int doit(char *ctx[4]);
144#else
145int doit();
146#endif
147 128
148static void print_stats(fp,ctx) 129int doit(char *ctx[4]);
149FILE *fp; 130static void print_stats(FILE *fp, SSL_CTX *ctx)
150SSL_CTX *ctx;
151{ 131{
152 fprintf(fp,"%4ld items in the session cache\n", 132 fprintf(fp,"%4ld items in the session cache\n",
153 SSL_CTX_sess_number(ctx)); 133 SSL_CTX_sess_number(ctx));
@@ -164,7 +144,7 @@ SSL_CTX *ctx;
164 fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx)); 144 fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx));
165 } 145 }
166 146
167static void sv_usage() 147static void sv_usage(void)
168 { 148 {
169 fprintf(stderr,"usage: ssltest [args ...]\n"); 149 fprintf(stderr,"usage: ssltest [args ...]\n");
170 fprintf(stderr,"\n"); 150 fprintf(stderr,"\n");
@@ -182,9 +162,7 @@ static void sv_usage()
182 fprintf(stderr," -ssl3 - just SSLv3n\n"); 162 fprintf(stderr," -ssl3 - just SSLv3n\n");
183 } 163 }
184 164
185int main(argc, argv) 165int main(int argc, char *argv[])
186int argc;
187char *argv[];
188 { 166 {
189 char *CApath=NULL,*CAfile=NULL; 167 char *CApath=NULL,*CAfile=NULL;
190 int badop=0; 168 int badop=0;
@@ -197,6 +175,8 @@ char *argv[];
197 char *ccert=TEST_CLIENT_CERT; 175 char *ccert=TEST_CLIENT_CERT;
198 SSL_METHOD *ssl_method=SSLv23_method(); 176 SSL_METHOD *ssl_method=SSLv23_method();
199 177
178 RAND_seed(rnd_seed, sizeof rnd_seed);
179
200 if (bio_err == NULL) 180 if (bio_err == NULL)
201 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); 181 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
202 if (bio_stdout == NULL) 182 if (bio_stdout == NULL)
@@ -271,7 +251,7 @@ bad:
271 if (cipher == NULL) cipher=getenv("SSL_CIPHER"); 251 if (cipher == NULL) cipher=getenv("SSL_CIPHER");
272 252
273 SSL_load_error_strings(); 253 SSL_load_error_strings();
274 SSLeay_add_ssl_algorithms(); 254 OpenSSL_add_ssl_algorithms();
275 255
276 c_ctx=SSL_CTX_new(ssl_method); 256 c_ctx=SSL_CTX_new(ssl_method);
277 s_ctx=SSL_CTX_new(ssl_method); 257 s_ctx=SSL_CTX_new(ssl_method);
@@ -286,8 +266,15 @@ bad:
286 SSL_CTX_set_session_cache_mode(c_ctx, 266 SSL_CTX_set_session_cache_mode(c_ctx,
287 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER); 267 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
288 268
289 SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM); 269 if (!SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM))
290 SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM); 270 {
271 ERR_print_errors(bio_err);
272 }
273 else if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM))
274 {
275 ERR_print_errors(bio_err);
276 goto end;
277 }
291 278
292 if (client_auth) 279 if (client_auth)
293 { 280 {
@@ -358,8 +345,7 @@ end:
358#define C_DONE 1 345#define C_DONE 1
359#define S_DONE 2 346#define S_DONE 2
360 347
361int ndoit(ssl_ctx) 348int ndoit(SSL_CTX *ssl_ctx[2])
362SSL_CTX *ssl_ctx[2];
363 { 349 {
364 int i; 350 int i;
365 int ret; 351 int ret;
@@ -405,8 +391,7 @@ SSL_CTX *ssl_ctx[2];
405 return(0); 391 return(0);
406 } 392 }
407 393
408int doit(ctx) 394int doit(char *ctx[4])
409char *ctx[4];
410 { 395 {
411 SSL_CTX *s_ctx,*c_ctx; 396 SSL_CTX *s_ctx,*c_ctx;
412 static char cbuf[200],sbuf[200]; 397 static char cbuf[200],sbuf[200];
@@ -518,6 +503,7 @@ char *ctx[4];
518 else 503 else
519 { 504 {
520 fprintf(stderr,"ERROR in CLIENT\n"); 505 fprintf(stderr,"ERROR in CLIENT\n");
506 ERR_print_errors_fp(stderr);
521 return(1); 507 return(1);
522 } 508 }
523 } 509 }
@@ -549,6 +535,7 @@ char *ctx[4];
549 else 535 else
550 { 536 {
551 fprintf(stderr,"ERROR in CLIENT\n"); 537 fprintf(stderr,"ERROR in CLIENT\n");
538 ERR_print_errors_fp(stderr);
552 return(1); 539 return(1);
553 } 540 }
554 } 541 }
@@ -681,38 +668,39 @@ err:
681 return(0); 668 return(0);
682 } 669 }
683 670
684int MS_CALLBACK verify_callback(ok, xs, xi, depth, error, arg) 671int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
685int ok;
686X509 *xs;
687X509 *xi;
688int depth;
689int error;
690char *arg;
691 { 672 {
692 char buf[256]; 673 char *s, buf[256];
693 674
694 if (verbose) 675 if (verbose)
695 { 676 {
696 X509_NAME_oneline(X509_get_subject_name(xs),buf,256); 677 s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
697 if (ok) 678 buf,256);
698 fprintf(stderr,"depth=%d %s\n",depth,buf); 679 if (s != NULL)
699 else 680 {
700 fprintf(stderr,"depth=%d error=%d %s\n",depth,error,buf); 681 if (ok)
682 fprintf(stderr,"depth=%d %s\n",
683 ctx->error_depth,buf);
684 else
685 fprintf(stderr,"depth=%d error=%d %s\n",
686 ctx->error_depth,ctx->error,buf);
687 }
701 } 688 }
702 return(ok); 689 return(ok);
703 } 690 }
704 691
705#define THREAD_STACK_SIZE (16*1024) 692#define THREAD_STACK_SIZE (16*1024)
706 693
707#ifdef WIN32 694#ifdef OPENSSL_SYS_WIN32
708 695
709static HANDLE lock_cs[CRYPTO_NUM_LOCKS]; 696static HANDLE *lock_cs;
710 697
711void thread_setup() 698void thread_setup(void)
712 { 699 {
713 int i; 700 int i;
714 701
715 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 702 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
703 for (i=0; i<CRYPTO_num_locks(); i++)
716 { 704 {
717 lock_cs[i]=CreateMutex(NULL,FALSE,NULL); 705 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
718 } 706 }
@@ -721,20 +709,17 @@ void thread_setup()
721 /* id callback defined */ 709 /* id callback defined */
722 } 710 }
723 711
724void thread_cleanup() 712void thread_cleanup(void)
725 { 713 {
726 int i; 714 int i;
727 715
728 CRYPTO_set_locking_callback(NULL); 716 CRYPTO_set_locking_callback(NULL);
729 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 717 for (i=0; i<CRYPTO_num_locks(); i++)
730 CloseHandle(lock_cs[i]); 718 CloseHandle(lock_cs[i]);
719 OPENSSL_free(lock_cs);
731 } 720 }
732 721
733void win32_locking_callback(mode,type,file,line) 722void win32_locking_callback(int mode, int type, char *file, int line)
734int mode;
735int type;
736char *file;
737int line;
738 { 723 {
739 if (mode & CRYPTO_LOCK) 724 if (mode & CRYPTO_LOCK)
740 { 725 {
@@ -746,8 +731,7 @@ int line;
746 } 731 }
747 } 732 }
748 733
749void do_threads(s_ctx,c_ctx) 734void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
750SSL_CTX *s_ctx,*c_ctx;
751 { 735 {
752 double ret; 736 double ret;
753 SSL_CTX *ssl_ctx[2]; 737 SSL_CTX *ssl_ctx[2];
@@ -798,19 +782,21 @@ SSL_CTX *s_ctx,*c_ctx;
798 printf("win32 threads done - %.3f seconds\n",ret); 782 printf("win32 threads done - %.3f seconds\n",ret);
799 } 783 }
800 784
801#endif /* WIN32 */ 785#endif /* OPENSSL_SYS_WIN32 */
802 786
803#ifdef SOLARIS 787#ifdef SOLARIS
804 788
805static mutex_t lock_cs[CRYPTO_NUM_LOCKS]; 789static mutex_t *lock_cs;
806/*static rwlock_t lock_cs[CRYPTO_NUM_LOCKS]; */ 790/*static rwlock_t *lock_cs; */
807static long lock_count[CRYPTO_NUM_LOCKS]; 791static long *lock_count;
808 792
809void thread_setup() 793void thread_setup(void)
810 { 794 {
811 int i; 795 int i;
812 796
813 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 797 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
798 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
799 for (i=0; i<CRYPTO_num_locks(); i++)
814 { 800 {
815 lock_count[i]=0; 801 lock_count[i]=0;
816 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */ 802 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
@@ -821,40 +807,42 @@ void thread_setup()
821 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback); 807 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
822 } 808 }
823 809
824void thread_cleanup() 810void thread_cleanup(void)
825 { 811 {
826 int i; 812 int i;
827 813
828 CRYPTO_set_locking_callback(NULL); 814 CRYPTO_set_locking_callback(NULL);
829fprintf(stderr,"cleanup\n"); 815
830 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 816 fprintf(stderr,"cleanup\n");
817
818 for (i=0; i<CRYPTO_num_locks(); i++)
831 { 819 {
832 /* rwlock_destroy(&(lock_cs[i])); */ 820 /* rwlock_destroy(&(lock_cs[i])); */
833 mutex_destroy(&(lock_cs[i])); 821 mutex_destroy(&(lock_cs[i]));
834 fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i)); 822 fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
835 } 823 }
836fprintf(stderr,"done cleanup\n"); 824 OPENSSL_free(lock_cs);
825 OPENSSL_free(lock_count);
826
827 fprintf(stderr,"done cleanup\n");
828
837 } 829 }
838 830
839void solaris_locking_callback(mode,type,file,line) 831void solaris_locking_callback(int mode, int type, char *file, int line)
840int mode;
841int type;
842char *file;
843int line;
844 { 832 {
845#ifdef undef 833#ifdef undef
846fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", 834 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
847 CRYPTO_thread_id(), 835 CRYPTO_thread_id(),
848 (mode&CRYPTO_LOCK)?"l":"u", 836 (mode&CRYPTO_LOCK)?"l":"u",
849 (type&CRYPTO_READ)?"r":"w",file,line); 837 (type&CRYPTO_READ)?"r":"w",file,line);
850#endif 838#endif
851 839
852/* 840 /*
853if (CRYPTO_LOCK_SSL_CERT == type) 841 if (CRYPTO_LOCK_SSL_CERT == type)
854 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n", 842 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
855 CRYPTO_thread_id(), 843 CRYPTO_thread_id(),
856 mode,file,line); 844 mode,file,line);
857*/ 845 */
858 if (mode & CRYPTO_LOCK) 846 if (mode & CRYPTO_LOCK)
859 { 847 {
860 /* if (mode & CRYPTO_READ) 848 /* if (mode & CRYPTO_READ)
@@ -872,8 +860,7 @@ if (CRYPTO_LOCK_SSL_CERT == type)
872 } 860 }
873 } 861 }
874 862
875void do_threads(s_ctx,c_ctx) 863void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
876SSL_CTX *s_ctx,*c_ctx;
877 { 864 {
878 SSL_CTX *ssl_ctx[2]; 865 SSL_CTX *ssl_ctx[2];
879 thread_t thread_ctx[MAX_THREAD_NUMBER]; 866 thread_t thread_ctx[MAX_THREAD_NUMBER];
@@ -902,7 +889,7 @@ SSL_CTX *s_ctx,*c_ctx;
902 s_ctx->references,c_ctx->references); 889 s_ctx->references,c_ctx->references);
903 } 890 }
904 891
905unsigned long solaris_thread_id() 892unsigned long solaris_thread_id(void)
906 { 893 {
907 unsigned long ret; 894 unsigned long ret;
908 895
@@ -915,9 +902,9 @@ unsigned long solaris_thread_id()
915 902
916 903
917static usptr_t *arena; 904static usptr_t *arena;
918static usema_t *lock_cs[CRYPTO_NUM_LOCKS]; 905static usema_t **lock_cs;
919 906
920void thread_setup() 907void thread_setup(void)
921 { 908 {
922 int i; 909 int i;
923 char filename[20]; 910 char filename[20];
@@ -932,7 +919,8 @@ void thread_setup()
932 arena=usinit(filename); 919 arena=usinit(filename);
933 unlink(filename); 920 unlink(filename);
934 921
935 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 922 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
923 for (i=0; i<CRYPTO_num_locks(); i++)
936 { 924 {
937 lock_cs[i]=usnewsema(arena,1); 925 lock_cs[i]=usnewsema(arena,1);
938 } 926 }
@@ -941,12 +929,12 @@ void thread_setup()
941 CRYPTO_set_locking_callback((void (*)())irix_locking_callback); 929 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
942 } 930 }
943 931
944void thread_cleanup() 932void thread_cleanup(void)
945 { 933 {
946 int i; 934 int i;
947 935
948 CRYPTO_set_locking_callback(NULL); 936 CRYPTO_set_locking_callback(NULL);
949 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 937 for (i=0; i<CRYPTO_num_locks(); i++)
950 { 938 {
951 char buf[10]; 939 char buf[10];
952 940
@@ -954,13 +942,10 @@ void thread_cleanup()
954 usdumpsema(lock_cs[i],stdout,buf); 942 usdumpsema(lock_cs[i],stdout,buf);
955 usfreesema(lock_cs[i],arena); 943 usfreesema(lock_cs[i],arena);
956 } 944 }
945 OPENSSL_free(lock_cs);
957 } 946 }
958 947
959void irix_locking_callback(mode,type,file,line) 948void irix_locking_callback(int mode, int type, char *file, int line)
960int mode;
961int type;
962char *file;
963int line;
964 { 949 {
965 if (mode & CRYPTO_LOCK) 950 if (mode & CRYPTO_LOCK)
966 { 951 {
@@ -974,8 +959,7 @@ int line;
974 } 959 }
975 } 960 }
976 961
977void do_threads(s_ctx,c_ctx) 962void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
978SSL_CTX *s_ctx,*c_ctx;
979 { 963 {
980 SSL_CTX *ssl_ctx[2]; 964 SSL_CTX *ssl_ctx[2];
981 int thread_ctx[MAX_THREAD_NUMBER]; 965 int thread_ctx[MAX_THREAD_NUMBER];
@@ -1000,7 +984,7 @@ SSL_CTX *s_ctx,*c_ctx;
1000 s_ctx->references,c_ctx->references); 984 s_ctx->references,c_ctx->references);
1001 } 985 }
1002 986
1003unsigned long irix_thread_id() 987unsigned long irix_thread_id(void)
1004 { 988 {
1005 unsigned long ret; 989 unsigned long ret;
1006 990
@@ -1011,14 +995,16 @@ unsigned long irix_thread_id()
1011 995
1012#ifdef PTHREADS 996#ifdef PTHREADS
1013 997
1014static pthread_mutex_t lock_cs[CRYPTO_NUM_LOCKS]; 998static pthread_mutex_t *lock_cs;
1015static long lock_count[CRYPTO_NUM_LOCKS]; 999static long *lock_count;
1016 1000
1017void thread_setup() 1001void thread_setup(void)
1018 { 1002 {
1019 int i; 1003 int i;
1020 1004
1021 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 1005 lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
1006 lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1007 for (i=0; i<CRYPTO_num_locks(); i++)
1022 { 1008 {
1023 lock_count[i]=0; 1009 lock_count[i]=0;
1024 pthread_mutex_init(&(lock_cs[i]),NULL); 1010 pthread_mutex_init(&(lock_cs[i]),NULL);
@@ -1028,26 +1014,26 @@ void thread_setup()
1028 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); 1014 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
1029 } 1015 }
1030 1016
1031void thread_cleanup() 1017void thread_cleanup(void)
1032 { 1018 {
1033 int i; 1019 int i;
1034 1020
1035 CRYPTO_set_locking_callback(NULL); 1021 CRYPTO_set_locking_callback(NULL);
1036 fprintf(stderr,"cleanup\n"); 1022 fprintf(stderr,"cleanup\n");
1037 for (i=0; i<CRYPTO_NUM_LOCKS; i++) 1023 for (i=0; i<CRYPTO_num_locks(); i++)
1038 { 1024 {
1039 pthread_mutex_destroy(&(lock_cs[i])); 1025 pthread_mutex_destroy(&(lock_cs[i]));
1040 fprintf(stderr,"%8ld:%s\n",lock_count[i], 1026 fprintf(stderr,"%8ld:%s\n",lock_count[i],
1041 CRYPTO_get_lock_name(i)); 1027 CRYPTO_get_lock_name(i));
1042 } 1028 }
1029 OPENSSL_free(lock_cs);
1030 OPENSSL_free(lock_count);
1031
1043 fprintf(stderr,"done cleanup\n"); 1032 fprintf(stderr,"done cleanup\n");
1044 } 1033 }
1045 1034
1046void pthreads_locking_callback(mode,type,file,line) 1035void pthreads_locking_callback(int mode, int type, char *file,
1047int mode; 1036 int line)
1048int type;
1049char *file;
1050int line;
1051 { 1037 {
1052#ifdef undef 1038#ifdef undef
1053 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", 1039 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
@@ -1072,8 +1058,7 @@ int line;
1072 } 1058 }
1073 } 1059 }
1074 1060
1075void do_threads(s_ctx,c_ctx) 1061void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1076SSL_CTX *s_ctx,*c_ctx;
1077 { 1062 {
1078 SSL_CTX *ssl_ctx[2]; 1063 SSL_CTX *ssl_ctx[2];
1079 pthread_t thread_ctx[MAX_THREAD_NUMBER]; 1064 pthread_t thread_ctx[MAX_THREAD_NUMBER];
@@ -1098,10 +1083,10 @@ SSL_CTX *s_ctx,*c_ctx;
1098 } 1083 }
1099 1084
1100 printf("pthreads threads done (%d,%d)\n", 1085 printf("pthreads threads done (%d,%d)\n",
1101 s_ctx->references,c_ctx->references); 1086 s_ctx->references,c_ctx->references);
1102 } 1087 }
1103 1088
1104unsigned long pthreads_thread_id() 1089unsigned long pthreads_thread_id(void)
1105 { 1090 {
1106 unsigned long ret; 1091 unsigned long ret;
1107 1092
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