26 #ifndef MHD_PLATFORM_INTERFACE_H
27 #define MHD_PLATFORM_INTERFACE_H
30 #if defined(_WIN32) && !defined(__CYGWIN__)
37 #if !defined(_WIN32) || defined(__CYGWIN__)
44 #define MHD_str_equal_caseless_(a,b) (0==strcasecmp((a),(b)))
52 #define MHD_str_equal_caseless_(a,b) (0==_stricmp((a),(b)))
55 #if !defined(_WIN32) || defined(__CYGWIN__)
63 #define MHD_str_equal_caseless_n_(a,b,n) (0==strncasecmp((a),(b),(n)))
72 #define MHD_str_equal_caseless_n_(a,b,n) (0==_strnicmp((a),(b),(n)))
76 #if !defined(_WIN32) || defined(__CYGWIN__)
77 #define MHD_snprintf_ snprintf
79 #define MHD_snprintf_ W32_snprintf
85 #if !defined(_WIN32) || defined(__CYGWIN__)
86 #define MHD_socket_close_(fd) close((fd))
88 #define MHD_socket_close_(fd) closesocket((fd))
92 #if !defined(_WIN32) || defined(__CYGWIN__)
93 #define MHD_socket_errno_ errno
95 #define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
100 #if !defined(_WIN32) || defined(__CYGWIN__)
101 #define MHD_socket_last_strerr_() strerror(errno)
103 #define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
107 #if !defined(_WIN32) || defined(__CYGWIN__)
108 #define MHD_strerror_(errnum) strerror((errnum))
110 #define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
114 #if !defined(_WIN32) || defined(__CYGWIN__)
115 #define MHD_set_socket_errno_(errnum) errno=(errnum)
117 #define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
121 #if !defined(MHD_WINSOCK_SOCKETS)
122 #define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
124 #define MHD_SYS_select_(n,r,w,e,t) select((int)0,(r),(w),(e),(t))
129 #ifndef MHD_DONT_USE_PIPES
130 #define MHD_pipe_(fdarr) pipe((fdarr))
132 #if !defined(_WIN32) || defined(__CYGWIN__)
133 #define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))
135 #define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr))
141 #ifndef MHD_DONT_USE_PIPES
142 #define MHD_pipe_errno_ errno
144 #define MHD_pipe_errno_ MHD_socket_errno_
149 #ifndef MHD_DONT_USE_PIPES
150 #define MHD_pipe_last_strerror_() strerror(errno)
152 #define MHD_pipe_last_strerror_() MHD_socket_last_strerr_()
157 #ifndef MHD_DONT_USE_PIPES
158 #define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz))
160 #define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0)
165 #ifndef MHD_DONT_USE_PIPES
166 #define MHD_pipe_read_(fd, ptr, sz) read((fd), (void*)(ptr), (sz))
168 #define MHD_pipe_read_(fd, ptr, sz) recv((fd), (char*)(ptr), (sz), 0)
173 #ifndef MHD_DONT_USE_PIPES
174 #define MHD_pipe_close_(fd) close((fd))
176 #define MHD_pipe_close_(fd) MHD_socket_close_((fd))
180 #ifndef MHD_DONT_USE_PIPES
181 #define MHD_INVALID_PIPE_ (-1)
183 #define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET
186 #if !defined(_WIN32) || defined(__CYGWIN__)
187 #define MHD_random_() random()
189 #define MHD_random_() MHD_W32_random_()
192 #if defined(MHD_USE_POSIX_THREADS)
193 typedef pthread_t MHD_thread_handle_;
194 #elif defined(MHD_USE_W32_THREADS)
196 typedef HANDLE MHD_thread_handle_;
198 #error "No threading API is available."
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
209 #if defined(MHD_USE_POSIX_THREADS)
215 #define MHD_join_thread_(thread) pthread_join((thread), NULL)
216 #elif defined(MHD_USE_W32_THREADS)
223 #define MHD_join_thread_(thread) (WAIT_OBJECT_0 == WaitForSingleObject((thread), INFINITE) ? (CloseHandle((thread)), 0) : 1 )
226 #if defined(MHD_USE_W32_THREADS)
227 #define MHD_W32_MUTEX_ 1
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_;
234 #error "No base mutex API is available."
237 #if defined(MHD_PTHREAD_MUTEX_)
243 #define MHD_mutex_create_(mutex) \
244 ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
245 #elif defined(MHD_W32_MUTEX_)
251 #define MHD_mutex_create_(mutex) \
252 ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO)
255 #if defined(MHD_PTHREAD_MUTEX_)
261 #define MHD_mutex_destroy_(mutex) \
262 ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO)
263 #elif defined(MHD_W32_MUTEX_)
269 #define MHD_mutex_destroy_(mutex) \
270 ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO)
273 #if defined(MHD_PTHREAD_MUTEX_)
281 #define MHD_mutex_lock_(mutex) \
282 ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO)
283 #elif defined(MHD_W32_MUTEX_)
291 #define MHD_mutex_lock_(mutex) \
292 ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO)
295 #if defined(MHD_PTHREAD_MUTEX_)
303 #define MHD_mutex_trylock_(mutex) \
304 ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO)
305 #elif defined(MHD_W32_MUTEX_)
313 #define MHD_mutex_trylock_(mutex) \
314 ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO)
317 #if defined(MHD_PTHREAD_MUTEX_)
323 #define MHD_mutex_unlock_(mutex) \
324 ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO)
325 #elif defined(MHD_W32_MUTEX_)
331 #define MHD_mutex_unlock_(mutex) \
332 ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO)
335 #endif // MHD_PLATFORM_INTERFACE_H
internal functions for W32 systems