1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-05 20:55:46 +03:00

Implemented the noop and native pointers

This commit is contained in:
Aris Adamantiadis
2010-09-06 16:52:14 +02:00
parent fbe102bada
commit 49f57a8d0d
6 changed files with 95 additions and 44 deletions

View File

@@ -255,8 +255,27 @@ LIBSSH_API int ssh_set_callbacks(ssh_session session, ssh_callbacks cb);
*/ */
typedef int (*ssh_thread_callback) (void **lock); typedef int (*ssh_thread_callback) (void **lock);
/**
* @brief Type of the threading solution implemented behind
* these callbacks
*/
enum ssh_threads_type_e {
/** The thread callbacks do nothing */
ssh_threads_type_noop,
/** The thread callbacks use pthread */
ssh_threads_type_pthread,
/** The thread callbacks use win32 threads */
ssh_threads_type_win32,
/** The thread callbacks use pth */
ssh_threads_type_pth,
/** The thread callbacks are unknown or different */
ssh_threads_type_other
};
typedef unsigned long (*ssh_thread_id_callback) (void); typedef unsigned long (*ssh_thread_id_callback) (void);
struct ssh_threads_callbacks_struct { struct ssh_threads_callbacks_struct {
enum ssh_threads_type_e type;
ssh_thread_callback mutex_init; ssh_thread_callback mutex_init;
ssh_thread_callback mutex_destroy; ssh_thread_callback mutex_destroy;
ssh_thread_callback mutex_lock; ssh_thread_callback mutex_lock;
@@ -277,7 +296,9 @@ struct ssh_threads_callbacks_struct {
LIBSSH_API int ssh_threads_set_callbacks(struct ssh_threads_callbacks_struct LIBSSH_API int ssh_threads_set_callbacks(struct ssh_threads_callbacks_struct
*cb); *cb);
extern struct ssh_threads_callbacks_struct ssh_pthread_callbacks; extern struct ssh_threads_callbacks_struct ssh_threads_pthread;
extern struct ssh_threads_callbacks_struct ssh_threads_noop;
extern struct ssh_threads_callbacks_struct ssh_threads_native;
/** @} */ /** @} */
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -30,6 +30,25 @@
#include "libssh/priv.h" #include "libssh/priv.h"
#include "libssh/threads.h" #include "libssh/threads.h"
static int threads_noop (void **lock){
(void)lock;
return 0;
}
static unsigned long threads_id_noop (void){
return 1;
}
struct ssh_threads_callbacks_struct ssh_threads_noop =
{
.type=ssh_threads_type_noop,
.mutex_init=threads_noop,
.mutex_destroy=threads_noop,
.mutex_lock=threads_noop,
.mutex_unlock=threads_noop,
.thread_id=threads_id_noop
};
static struct ssh_threads_callbacks_struct *user_callbacks; static struct ssh_threads_callbacks_struct *user_callbacks;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
@@ -107,7 +126,7 @@ int ssh_threads_init(void){
* already the case * already the case
*/ */
if(user_callbacks == NULL){ if(user_callbacks == NULL){
return SSH_ERROR; // Can't do anything to initialize threading user_callbacks=&ssh_threads_noop;
} }
/* Then initialize the crypto libraries threading callbacks */ /* Then initialize the crypto libraries threading callbacks */

View File

@@ -33,7 +33,6 @@ set(LIBSSH_THREADS_LINK_LIBRARIES
) )
set(libssh_threads_SRCS set(libssh_threads_SRCS
native.c
) )
# build and link pthread # build and link pthread

View File

@@ -1,4 +0,0 @@
#include "config.h"
#include <libssh/callbacks.h>
struct ssh_threads_callbacks_struct ssh_pthread_callbacks;

View File

@@ -45,43 +45,59 @@
* callbacks for threading * callbacks for threading
* *
*/ */
#define SSH_THREADS_PTHREAD(name) \
static int ssh_pthread_mutex_init (void **priv){ \ static int ssh_pthread_mutex_init (void **priv){
int err = 0; \ int err = 0;
*priv = malloc (sizeof (pthread_mutex_t)); \ *priv = malloc (sizeof (pthread_mutex_t));
if (*priv==NULL) \ if (*priv==NULL)
return ENOMEM; \ return ENOMEM;
err = pthread_mutex_init (*priv, NULL); \ err = pthread_mutex_init (*priv, NULL);
if (err != 0){ \ if (err != 0){
free (*priv); \ free (*priv);
*priv=NULL; \ *priv=NULL;
} \ }
return err; \ return err;
} \
static int ssh_pthread_mutex_destroy (void **lock) { \
int err = pthread_mutex_destroy (*lock); \
free (*lock); \
*lock=NULL; \
return err; \
} \
static int ssh_pthread_mutex_lock (void **lock) { \
return pthread_mutex_lock (*lock); \
} \
static int ssh_pthread_mutex_unlock (void **lock){ \
return pthread_mutex_unlock (*lock); \
} \
static unsigned long ssh_pthread_thread_id (void){ \
return (unsigned long) pthread_self(); \
} \
static struct ssh_threads_callbacks_struct name= \
{ \
.mutex_init=ssh_pthread_mutex_init, \
.mutex_destroy=ssh_pthread_mutex_destroy, \
.mutex_lock=ssh_pthread_mutex_lock, \
.mutex_unlock=ssh_pthread_mutex_unlock, \
.thread_id=ssh_pthread_thread_id \
} }
SSH_THREADS_PTHREAD(ssh_pthread_user_callbacks); static int ssh_pthread_mutex_destroy (void **lock) {
int err = pthread_mutex_destroy (*lock);
free (*lock);
*lock=NULL;
return err;
}
static int ssh_pthread_mutex_lock (void **lock) {
return pthread_mutex_lock (*lock);
}
static int ssh_pthread_mutex_unlock (void **lock){
return pthread_mutex_unlock (*lock);
}
static unsigned long ssh_pthread_thread_id (void){
return (unsigned long) pthread_self();
}
struct ssh_threads_callbacks_struct ssh_threads_pthread =
{
.type=ssh_threads_type_pthread,
.mutex_init=ssh_pthread_mutex_init,
.mutex_destroy=ssh_pthread_mutex_destroy,
.mutex_lock=ssh_pthread_mutex_lock,
.mutex_unlock=ssh_pthread_mutex_unlock,
.thread_id=ssh_pthread_thread_id
};
#ifdef THREAD_NATIVE_PTHREAD
struct ssh_threads_callbacks_struct ssh_threads_native =
{
.type=ssh_threads_type_pthread,
.mutex_init=ssh_pthread_mutex_init,
.mutex_destroy=ssh_pthread_mutex_destroy,
.mutex_lock=ssh_pthread_mutex_lock,
.mutex_unlock=ssh_pthread_mutex_unlock,
.thread_id=ssh_pthread_thread_id
};
#endif
#endif /* HAVE_PTHREAD */ #endif /* HAVE_PTHREAD */

View File

@@ -15,7 +15,7 @@
static void setup(){ static void setup(){
printf("setup\n"); printf("setup\n");
ssh_threads_set_callbacks(&ssh_pthread_callbacks); ssh_threads_set_callbacks(&ssh_threads_pthread);
ssh_init(); ssh_init();
} }