Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034 #include <pthread.h>
00035 #include <openssl/err.h>
00036
00037 #define MUTEX_TYPE pthread_mutex_t
00038 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
00039 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
00040 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
00041 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
00042 #define THREAD_ID pthread_self()
00043
00044
00045 void handle_error(const char *file, int lineno, const char *msg)
00046 {
00047 fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
00048 ERR_print_errors_fp(stderr);
00049
00050 }
00051
00052
00053 static MUTEX_TYPE *mutex_buf= NULL;
00054
00055 static void locking_function(int mode, int n, const char *file, int line)
00056 {
00057 if(mode & CRYPTO_LOCK)
00058 MUTEX_LOCK(mutex_buf[n]);
00059 else
00060 MUTEX_UNLOCK(mutex_buf[n]);
00061 }
00062
00063 static unsigned long id_function(void)
00064 {
00065 return ((unsigned long)THREAD_ID);
00066 }
00067
00068 int thread_setup(void)
00069 {
00070 int i;
00071
00072 mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
00073 if(!mutex_buf)
00074 return 0;
00075 for(i = 0; i < CRYPTO_num_locks(); i++)
00076 MUTEX_SETUP(mutex_buf[i]);
00077 CRYPTO_set_id_callback(id_function);
00078 CRYPTO_set_locking_callback(locking_function);
00079 return 1;
00080 }
00081
00082 int thread_cleanup(void)
00083 {
00084 int i;
00085
00086 if(!mutex_buf)
00087 return 0;
00088 CRYPTO_set_id_callback(NULL);
00089 CRYPTO_set_locking_callback(NULL);
00090 for(i = 0; i < CRYPTO_num_locks(); i++)
00091 MUTEX_CLEANUP(mutex_buf[i]);
00092 free(mutex_buf);
00093 mutex_buf = NULL;
00094 return 1;
00095 }