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 <openssl/x509.h>
90 #include <openssl/ssl.h>
91 #include <openssl/err.h>
92 #include <openssl/rand.h>
94 #ifdef OPENSSL_SYS_NETWARE
95 # define TEST_SERVER_CERT "/openssl/apps/server.pem"
96 # define TEST_CLIENT_CERT "/openssl/apps/client.pem"
98 # define TEST_SERVER_CERT "../../apps/server.pem"
99 # define TEST_CLIENT_CERT "../../apps/client.pem"
102 #define MAX_THREAD_NUMBER 100
104 int verify_callback(int ok, X509_STORE_CTX *xs);
105 void thread_setup(void);
106 void thread_cleanup(void);
107 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx);
109 void irix_locking_callback(int mode, int type, const char *file, int line);
110 void solaris_locking_callback(int mode, int type, const char *file, int line);
111 void win32_locking_callback(int mode, int type, const char *file, int line);
112 void pthreads_locking_callback(int mode, int type, const char *file, int line);
113 void netware_locking_callback(int mode, int type, const char *file, int line);
114 void beos_locking_callback(int mode, int type, const char *file, int line);
116 void irix_thread_id(CRYPTO_THREADID *tid);
117 void solaris_thread_id(CRYPTO_THREADID *tid);
118 void pthreads_thread_id(CRYPTO_THREADID *tid);
119 void netware_thread_id(CRYPTO_THREADID *tid);
120 void beos_thread_id(CRYPTO_THREADID *tid);
122 #if defined(OPENSSL_SYS_NETWARE)
123 static MPKMutex *lock_cs;
124 static MPKSema ThreadSem;
125 static long *lock_count;
129 BIO *bio_stdout = NULL;
131 static char *cipher = NULL;
134 static int s_nbio = 0;
137 int thread_number = 10;
138 int number_of_loops = 10;
142 static const char rnd_seed[] =
143 "string to make the random number generator think it has entropy";
145 int doit(char *ctx[4]);
146 static void print_stats(BIO *bio, SSL_CTX *ctx)
148 BIO_printf(bio, "%4ld items in the session cache\n",
149 SSL_CTX_sess_number(ctx));
150 BIO_printf(bio, "%4d client connects (SSL_connect())\n",
151 SSL_CTX_sess_connect(ctx));
152 BIO_printf(bio, "%4d client connects that finished\n",
153 SSL_CTX_sess_connect_good(ctx));
154 BIO_printf(bio, "%4d server connects (SSL_accept())\n",
155 SSL_CTX_sess_accept(ctx));
156 BIO_printf(bio, "%4d server connects that finished\n",
157 SSL_CTX_sess_accept_good(ctx));
158 BIO_printf(bio, "%4d session cache hits\n", SSL_CTX_sess_hits(ctx));
159 BIO_printf(bio, "%4d session cache misses\n", SSL_CTX_sess_misses(ctx));
160 BIO_printf(bio, "%4d session cache timeouts\n", SSL_CTX_sess_timeouts(ctx));
163 static void sv_usage(void)
165 BIO_printf(bio_err, "usage: ssltest [args ...]\n");
166 BIO_printf(bio_err, "\n");
167 BIO_printf(bio_err, " -server_auth - check server certificate\n");
168 BIO_printf(bio_err, " -client_auth - do client authentication\n");
169 BIO_printf(bio_err, " -v - more output\n");
170 BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n");
171 BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n");
172 BIO_printf(bio_err, " -threads arg - number of threads\n");
173 BIO_printf(bio_err, " -loops arg - number of 'connections', per thread\n");
174 BIO_printf(bio_err, " -reconnect - reuse session-id's\n");
175 BIO_printf(bio_err, " -stats - server session-id cache stats\n");
176 BIO_printf(bio_err, " -cert arg - server certificate/key\n");
177 BIO_printf(bio_err, " -ccert arg - client certificate/key\n");
178 BIO_printf(bio_err, " -ssl3 - just SSLv3n\n");
181 int main(int argc, char *argv[])
183 char *CApath = NULL, *CAfile = NULL;
188 SSL_CTX *s_ctx = NULL;
189 SSL_CTX *c_ctx = NULL;
190 char *scert = TEST_SERVER_CERT;
191 char *ccert = TEST_CLIENT_CERT;
192 const SSL_METHOD *ssl_method = SSLv23_method();
194 RAND_seed(rnd_seed, sizeof rnd_seed);
197 bio_err = BIO_new_fd(2, BIO_NOCLOSE);
198 if (bio_stdout == NULL)
199 bio_stdout = BIO_new_fd(1, BIO_NOCLOSE);
204 if (strcmp(*argv, "-server_auth") == 0)
206 else if (strcmp(*argv, "-client_auth") == 0)
208 else if (strcmp(*argv, "-reconnect") == 0)
210 else if (strcmp(*argv, "-stats") == 0)
212 else if (strcmp(*argv, "-ssl3") == 0)
213 ssl_method = SSLv3_method();
214 else if (strcmp(*argv, "-ssl2") == 0)
215 ssl_method = SSLv2_method();
216 else if (strcmp(*argv, "-CApath") == 0) {
220 } else if (strcmp(*argv, "-CAfile") == 0) {
224 } else if (strcmp(*argv, "-cert") == 0) {
228 } else if (strcmp(*argv, "-ccert") == 0) {
232 } else if (strcmp(*argv, "-threads") == 0) {
235 thread_number = atoi(*(++argv));
236 if (thread_number == 0)
238 if (thread_number > MAX_THREAD_NUMBER)
239 thread_number = MAX_THREAD_NUMBER;
240 } else if (strcmp(*argv, "-loops") == 0) {
243 number_of_loops = atoi(*(++argv));
244 if (number_of_loops == 0)
247 BIO_printf(bio_err, "unknown option %s\n", *argv);
260 if (cipher == NULL && OPENSSL_issetugid() == 0)
261 cipher = getenv("SSL_CIPHER");
263 SSL_load_error_strings();
264 OpenSSL_add_ssl_algorithms();
266 c_ctx = SSL_CTX_new(ssl_method);
267 s_ctx = SSL_CTX_new(ssl_method);
268 if ((c_ctx == NULL) || (s_ctx == NULL)) {
269 ERR_print_errors(bio_err);
273 SSL_CTX_set_session_cache_mode(s_ctx,
274 SSL_SESS_CACHE_NO_AUTO_CLEAR |
275 SSL_SESS_CACHE_SERVER);
276 SSL_CTX_set_session_cache_mode(c_ctx,
277 SSL_SESS_CACHE_NO_AUTO_CLEAR |
278 SSL_SESS_CACHE_SERVER);
280 if (!SSL_CTX_use_certificate_file(s_ctx, scert, SSL_FILETYPE_PEM)) {
281 BIO_printf(bio_err, "SSL_CTX_use_certificate_file (%s)\n", scert);
282 ERR_print_errors(bio_err);
285 if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx, scert, SSL_FILETYPE_PEM)) {
286 BIO_printf(bio_err, "SSL_CTX_use_RSAPrivateKey_file (%s)\n", scert);
287 ERR_print_errors(bio_err);
292 SSL_CTX_use_certificate_file(c_ctx, ccert, SSL_FILETYPE_PEM);
293 SSL_CTX_use_RSAPrivateKey_file(c_ctx, ccert, SSL_FILETYPE_PEM);
296 if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) ||
297 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
298 (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) ||
299 (!SSL_CTX_set_default_verify_paths(c_ctx))) {
300 BIO_printf(bio_err, "SSL_load_verify_locations\n");
301 ERR_print_errors(bio_err);
306 BIO_printf(bio_err, "client authentication\n");
307 SSL_CTX_set_verify(s_ctx,
308 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
312 BIO_printf(bio_err, "server authentication\n");
313 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
317 do_threads(s_ctx, c_ctx);
322 BIO_printf(bio_err, "Client SSL_CTX stats then free it\n");
323 print_stats(bio_err, c_ctx);
327 BIO_printf(bio_err, "Server SSL_CTX stats then free it\n");
328 print_stats(bio_err, s_ctx);
330 BIO_printf(bio_err, "-----\n");
331 lh_SSL_SESSION_stats_bio(SSL_CTX_sessions(s_ctx), bio_err);
332 BIO_printf(bio_err, "-----\n");
333 /*- lh_SSL_SESSION_node_stats_bio(SSL_CTX_sessions(s_ctx),bio_err);
334 BIO_printf(bio_err,"-----\n"); */
335 lh_SSL_SESSION_node_usage_stats_bio(SSL_CTX_sessions(s_ctx), bio_err);
336 BIO_printf(bio_err, "-----\n");
339 BIO_printf(bio_err, "done free\n");
350 int ndoit(SSL_CTX *ssl_ctx[2])
355 CRYPTO_THREADID thread_id;
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 CRYPTO_THREADID_current(&thread_id);
369 BIO_printf(bio_stdout, "started thread %lu\n",
370 CRYPTO_THREADID_hash(&thread_id));
371 for (i = 0; i < number_of_loops; i++) {
372 /*- BIO_printf(bio_err,"%4d %2d ctx->ref (%3d,%3d)\n",
373 CRYPTO_THREADID_hash(&thread_id),i,
374 ssl_ctx[0]->references,
375 ssl_ctx[1]->references); */
376 /* pthread_delay_np(&tm); */
380 BIO_printf(bio_stdout, "error[%d] %lu - %d\n",
381 i, CRYPTO_THREADID_hash(&thread_id), ret);
385 BIO_printf(bio_stdout, "DONE %lu\n", CRYPTO_THREADID_hash(&thread_id));
387 SSL_free((SSL *)ctx[2]);
388 SSL_free((SSL *)ctx[3]);
390 #ifdef OPENSSL_SYS_NETWARE
391 MPKSemaphoreSignal(ThreadSem);
396 int doit(char *ctx[4])
398 SSL_CTX *s_ctx, *c_ctx;
399 static char cbuf[200], sbuf[200];
406 int c_r, c_w, s_r, s_w;
410 int c_write, s_write;
411 int do_server = 0, do_client = 0;
413 s_ctx = (SSL_CTX *)ctx[0];
414 c_ctx = (SSL_CTX *)ctx[1];
417 s_ssl = (SSL *)ctx[2];
419 s_ssl = SSL_new(s_ctx);
422 c_ssl = (SSL *)ctx[3];
424 c_ssl = SSL_new(c_ctx);
426 if ((s_ssl == NULL) || (c_ssl == NULL))
429 c_to_s = BIO_new(BIO_s_mem());
430 s_to_c = BIO_new(BIO_s_mem());
431 if ((s_to_c == NULL) || (c_to_s == NULL))
434 c_bio = BIO_new(BIO_f_ssl());
435 s_bio = BIO_new(BIO_f_ssl());
436 if ((c_bio == NULL) || (s_bio == NULL))
439 SSL_set_connect_state(c_ssl);
440 SSL_set_bio(c_ssl, s_to_c, c_to_s);
441 BIO_set_ssl(c_bio, c_ssl, (ctx[2] == NULL) ? BIO_CLOSE : BIO_NOCLOSE);
443 SSL_set_accept_state(s_ssl);
444 SSL_set_bio(s_ssl, c_to_s, s_to_c);
445 BIO_set_ssl(s_bio, s_ssl, (ctx[3] == NULL) ? BIO_CLOSE : BIO_NOCLOSE);
453 c_write = 1, s_write = 0;
455 /* We can always do writes */
460 i = (int)BIO_pending(s_bio);
461 if ((i && s_r) || s_w)
464 i = (int)BIO_pending(c_bio);
465 if ((i && c_r) || c_w)
468 if (do_server && verbose) {
469 if (SSL_in_init(s_ssl))
470 BIO_printf(bio_stdout, "server waiting in SSL_accept - %s\n",
471 SSL_state_string_long(s_ssl));
473 BIO_printf(bio_stdout, "server:SSL_write()\n");
475 BIO_printf(bio_stdout, "server:SSL_read()\n");
478 if (do_client && verbose) {
479 if (SSL_in_init(c_ssl))
480 BIO_printf(bio_stdout, "client waiting in SSL_connect - %s\n",
481 SSL_state_string_long(c_ssl));
483 BIO_printf(bio_stdout, "client:SSL_write()\n");
485 BIO_printf(bio_stdout, "client:SSL_read()\n");
488 if (!do_client && !do_server) {
489 BIO_printf(bio_stdout, "ERROR IN STARTUP\n");
492 if (do_client && !(done & C_DONE)) {
494 i = BIO_write(c_bio, "hello from client\n", 18);
498 if (BIO_should_retry(c_bio)) {
499 if (BIO_should_read(c_bio))
501 if (BIO_should_write(c_bio))
504 BIO_printf(bio_err, "ERROR in CLIENT\n");
505 ERR_print_errors_fp(stderr);
509 BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n");
516 i = BIO_read(c_bio, cbuf, 100);
520 if (BIO_should_retry(c_bio)) {
521 if (BIO_should_read(c_bio))
523 if (BIO_should_write(c_bio))
526 BIO_printf(bio_err, "ERROR in CLIENT\n");
527 ERR_print_errors_fp(stderr);
531 BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n");
536 BIO_printf(bio_stdout, "CLIENT:from server:");
537 BIO_write(bio_stdout, cbuf, i);
538 BIO_flush(bio_stdout);
544 if (do_server && !(done & S_DONE)) {
546 i = BIO_read(s_bio, sbuf, 100);
550 if (BIO_should_retry(s_bio)) {
551 if (BIO_should_read(s_bio))
553 if (BIO_should_write(s_bio))
556 BIO_printf(bio_err, "ERROR in SERVER\n");
557 ERR_print_errors_fp(stderr);
561 BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n");
567 BIO_printf(bio_stdout, "SERVER:from client:");
568 BIO_write(bio_stdout, sbuf, i);
569 BIO_flush(bio_stdout);
573 i = BIO_write(s_bio, "hello from server\n", 18);
577 if (BIO_should_retry(s_bio)) {
578 if (BIO_should_read(s_bio))
580 if (BIO_should_write(s_bio))
583 BIO_printf(bio_err, "ERROR in SERVER\n");
584 ERR_print_errors_fp(stderr);
588 BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n");
598 if ((done & S_DONE) && (done & C_DONE))
600 #if defined(OPENSSL_SYS_NETWARE)
601 ThreadSwitchWithDelay();
605 SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
606 SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
609 BIO_printf(bio_stdout, "DONE\n");
613 * We have to set the BIO's to NULL otherwise they will be free()ed
614 * twice. Once when th s_ssl is SSL_free()ed and again when c_ssl is
615 * SSL_free()ed. This is a hack required because s_ssl and c_ssl are
616 * sharing the same BIO structure and SSL_set_bio() and SSL_free()
617 * automatically BIO_free non NULL entries. You should not normally do
618 * this or be required to do this
630 /* The SSL's are optionally freed in the following calls */
643 int verify_callback(int ok, X509_STORE_CTX *ctx)
648 s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
652 BIO_printf(bio_err, "depth=%d %s\n", ctx->error_depth, buf);
654 BIO_printf(bio_err, "depth=%d error=%d %s\n",
655 ctx->error_depth, ctx->error, buf);
661 #define THREAD_STACK_SIZE (16*1024)
663 #ifdef OPENSSL_SYS_WIN32
665 static HANDLE *lock_cs;
667 void thread_setup(void)
671 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
672 for (i = 0; i < CRYPTO_num_locks(); i++) {
673 lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
676 CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
677 win32_locking_callback);
678 /* id callback defined */
681 void thread_cleanup(void)
685 CRYPTO_set_locking_callback(NULL);
686 for (i = 0; i < CRYPTO_num_locks(); i++)
687 CloseHandle(lock_cs[i]);
688 OPENSSL_free(lock_cs);
691 void win32_locking_callback(int mode, int type, const char *file, int line)
693 if (mode & CRYPTO_LOCK) {
694 WaitForSingleObject(lock_cs[type], INFINITE);
696 ReleaseMutex(lock_cs[type]);
700 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
704 DWORD thread_id[MAX_THREAD_NUMBER];
705 HANDLE thread_handle[MAX_THREAD_NUMBER];
707 SYSTEMTIME start, end;
712 GetSystemTime(&start);
713 for (i = 0; i < thread_number; i++) {
714 thread_handle[i] = CreateThread(NULL,
716 (LPTHREAD_START_ROUTINE) ndoit,
717 (void *)ssl_ctx, 0L, &(thread_id[i]));
720 BIO_printf(bio_stdout, "reaping\n");
721 for (i = 0; i < thread_number; i += 50) {
724 j = (thread_number < (i + 50)) ? (thread_number - i) : 50;
726 if (WaitForMultipleObjects(j,
727 (CONST HANDLE *) & (thread_handle[i]),
730 BIO_printf(bio_err, "WaitForMultipleObjects failed:%d\n",
737 if (start.wDayOfWeek > end.wDayOfWeek)
739 ret = (end.wDayOfWeek - start.wDayOfWeek) * 24;
741 ret = (ret + end.wHour - start.wHour) * 60;
742 ret = (ret + end.wMinute - start.wMinute) * 60;
743 ret = (ret + end.wSecond - start.wSecond);
744 ret += (end.wMilliseconds - start.wMilliseconds) / 1000.0;
746 BIO_printf(bio_stdout, "win32 threads done - %.3f seconds\n", ret);
749 #endif /* OPENSSL_SYS_WIN32 */
753 static mutex_t *lock_cs;
755 * static rwlock_t *lock_cs;
757 static long *lock_count;
759 void thread_setup(void)
763 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
764 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
765 for (i = 0; i < CRYPTO_num_locks(); i++) {
767 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
768 mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
771 CRYPTO_set_id_callback(solaris_thread_id);
772 CRYPTO_set_locking_callback(solaris_locking_callback);
775 void thread_cleanup(void)
779 CRYPTO_set_locking_callback(NULL);
781 BIO_printf(bio_err, "cleanup\n");
783 for (i = 0; i < CRYPTO_num_locks(); i++) {
784 /* rwlock_destroy(&(lock_cs[i])); */
785 mutex_destroy(&(lock_cs[i]));
786 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
788 OPENSSL_free(lock_cs);
789 OPENSSL_free(lock_count);
791 BIO_printf(bio_err, "done cleanup\n");
795 void solaris_locking_callback(int mode, int type, const char *file, int line)
798 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n",
800 (mode & CRYPTO_LOCK) ? "l" : "u",
801 (type & CRYPTO_READ) ? "r" : "w", file, line);
805 if (CRYPTO_LOCK_SSL_CERT == type)
806 BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n",
810 if (mode & CRYPTO_LOCK) {
812 if (mode & CRYPTO_READ)
813 rw_rdlock(&(lock_cs[type]));
815 rw_wrlock(&(lock_cs[type])); */
817 mutex_lock(&(lock_cs[type]));
820 /* rw_unlock(&(lock_cs[type])); */
821 mutex_unlock(&(lock_cs[type]));
825 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
828 thread_t thread_ctx[MAX_THREAD_NUMBER];
834 thr_setconcurrency(thread_number);
835 for (i = 0; i < thread_number; i++) {
836 thr_create(NULL, THREAD_STACK_SIZE,
837 (void *(*)())ndoit, (void *)ssl_ctx, 0L, &(thread_ctx[i]));
840 BIO_printf(bio_stdout, "reaping\n");
841 for (i = 0; i < thread_number; i++) {
842 thr_join(thread_ctx[i], NULL, NULL);
845 #if 0 /* We can't currently find out the reference amount */
846 BIO_printf(bio_stdout, "solaris threads done (%d,%d)\n",
847 s_ctx->references, c_ctx->references);
849 BIO_printf(bio_stdout, "solaris threads done\n");
853 void solaris_thread_id(CRYPTO_THREADID *tid)
855 CRYPTO_THREADID_set_numeric((unsigned long)thr_self());
861 static usptr_t *arena;
862 static usema_t **lock_cs;
864 void thread_setup(void)
869 strcpy(filename, "/tmp/mttest.XXXXXX");
872 usconfig(CONF_STHREADIOOFF);
873 usconfig(CONF_STHREADMALLOCOFF);
874 usconfig(CONF_INITUSERS, 100);
875 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
876 arena = usinit(filename);
879 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
880 for (i = 0; i < CRYPTO_num_locks(); i++) {
881 lock_cs[i] = usnewsema(arena, 1);
884 CRYPTO_set_id_callback(irix_thread_id);
885 CRYPTO_set_locking_callback(irix_locking_callback);
888 void thread_cleanup(void)
892 CRYPTO_set_locking_callback(NULL);
893 for (i = 0; i < CRYPTO_num_locks(); i++) {
896 sprintf(buf, "%2d:", i);
897 usdumpsema(lock_cs[i], stdout, buf);
898 usfreesema(lock_cs[i], arena);
900 OPENSSL_free(lock_cs);
903 void irix_locking_callback(int mode, int type, const char *file, int line)
905 if (mode & CRYPTO_LOCK) {
906 BIO_printf(bio_stdout, "lock %d\n", type);
907 uspsema(lock_cs[type]);
909 BIO_printf(bio_stdout, "unlock %d\n", type);
910 usvsema(lock_cs[type]);
914 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
917 int thread_ctx[MAX_THREAD_NUMBER];
923 for (i = 0; i < thread_number; i++) {
924 thread_ctx[i] = sproc((void (*)())ndoit,
925 PR_SADDR | PR_SFDS, (void *)ssl_ctx);
928 BIO_printf(bio_stdout, "reaping\n");
929 for (i = 0; i < thread_number; i++) {
933 #if 0 /* We can't currently find out the reference amount */
934 BIO_printf(bio_stdout, "irix threads done (%d,%d)\n",
935 s_ctx->references, c_ctx->references);
937 BIO_printf(bio_stdout, "irix threads done\n");
941 unsigned long irix_thread_id(void)
943 CRYPTO_THREADID_set_numeric((unsigned long)getpid());
949 static pthread_mutex_t *lock_cs;
950 static long *lock_count;
952 void thread_setup(void)
956 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
957 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
958 for (i = 0; i < CRYPTO_num_locks(); i++) {
960 pthread_mutex_init(&(lock_cs[i]), NULL);
963 CRYPTO_THREADID_set_callback(pthreads_thread_id);
964 CRYPTO_set_locking_callback(pthreads_locking_callback);
967 void thread_cleanup(void)
971 CRYPTO_set_locking_callback(NULL);
972 BIO_printf(bio_err, "cleanup\n");
973 for (i = 0; i < CRYPTO_num_locks(); i++) {
974 pthread_mutex_destroy(&(lock_cs[i]));
975 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
977 OPENSSL_free(lock_cs);
978 OPENSSL_free(lock_count);
980 BIO_printf(bio_err, "done cleanup\n");
983 void pthreads_locking_callback(int mode, int type, const char *file, int line)
986 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n",
988 (mode & CRYPTO_LOCK) ? "l" : "u",
989 (type & CRYPTO_READ) ? "r" : "w", file, line);
992 if (CRYPTO_LOCK_SSL_CERT == type)
993 BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n",
997 if (mode & CRYPTO_LOCK) {
998 pthread_mutex_lock(&(lock_cs[type]));
1001 pthread_mutex_unlock(&(lock_cs[type]));
1005 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1007 SSL_CTX *ssl_ctx[2];
1008 pthread_t thread_ctx[MAX_THREAD_NUMBER];
1015 * thr_setconcurrency(thread_number);
1017 for (i = 0; i < thread_number; i++) {
1018 pthread_create(&(thread_ctx[i]), NULL,
1019 (void *(*)())ndoit, (void *)ssl_ctx);
1022 BIO_printf(bio_stdout, "reaping\n");
1023 for (i = 0; i < thread_number; i++) {
1024 pthread_join(thread_ctx[i], NULL);
1027 #if 0 /* We can't currently find out the reference amount */
1028 BIO_printf(bio_stdout, "pthreads threads done (%d,%d)\n",
1029 s_ctx->references, c_ctx->references);
1031 BIO_printf(bio_stdout, "pthreads threads done\n");
1035 void pthreads_thread_id(CRYPTO_THREADID *tid)
1037 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1040 #endif /* PTHREADS */
1042 #ifdef OPENSSL_SYS_NETWARE
1044 void thread_setup(void)
1048 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex));
1049 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1050 for (i = 0; i < CRYPTO_num_locks(); i++) {
1052 lock_cs[i] = MPKMutexAlloc("OpenSSL mutex");
1055 ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0);
1057 CRYPTO_set_id_callback(netware_thread_id);
1058 CRYPTO_set_locking_callback(netware_locking_callback);
1061 void thread_cleanup(void)
1065 CRYPTO_set_locking_callback(NULL);
1067 BIO_printf(bio_stdout, "thread_cleanup\n");
1069 for (i = 0; i < CRYPTO_num_locks(); i++) {
1070 MPKMutexFree(lock_cs[i]);
1071 BIO_printf(bio_stdout, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
1073 OPENSSL_free(lock_cs);
1074 OPENSSL_free(lock_count);
1076 MPKSemaphoreFree(ThreadSem);
1078 BIO_printf(bio_stdout, "done cleanup\n");
1081 void netware_locking_callback(int mode, int type, const char *file, int line)
1083 if (mode & CRYPTO_LOCK) {
1084 MPKMutexLock(lock_cs[type]);
1087 MPKMutexUnlock(lock_cs[type]);
1090 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1092 SSL_CTX *ssl_ctx[2];
1097 for (i = 0; i < thread_number; i++) {
1098 BeginThread((void (*)(void *))ndoit, NULL, THREAD_STACK_SIZE,
1100 ThreadSwitchWithDelay();
1103 BIO_printf(bio_stdout, "reaping\n");
1105 /* loop until all threads have signaled the semaphore */
1106 for (i = 0; i < thread_number; i++) {
1107 MPKSemaphoreWait(ThreadSem);
1109 #if 0 /* We can't currently find out the reference amount */
1110 BIO_printf(bio_stdout, "netware threads done (%d,%d)\n",
1111 s_ctx->references, c_ctx->references);
1113 BIO_printf(bio_stdout, "netware threads done\n");
1117 unsigned long netware_thread_id(void)
1119 CRYPTO_THREADID_set_numeric((unsigned long)GetThreadID());
1121 #endif /* NETWARE */
1125 # include <Locker.h>
1127 static BLocker **lock_cs;
1128 static long *lock_count;
1130 void thread_setup(void)
1135 (BLocker **) OPENSSL_malloc(CRYPTO_num_locks() * sizeof(BLocker *));
1136 lock_count = (long *)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1137 for (i = 0; i < CRYPTO_num_locks(); i++) {
1139 lock_cs[i] = new BLocker(CRYPTO_get_lock_name(i));
1142 CRYPTO_set_id_callback((unsigned long (*)())beos_thread_id);
1143 CRYPTO_set_locking_callback(beos_locking_callback);
1146 void thread_cleanup(void)
1150 CRYPTO_set_locking_callback(NULL);
1151 BIO_printf(bio_err, "cleanup\n");
1152 for (i = 0; i < CRYPTO_num_locks(); i++) {
1154 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i));
1156 OPENSSL_free(lock_cs);
1157 OPENSSL_free(lock_count);
1159 BIO_printf(bio_err, "done cleanup\n");
1162 void beos_locking_callback(int mode, int type, const char *file, int line)
1165 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n",
1167 (mode & CRYPTO_LOCK) ? "l" : "u",
1168 (type & CRYPTO_READ) ? "r" : "w", file, line);
1170 if (mode & CRYPTO_LOCK) {
1171 lock_cs[type]->Lock();
1174 lock_cs[type]->Unlock();
1178 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
1180 SSL_CTX *ssl_ctx[2];
1181 thread_id thread_ctx[MAX_THREAD_NUMBER];
1187 for (i = 0; i < thread_number; i++) {
1188 thread_ctx[i] = spawn_thread((thread_func) ndoit,
1189 NULL, B_NORMAL_PRIORITY,
1191 resume_thread(thread_ctx[i]);
1194 BIO_printf(bio_stdout, "waiting...\n");
1195 for (i = 0; i < thread_number; i++) {
1197 wait_for_thread(thread_ctx[i], &result);
1200 BIO_printf(bio_stdout, "beos threads done (%d,%d)\n",
1201 s_ctx->references, c_ctx->references);
1204 unsigned long beos_thread_id(void)
1208 ret = (unsigned long)find_thread(NULL);
1212 #endif /* BEOS_THREADS */