1 /* ath-pthread.c - pthread 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
30 #ifdef HAVE_SYS_SELECT_H
31 # include <sys/select.h>
33 # ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
37 #include <sys/types.h>
45 /* The lock we take while checking for lazy lock initialization. */
46 static pthread_mutex_t check_init_lock = PTHREAD_MUTEX_INITIALIZER;
48 /* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if
49 it is not already initialized. */
51 mutex_pthread_init (ath_mutex_t *priv, int just_check)
56 pthread_mutex_lock (&check_init_lock);
57 if (!*priv || !just_check)
59 pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t));
64 err = pthread_mutex_init (lock, NULL);
68 *priv = (ath_mutex_t) lock;
72 pthread_mutex_unlock (&check_init_lock);
87 return (uintptr_t) pthread_self ();
92 ath_mutex_init (ath_mutex_t *lock)
94 return mutex_pthread_init (lock, 0);
99 ath_mutex_destroy (ath_mutex_t *lock)
101 int err = mutex_pthread_init (lock, 1);
104 err = pthread_mutex_destroy ((pthread_mutex_t *) *lock);
112 ath_mutex_lock (ath_mutex_t *lock)
114 int ret = mutex_pthread_init (lock, 1);
118 return pthread_mutex_lock ((pthread_mutex_t *) *lock);
123 ath_mutex_unlock (ath_mutex_t *lock)
125 int ret = mutex_pthread_init (lock, 1);
129 return pthread_mutex_unlock ((pthread_mutex_t *) *lock);
134 ath_read (int fd, void *buf, size_t nbytes)
136 return read (fd, buf, nbytes);
141 ath_write (int fd, const void *buf, size_t nbytes)
143 return write (fd, buf, nbytes);
148 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
149 struct timeval *timeout)
151 return select (nfd, rset, wset, eset, timeout);
156 ath_waitpid (pid_t pid, int *status, int options)
158 return waitpid (pid, status, options);
163 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
165 return accept (s, addr, length_ptr);
170 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
172 return connect (s, addr, length);
176 ath_sendmsg (int s, const struct msghdr *msg, int flags)
178 return sendmsg (s, msg, flags);
183 ath_recvmsg (int s, struct msghdr *msg, int flags)
185 return recvmsg (s, msg, flags);