1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23 #include <libssh/callbacks.h>
24
25 #ifdef HAVE_PTHREAD
26
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <pthread.h>
30
31 /** @brief Defines the needed callbacks for pthread. Use this if your
32 * OS supports libpthread and want to use it for threading.
33 * @code
34 * #include <libssh/callbacks.h>
35 * #include <errno.h>
36 * #include <pthread.h>
37 * SSH_THREADS_PTHREAD(ssh_pthread_callbacks);
38 * int main(){
39 * ssh_init_set_threads_callbacks(&ssh_pthread_callbacks);
40 * ssh_init();
41 * ...
42 * }
43 * @endcode
44 * @param name name of the structure to be declared, containing the
45 * callbacks for threading
46 *
47 */
48
ssh_pthread_mutex_init(void ** priv)49 static int ssh_pthread_mutex_init (void **priv){
50 int err = 0;
51 *priv = malloc (sizeof (pthread_mutex_t));
52 if (*priv==NULL)
53 return ENOMEM;
54 err = pthread_mutex_init (*priv, NULL);
55 if (err != 0){
56 free (*priv);
57 *priv=NULL;
58 }
59 return err;
60 }
61
ssh_pthread_mutex_destroy(void ** lock)62 static int ssh_pthread_mutex_destroy (void **lock) {
63 int err = pthread_mutex_destroy (*lock);
64 free (*lock);
65 *lock=NULL;
66 return err;
67 }
68
ssh_pthread_mutex_lock(void ** lock)69 static int ssh_pthread_mutex_lock (void **lock) {
70 return pthread_mutex_lock (*lock);
71 }
72
ssh_pthread_mutex_unlock(void ** lock)73 static int ssh_pthread_mutex_unlock (void **lock){
74 return pthread_mutex_unlock (*lock);
75 }
76
ssh_pthread_thread_id(void)77 static unsigned long ssh_pthread_thread_id (void){
78 #if _WIN32
79 return (unsigned long) pthread_self().p;
80 #else
81 return (unsigned long) pthread_self();
82 #endif
83 }
84
85 static struct ssh_threads_callbacks_struct ssh_threads_pthread =
86 {
87 .type="threads_pthread",
88 .mutex_init=ssh_pthread_mutex_init,
89 .mutex_destroy=ssh_pthread_mutex_destroy,
90 .mutex_lock=ssh_pthread_mutex_lock,
91 .mutex_unlock=ssh_pthread_mutex_unlock,
92 .thread_id=ssh_pthread_thread_id
93 };
94
ssh_threads_get_pthread()95 struct ssh_threads_callbacks_struct *ssh_threads_get_pthread(){
96 return &ssh_threads_pthread;
97 }
98
99 #endif /* HAVE_PTHREAD */
100