Line data Source code
1 : /* ath-pthread.c - pthread module for self-adapting thread-safeness library
2 : Copyright (C) 2002, 2003, 2004 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
6 : GPGME is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : GPGME is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include <stdlib.h>
26 : #include <errno.h>
27 : #ifdef HAVE_UNISTD_H
28 : # include <unistd.h>
29 : #endif
30 : #ifdef HAVE_SYS_SELECT_H
31 : # include <sys/select.h>
32 : #else
33 : # ifdef HAVE_SYS_TIME_H
34 : # include <sys/time.h>
35 : # endif
36 : #endif
37 : #include <sys/types.h>
38 : #include <sys/wait.h>
39 :
40 : #include <pthread.h>
41 :
42 : #include "gpgme.h"
43 :
44 : #include "ath.h"
45 :
46 :
47 : /* The lock we take while checking for lazy lock initialization. */
48 : static pthread_mutex_t check_init_lock = PTHREAD_MUTEX_INITIALIZER;
49 :
50 : /* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if
51 : it is not already initialized. */
52 : static int
53 4145 : mutex_pthread_init (ath_mutex_t *priv, int just_check)
54 : {
55 4145 : int err = 0;
56 :
57 4145 : if (just_check)
58 4145 : pthread_mutex_lock (&check_init_lock);
59 4145 : if (!*priv || !just_check)
60 : {
61 83 : pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t));
62 83 : if (!lock)
63 0 : err = ENOMEM;
64 83 : if (!err)
65 : {
66 83 : err = pthread_mutex_init (lock, NULL);
67 83 : if (err)
68 0 : free (lock);
69 : else
70 83 : *priv = (ath_mutex_t) lock;
71 : }
72 : }
73 4145 : if (just_check)
74 4145 : pthread_mutex_unlock (&check_init_lock);
75 4145 : return err;
76 : }
77 :
78 :
79 : void
80 0 : ath_init (void)
81 : {
82 : /* Nothing to do. */
83 0 : }
84 :
85 :
86 : uintptr_t
87 0 : ath_self (void)
88 : {
89 0 : return (uintptr_t) pthread_self ();
90 : }
91 :
92 :
93 : int
94 0 : ath_mutex_init (ath_mutex_t *lock)
95 : {
96 0 : return mutex_pthread_init (lock, 0);
97 : }
98 :
99 :
100 : int
101 75 : ath_mutex_destroy (ath_mutex_t *lock)
102 : {
103 75 : int err = mutex_pthread_init (lock, 1);
104 75 : if (!err)
105 : {
106 75 : err = pthread_mutex_destroy ((pthread_mutex_t *) *lock);
107 75 : free (*lock);
108 : }
109 75 : return err;
110 : }
111 :
112 :
113 : int
114 2035 : ath_mutex_lock (ath_mutex_t *lock)
115 : {
116 2035 : int ret = mutex_pthread_init (lock, 1);
117 2035 : if (ret)
118 0 : return ret;
119 :
120 2035 : return pthread_mutex_lock ((pthread_mutex_t *) *lock);
121 : }
122 :
123 :
124 : int
125 2035 : ath_mutex_unlock (ath_mutex_t *lock)
126 : {
127 2035 : int ret = mutex_pthread_init (lock, 1);
128 2035 : if (ret)
129 0 : return ret;
130 :
131 2035 : return pthread_mutex_unlock ((pthread_mutex_t *) *lock);
132 : }
133 :
134 :
135 : gpgme_ssize_t
136 612 : ath_read (int fd, void *buf, size_t nbytes)
137 : {
138 612 : return read (fd, buf, nbytes);
139 : }
140 :
141 :
142 : gpgme_ssize_t
143 35 : ath_write (int fd, const void *buf, size_t nbytes)
144 : {
145 35 : return write (fd, buf, nbytes);
146 : }
147 :
148 :
149 : gpgme_ssize_t
150 1191 : ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
151 : struct timeval *timeout)
152 : {
153 1191 : return select (nfd, rset, wset, eset, timeout);
154 : }
155 :
156 :
157 : gpgme_ssize_t
158 125 : ath_waitpid (pid_t pid, int *status, int options)
159 : {
160 125 : return waitpid (pid, status, options);
161 : }
162 :
163 :
164 : int
165 0 : ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
166 : {
167 0 : return accept (s, addr, length_ptr);
168 : }
169 :
170 :
171 : int
172 0 : ath_connect (int s, const struct sockaddr *addr, socklen_t length)
173 : {
174 0 : return connect (s, addr, length);
175 : }
176 :
177 : int
178 0 : ath_sendmsg (int s, const struct msghdr *msg, int flags)
179 : {
180 0 : return sendmsg (s, msg, flags);
181 : }
182 :
183 :
184 : int
185 0 : ath_recvmsg (int s, struct msghdr *msg, int flags)
186 : {
187 0 : return recvmsg (s, msg, flags);
188 : }
|