123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #ifdef LINUX
- # include <typedefs.h>
- #endif
- #ifdef OPENSSL_SYS_WIN32
- # include <windows.h>
- #endif
- #ifdef SOLARIS
- # include <synch.h>
- # include <thread.h>
- #endif
- #ifdef IRIX
- # include <ulocks.h>
- # include <sys/prctl.h>
- #endif
- #ifdef PTHREADS
- # include <pthread.h>
- #endif
- #include <openssl/lhash.h>
- #include <openssl/crypto.h>
- #include <openssl/buffer.h>
- #include "../../e_os.h"
- #include <openssl/x509.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- void CRYPTO_thread_setup(void);
- void CRYPTO_thread_cleanup(void);
- static void irix_locking_callback(int mode, int type, char *file, int line);
- static void solaris_locking_callback(int mode, int type, char *file,
- int line);
- static void win32_locking_callback(int mode, int type, char *file, int line);
- static void pthreads_locking_callback(int mode, int type, char *file,
- int line);
- static unsigned long irix_thread_id(void);
- static unsigned long solaris_thread_id(void);
- static unsigned long pthreads_thread_id(void);
- #define THREAD_STACK_SIZE (16*1024)
- #ifdef OPENSSL_SYS_WIN32
- static HANDLE *lock_cs;
- void CRYPTO_thread_setup(void)
- {
- int i;
- lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
- if (!lock_cs) {
-
- return;
- }
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
- }
- CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
- win32_locking_callback);
-
- return (1);
- }
- static void CRYPTO_thread_cleanup(void)
- {
- int i;
- CRYPTO_set_locking_callback(NULL);
- for (i = 0; i < CRYPTO_num_locks(); i++)
- CloseHandle(lock_cs[i]);
- OPENSSL_free(lock_cs);
- }
- void win32_locking_callback(int mode, int type, char *file, int line)
- {
- if (mode & CRYPTO_LOCK) {
- WaitForSingleObject(lock_cs[type], INFINITE);
- } else {
- ReleaseMutex(lock_cs[type]);
- }
- }
- #endif
- #ifdef SOLARIS
- # define USE_MUTEX
- # ifdef USE_MUTEX
- static mutex_t *lock_cs;
- # else
- static rwlock_t *lock_cs;
- # endif
- static long *lock_count;
- void CRYPTO_thread_setup(void)
- {
- int i;
- # ifdef USE_MUTEX
- lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
- # else
- lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t));
- # endif
- if (!lock_cs) {
-
- return;
- }
- lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- lock_count[i] = 0;
- # ifdef USE_MUTEX
- mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
- # else
- rwlock_init(&(lock_cs[i]), USYNC_THREAD, NULL);
- # endif
- }
- CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
- CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
- }
- void CRYPTO_thread_cleanup(void)
- {
- int i;
- CRYPTO_set_locking_callback(NULL);
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- # ifdef USE_MUTEX
- mutex_destroy(&(lock_cs[i]));
- # else
- rwlock_destroy(&(lock_cs[i]));
- # endif
- }
- OPENSSL_free(lock_cs);
- OPENSSL_free(lock_count);
- }
- void solaris_locking_callback(int mode, int type, char *file, int line)
- {
- # if 0
- 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 0
- if (CRYPTO_LOCK_SSL_CERT == type)
- fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
- CRYPTO_thread_id(), mode, file, line);
- # endif
- if (mode & CRYPTO_LOCK) {
- # ifdef USE_MUTEX
- mutex_lock(&(lock_cs[type]));
- # else
- if (mode & CRYPTO_READ)
- rw_rdlock(&(lock_cs[type]));
- else
- rw_wrlock(&(lock_cs[type]));
- # endif
- lock_count[type]++;
- } else {
- # ifdef USE_MUTEX
- mutex_unlock(&(lock_cs[type]));
- # else
- rw_unlock(&(lock_cs[type]));
- # endif
- }
- }
- unsigned long solaris_thread_id(void)
- {
- unsigned long ret;
- ret = (unsigned long)thr_self();
- return (ret);
- }
- #endif
- #ifdef IRIX
- static usptr_t *arena;
- static usema_t **lock_cs;
- void CRYPTO_thread_setup(void)
- {
- int i;
- char filename[20];
- lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
- if (!lock_cs) {
-
- return;
- }
- strcpy(filename, "/tmp/mttest.XXXXXX");
- mktemp(filename);
- usconfig(CONF_STHREADIOOFF);
- usconfig(CONF_STHREADMALLOCOFF);
- usconfig(CONF_INITUSERS, 100);
- usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
- arena = usinit(filename);
- unlink(filename);
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- lock_cs[i] = usnewsema(arena, 1);
- }
- CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
- CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
- }
- void CRYPTO_thread_cleanup(void)
- {
- int i;
- CRYPTO_set_locking_callback(NULL);
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- char buf[10];
- sprintf(buf, "%2d:", i);
- usdumpsema(lock_cs[i], stdout, buf);
- usfreesema(lock_cs[i], arena);
- }
- OPENSSL_free(lock_cs);
- }
- void irix_locking_callback(int mode, int type, char *file, int line)
- {
- if (mode & CRYPTO_LOCK) {
- uspsema(lock_cs[type]);
- } else {
- usvsema(lock_cs[type]);
- }
- }
- unsigned long irix_thread_id(void)
- {
- unsigned long ret;
- ret = (unsigned long)getpid();
- return (ret);
- }
- #endif
- #ifdef PTHREADS
- static pthread_mutex_t *lock_cs;
- static long *lock_count;
- void CRYPTO_thread_setup(void)
- {
- int i;
- lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
- lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
- if (!lock_cs || !lock_count) {
-
- if (lock_cs)
- OPENSSL_free(lock_cs);
- if (lock_count)
- OPENSSL_free(lock_count);
- return;
- }
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- lock_count[i] = 0;
- pthread_mutex_init(&(lock_cs[i]), NULL);
- }
- CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
- CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
- }
- void thread_cleanup(void)
- {
- int i;
- CRYPTO_set_locking_callback(NULL);
- for (i = 0; i < CRYPTO_num_locks(); i++) {
- pthread_mutex_destroy(&(lock_cs[i]));
- }
- OPENSSL_free(lock_cs);
- OPENSSL_free(lock_count);
- }
- void pthreads_locking_callback(int mode, int type, char *file, int line)
- {
- # if 0
- 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 0
- if (CRYPTO_LOCK_SSL_CERT == type)
- fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
- CRYPTO_thread_id(), mode, file, line);
- # endif
- if (mode & CRYPTO_LOCK) {
- pthread_mutex_lock(&(lock_cs[type]));
- lock_count[type]++;
- } else {
- pthread_mutex_unlock(&(lock_cs[type]));
- }
- }
- unsigned long pthreads_thread_id(void)
- {
- unsigned long ret;
- ret = (unsigned long)pthread_self();
- return (ret);
- }
- #endif
|