Skip to content

Instantly share code, notes, and snippets.

@L-P
Last active August 29, 2015 14:01
Show Gist options
  • Save L-P/8d0237da054c3301af36 to your computer and use it in GitHub Desktop.
Save L-P/8d0237da054c3301af36 to your computer and use it in GitHub Desktop.
C++11 thread support for the SSH Library.
/**
* C++11 thread support for the SSH Library.
* Copyright (c) 2014 Léo Peltier <[email protected]>
*/
#include <mutex>
#include <thread>
#include <libssh/callbacks.h>
#include "libssh_std_thread.hpp"
static int ssh_std_thread_mutex_init(void **priv) {
*priv = new std::mutex();
if(*priv == nullptr)
return ENOMEM;
return 0;
}
static int ssh_std_thread_mutex_destroy(void **priv) {
delete static_cast<std::mutex *>(*priv);
*priv = nullptr;
return 0;
}
static int ssh_std_thread_mutex_lock(void **lock) {
static_cast<std::mutex *>(*lock)->lock();
return 0;
}
static int ssh_std_thread_mutex_unlock(void **unlock) {
static_cast<std::mutex *>(*unlock)->unlock();
return 0;
}
static unsigned long ssh_std_thread_thread_id(void) {
std::hash<std::thread::id> hash;
return hash(std::this_thread::get_id());
}
static struct ssh_threads_callbacks_struct ssh_threads_std_thread = {
/* .type = */ "std_thread",
/* .mutex_init = */ &ssh_std_thread_mutex_init,
/* .mutex_destroy = */ &ssh_std_thread_mutex_destroy,
/* .mutex_lock = */ &ssh_std_thread_mutex_lock,
/* .mutex_unlock = */ &ssh_std_thread_mutex_unlock,
/* .thread_id = */ &ssh_std_thread_thread_id
};
struct ssh_threads_callbacks_struct * ssh_threads_get_std_threads(void) {
return &ssh_threads_std_thread;
}
/**
* C++11 thread support for the SSH Library.
* Copyright (c) 2014 Léo Peltier <[email protected]>
*/
#ifndef _LIBSSH_STD_THREAD_HPP
#define _LIBSSH_STD_THREAD_HPP
/// @see ssh_threads_set_callbacks.
struct ssh_threads_callbacks_struct * ssh_threads_get_std_threads(void);
#endif // #ifndef _LIBSSH_STD_THREAD_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment