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.]
64 # include <typedefs.h>
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_FP_API
97 # include "../buffer/bss_file.c"
100 #ifdef OPENSSL_SYS_NETWARE
101 # define TEST_SERVER_CERT "/openssl/apps/server.pem"
102 # define TEST_CLIENT_CERT "/openssl/apps/client.pem"
104 # define TEST_SERVER_CERT "../../apps/server.pem"
105 # define TEST_CLIENT_CERT "../../apps/client.pem"
108 #define MAX_THREAD_NUMBER 100
110 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
111 void thread_setup(void);
112 void thread_cleanup(void);
113 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx);
115 void irix_locking_callback(int mode, int type, char *file, int line);
116 void solaris_locking_callback(int mode, int type, char *file, int line);
117 void win32_locking_callback(int mode, int type, char *file, int line);
118 void pthreads_locking_callback(int mode, int type, char *file, int line);
119 void netware_locking_callback(int mode, int type, 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);
126 #if defined(OPENSSL_SYS_NETWARE)
127 static MPKMutex *lock_cs;
128 static MPKSema ThreadSem;
129 static long *lock_count;
133 BIO *bio_stdout = NULL;
135 static char *cipher = NULL;
138 static int s_nbio = 0;
141 int thread_number = 10;
142 int number_of_loops = 10;
146 static const char rnd_seed[] =
147 "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;
192 SSL_CTX *s_ctx = NULL;
193 SSL_CTX *c_ctx = 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);
208 if (strcmp(*argv, "-server_auth") == 0)
210 else if (strcmp(*argv, "-client_auth") == 0)
212 else if (strcmp(*argv, "-reconnect") == 0)
214 else if (strcmp(*argv, "-stats") == 0)
216 else if (strcmp(*argv, "-ssl3") == 0)
217 ssl_method = SSLv3_method();
218 else if (strcmp(*argv, "-ssl2") == 0)
219 ssl_method = SSLv2_method();
220 else if (strcmp(*argv, "-CApath") == 0) {
224 } else if (strcmp(*argv, "-CAfile") == 0) {
228 } else if (strcmp(*argv, "-cert") == 0) {
232 } else if (strcmp(*argv, "-ccert") == 0) {
236 } else if (strcmp(*argv, "-threads") == 0) {
239 thread_number = atoi(*(++argv));
240 if (thread_number == 0)
242 if (thread_number > MAX_THREAD_NUMBER)
243 thread_number = MAX_THREAD_NUMBER;
244 } else if (strcmp(*argv, "-loops") == 0) {
247 number_of_loops = atoi(*(++argv));
248 if (number_of_loops == 0)
251 fprintf(stderr, "unknown option %s\n", *argv);
264 if (cipher == NULL && OPENSSL_issetugid() == 0)
265 cipher = getenv("SSL_CIPHER");
267 SSL_load_error_strings();
268 OpenSSL_add_ssl_algorithms();
270 c_ctx = SSL_CTX_new(ssl_method);
271 s_ctx = SSL_CTX_new(ssl_method);
272 if ((c_ctx == NULL) || (s_ctx == NULL)) {
273 ERR_print_errors(bio_err);
277 SSL_CTX_set_session_cache_mode(s_ctx,
278 SSL_SESS_CACHE_NO_AUTO_CLEAR |
279 SSL_SESS_CACHE_SERVER);
280 SSL_CTX_set_session_cache_mode(c_ctx,
281 SSL_SESS_CACHE_NO_AUTO_CLEAR |
282 SSL_SESS_CACHE_SERVER);
284 if (!SSL_CTX_use_certificate_file(s_ctx, scert, SSL_FILETYPE_PEM)) {
285 ERR_print_errors(bio_err);
287 if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx, scert, SSL_FILETYPE_PEM)) {
288 ERR_print_errors(bio_err);
293 SSL_CTX_use_certificate_file(c_ctx, ccert, SSL_FILETYPE_PEM);
294 SSL_CTX_use_RSAPrivateKey_file(c_ctx, ccert, SSL_FILETYPE_PEM);
297 if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) ||
298 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
299 (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) ||
300 (!SSL_CTX_set_default_verify_paths(c_ctx))) {
301 fprintf(stderr, "SSL_load_verify_locations\n");
302 ERR_print_errors(bio_err);
307 fprintf(stderr, "client authentication\n");
308 SSL_CTX_set_verify(s_ctx,
309 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
313 fprintf(stderr, "server authentication\n");
314 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
318 do_threads(s_ctx, c_ctx);
323 fprintf(stderr, "Client SSL_CTX stats then free it\n");
324 print_stats(stderr, c_ctx);
328 fprintf(stderr, "Server SSL_CTX stats then free it\n");
329 print_stats(stderr, s_ctx);
331 fprintf(stderr, "-----\n");
332 lh_stats(SSL_CTX_sessions(s_ctx), stderr);
333 fprintf(stderr, "-----\n");
334 /*- lh_node_stats(SSL_CTX_sessions(s_ctx),stderr);
335 fprintf(stderr,"-----\n"); */
336 lh_node_usage_stats(SSL_CTX_sessions(s_ctx), stderr);
337 fprintf(stderr, "-----\n");
340 fprintf(stderr, "done free\n");
351 int ndoit(SSL_CTX *ssl_ctx[2])
357 ctx[0] = (char *)ssl_ctx[0];
358 ctx[1] = (char *)ssl_ctx[1];
361 ctx[2] = (char *)SSL_new(ssl_ctx[0]);
362 ctx[3] = (char *)SSL_new(ssl_ctx[1]);
368 fprintf(stdout, "started thread %lu\n", CRYPTO_thread_id());
369 for (i = 0; i < number_of_loops; i++) {
370 /*- fprintf(stderr,"%4d %2d ctx->ref (%3d,%3d)\n",
371 CRYPTO_thread_id(),i,
372 ssl_ctx[0]->references,
373 ssl_ctx[1]->references); */
374 /* pthread_delay_np(&tm); */
378 fprintf(stdout, "error[%d] %lu - %d\n",
379 i, CRYPTO_thread_id(), ret);
383 fprintf(stdout, "DONE %lu\n", CRYPTO_thread_id());
385 SSL_free((SSL *)ctx[2]);
386 SSL_free((SSL *)ctx[3]);
388 #ifdef OPENSSL_SYS_NETWARE
389 MPKSemaphoreSignal(ThreadSem);
394 int doit(char *ctx[4])
396 SSL_CTX *s_ctx, *c_ctx;
397 static char cbuf[200], sbuf[200];
404 int c_r, c_w, s_r, s_w;
408 int c_write, s_write;
409 int do_server = 0, do_client = 0;
411 s_ctx = (SSL_CTX *)ctx[0];
412 c_ctx = (SSL_CTX *)ctx[1];
415 s_ssl = (SSL *)ctx[2];
417 s_ssl = SSL_new(s_ctx);
420 c_ssl = (SSL *)ctx[3];
422 c_ssl = SSL_new(c_ctx);
424 if ((s_ssl == NULL) || (c_ssl == NULL))
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))
432 c_bio = BIO_new(BIO_f_ssl());
433 s_bio = BIO_new(BIO_f_ssl());
434 if ((c_bio == NULL) || (s_bio == NULL))
437 SSL_set_connect_state(c_ssl);
438 SSL_set_bio(c_ssl, s_to_c, c_to_s);
439 BIO_set_ssl(c_bio, c_ssl, (ctx[2] == NULL) ? BIO_CLOSE : BIO_NOCLOSE);
441 SSL_set_accept_state(s_ssl);
442 SSL_set_bio(s_ssl, c_to_s, s_to_c);
443 BIO_set_ssl(s_bio, s_ssl, (ctx[3] == NULL) ? BIO_CLOSE : BIO_NOCLOSE);
451 c_write = 1, s_write = 0;
453 /* We can always do writes */
458 i = (int)BIO_pending(s_bio);
459 if ((i && s_r) || s_w)
462 i = (int)BIO_pending(c_bio);
463 if ((i && c_r) || c_w)
466 if (do_server && verbose) {
467 if (SSL_in_init(s_ssl))
468 printf("server waiting in SSL_accept - %s\n",
469 SSL_state_string_long(s_ssl));
471 printf("server:SSL_write()\n");
473 printf("server:SSL_read()\n");
476 if (do_client && verbose) {
477 if (SSL_in_init(c_ssl))
478 printf("client waiting in SSL_connect - %s\n",
479 SSL_state_string_long(c_ssl));
481 printf("client:SSL_write()\n");
483 printf("client:SSL_read()\n");
486 if (!do_client && !do_server) {
487 fprintf(stdout, "ERROR IN STARTUP\n");
490 if (do_client && !(done & C_DONE)) {
492 i = BIO_write(c_bio, "hello from client\n", 18);
496 if (BIO_should_retry(c_bio)) {
497 if (BIO_should_read(c_bio))
499 if (BIO_should_write(c_bio))
502 fprintf(stderr, "ERROR in CLIENT\n");
503 ERR_print_errors_fp(stderr);
507 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
514 i = BIO_read(c_bio, cbuf, 100);
518 if (BIO_should_retry(c_bio)) {
519 if (BIO_should_read(c_bio))
521 if (BIO_should_write(c_bio))
524 fprintf(stderr, "ERROR in CLIENT\n");
525 ERR_print_errors_fp(stderr);
529 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
534 fprintf(stdout, "CLIENT:from server:");
535 fwrite(cbuf, 1, i, stdout);
542 if (do_server && !(done & S_DONE)) {
544 i = BIO_read(s_bio, sbuf, 100);
548 if (BIO_should_retry(s_bio)) {
549 if (BIO_should_read(s_bio))
551 if (BIO_should_write(s_bio))
554 fprintf(stderr, "ERROR in SERVER\n");
555 ERR_print_errors_fp(stderr);
559 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
565 fprintf(stdout, "SERVER:from client:");
566 fwrite(sbuf, 1, i, stdout);
571 i = BIO_write(s_bio, "hello from server\n", 18);
575 if (BIO_should_retry(s_bio)) {
576 if (BIO_should_read(s_bio))
578 if (BIO_should_write(s_bio))
581 fprintf(stderr, "ERROR in SERVER\n");
582 ERR_print_errors_fp(stderr);
586 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
596 if ((done & S_DONE) && (done & C_DONE))
598 #if defined(OPENSSL_SYS_NETWARE)
599 ThreadSwitchWithDelay();
603 SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
604 SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
607 fprintf(stdout, "DONE\n");
611 * We have to set the BIO's to NULL otherwise they will be free()ed
612 * twice. Once when th s_ssl is SSL_free()ed and again when c_ssl is
613 * SSL_free()ed. This is a hack required because s_ssl and c_ssl are
614 * sharing the same BIO structure and SSL_set_bio() and SSL_free()
615 * automatically BIO_free non NULL entries. You should not normally do
616 * this or be required to do this
628 /* The SSL's are optionally freed in the following calls */
641 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
646 s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
650 fprintf(stderr, "depth=%d %s\n", ctx->error_depth, buf);
652 fprintf(stderr, "depth=%d error=%d %s\n",
653 ctx->error_depth, ctx->error, buf);
659 #define THREAD_STACK_SIZE (16*1024)
661 #ifdef OPENSSL_SYS_WIN32
663 static HANDLE *lock_cs;
665 void thread_setup(void)
669 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
670 for (i = 0; i < CRYPTO_num_locks(); i++) {
671 lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
674 CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
675 win32_locking_callback);
676 /* id callback defined */
679 void thread_cleanup(void)
683 CRYPTO_set_locking_callback(NULL);
684 for (i = 0; i < CRYPTO_num_locks(); i++)
685 CloseHandle(lock_cs[i]);
686 OPENSSL_free(lock_cs);
689 void win32_locking_callback(int mode, int type, char *file, int line)
691 if (mode & CRYPTO_LOCK) {
692 WaitForSingleObject(lock_cs[type], INFINITE);
694 ReleaseMutex(lock_cs[type]);
698 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
702 DWORD thread_id[MAX_THREAD_NUMBER];
703 HANDLE thread_handle[MAX_THREAD_NUMBER];
705 SYSTEMTIME start, end;
710 GetSystemTime(&start);
711 for (i = 0; i < thread_number; i++) {
712 thread_handle[i] = CreateThread(NULL,
714 (LPTHREAD_START_ROUTINE) ndoit,
715 (void *)ssl_ctx, 0L, &(thread_id[i]));
719 for (i = 0; i < thread_number; i += 50) {
722 j = (thread_number < (i + 50)) ? (thread_number - i) : 50;
724 if (WaitForMultipleObjects(j,
725 (CONST HANDLE *) & (thread_handle[i]),
728 fprintf(stderr, "WaitForMultipleObjects failed:%d\n",
735 if (start.wDayOfWeek > end.wDayOfWeek)
737 ret = (end.wDayOfWeek - start.wDayOfWeek) * 24;
739 ret = (ret + end.wHour - start.wHour) * 60;
740 ret = (ret + end.wMinute - start.wMinute) * 60;
741 ret = (ret + end.wSecond - start.wSecond);
742 ret += (end.wMilliseconds - start.wMilliseconds) / 1000.0;
744 printf("win32 threads done - %.3f seconds\n", ret);
747 #endif /* OPENSSL_SYS_WIN32 */
751 static mutex_t *lock_cs;
753 * static rwlock_t *lock_cs;
755 static long *lock_count;
757 void thread_setup(void)
761 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
762 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
763 for (i = 0; i < CRYPTO_num_locks(); i++) {
765 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
766 mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
769 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
770 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
773 void thread_cleanup(void)
777 CRYPTO_set_locking_callback(NULL);
779 fprintf(stderr, "cleanup\n");
781 for (i = 0; i < CRYPTO_num_locks(); i++) {
782 /* rwlock_destroy(&(lock_cs[i])); */
783 mutex_destroy(&(lock_cs[i]));
784 fprintf(stderr, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
786 OPENSSL_free(lock_cs);
787 OPENSSL_free(lock_count);
789 fprintf(stderr, "done cleanup\n");
793 void solaris_locking_callback(int mode, int type, char *file, int line)
796 fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
798 (mode & CRYPTO_LOCK) ? "l" : "u",
799 (type & CRYPTO_READ) ? "r" : "w", file, line);
803 if (CRYPTO_LOCK_SSL_CERT == type)
804 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
808 if (mode & CRYPTO_LOCK) {
810 if (mode & CRYPTO_READ)
811 rw_rdlock(&(lock_cs[type]));
813 rw_wrlock(&(lock_cs[type])); */
815 mutex_lock(&(lock_cs[type]));
818 /* rw_unlock(&(lock_cs[type])); */
819 mutex_unlock(&(lock_cs[type]));
823 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
826 thread_t thread_ctx[MAX_THREAD_NUMBER];
832 thr_setconcurrency(thread_number);
833 for (i = 0; i < thread_number; i++) {
834 thr_create(NULL, THREAD_STACK_SIZE,
835 (void *(*)())ndoit, (void *)ssl_ctx, 0L, &(thread_ctx[i]));
839 for (i = 0; i < thread_number; i++) {
840 thr_join(thread_ctx[i], NULL, NULL);
843 printf("solaris threads done (%d,%d)\n",
844 s_ctx->references, c_ctx->references);
847 unsigned long solaris_thread_id(void)
851 ret = (unsigned long)thr_self();
858 static usptr_t *arena;
859 static usema_t **lock_cs;
861 void thread_setup(void)
866 strcpy(filename, "/tmp/mttest.XXXXXX");
869 usconfig(CONF_STHREADIOOFF);
870 usconfig(CONF_STHREADMALLOCOFF);
871 usconfig(CONF_INITUSERS, 100);
872 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
873 arena = usinit(filename);
876 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
877 for (i = 0; i < CRYPTO_num_locks(); i++) {
878 lock_cs[i] = usnewsema(arena, 1);
881 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
882 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
885 void thread_cleanup(void)
889 CRYPTO_set_locking_callback(NULL);
890 for (i = 0; i < CRYPTO_num_locks(); i++) {
893 sprintf(buf, "%2d:", i);
894 usdumpsema(lock_cs[i], stdout, buf);
895 usfreesema(lock_cs[i], arena);
897 OPENSSL_free(lock_cs);
900 void irix_locking_callback(int mode, int type, char *file, int line)
902 if (mode & CRYPTO_LOCK) {
903 printf("lock %d\n", type);
904 uspsema(lock_cs[type]);
906 printf("unlock %d\n", type);
907 usvsema(lock_cs[type]);
911 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
914 int thread_ctx[MAX_THREAD_NUMBER];
920 for (i = 0; i < thread_number; i++) {
921 thread_ctx[i] = sproc((void (*)())ndoit,
922 PR_SADDR | PR_SFDS, (void *)ssl_ctx);
926 for (i = 0; i < thread_number; i++) {
930 printf("irix threads done (%d,%d)\n",
931 s_ctx->references, c_ctx->references);
934 unsigned long irix_thread_id(void)
938 ret = (unsigned long)getpid();
945 static pthread_mutex_t *lock_cs;
946 static long *lock_count;
948 void thread_setup(void)
952 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
953 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
954 for (i = 0; i < CRYPTO_num_locks(); i++) {
956 pthread_mutex_init(&(lock_cs[i]), NULL);
959 CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
960 CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
963 void thread_cleanup(void)
967 CRYPTO_set_locking_callback(NULL);
968 fprintf(stderr, "cleanup\n");
969 for (i = 0; i < CRYPTO_num_locks(); i++) {
970 pthread_mutex_destroy(&(lock_cs[i]));
971 fprintf(stderr, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
973 OPENSSL_free(lock_cs);
974 OPENSSL_free(lock_count);
976 fprintf(stderr, "done cleanup\n");
979 void pthreads_locking_callback(int mode, int type, char *file, int line)
982 fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
984 (mode & CRYPTO_LOCK) ? "l" : "u",
985 (type & CRYPTO_READ) ? "r" : "w", file, line);
988 if (CRYPTO_LOCK_SSL_CERT == type)
989 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
993 if (mode & CRYPTO_LOCK) {
994 pthread_mutex_lock(&(lock_cs[type]));
997 pthread_mutex_unlock(&(lock_cs[type]));
1001 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1003 SSL_CTX *ssl_ctx[2];
1004 pthread_t thread_ctx[MAX_THREAD_NUMBER];
1011 * thr_setconcurrency(thread_number);
1013 for (i = 0; i < thread_number; i++) {
1014 pthread_create(&(thread_ctx[i]), NULL,
1015 (void *(*)())ndoit, (void *)ssl_ctx);
1018 printf("reaping\n");
1019 for (i = 0; i < thread_number; i++) {
1020 pthread_join(thread_ctx[i], NULL);
1023 printf("pthreads threads done (%d,%d)\n",
1024 s_ctx->references, c_ctx->references);
1027 unsigned long pthreads_thread_id(void)
1031 ret = (unsigned long)pthread_self();
1035 #endif /* PTHREADS */
1037 #ifdef OPENSSL_SYS_NETWARE
1039 void thread_setup(void)
1043 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex));
1044 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1045 for (i = 0; i < CRYPTO_num_locks(); i++) {
1047 lock_cs[i] = MPKMutexAlloc("OpenSSL mutex");
1050 ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0);
1052 CRYPTO_set_id_callback((unsigned long (*)())netware_thread_id);
1053 CRYPTO_set_locking_callback((void (*)())netware_locking_callback);
1056 void thread_cleanup(void)
1060 CRYPTO_set_locking_callback(NULL);
1062 fprintf(stdout, "thread_cleanup\n");
1064 for (i = 0; i < CRYPTO_num_locks(); i++) {
1065 MPKMutexFree(lock_cs[i]);
1066 fprintf(stdout, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
1068 OPENSSL_free(lock_cs);
1069 OPENSSL_free(lock_count);
1071 MPKSemaphoreFree(ThreadSem);
1073 fprintf(stdout, "done cleanup\n");
1076 void netware_locking_callback(int mode, int type, char *file, int line)
1078 if (mode & CRYPTO_LOCK) {
1079 MPKMutexLock(lock_cs[type]);
1082 MPKMutexUnlock(lock_cs[type]);
1085 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1087 SSL_CTX *ssl_ctx[2];
1092 for (i = 0; i < thread_number; i++) {
1093 BeginThread((void (*)(void *))ndoit, NULL, THREAD_STACK_SIZE,
1095 ThreadSwitchWithDelay();
1098 printf("reaping\n");
1100 /* loop until all threads have signaled the semaphore */
1101 for (i = 0; i < thread_number; i++) {
1102 MPKSemaphoreWait(ThreadSem);
1104 printf("netware threads done (%d,%d)\n",
1105 s_ctx->references, c_ctx->references);
1108 unsigned long netware_thread_id(void)
1112 ret = (unsigned long)GetThreadID();
1115 #endif /* NETWARE */