GNU libmicrohttpd  0.9.29
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
platform_interface.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  (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 
83 
84 /* MHD_socket_close_(fd) close any FDs (non-W32) / close only socket FDs (W32) */
85 #if !defined(_WIN32) || defined(__CYGWIN__)
86 #define MHD_socket_close_(fd) close((fd))
87 #else
88 #define MHD_socket_close_(fd) closesocket((fd))
89 #endif
90 
91 /* MHD_socket_errno_ is errno of last function (non-W32) / errno of last socket function (W32) */
92 #if !defined(_WIN32) || defined(__CYGWIN__)
93 #define MHD_socket_errno_ errno
94 #else
95 #define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
96 #endif
97 
98 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
99  * description string of last socket error (W32) */
100 #if !defined(_WIN32) || defined(__CYGWIN__)
101 #define MHD_socket_last_strerr_() strerror(errno)
102 #else
103 #define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
104 #endif
105 
106 /* MHD_strerror_ is strerror (both non-W32/W32) */
107 #if !defined(_WIN32) || defined(__CYGWIN__)
108 #define MHD_strerror_(errnum) strerror((errnum))
109 #else
110 #define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
111 #endif
112 
113 /* MHD_set_socket_errno_ set errno to errnum (non-W32) / set socket last error to errnum (W32) */
114 #if !defined(_WIN32) || defined(__CYGWIN__)
115 #define MHD_set_socket_errno_(errnum) errno=(errnum)
116 #else
117 #define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
118 #endif
119 
120 /* MHD_SYS_select_ is wrapper macro for system select() function */
121 #if !defined(MHD_WINSOCK_SOCKETS)
122 #define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
123 #else
124 #define MHD_SYS_select_(n,r,w,e,t) select((int)0,(r),(w),(e),(t))
125 #endif
126 
127 /* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) /
128  * create two connected sockets (MHD_DONT_USE_PIPES) */
129 #ifndef MHD_DONT_USE_PIPES
130 #define MHD_pipe_(fdarr) pipe((fdarr))
131 #else /* MHD_DONT_USE_PIPES */
132 #if !defined(_WIN32) || defined(__CYGWIN__)
133 #define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))
134 #else /* !defined(_WIN32) || defined(__CYGWIN__) */
135 #define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr))
136 #endif /* !defined(_WIN32) || defined(__CYGWIN__) */
137 #endif /* MHD_DONT_USE_PIPES */
138 
139 /* MHD_pipe_errno_ is errno of last function (!MHD_DONT_USE_PIPES) /
140  * errno of last emulated pipe function (MHD_DONT_USE_PIPES) */
141 #ifndef MHD_DONT_USE_PIPES
142 #define MHD_pipe_errno_ errno
143 #else
144 #define MHD_pipe_errno_ MHD_socket_errno_
145 #endif
146 
147 /* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) /
148  * description string of last pipe error (MHD_DONT_USE_PIPES) */
149 #ifndef MHD_DONT_USE_PIPES
150 #define MHD_pipe_last_strerror_() strerror(errno)
151 #else
152 #define MHD_pipe_last_strerror_() MHD_socket_last_strerr_()
153 #endif
154 
155 /* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) /
156  * write data to emulated pipe (MHD_DONT_USE_PIPES) */
157 #ifndef MHD_DONT_USE_PIPES
158 #define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz))
159 #else
160 #define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0)
161 #endif
162 
163 /* MHD_pipe_read_ read data from real pipe (!MHD_DONT_USE_PIPES) /
164  * read data from emulated pipe (MHD_DONT_USE_PIPES) */
165 #ifndef MHD_DONT_USE_PIPES
166 #define MHD_pipe_read_(fd, ptr, sz) read((fd), (void*)(ptr), (sz))
167 #else
168 #define MHD_pipe_read_(fd, ptr, sz) recv((fd), (char*)(ptr), (sz), 0)
169 #endif
170 
171 /* MHD_pipe_close_(fd) close any FDs (non-W32) /
172  * close emulated pipe FDs (W32) */
173 #ifndef MHD_DONT_USE_PIPES
174 #define MHD_pipe_close_(fd) close((fd))
175 #else
176 #define MHD_pipe_close_(fd) MHD_socket_close_((fd))
177 #endif
178 
179 /* MHD_INVALID_PIPE_ is a value of bad pipe FD */
180 #ifndef MHD_DONT_USE_PIPES
181 #define MHD_INVALID_PIPE_ (-1)
182 #else
183 #define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET
184 #endif
185 
186 #if !defined(_WIN32) || defined(__CYGWIN__)
187 #define MHD_random_() random()
188 #else
189 #define MHD_random_() MHD_W32_random_()
190 #endif
191 
192 #if defined(MHD_USE_POSIX_THREADS)
193 typedef pthread_t MHD_thread_handle_;
194 #elif defined(MHD_USE_W32_THREADS)
195 #include <windows.h>
196 typedef HANDLE MHD_thread_handle_;
197 #else
198 #error "No threading API is available."
199 #endif
200 
201 #if defined(MHD_USE_POSIX_THREADS)
202 #define MHD_THRD_RTRN_TYPE_ void*
203 #define MHD_THRD_CALL_SPEC_
204 #elif defined(MHD_USE_W32_THREADS)
205 #define MHD_THRD_RTRN_TYPE_ DWORD
206 #define MHD_THRD_CALL_SPEC_ WINAPI
207 #endif
208 
209 #if defined(MHD_USE_POSIX_THREADS)
210 
215 #define MHD_join_thread_(thread) pthread_join((thread), NULL)
216 #elif defined(MHD_USE_W32_THREADS)
217 
223 #define MHD_join_thread_(thread) (WAIT_OBJECT_0 == WaitForSingleObject((thread), INFINITE) ? (CloseHandle((thread)), 0) : 1 )
224 #endif
225 
226 #if defined(MHD_USE_W32_THREADS)
227 #define MHD_W32_MUTEX_ 1
228 #include <windows.h>
229 typedef CRITICAL_SECTION MHD_mutex_;
230 #elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
231 #define MHD_PTHREAD_MUTEX_ 1
232 typedef pthread_mutex_t MHD_mutex_;
233 #else
234 #error "No base mutex API is available."
235 #endif
236 
237 #if defined(MHD_PTHREAD_MUTEX_)
238 
243 #define MHD_mutex_create_(mutex) \
244  ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
245 #elif defined(MHD_W32_MUTEX_)
246 
251 #define MHD_mutex_create_(mutex) \
252  ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO)
253 #endif
254 
255 #if defined(MHD_PTHREAD_MUTEX_)
256 
261 #define MHD_mutex_destroy_(mutex) \
262  ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO)
263 #elif defined(MHD_W32_MUTEX_)
264 
269 #define MHD_mutex_destroy_(mutex) \
270  ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO)
271 #endif
272 
273 #if defined(MHD_PTHREAD_MUTEX_)
274 
281 #define MHD_mutex_lock_(mutex) \
282  ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO)
283 #elif defined(MHD_W32_MUTEX_)
284 
291 #define MHD_mutex_lock_(mutex) \
292  ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO)
293 #endif
294 
295 #if defined(MHD_PTHREAD_MUTEX_)
296 
303 #define MHD_mutex_trylock_(mutex) \
304  ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO)
305 #elif defined(MHD_W32_MUTEX_)
306 
313 #define MHD_mutex_trylock_(mutex) \
314  ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO)
315 #endif
316 
317 #if defined(MHD_PTHREAD_MUTEX_)
318 
323 #define MHD_mutex_unlock_(mutex) \
324  ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO)
325 #elif defined(MHD_W32_MUTEX_)
326 
331 #define MHD_mutex_unlock_(mutex) \
332  ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO)
333 #endif
334 
335 #endif // MHD_PLATFORM_INTERFACE_H
internal functions for W32 systems
platform-specific includes for libmicrohttpd