Try to make configure.ac a bit smaller.
[gpgme.git] / src / ath-pthread.c
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 "ath.h"
43
44
45 /* The lock we take while checking for lazy lock initialization.  */
46 static pthread_mutex_t check_init_lock = PTHREAD_MUTEX_INITIALIZER;
47
48 /* Initialize the mutex *PRIV.  If JUST_CHECK is true, only do this if
49    it is not already initialized.  */
50 static int
51 mutex_pthread_init (ath_mutex_t *priv, int just_check)
52 {
53   int err = 0;
54
55   if (just_check)
56     pthread_mutex_lock (&check_init_lock);
57   if (!*priv || !just_check)
58     {
59       pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t));
60       if (!lock)
61         err = ENOMEM;
62       if (!err)
63         {
64           err = pthread_mutex_init (lock, NULL);
65           if (err)
66             free (lock);
67           else
68             *priv = (ath_mutex_t) lock;
69         }
70     }
71   if (just_check)
72     pthread_mutex_unlock (&check_init_lock);
73   return err;
74 }
75
76
77 void
78 ath_init (void)
79 {
80   /* Nothing to do.  */
81 }
82
83
84 uintptr_t
85 ath_self (void)
86 {
87   return (uintptr_t) pthread_self ();
88 }
89
90
91 int
92 ath_mutex_init (ath_mutex_t *lock)
93 {
94   return mutex_pthread_init (lock, 0);
95 }
96
97
98 int
99 ath_mutex_destroy (ath_mutex_t *lock)
100 {
101   int err = mutex_pthread_init (lock, 1);
102   if (!err)
103     {
104       err = pthread_mutex_destroy ((pthread_mutex_t *) *lock);
105       free (*lock);
106     }
107   return err;
108 }
109
110
111 int
112 ath_mutex_lock (ath_mutex_t *lock)
113 {
114   int ret = mutex_pthread_init (lock, 1);
115   if (ret)
116     return ret;
117
118   return pthread_mutex_lock ((pthread_mutex_t *) *lock);
119 }
120
121
122 int
123 ath_mutex_unlock (ath_mutex_t *lock)
124 {
125   int ret = mutex_pthread_init (lock, 1);
126   if (ret)
127     return ret;
128
129   return pthread_mutex_unlock ((pthread_mutex_t *) *lock);
130 }
131
132
133 ssize_t
134 ath_read (int fd, void *buf, size_t nbytes)
135 {
136   return read (fd, buf, nbytes);
137 }
138
139
140 ssize_t
141 ath_write (int fd, const void *buf, size_t nbytes)
142 {
143   return write (fd, buf, nbytes);
144 }
145
146
147 ssize_t
148 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
149             struct timeval *timeout)
150 {
151   return select (nfd, rset, wset, eset, timeout);
152 }
153
154  
155 ssize_t
156 ath_waitpid (pid_t pid, int *status, int options)
157 {
158   return waitpid (pid, status, options);
159 }
160
161
162 int
163 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
164 {
165   return accept (s, addr, length_ptr);
166 }
167
168
169 int
170 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
171 {
172   return connect (s, addr, length);
173 }
174
175 int
176 ath_sendmsg (int s, const struct msghdr *msg, int flags)
177 {
178   return sendmsg (s, msg, flags);
179 }
180
181
182 int
183 ath_recvmsg (int s, struct msghdr *msg, int flags)
184 {
185   return recvmsg (s, msg, flags);
186 }