Hopefully last changes for building with MSC.
[gpgme.git] / src / ath.c
1 /* ath.c - Thread-safeness library.
2    Copyright (C) 2002, 2003, 2004 g10 Code GmbH
3
4    This file is part of GPGME.
5  
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.
10    
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.
15    
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
19    02111-1307, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <assert.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_SELECT_H
30 # include <sys/select.h>
31 #else
32 # ifdef HAVE_SYS_TIME_H
33 #  include <sys/time.h>
34 # endif
35 #endif
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39 #ifndef HAVE_W32_SYSTEM
40 #include <sys/wait.h>
41 #endif
42
43 #ifdef _MSC_VER
44   typedef long ssize_t;
45   typedef int  pid_t;
46 #endif
47
48 #include "ath.h"
49
50
51 #define MUTEX_UNLOCKED  ((ath_mutex_t) 0)
52 #define MUTEX_LOCKED    ((ath_mutex_t) 1)
53 #define MUTEX_DESTROYED ((ath_mutex_t) 2)
54
55
56 #ifdef HAVE_W32_SYSTEM
57 #include <windows.h>
58 uintptr_t
59 ath_self (void)
60 {
61   return (uintptr_t) GetCurrentThreadId ();
62 }
63 #else
64 # ifdef __linux
65 #include <sys/syscall.h>
66 uintptr_t
67 ath_self (void)
68 {
69   /* Just to catch users who don't use gpgme-pthread.  */
70   return (uintptr_t) syscall (SYS_gettid);
71 }
72 # else
73 uintptr_t
74 ath_self (void)
75 {
76   return (uintptr_t) getpid ();
77 }
78 # endif
79 #endif
80
81
82 int
83 ath_mutex_init (ath_mutex_t *lock)
84 {
85 #ifndef NDEBUG
86   *lock = MUTEX_UNLOCKED;
87 #endif
88   return 0;
89 }
90
91
92 int
93 ath_mutex_destroy (ath_mutex_t *lock)
94 {
95 #ifndef NDEBUG
96   assert (*lock == MUTEX_UNLOCKED);
97
98   *lock = MUTEX_DESTROYED;
99 #endif
100   return 0;
101 }
102
103
104 int
105 ath_mutex_lock (ath_mutex_t *lock)
106 {
107 #ifndef NDEBUG
108   assert (*lock == MUTEX_UNLOCKED);
109
110   *lock = MUTEX_LOCKED;
111 #endif
112   return 0;
113 }
114
115
116 int
117 ath_mutex_unlock (ath_mutex_t *lock)
118 {
119 #ifndef NDEBUG
120   assert (*lock == MUTEX_LOCKED);
121
122   *lock = MUTEX_UNLOCKED;
123 #endif
124   return 0;
125 }
126
127
128 ssize_t
129 ath_read (int fd, void *buf, size_t nbytes)
130 {
131   return read (fd, buf, nbytes);
132 }
133
134
135 ssize_t
136 ath_write (int fd, const void *buf, size_t nbytes)
137 {
138   return write (fd, buf, nbytes);
139 }
140
141
142 ssize_t
143 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
144             struct timeval *timeout)
145 {
146 #ifdef HAVE_W32_SYSTEM
147   return -1; /* Not supported. */
148 #else
149   return select (nfd, rset, wset, eset, timeout);
150 #endif
151 }
152
153  
154 ssize_t
155 ath_waitpid (pid_t pid, int *status, int options)
156 {
157 #ifdef HAVE_W32_SYSTEM
158   return -1; /* Not supported. */
159 #else
160   return waitpid (pid, status, options);
161 #endif
162 }
163
164
165 int
166 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
167 {
168 #ifdef HAVE_W32_SYSTEM
169   return -1; /* Not supported. */
170 #else
171   return accept (s, addr, length_ptr);
172 #endif
173 }
174
175
176 int
177 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
178 {
179 #ifdef HAVE_W32_SYSTEM
180   return -1; /* Not supported. */
181 #else
182   return connect (s, addr, length);
183 #endif
184 }
185
186
187 int
188 ath_sendmsg (int s, const struct msghdr *msg, int flags)
189 {
190 #ifdef HAVE_W32_SYSTEM
191   return -1; /* Not supported. */
192 #else
193   return sendmsg (s, msg, flags);
194 #endif
195 }
196
197
198 int
199 ath_recvmsg (int s, struct msghdr *msg, int flags)
200 {
201 #ifdef HAVE_W32_SYSTEM
202   return -1; /* Not supported. */
203 #else
204   return recvmsg (s, msg, flags);
205 #endif
206 }