diff options
author | Aris Adamantiadis <aris@0xbadc0de.be> | 2010-08-31 16:49:55 +0200 |
---|---|---|
committer | Aris Adamantiadis <aris@0xbadc0de.be> | 2010-08-31 16:49:55 +0200 |
commit | 50d8d75d890e16e8d4d3f4dd51de05812c0c3a1f (patch) | |
tree | 98ba609d2475102b73717a98b912367ee1336d76 | |
parent | bcc2d8474ccfc96a24896b2f6d52673e61e66452 (diff) | |
download | libssh-50d8d75d890e16e8d4d3f4dd51de05812c0c3a1f.tar.gz libssh-50d8d75d890e16e8d4d3f4dd51de05812c0c3a1f.tar.xz libssh-50d8d75d890e16e8d4d3f4dd51de05812c0c3a1f.zip |
some more work on threading but not complete yet
-rw-r--r-- | libssh/threads.c | 79 | ||||
-rw-r--r-- | tests/unittests/torture_rand.c | 4 |
2 files changed, 55 insertions, 28 deletions
diff --git a/libssh/threads.c b/libssh/threads.c index 24826e1d..c7fc37bf 100644 --- a/libssh/threads.c +++ b/libssh/threads.c @@ -32,63 +32,90 @@ #ifdef HAVE_LIBGCRYPT +#define HAVE_PTHREADS +#ifdef HAVE_PTHREADS #include <errno.h> #include <pthread.h> -static int gcry_pthread_mutex_init (void **priv){ +static int ssh_pthread_mutex_init (void **priv){ int err = 0; - pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); - - if (!lock) - err = ENOMEM; - if (!err) - { - err = pthread_mutex_init (lock, NULL); - if (err) - free (lock); - else - *priv = lock; + *priv = malloc (sizeof (pthread_mutex_t)); + + if (*priv==NULL) + return ENOMEM; + err = pthread_mutex_init (*priv, NULL); + if (err != 0){ + free (*priv); + *priv=NULL; } return err; } -static int gcry_pthread_mutex_destroy (void **lock) { - int err = pthread_mutex_destroy ((pthread_mutex_t*)*lock); +static int ssh_pthread_mutex_destroy (void **lock) { + int err = pthread_mutex_destroy (*lock); free (*lock); + *lock=NULL; return err; } -static int gcry_pthread_mutex_lock (void **lock) { - return pthread_mutex_lock ((pthread_mutex_t*)*lock); + +static int ssh_pthread_mutex_lock (void **lock) { + return pthread_mutex_lock (*lock); } -static int gcry_pthread_mutex_unlock (void **lock){ - return pthread_mutex_unlock ((pthread_mutex_t*)*lock); + +static int ssh_pthread_mutex_unlock (void **lock){ + return pthread_mutex_unlock (*lock); } -static struct gcry_thread_cbs gcrypt_threads= +static struct ssh_threads_callbacks_struct ssh_gcrypt_user_callbacks= { - .option=GCRY_THREAD_OPTION_VERSION << 8 || GCRY_THREAD_OPTION_PTHREAD, - .mutex_init=gcry_pthread_mutex_init, - .mutex_destroy=gcry_pthread_mutex_destroy, - .mutex_lock=gcry_pthread_mutex_lock, - .mutex_unlock=gcry_pthread_mutex_unlock + .mutex_init=ssh_pthread_mutex_init, + .mutex_destroy=ssh_pthread_mutex_destroy, + .mutex_lock=ssh_pthread_mutex_lock, + .mutex_unlock=ssh_pthread_mutex_unlock }; #endif +static struct gcry_thread_cbs gcrypt_threads_callbacks; + +#endif + static struct ssh_threads_callbacks_struct *user_callbacks; + +#ifdef HAVE_LIBGCRYPT +static void copy_callback(struct ssh_threads_callbacks_struct *cb){ + gcrypt_threads_callbacks.option= GCRY_THREAD_OPTION_VERSION << 8 || GCRY_THREAD_OPTION_USER; + gcrypt_threads_callbacks.mutex_init=cb->mutex_init; + gcrypt_threads_callbacks.mutex_destroy=cb->mutex_destroy; + gcrypt_threads_callbacks.mutex_lock=cb->mutex_lock; + gcrypt_threads_callbacks.mutex_unlock=cb->mutex_unlock; +} +#endif + /** @internal * @brief inits the threading with the backend cryptographic libraries */ int ssh_threads_init(void){ #ifdef HAVE_LIBGCRYPT - gcry_control(GCRYCTL_SET_THREAD_CBS, &gcrypt_threads); + if(user_callbacks != NULL){ + copy_callback(user_callbacks); + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcrypt_threads_callbacks); + return SSH_OK; + } +#ifdef HAVE_PTHREADS + else { + copy_callback(&ssh_gcrypt_user_callbacks); + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcrypt_threads_callbacks); + return SSH_OK; + } +#endif #else #endif - return 0; + return SSH_ERROR; } int ssh_init_set_threads_callbacks(struct ssh_threads_callbacks_struct *cb){ diff --git a/tests/unittests/torture_rand.c b/tests/unittests/torture_rand.c index 12dfab7a..087389c7 100644 --- a/tests/unittests/torture_rand.c +++ b/tests/unittests/torture_rand.c @@ -3,8 +3,8 @@ #include <pthread.h> #include "torture.h" -#define NUM_LOOPS 50000 -#define NUM_THREADS 200 +#define NUM_LOOPS 1000 +#define NUM_THREADS 100 static void setup(){ ssh_init(); |