1 /* ath-pth.c - Pth module for self-adapting thread-safeness library
2 Copyright (C) 2002, 2003 g10 Code GmbH
4 This file is part of GPGME.
6 GPGME is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GPGME; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 /* The lock we take while checking for lazy lock initialization. */
34 static pth_mutex_t check_init_lock = PTH_MUTEX_INIT;
36 /* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if
37 it is not already initialized. */
39 mutex_pth_init (ath_mutex_t *priv, int just_check)
44 pth_mutex_acquire (&check_init_lock, 0, NULL);
45 if (!*priv || !just_check)
47 pth_mutex_t *lock = malloc (sizeof (pth_mutex_t));
52 err = pth_mutex_init (lock);
61 *priv = (ath_mutex_t) lock;
65 pth_mutex_release (&check_init_lock);
78 ath_mutex_init (ath_mutex_t *lock)
80 return mutex_pth_init (lock, 0);
85 ath_mutex_destroy (ath_mutex_t *lock)
87 int err = mutex_pth_init (lock, 1);
90 /* GNU Pth has no destructor function. */
98 ath_mutex_lock (ath_mutex_t *lock)
100 int ret = mutex_pth_init (lock, 1);
104 ret = pth_mutex_acquire ((pth_mutex_t *) *lock, 0, NULL);
105 return ret == FALSE ? errno : 0;
110 ath_mutex_unlock (ath_mutex_t *lock)
112 int ret = mutex_pth_init (lock, 1);
116 ret = pth_mutex_release ((pth_mutex_t *) *lock);
117 return ret == FALSE ? errno : 0;
122 ath_read (int fd, void *buf, size_t nbytes)
124 return pth_read (fd, buf, nbytes);
129 ath_write (int fd, const void *buf, size_t nbytes)
131 return pth_write (fd, buf, nbytes);
136 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
137 struct timeval *timeout)
139 return pth_select (nfd, rset, wset, eset, timeout);
144 ath_waitpid (pid_t pid, int *status, int options)
146 return pth_waitpid (pid, status, options);
151 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
153 return pth_accept (s, addr, length_ptr);
158 ath_connect (int s, struct sockaddr *addr, socklen_t length)
160 return pth_connect (s, addr, length);
164 ath_sendmsg (int s, const struct msghdr *msg, int flags)
166 /* FIXME: GNU Pth is missing pth_sendmsg. */
167 return sendmsg (s, msg, flags);
172 ath_recvmsg (int s, struct msghdr *msg, int flags)
174 /* FIXME: GNU Pth is missing pth_recvmsg. */
175 return recvmsg (s, msg, flags);