/src/libssh/src/threads/pthread.c
Line | Count | Source |
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/threads.h" |
24 | | #include <libssh/callbacks.h> |
25 | | |
26 | | #include <errno.h> |
27 | | #include <stdlib.h> |
28 | | #include <pthread.h> |
29 | | |
30 | | static int ssh_pthread_mutex_init (void **mutex) |
31 | 0 | { |
32 | 0 | int rc = 0; |
33 | |
|
34 | 0 | if (mutex == NULL) { |
35 | 0 | return EINVAL; |
36 | 0 | } |
37 | | |
38 | 0 | *mutex = malloc(sizeof(pthread_mutex_t)); |
39 | 0 | if (*mutex == NULL) { |
40 | 0 | return ENOMEM; |
41 | 0 | } |
42 | | |
43 | 0 | rc = pthread_mutex_init ((pthread_mutex_t *)*mutex, NULL); |
44 | 0 | if (rc){ |
45 | 0 | free (*mutex); |
46 | 0 | *mutex = NULL; |
47 | 0 | } |
48 | |
|
49 | 0 | return rc; |
50 | 0 | } |
51 | | |
52 | | static int ssh_pthread_mutex_destroy (void **mutex) |
53 | 0 | { |
54 | |
|
55 | 0 | int rc = 0; |
56 | |
|
57 | 0 | if (mutex == NULL) { |
58 | 0 | return EINVAL; |
59 | 0 | } |
60 | | |
61 | 0 | rc = pthread_mutex_destroy ((pthread_mutex_t *)*mutex); |
62 | |
|
63 | 0 | free (*mutex); |
64 | 0 | *mutex = NULL; |
65 | |
|
66 | 0 | return rc; |
67 | 0 | } |
68 | | |
69 | | static int ssh_pthread_mutex_lock (void **mutex) |
70 | 0 | { |
71 | 0 | return pthread_mutex_lock((pthread_mutex_t *)*mutex); |
72 | 0 | } |
73 | | |
74 | | static int ssh_pthread_mutex_unlock (void **mutex) |
75 | 0 | { |
76 | 0 | return pthread_mutex_unlock((pthread_mutex_t *)*mutex); |
77 | 0 | } |
78 | | |
79 | | static unsigned long ssh_pthread_thread_id (void) |
80 | 0 | { |
81 | | #if defined(_WIN32) && !defined(__WINPTHREADS_VERSION) |
82 | | return (unsigned long) pthread_self().p; |
83 | | #else |
84 | 0 | return (unsigned long) pthread_self(); |
85 | 0 | #endif |
86 | 0 | } |
87 | | |
88 | | static struct ssh_threads_callbacks_struct ssh_threads_pthread = |
89 | | { |
90 | | .type = "threads_pthread", |
91 | | .mutex_init = ssh_pthread_mutex_init, |
92 | | .mutex_destroy = ssh_pthread_mutex_destroy, |
93 | | .mutex_lock = ssh_pthread_mutex_lock, |
94 | | .mutex_unlock = ssh_pthread_mutex_unlock, |
95 | | .thread_id = ssh_pthread_thread_id |
96 | | }; |
97 | | |
98 | | /* Threads interface implementation */ |
99 | | |
100 | | #if (HAVE_PTHREAD) |
101 | | void ssh_mutex_lock(SSH_MUTEX *mutex) |
102 | 4 | { |
103 | 4 | int rc; |
104 | | |
105 | 4 | if (mutex == NULL) { |
106 | 0 | exit(EINVAL); |
107 | 0 | } |
108 | | |
109 | 4 | rc = pthread_mutex_lock(mutex); |
110 | | |
111 | 4 | if (rc) { |
112 | 0 | exit(rc); |
113 | 0 | } |
114 | 4 | } |
115 | | |
116 | | void ssh_mutex_unlock(SSH_MUTEX *mutex) |
117 | 4 | { |
118 | 4 | int rc; |
119 | | |
120 | 4 | if (mutex == NULL) { |
121 | 0 | exit(EINVAL); |
122 | 0 | } |
123 | | |
124 | 4 | rc = pthread_mutex_unlock(mutex); |
125 | | |
126 | 4 | if (rc) { |
127 | 0 | exit(rc); |
128 | 0 | } |
129 | 4 | } |
130 | | |
131 | | struct ssh_threads_callbacks_struct *ssh_threads_get_default(void) |
132 | 2 | { |
133 | 2 | return &ssh_threads_pthread; |
134 | 2 | } |
135 | | #endif |
136 | | |
137 | | struct ssh_threads_callbacks_struct *ssh_threads_get_pthread(void) |
138 | 0 | { |
139 | 0 | return &ssh_threads_pthread; |
140 | 0 | } |