1 /* ath-pth.c - Pth module for self-adapting thread-safeness library
2 Copyright (C) 2002, 2003, 2004 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 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.
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.
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
34 /* The lock we take while checking for lazy lock initialization. */
35 static pth_mutex_t check_init_lock = PTH_MUTEX_INIT;
41 return (uintptr_t) pth_self ();
45 /* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if
46 it is not already initialized. */
48 mutex_pth_init (ath_mutex_t *priv, int just_check)
53 pth_mutex_acquire (&check_init_lock, 0, NULL);
54 if (!*priv || !just_check)
56 pth_mutex_t *lock = malloc (sizeof (pth_mutex_t));
61 err = pth_mutex_init (lock);
70 *priv = (ath_mutex_t) lock;
74 pth_mutex_release (&check_init_lock);
87 ath_mutex_init (ath_mutex_t *lock)
89 return mutex_pth_init (lock, 0);
94 ath_mutex_destroy (ath_mutex_t *lock)
96 int err = mutex_pth_init (lock, 1);
99 /* GNU Pth has no destructor function. */
107 ath_mutex_lock (ath_mutex_t *lock)
109 int ret = mutex_pth_init (lock, 1);
113 ret = pth_mutex_acquire ((pth_mutex_t *) *lock, 0, NULL);
114 return ret == FALSE ? errno : 0;
119 ath_mutex_unlock (ath_mutex_t *lock)
121 int ret = mutex_pth_init (lock, 1);
125 ret = pth_mutex_release ((pth_mutex_t *) *lock);
126 return ret == FALSE ? errno : 0;
131 ath_read (int fd, void *buf, size_t nbytes)
133 return pth_read (fd, buf, nbytes);
138 ath_write (int fd, const void *buf, size_t nbytes)
140 return pth_write (fd, buf, nbytes);
145 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
146 struct timeval *timeout)
148 return pth_select (nfd, rset, wset, eset, timeout);
153 ath_waitpid (pid_t pid, int *status, int options)
155 return pth_waitpid (pid, status, options);
160 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
162 return pth_accept (s, addr, length_ptr);
167 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
169 return pth_connect (s, addr, length);
173 ath_sendmsg (int s, const struct msghdr *msg, int flags)
175 /* FIXME: GNU Pth is missing pth_sendmsg. */
176 return sendmsg (s, msg, flags);
181 ath_recvmsg (int s, struct msghdr *msg, int flags)
183 /* FIXME: GNU Pth is missing pth_recvmsg. */
184 return recvmsg (s, msg, flags);