aboutsummaryrefslogtreecommitdiff
path: root/src/include/platform_interface.h
blob: 13f54530dce45feadafd177e2b29dae65d8da2ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
  This file is part of libmicrohttpd
  Copyright (C) 2014 Karlson2k (Evgeny Grin)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.
  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file include/platform_interface.h
 * @brief  internal platform abstraction functions
 * @author Karlson2k (Evgeny Grin)
 */

#ifndef MHD_PLATFORM_INTERFACE_H
#define MHD_PLATFORM_INTERFACE_H

#include "platform.h"
#if defined(_WIN32) && !defined(__CYGWIN__)
#include "w32functions.h"
#endif

/* *****************************
     General function mapping
   *****************************/

/* Platform-independent snprintf name */
#if defined(HAVE_SNPRINTF)
#define MHD_snprintf_ snprintf
#else  /* ! HAVE_SNPRINTF */
#if defined(_WIN32)
#define MHD_snprintf_ W32_snprintf
#else  /* ! _WIN32*/
#error Your platform does not support snprintf() and MHD does not know how to emulate it on your platform.
#endif /* ! _WIN32*/
#endif /* ! HAVE_SNPRINTF */


/**
 * _MHD_socket_funcs_size is type used to specify size for send and recv
 * functions
 */
#if !defined(MHD_WINSOCK_SOCKETS)
typedef size_t _MHD_socket_funcs_size;
#else
typedef int _MHD_socket_funcs_size;
#endif

/**
 * MHD_socket_close_(fd) close any FDs (non-W32) / close only socket
 * FDs (W32).  Note that on HP-UNIX, this function may leak the FD if
 * errno is set to EINTR.  Do not use HP-UNIX.
 *
 * @param fd descriptor to close
 * @return 0 on success (error codes like EINTR and EIO are counted as success,
 *           only EBADF counts as an error!)
 */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0)
#else
#define MHD_socket_close_(fd) closesocket((fd))
#endif

/**
 * MHD_socket_errno_ is errno of last function (non-W32) / errno of
 * last socket function (W32)
 */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_socket_errno_ errno
#else
#define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
#endif

/* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
 *                            description string of last socket error (W32) */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_socket_last_strerr_() strerror(errno)
#else
#define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
#endif

/* MHD_strerror_ is strerror (both non-W32/W32) */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_strerror_(errnum) strerror((errnum))
#else
#define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
#endif

/* MHD_set_socket_errno_ set errno to errnum (non-W32) / set socket last error to errnum (W32) */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_set_socket_errno_(errnum) errno=(errnum)
#else
#define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
#endif

/* MHD_SYS_select_ is wrapper macro for system select() function */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
#else
#define MHD_SYS_select_(n,r,w,e,t) \
 ( (!(r) || ((fd_set*)(r))->fd_count == 0) && \
   (!(w) || ((fd_set*)(w))->fd_count == 0) && \
   (!(e) || ((fd_set*)(e))->fd_count == 0) ) ? \
 ( (t) ? (Sleep((t)->tv_sec * 1000 + (t)->tv_usec / 1000), 0) : 0 ) : \
   (select((int)0,(r),(w),(e),(t)))
#endif

#if defined(HAVE_POLL)
/* MHD_sys_poll_ is wrapper macro for system poll() function */
#if !defined(MHD_WINSOCK_SOCKETS)
#define MHD_sys_poll_ poll
#else  /* MHD_WINSOCK_SOCKETS */
#define MHD_sys_poll_ WSAPoll
#endif /* MHD_WINSOCK_SOCKETS */
#endif /* HAVE_POLL */

/* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) /
 *           create two connected sockets (MHD_DONT_USE_PIPES) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_(fdarr) pipe((fdarr))
#else /* MHD_DONT_USE_PIPES */
#if !defined(_WIN32) || defined(__CYGWIN__)
#define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))
#else /* !defined(_WIN32) || defined(__CYGWIN__) */
#define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr))
#endif /* !defined(_WIN32) || defined(__CYGWIN__) */
#endif /* MHD_DONT_USE_PIPES */

/* MHD_pipe_errno_ is errno of last function (!MHD_DONT_USE_PIPES) /
 *                    errno of last emulated pipe function (MHD_DONT_USE_PIPES) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_errno_ errno
#else
#define MHD_pipe_errno_ MHD_socket_errno_
#endif

/* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) /
 *                            description string of last pipe error (MHD_DONT_USE_PIPES) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_last_strerror_() strerror(errno)
#else
#define MHD_pipe_last_strerror_() MHD_socket_last_strerr_()
#endif

/* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) /
 *                 write data to emulated pipe (MHD_DONT_USE_PIPES) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz))
#else
#define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0)
#endif

/* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) /
 *                drain data from emulated pipe (MHD_DONT_USE_PIPES) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_drain_(fd) do { long tmp; while (0 < read((fd), (void*)&tmp, sizeof (tmp))) ; } while (0)
#else
#define MHD_pipe_drain_(fd) do { long tmp; while (0 < recv((fd), (void*)&tmp, sizeof (tmp), 0)) ; } while (0)
#endif

/* MHD_pipe_close_(fd) close any FDs (non-W32) /
 *                     close emulated pipe FDs (W32) */
#ifndef MHD_DONT_USE_PIPES
#define MHD_pipe_close_(fd) close((fd))
#else
#define MHD_pipe_close_(fd) MHD_socket_close_((fd))
#endif

/* MHD_INVALID_PIPE_ is a value of bad pipe FD */
#ifndef MHD_DONT_USE_PIPES
#define MHD_INVALID_PIPE_ (-1)
#else
#define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET
#endif

#if !defined(_WIN32) || defined(__CYGWIN__)
#define MHD_random_() random()
#else
#define MHD_random_() MHD_W32_random_()
#endif
#if defined(MHD_USE_W32_THREADS)
#define MHD_W32_MUTEX_ 1
#include <windows.h>
typedef CRITICAL_SECTION MHD_mutex_;
#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
#define MHD_PTHREAD_MUTEX_ 1
typedef pthread_mutex_t MHD_mutex_;
#else
#error "No base mutex API is available."
#endif

#if defined(MHD_PTHREAD_MUTEX_)
/**
 * Create new mutex.
 * @param mutex pointer to the mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_create_(mutex) \
  ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
#elif defined(MHD_W32_MUTEX_)
/**
 * Create new mutex.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_create_(mutex) \
  ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO)
#endif

#if defined(MHD_PTHREAD_MUTEX_)
/**
 * Destroy previously created mutex.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_destroy_(mutex) \
  ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO)
#elif defined(MHD_W32_MUTEX_)
/**
 * Destroy previously created mutex.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_destroy_(mutex) \
  ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO)
#endif

#if defined(MHD_PTHREAD_MUTEX_)
/**
 * Acquire lock on previously created mutex.
 * If mutex was already locked by other thread, function
 * blocks until mutex becomes available.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_lock_(mutex) \
  ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO)
#elif defined(MHD_W32_MUTEX_)
/**
 * Acquire lock on previously created mutex.
 * If mutex was already locked by other thread, function
 * blocks until mutex becomes available.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_lock_(mutex) \
  ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO)
#endif

#if defined(MHD_PTHREAD_MUTEX_)
/**
 * Try to acquire lock on previously created mutex.
 * Function returns immediately.
 * @param mutex pointer to mutex
 * @return #MHD_YES if mutex is locked, #MHD_NO if
 * mutex was not locked.
 */
#define MHD_mutex_trylock_(mutex) \
  ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO)
#elif defined(MHD_W32_MUTEX_)
/**
 * Try to acquire lock on previously created mutex.
 * Function returns immediately.
 * @param mutex pointer to mutex
 * @return #MHD_YES if mutex is locked, #MHD_NO if
 * mutex was not locked.
 */
#define MHD_mutex_trylock_(mutex) \
  ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO)
#endif

#if defined(MHD_PTHREAD_MUTEX_)
/**
 * Unlock previously created and locked mutex.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_unlock_(mutex) \
  ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO)
#elif defined(MHD_W32_MUTEX_)
/**
 * Unlock previously created and locked mutex.
 * @param mutex pointer to mutex
 * @return #MHD_YES on success, #MHD_NO on failure
 */
#define MHD_mutex_unlock_(mutex) \
  ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO)
#endif

#endif /* MHD_PLATFORM_INTERFACE_H */