1 /* ath.c - 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
29 #ifdef HAVE_SYS_SELECT_H
30 # include <sys/select.h>
32 # ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 #ifndef HAVE_W32_SYSTEM
51 #define MUTEX_UNLOCKED ((ath_mutex_t) 0)
52 #define MUTEX_LOCKED ((ath_mutex_t) 1)
53 #define MUTEX_DESTROYED ((ath_mutex_t) 2)
56 #ifdef HAVE_W32_SYSTEM
61 return (uintptr_t) GetCurrentThreadId ();
65 #include <sys/syscall.h>
69 /* Just to catch users who don't use gpgme-pthread. */
70 return (uintptr_t) syscall (SYS_gettid);
76 return (uintptr_t) getpid ();
83 ath_mutex_init (ath_mutex_t *lock)
86 *lock = MUTEX_UNLOCKED;
93 ath_mutex_destroy (ath_mutex_t *lock)
96 assert (*lock == MUTEX_UNLOCKED);
98 *lock = MUTEX_DESTROYED;
105 ath_mutex_lock (ath_mutex_t *lock)
108 assert (*lock == MUTEX_UNLOCKED);
110 *lock = MUTEX_LOCKED;
117 ath_mutex_unlock (ath_mutex_t *lock)
120 assert (*lock == MUTEX_LOCKED);
122 *lock = MUTEX_UNLOCKED;
129 ath_read (int fd, void *buf, size_t nbytes)
131 return read (fd, buf, nbytes);
136 ath_write (int fd, const void *buf, size_t nbytes)
138 return write (fd, buf, nbytes);
143 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
144 struct timeval *timeout)
146 #ifdef HAVE_W32_SYSTEM
147 return -1; /* Not supported. */
149 return select (nfd, rset, wset, eset, timeout);
155 ath_waitpid (pid_t pid, int *status, int options)
157 #ifdef HAVE_W32_SYSTEM
158 return -1; /* Not supported. */
160 return waitpid (pid, status, options);
166 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
168 #ifdef HAVE_W32_SYSTEM
169 return -1; /* Not supported. */
171 return accept (s, addr, length_ptr);
177 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
179 #ifdef HAVE_W32_SYSTEM
180 return -1; /* Not supported. */
182 return connect (s, addr, length);
188 ath_sendmsg (int s, const struct msghdr *msg, int flags)
190 #ifdef HAVE_W32_SYSTEM
191 return -1; /* Not supported. */
193 return sendmsg (s, msg, flags);
199 ath_recvmsg (int s, struct msghdr *msg, int flags)
201 #ifdef HAVE_W32_SYSTEM
202 return -1; /* Not supported. */
204 return recvmsg (s, msg, flags);