Cleanup mttest.c : modernise the threads setup
[openssl.git] / crypto / threads / mttest.c
index 60b0c06417db4d0c1c74e25d53157949bc4e7815..c3a54270e6082c23bcc0879dc2a89c1d7ee513cc 100644 (file)
 
 #define MAX_THREAD_NUMBER       100
 
 
 #define MAX_THREAD_NUMBER       100
 
-int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *xs);
+int verify_callback(int ok, X509_STORE_CTX *xs);
 void thread_setup(void);
 void thread_cleanup(void);
 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx);
 
 void thread_setup(void);
 void thread_cleanup(void);
 void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx);
 
-void irix_locking_callback(int mode, int type, char *file, int line);
-void solaris_locking_callback(int mode, int type, char *file, int line);
-void win32_locking_callback(int mode, int type, char *file, int line);
-void pthreads_locking_callback(int mode, int type, char *file, int line);
-void netware_locking_callback(int mode, int type, char *file, int line);
+void irix_locking_callback(int mode, int type, const char *file, int line);
+void solaris_locking_callback(int mode, int type, const char *file, int line);
+void win32_locking_callback(int mode, int type, const char *file, int line);
+void pthreads_locking_callback(int mode, int type, const char *file, int line);
+void netware_locking_callback(int mode, int type, const char *file, int line);
 void beos_locking_callback(int mode, int type, const char *file, int line);
 
 void beos_locking_callback(int mode, int type, const char *file, int line);
 
-unsigned long irix_thread_id(void);
-unsigned long solaris_thread_id(void);
-unsigned long pthreads_thread_id(void);
-unsigned long netware_thread_id(void);
-unsigned long beos_thread_id(void);
+void irix_thread_id(CRYPTO_THREADID *tid);
+void solaris_thread_id(CRYPTO_THREADID *tid);
+void pthreads_thread_id(CRYPTO_THREADID *tid);
+void netware_thread_id(CRYPTO_THREADID *tid);
+void beos_thread_id(CRYPTO_THREADID *tid);
 
 #if defined(OPENSSL_SYS_NETWARE)
 static MPKMutex *lock_cs;
 
 #if defined(OPENSSL_SYS_NETWARE)
 static MPKMutex *lock_cs;
@@ -194,7 +194,7 @@ int main(int argc, char *argv[])
     SSL_CTX *c_ctx = NULL;
     char *scert = TEST_SERVER_CERT;
     char *ccert = TEST_CLIENT_CERT;
     SSL_CTX *c_ctx = NULL;
     char *scert = TEST_SERVER_CERT;
     char *ccert = TEST_CLIENT_CERT;
-    SSL_METHOD *ssl_method = SSLv23_method();
+    SSL_METHOD *ssl_method = TLS_method();
 
     RAND_seed(rnd_seed, sizeof rnd_seed);
 
 
     RAND_seed(rnd_seed, sizeof rnd_seed);
 
@@ -352,6 +352,7 @@ int ndoit(SSL_CTX *ssl_ctx[2])
     int i;
     int ret;
     char *ctx[4];
     int i;
     int ret;
     char *ctx[4];
+    CRYPTO_THREADID thread_id;
 
     ctx[0] = (char *)ssl_ctx[0];
     ctx[1] = (char *)ssl_ctx[1];
 
     ctx[0] = (char *)ssl_ctx[0];
     ctx[1] = (char *)ssl_ctx[1];
@@ -364,22 +365,24 @@ int ndoit(SSL_CTX *ssl_ctx[2])
         ctx[3] = NULL;
     }
 
         ctx[3] = NULL;
     }
 
-    fprintf(stdout, "started thread %lu\n", CRYPTO_thread_id());
+    CRYPTO_THREADID_current(&thread_id);
+    BIO_printf(bio_stdout, "started thread %lu\n",
+              CRYPTO_THREADID_hash(&thread_id));
     for (i = 0; i < number_of_loops; i++) {
     for (i = 0; i < number_of_loops; i++) {
-/*-             fprintf(stderr,"%4d %2d ctx->ref (%3d,%3d)\n",
-                        CRYPTO_thread_id(),i,
-                        ssl_ctx[0]->references,
-                        ssl_ctx[1]->references); */
-        /*      pthread_delay_np(&tm); */
+/*-     BIO_printf(bio_err,"%4d %2d ctx->ref (%3d,%3d)\n",
+                   CRYPTO_THREADID_hash(&thread_id),i,
+                   ssl_ctx[0]->references,
+                   ssl_ctx[1]->references); */
+/*      pthread_delay_np(&tm); */
 
         ret = doit(ctx);
         if (ret != 0) {
 
         ret = doit(ctx);
         if (ret != 0) {
-            fprintf(stdout, "error[%d] %lu - %d\n",
-                    i, CRYPTO_thread_id(), ret);
+            BIO_printf(bio_stdout, "error[%d] %lu - %d\n",
+                       i, CRYPTO_THREADID_hash(&thread_id), ret);
             return (ret);
         }
     }
             return (ret);
         }
     }
-    fprintf(stdout, "DONE %lu\n", CRYPTO_thread_id());
+    BIO_printf(bio_stdout, "DONE %lu\n", CRYPTO_THREADID_hash(&thread_id));
     if (reconnect) {
         SSL_free((SSL *)ctx[2]);
         SSL_free((SSL *)ctx[3]);
     if (reconnect) {
         SSL_free((SSL *)ctx[2]);
         SSL_free((SSL *)ctx[3]);
@@ -529,11 +532,6 @@ int doit(char *ctx[4])
                     return (1);
                 } else {
                     done |= C_DONE;
                     return (1);
                 } else {
                     done |= C_DONE;
-#ifdef undef
-                    fprintf(stdout, "CLIENT:from server:");
-                    fwrite(cbuf, 1, i, stdout);
-                    fflush(stdout);
-#endif
                 }
             }
         }
                 }
             }
         }
@@ -560,11 +558,6 @@ int doit(char *ctx[4])
                 } else {
                     s_write = 1;
                     s_w = 1;
                 } else {
                     s_write = 1;
                     s_w = 1;
-#ifdef undef
-                    fprintf(stdout, "SERVER:from client:");
-                    fwrite(sbuf, 1, i, stdout);
-                    fflush(stdout);
-#endif
                 }
             } else {
                 i = BIO_write(s_bio, "hello from server\n", 18);
                 }
             } else {
                 i = BIO_write(s_bio, "hello from server\n", 18);
@@ -602,9 +595,6 @@ int doit(char *ctx[4])
     SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
     SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
 
     SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
     SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
 
-#ifdef undef
-    fprintf(stdout, "DONE\n");
-#endif
  err:
     /*
      * We have to set the BIO's to NULL otherwise they will be free()ed
  err:
     /*
      * We have to set the BIO's to NULL otherwise they will be free()ed
@@ -625,19 +615,15 @@ int doit(char *ctx[4])
     }
 
     /* The SSL's are optionally freed in the following calls */
     }
 
     /* The SSL's are optionally freed in the following calls */
-    if (c_to_s != NULL)
-        BIO_free(c_to_s);
-    if (s_to_c != NULL)
-        BIO_free(s_to_c);
-
-    if (c_bio != NULL)
-        BIO_free(c_bio);
-    if (s_bio != NULL)
-        BIO_free(s_bio);
+    BIO_free(c_to_s);
+    BIO_free(s_to_c);
+
+    BIO_free(c_bio);
+    BIO_free(s_bio);
     return (0);
 }
 
     return (0);
 }
 
-int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
+int verify_callback(int ok, X509_STORE_CTX *ctx)
 {
     char *s, buf[256];
 
 {
     char *s, buf[256];
 
@@ -685,7 +671,7 @@ void thread_cleanup(void)
     OPENSSL_free(lock_cs);
 }
 
     OPENSSL_free(lock_cs);
 }
 
-void win32_locking_callback(int mode, int type, char *file, int line)
+void win32_locking_callback(int mode, int type, const char *file, int line)
 {
     if (mode & CRYPTO_LOCK) {
         WaitForSingleObject(lock_cs[type], INFINITE);
 {
     if (mode & CRYPTO_LOCK) {
         WaitForSingleObject(lock_cs[type], INFINITE);
@@ -765,8 +751,8 @@ void thread_setup(void)
         mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
     }
 
         mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
     }
 
-    CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
-    CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
+    CRYPTO_set_id_callback(solaris_thread_id);
+    CRYPTO_set_locking_callback(solaris_locking_callback);
 }
 
 void thread_cleanup(void)
 }
 
 void thread_cleanup(void)
@@ -789,32 +775,12 @@ void thread_cleanup(void)
 
 }
 
 
 }
 
-void solaris_locking_callback(int mode, int type, char *file, int line)
+void solaris_locking_callback(int mode, int type, const char *file, int line)
 {
 {
-# ifdef undef
-    fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
-            CRYPTO_thread_id(),
-            (mode & CRYPTO_LOCK) ? "l" : "u",
-            (type & CRYPTO_READ) ? "r" : "w", file, line);
-# endif
-
-    /*-
-    if (CRYPTO_LOCK_SSL_CERT == type)
-    fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
-            CRYPTO_thread_id(),
-            mode,file,line);
-    */
     if (mode & CRYPTO_LOCK) {
     if (mode & CRYPTO_LOCK) {
-        /*-
-        if (mode & CRYPTO_READ)
-                rw_rdlock(&(lock_cs[type]));
-        else
-                rw_wrlock(&(lock_cs[type])); */
-
         mutex_lock(&(lock_cs[type]));
         lock_count[type]++;
     } else {
         mutex_lock(&(lock_cs[type]));
         lock_count[type]++;
     } else {
-/*      rw_unlock(&(lock_cs[type]));  */
         mutex_unlock(&(lock_cs[type]));
     }
 }
         mutex_unlock(&(lock_cs[type]));
     }
 }
@@ -843,12 +809,9 @@ void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
            s_ctx->references, c_ctx->references);
 }
 
            s_ctx->references, c_ctx->references);
 }
 
-unsigned long solaris_thread_id(void)
+void solaris_thread_id(CRYPTO_THREADID *tid)
 {
 {
-    unsigned long ret;
-
-    ret = (unsigned long)thr_self();
-    return (ret);
+    CRYPTO_THREADID_set_numeric((unsigned long)thr_self());
 }
 #endif                          /* SOLARIS */
 
 }
 #endif                          /* SOLARIS */
 
@@ -877,8 +840,8 @@ void thread_setup(void)
         lock_cs[i] = usnewsema(arena, 1);
     }
 
         lock_cs[i] = usnewsema(arena, 1);
     }
 
-    CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
-    CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
+    CRYPTO_set_id_callback(irix_thread_id);
+    CRYPTO_set_locking_callback(irix_locking_callback);
 }
 
 void thread_cleanup(void)
 }
 
 void thread_cleanup(void)
@@ -896,7 +859,7 @@ void thread_cleanup(void)
     OPENSSL_free(lock_cs);
 }
 
     OPENSSL_free(lock_cs);
 }
 
-void irix_locking_callback(int mode, int type, char *file, int line)
+void irix_locking_callback(int mode, int type, const char *file, int line)
 {
     if (mode & CRYPTO_LOCK) {
         printf("lock %d\n", type);
 {
     if (mode & CRYPTO_LOCK) {
         printf("lock %d\n", type);
@@ -932,10 +895,7 @@ void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
 
 unsigned long irix_thread_id(void)
 {
 
 unsigned long irix_thread_id(void)
 {
-    unsigned long ret;
-
-    ret = (unsigned long)getpid();
-    return (ret);
+    CRYPTO_THREADID_set_numeric((unsigned long)getpid());
 }
 #endif                          /* IRIX */
 
 }
 #endif                          /* IRIX */
 
@@ -955,8 +915,8 @@ void thread_setup(void)
         pthread_mutex_init(&(lock_cs[i]), NULL);
     }
 
         pthread_mutex_init(&(lock_cs[i]), NULL);
     }
 
-    CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
-    CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
+    CRYPTO_THREADID_set_callback(pthreads_thread_id);
+    CRYPTO_set_locking_callback(pthreads_locking_callback);
 }
 
 void thread_cleanup(void)
 }
 
 void thread_cleanup(void)
@@ -975,20 +935,8 @@ void thread_cleanup(void)
     fprintf(stderr, "done cleanup\n");
 }
 
     fprintf(stderr, "done cleanup\n");
 }
 
-void pthreads_locking_callback(int mode, int type, char *file, int line)
+void pthreads_locking_callback(int mode, int type, const char *file, int line)
 {
 {
-# ifdef undef
-    fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
-            CRYPTO_thread_id(),
-            (mode & CRYPTO_LOCK) ? "l" : "u",
-            (type & CRYPTO_READ) ? "r" : "w", file, line);
-# endif
-/*-
-    if (CRYPTO_LOCK_SSL_CERT == type)
-            fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
-            CRYPTO_thread_id(),
-            mode,file,line);
-*/
     if (mode & CRYPTO_LOCK) {
         pthread_mutex_lock(&(lock_cs[type]));
         lock_count[type]++;
     if (mode & CRYPTO_LOCK) {
         pthread_mutex_lock(&(lock_cs[type]));
         lock_count[type]++;
@@ -1023,12 +971,9 @@ void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
            s_ctx->references, c_ctx->references);
 }
 
            s_ctx->references, c_ctx->references);
 }
 
-unsigned long pthreads_thread_id(void)
+void pthreads_thread_id(CRYPTO_THREADID *tid)
 {
 {
-    unsigned long ret;
-
-    ret = (unsigned long)pthread_self();
-    return (ret);
+    CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
 }
 
 #endif                          /* PTHREADS */
 }
 
 #endif                          /* PTHREADS */
@@ -1048,8 +993,8 @@ void thread_setup(void)
 
     ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0);
 
 
     ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0);
 
-    CRYPTO_set_id_callback((unsigned long (*)())netware_thread_id);
-    CRYPTO_set_locking_callback((void (*)())netware_locking_callback);
+    CRYPTO_set_id_callback(netware_thread_id);
+    CRYPTO_set_locking_callback(netware_locking_callback);
 }
 
 void thread_cleanup(void)
 }
 
 void thread_cleanup(void)
@@ -1072,7 +1017,7 @@ void thread_cleanup(void)
     fprintf(stdout, "done cleanup\n");
 }
 
     fprintf(stdout, "done cleanup\n");
 }
 
-void netware_locking_callback(int mode, int type, char *file, int line)
+void netware_locking_callback(int mode, int type, const char *file, int line)
 {
     if (mode & CRYPTO_LOCK) {
         MPKMutexLock(lock_cs[type]);
 {
     if (mode & CRYPTO_LOCK) {
         MPKMutexLock(lock_cs[type]);
@@ -1106,9 +1051,6 @@ void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx)
 
 unsigned long netware_thread_id(void)
 {
 
 unsigned long netware_thread_id(void)
 {
-    unsigned long ret;
-
-    ret = (unsigned long)GetThreadID();
-    return (ret);
+    CRYPTO_THREADID_set_numeric((unsigned long)GetThreadID());
 }
 #endif                          /* NETWARE */
 }
 #endif                          /* NETWARE */