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 #include <sys/types.h>
37 #ifndef HAVE_W32_SYSTEM
49 #define MUTEX_UNLOCKED ((ath_mutex_t) 0)
50 #define MUTEX_LOCKED ((ath_mutex_t) 1)
51 #define MUTEX_DESTROYED ((ath_mutex_t) 2)
54 #ifdef HAVE_W32_SYSTEM
59 return (uintptr_t) GetCurrentThreadId ();
63 #include <sys/types.h>
64 #include <sys/syscall.h>
68 /* Just to catch users who don't use gpgme-pthread. */
69 return (uintptr_t) syscall (SYS_gettid);
75 return (uintptr_t) getpid ();
82 ath_mutex_init (ath_mutex_t *lock)
85 *lock = MUTEX_UNLOCKED;
92 ath_mutex_destroy (ath_mutex_t *lock)
95 assert (*lock == MUTEX_UNLOCKED);
97 *lock = MUTEX_DESTROYED;
104 ath_mutex_lock (ath_mutex_t *lock)
107 assert (*lock == MUTEX_UNLOCKED);
109 *lock = MUTEX_LOCKED;
116 ath_mutex_unlock (ath_mutex_t *lock)
119 assert (*lock == MUTEX_LOCKED);
121 *lock = MUTEX_UNLOCKED;
128 ath_read (int fd, void *buf, size_t nbytes)
130 return read (fd, buf, nbytes);
135 ath_write (int fd, const void *buf, size_t nbytes)
137 return write (fd, buf, nbytes);
142 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
143 struct timeval *timeout)
145 #ifdef HAVE_W32_SYSTEM
146 return -1; /* Not supported. */
148 return select (nfd, rset, wset, eset, timeout);
154 ath_waitpid (pid_t pid, int *status, int options)
156 #ifdef HAVE_W32_SYSTEM
157 return -1; /* Not supported. */
159 return waitpid (pid, status, options);
165 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
167 #ifdef HAVE_W32_SYSTEM
168 return -1; /* Not supported. */
170 return accept (s, addr, length_ptr);
176 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
178 #ifdef HAVE_W32_SYSTEM
179 return -1; /* Not supported. */
181 return connect (s, addr, length);
187 ath_sendmsg (int s, const struct msghdr *msg, int flags)
189 #ifdef HAVE_W32_SYSTEM
190 return -1; /* Not supported. */
192 return sendmsg (s, msg, flags);
198 ath_recvmsg (int s, struct msghdr *msg, int flags)
200 #ifdef HAVE_W32_SYSTEM
201 return -1; /* Not supported. */
203 return recvmsg (s, msg, flags);