Fixes for the MSC build
[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 #if defined(HAVE_W32CE_SYSTEM) && defined(_MSC_VER)
132   return -1; /* Not supported. */
133 #else
134   return read (fd, buf, nbytes);
135 #endif
136 }
137
138
139 ssize_t
140 ath_write (int fd, const void *buf, size_t nbytes)
141 {
142 #if defined(HAVE_W32CE_SYSTEM) && defined(_MSC_VER)
143   return -1; /* Not supported. */
144 #else
145   return write (fd, buf, nbytes);
146 #endif
147 }
148
149
150 ssize_t
151 ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
152             struct timeval *timeout)
153 {
154 #ifdef HAVE_W32_SYSTEM
155   return -1; /* Not supported. */
156 #else
157   return select (nfd, rset, wset, eset, timeout);
158 #endif
159 }
160
161  
162 ssize_t
163 ath_waitpid (pid_t pid, int *status, int options)
164 {
165 #ifdef HAVE_W32_SYSTEM
166   return -1; /* Not supported. */
167 #else
168   return waitpid (pid, status, options);
169 #endif
170 }
171
172
173 int
174 ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr)
175 {
176 #ifdef HAVE_W32_SYSTEM
177   return -1; /* Not supported. */
178 #else
179   return accept (s, addr, length_ptr);
180 #endif
181 }
182
183
184 int
185 ath_connect (int s, const struct sockaddr *addr, socklen_t length)
186 {
187 #ifdef HAVE_W32_SYSTEM
188   return -1; /* Not supported. */
189 #else
190   return connect (s, addr, length);
191 #endif
192 }
193
194
195 int
196 ath_sendmsg (int s, const struct msghdr *msg, int flags)
197 {
198 #ifdef HAVE_W32_SYSTEM
199   return -1; /* Not supported. */
200 #else
201   return sendmsg (s, msg, flags);
202 #endif
203 }
204
205
206 int
207 ath_recvmsg (int s, struct msghdr *msg, int flags)
208 {
209 #ifdef HAVE_W32_SYSTEM
210   return -1; /* Not supported. */
211 #else
212   return recvmsg (s, msg, flags);
213 #endif
214 }