GNU libmicrohttpd  0.9.29
platform_interface.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2014 Karlson2k (Evgeny Grin)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library.
17  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #ifndef MHD_PLATFORM_INTERFACE_H
27 #define MHD_PLATFORM_INTERFACE_H
28 
29 #include "platform.h"
30 #if defined(_WIN32) && !defined(__CYGWIN__)
31 #include "w32functions.h"
32 #endif
33 
34 /* *****************************
35  General function mapping
36  *****************************/
37 #if !defined(_WIN32) || defined(__CYGWIN__)
38 
44 #define MHD_str_equal_caseless_(a,b) (0==strcasecmp((a),(b)))
45 #else
46 
52 #define MHD_str_equal_caseless_(a,b) (0==_stricmp((a),(b)))
53 #endif
54 
55 #if !defined(_WIN32) || defined(__CYGWIN__)
56 
63 #define MHD_str_equal_caseless_n_(a,b,n) (0==strncasecmp((a),(b),(n)))
64 #else
65 
72 #define MHD_str_equal_caseless_n_(a,b,n) (0==_strnicmp((a),(b),(n)))
73 #endif
74 
75 /* Platform-independent snprintf name */
76 #if !defined(_WIN32) || defined(__CYGWIN__)
77 #define MHD_snprintf_ snprintf
78 #else
79 #define MHD_snprintf_ W32_snprintf
80 #endif
81 
82 
87 #if !defined(MHD_WINSOCK_SOCKETS)
88 typedef size_t _MHD_socket_funcs_size;
89 #else
90 typedef int _MHD_socket_funcs_size;
91 #endif
92 
102 #if !defined(MHD_WINSOCK_SOCKETS)
103 #define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0)
104 #else
105 #define MHD_socket_close_(fd) closesocket((fd))
106 #endif
107 
112 #if !defined(MHD_WINSOCK_SOCKETS)
113 #define MHD_socket_errno_ errno
114 #else
115 #define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
116 #endif
117 
118 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
119  * description string of last socket error (W32) */
120 #if !defined(MHD_WINSOCK_SOCKETS)
121 #define MHD_socket_last_strerr_() strerror(errno)
122 #else
123 #define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
124 #endif
125 
126 /* MHD_strerror_ is strerror (both non-W32/W32) */
127 #if !defined(MHD_WINSOCK_SOCKETS)
128 #define MHD_strerror_(errnum) strerror((errnum))
129 #else
130 #define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
131 #endif
132 
133 /* MHD_set_socket_errno_ set errno to errnum (non-W32) / set socket last error to errnum (W32) */
134 #if !defined(MHD_WINSOCK_SOCKETS)
135 #define MHD_set_socket_errno_(errnum) errno=(errnum)
136 #else
137 #define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
138 #endif
139 
140 /* MHD_SYS_select_ is wrapper macro for system select() function */
141 #if !defined(MHD_WINSOCK_SOCKETS)
142 #define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
143 #else
144 #define MHD_SYS_select_(n,r,w,e,t) select((int)0,(r),(w),(e),(t))
145 #endif
146 
147 #if defined(HAVE_POLL)
148 /* MHD_sys_poll_ is wrapper macro for system poll() function */
149 #if !defined(MHD_WINSOCK_SOCKETS)
150 #define MHD_sys_poll_ poll
151 #else /* MHD_WINSOCK_SOCKETS */
152 #define MHD_sys_poll_ WSAPoll
153 #endif /* MHD_WINSOCK_SOCKETS */
154 #endif /* HAVE_POLL */
155 
156 /* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) /
157  * create two connected sockets (MHD_DONT_USE_PIPES) */
158 #ifndef MHD_DONT_USE_PIPES
159 #define MHD_pipe_(fdarr) pipe((fdarr))
160 #else /* MHD_DONT_USE_PIPES */
161 #if !defined(_WIN32) || defined(__CYGWIN__)
162 #define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))
163 #else /* !defined(_WIN32) || defined(__CYGWIN__) */
164 #define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr))
165 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
166 #endif /* MHD_DONT_USE_PIPES */
167 
168 /* MHD_pipe_errno_ is errno of last function (!MHD_DONT_USE_PIPES) /
169  * errno of last emulated pipe function (MHD_DONT_USE_PIPES) */
170 #ifndef MHD_DONT_USE_PIPES
171 #define MHD_pipe_errno_ errno
172 #else
173 #define MHD_pipe_errno_ MHD_socket_errno_
174 #endif
175 
176 /* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) /
177  * description string of last pipe error (MHD_DONT_USE_PIPES) */
178 #ifndef MHD_DONT_USE_PIPES
179 #define MHD_pipe_last_strerror_() strerror(errno)
180 #else
181 #define MHD_pipe_last_strerror_() MHD_socket_last_strerr_()
182 #endif
183 
184 /* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) /
185  * write data to emulated pipe (MHD_DONT_USE_PIPES) */
186 #ifndef MHD_DONT_USE_PIPES
187 #define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz))
188 #else
189 #define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0)
190 #endif
191 
192 /* MHD_pipe_read_ read data from real pipe (!MHD_DONT_USE_PIPES) /
193  * read data from emulated pipe (MHD_DONT_USE_PIPES) */
194 #ifndef MHD_DONT_USE_PIPES
195 #define MHD_pipe_read_(fd, ptr, sz) read((fd), (void*)(ptr), (sz))
196 #else
197 #define MHD_pipe_read_(fd, ptr, sz) recv((fd), (char*)(ptr), (sz), 0)
198 #endif
199 
200 /* MHD_pipe_close_(fd) close any FDs (non-W32) /
201  * close emulated pipe FDs (W32) */
202 #ifndef MHD_DONT_USE_PIPES
203 #define MHD_pipe_close_(fd) close((fd))
204 #else
205 #define MHD_pipe_close_(fd) MHD_socket_close_((fd))
206 #endif
207 
208 /* MHD_INVALID_PIPE_ is a value of bad pipe FD */
209 #ifndef MHD_DONT_USE_PIPES
210 #define MHD_INVALID_PIPE_ (-1)
211 #else
212 #define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET
213 #endif
214 
215 #if !defined(_WIN32) || defined(__CYGWIN__)
216 #define MHD_random_() random()
217 #else
218 #define MHD_random_() MHD_W32_random_()
219 #endif
220 
221 #if defined(MHD_USE_POSIX_THREADS)
222 typedef pthread_t MHD_thread_handle_;
223 #elif defined(MHD_USE_W32_THREADS)
224 #include <windows.h>
225 typedef HANDLE MHD_thread_handle_;
226 #else
227 #error "No threading API is available."
228 #endif
229 
230 #if defined(MHD_USE_POSIX_THREADS)
231 #define MHD_THRD_RTRN_TYPE_ void*
232 #define MHD_THRD_CALL_SPEC_
233 #elif defined(MHD_USE_W32_THREADS)
234 #define MHD_THRD_RTRN_TYPE_ unsigned
235 #define MHD_THRD_CALL_SPEC_ __stdcall
236 #endif
237 
238 #if defined(MHD_USE_POSIX_THREADS)
239 
244 #define MHD_join_thread_(thread) pthread_join((thread), NULL)
245 #elif defined(MHD_USE_W32_THREADS)
246 
252 #define MHD_join_thread_(thread) (WAIT_OBJECT_0 == WaitForSingleObject((thread), INFINITE) ? (CloseHandle((thread)), 0) : 1 )
253 #endif
254 
255 #if defined(MHD_USE_W32_THREADS)
256 #define MHD_W32_MUTEX_ 1
257 #include <windows.h>
258 typedef CRITICAL_SECTION MHD_mutex_;
259 #elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
260 #define MHD_PTHREAD_MUTEX_ 1
261 typedef pthread_mutex_t MHD_mutex_;
262 #else
263 #error "No base mutex API is available."
264 #endif
265 
266 #if defined(MHD_PTHREAD_MUTEX_)
267 
272 #define MHD_mutex_create_(mutex) \
273  ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
274 #elif defined(MHD_W32_MUTEX_)
275 
280 #define MHD_mutex_create_(mutex) \
281  ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO)
282 #endif
283 
284 #if defined(MHD_PTHREAD_MUTEX_)
285 
290 #define MHD_mutex_destroy_(mutex) \
291  ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO)
292 #elif defined(MHD_W32_MUTEX_)
293 
298 #define MHD_mutex_destroy_(mutex) \
299  ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO)
300 #endif
301 
302 #if defined(MHD_PTHREAD_MUTEX_)
303 
310 #define MHD_mutex_lock_(mutex) \
311  ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO)
312 #elif defined(MHD_W32_MUTEX_)
313 
320 #define MHD_mutex_lock_(mutex) \
321  ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO)
322 #endif
323 
324 #if defined(MHD_PTHREAD_MUTEX_)
325 
332 #define MHD_mutex_trylock_(mutex) \
333  ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO)
334 #elif defined(MHD_W32_MUTEX_)
335 
342 #define MHD_mutex_trylock_(mutex) \
343  ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO)
344 #endif
345 
346 #if defined(MHD_PTHREAD_MUTEX_)
347 
352 #define MHD_mutex_unlock_(mutex) \
353  ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO)
354 #elif defined(MHD_W32_MUTEX_)
355 
360 #define MHD_mutex_unlock_(mutex) \
361  ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO)
362 #endif
363 
364 #endif /* MHD_PLATFORM_INTERFACE_H */
internal functions for W32 systems
platform-specific includes for libmicrohttpd
size_t _MHD_socket_funcs_size