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
28
#ifdef HAVE_SYS_SELECT_H
29
# include <sys/select.h>
31
# include <sys/time.h>
33
#include <sys/types.h>
41
/* The lock we take while checking for lazy lock initialization. */
42
static pthread_mutex_t check_init_lock = PTHREAD_MUTEX_INITIALIZER;
44
/* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if
45
it is not already initialized. */
47
mutex_pthread_init (ath_mutex_t *priv, int just_check)
52
pthread_mutex_lock (&check_init_lock);
53
if (!*priv || !just_check)
55
pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t));
60
err = pthread_mutex_init (lock, NULL);
64
*priv = (ath_mutex_t) lock;
68
pthread_mutex_unlock (&check_init_lock);
81
ath_mutex_init (ath_mutex_t *lock)
83
return mutex_pthread_init (lock, 0);
88
ath_mutex_destroy (ath_mutex_t *lock)
90
int err = mutex_pthread_init (lock, 1);
93
err = pthread_mutex_destroy ((pthread_mutex_t *) *lock);
101
ath_mutex_lock (ath_mutex_t *lock)
103
int ret = mutex_pthread_init (lock, 1);
107
return pthread_mutex_lock ((pthread_mutex_t *) *lock);
112
ath_mutex_unlock (ath_mutex_t *lock)
114
int ret = mutex_pthread_init (lock, 1);
118
return pthread_mutex_unlock ((pthread_mutex_t *) *lock);
123
ath_read (int fd, void *buf, size_t nbytes)
125
return read (fd, buf, nbytes);
130
ath_write (int fd, const void *buf, size_t nbytes)
132
return write (fd, buf, nbytes);
137
ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
138
struct timeval *timeout)
140
return select (nfd, rset, wset, eset, timeout);
145
ath_waitpid (pid_t pid, int *status, int options)
147
return waitpid (pid, status, options);
152
ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
154
return accept (s, addr, length_ptr);
159
ath_connect (int s, const struct sockaddr *addr, socklen_t length)
161
return connect (s, addr, length);
165
ath_sendmsg (int s, const struct msghdr *msg, int flags)
167
return sendmsg (s, msg, flags);
172
ath_recvmsg (int s, struct msghdr *msg, int flags)
174
return recvmsg (s, msg, flags);