GNU libmicrohttpd  0.9.59
mhd_itc.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2016 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; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 */
20 
33 #ifndef MHD_ITC_H
34 #define MHD_ITC_H 1
35 #include "mhd_itc_types.h"
36 
37 #include <fcntl.h>
38 
39 #ifndef MHD_PANIC
40 # include <stdio.h>
41 # include <stdlib.h>
42 /* Simple implementation of MHD_PANIC, to be used outside lib */
43 # define MHD_PANIC(msg) do { fprintf (stderr, \
44  "Abnormal termination at %d line in file %s: %s\n", \
45  (int)__LINE__, __FILE__, msg); abort();} while(0)
46 #endif /* ! MHD_PANIC */
47 
48 #if defined(_MHD_ITC_EVENTFD)
49 
50 /* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */
51 #include <sys/eventfd.h>
52 #include <stdint.h> /* for uint64_t */
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h> /* for read(), write(), errno */
55 #endif /* HAVE_UNISTD_H */
56 #ifdef HAVE_STRING_H
57 #include <string.h> /* for strerror() */
58 #endif
59 
60 
66 #define MHD_itc_init_(itc) (-1 != ((itc).fd = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK)))
67 
71 #define MHD_itc_last_strerror_() strerror(errno)
72 
76 static const uint64_t _MHD_itc_wr_data = 1;
77 
84 #define MHD_itc_activate_(itc, str) \
85  ((write((itc).fd, (const void*)&_MHD_itc_wr_data, 8) > 0) || (EAGAIN == errno))
86 
92 #define MHD_itc_r_fd_(itc) ((itc).fd)
93 
99 #define MHD_itc_w_fd_(itc) ((itc).fd)
100 
105 #define MHD_itc_clear_(itc) \
106  do { uint64_t __b; int __r; \
107  __r = read((itc).fd, &__b, sizeof(__b)); \
108  (void)__r; } while(0)
109 
115 #define MHD_itc_destroy_(itc) ((0 != close ((itc).fd)) || (EBADF != errno))
116 
126 #define MHD_ITC_IS_VALID_(itc) (-1 != ((itc).fd))
127 
132 #define MHD_itc_set_invalid_(itc) ((itc).fd = -1)
133 
134 
135 #elif defined(_MHD_ITC_PIPE)
136 
137 /* **************** Standard UNIX ITC implementation by pipe ********** */
138 
139 #if defined(HAVE_PIPE2_FUNC) && defined(HAVE_FCNTL_H)
140 # include <fcntl.h> /* for O_CLOEXEC, O_NONBLOCK */
141 #endif /* HAVE_PIPE2_FUNC && HAVE_FCNTL_H */
142 #ifdef HAVE_UNISTD_H
143 #include <unistd.h> /* for read(), write(), errno */
144 #endif /* HAVE_UNISTD_H */
145 #ifdef HAVE_STRING_H
146 #include <string.h> /* for strerror() */
147 #endif
148 
149 
155 #ifdef HAVE_PIPE2_FUNC
156 # define MHD_itc_init_(itc) (!pipe2((itc).fd, O_CLOEXEC | O_NONBLOCK))
157 #else /* ! HAVE_PIPE2_FUNC */
158 # define MHD_itc_init_(itc) \
159  ( (!pipe((itc).fd)) ? \
160  (MHD_itc_nonblocking_((itc)) ? \
161  (!0) : \
162  (MHD_itc_destroy_((itc)), 0) ) \
163  : (0) )
164 #endif /* ! HAVE_PIPE2_FUNC */
165 
169 #define MHD_itc_last_strerror_() strerror(errno)
170 
177 #define MHD_itc_activate_(itc, str) \
178  ((write((itc).fd[1], (const void*)(str), 1) > 0) || (EAGAIN == errno))
179 
180 
186 #define MHD_itc_r_fd_(itc) ((itc).fd[0])
187 
193 #define MHD_itc_w_fd_(itc) ((itc).fd[1])
194 
199 #define MHD_itc_clear_(itc) do \
200  { long __b; \
201  while(0 < read((itc).fd[0], &__b, sizeof(__b))) \
202  {} } while(0)
203 
209 #define MHD_itc_destroy_(itc) \
210  ( (0 == close ((itc).fd[0])) ? \
211  (0 == close ((itc).fd[1])) : \
212  ((close ((itc).fd[1])), 0) )
213 
223 #define MHD_ITC_IS_VALID_(itc) (-1 != (itc).fd[0])
224 
229 #define MHD_itc_set_invalid_(itc) ((itc).fd[0] = (itc).fd[1] = -1)
230 
231 #ifndef HAVE_PIPE2_FUNC
232 
238  int
239  MHD_itc_nonblocking_ (struct MHD_itc_ itc);
240 #endif /* ! HAVE_PIPE2_FUNC */
241 
242 
243 #elif defined(_MHD_ITC_SOCKETPAIR)
244 
245 /* **************** ITC implementation by socket pair ********** */
246 
247 #include "mhd_sockets.h"
248 
249 
255 #ifdef MHD_socket_pair_nblk_
256 # define MHD_itc_init_(itc) MHD_socket_pair_nblk_((itc).sk)
257 #else /* ! MHD_socket_pair_nblk_ */
258 # define MHD_itc_init_(itc) \
259  (MHD_socket_pair_((itc).sk) ? \
260  (MHD_itc_nonblocking_((itc)) ? \
261  (!0) : \
262  (MHD_itc_destroy_((itc)), 0) ) \
263  : (0))
264 #endif /* ! MHD_socket_pair_nblk_ */
265 
269 #define MHD_itc_last_strerror_() MHD_socket_last_strerr_()
270 
277 #define MHD_itc_activate_(itc, str) \
278  ((MHD_send_((itc).sk[1], (str), 1) > 0) || \
279  (MHD_SCKT_ERR_IS_EAGAIN_(MHD_socket_get_error_())))
280 
286 #define MHD_itc_r_fd_(itc) ((itc).sk[0])
287 
293 #define MHD_itc_w_fd_(itc) ((itc).sk[1])
294 
299 #define MHD_itc_clear_(itc) do \
300  { long __b; \
301  while(0 < recv((itc).sk[0], \
302  (char*)&__b, \
303  sizeof(__b), 0)) \
304  {} } while(0)
305 
311 #define MHD_itc_destroy_(itc) \
312  ( MHD_socket_close_((itc).sk[0]) ? \
313  MHD_socket_close_((itc).sk[1]) : \
314  ((void)MHD_socket_close_((itc).sk[1]), 0) )
315 
316 
326 #define MHD_ITC_IS_VALID_(itc) (MHD_INVALID_SOCKET != (itc).sk[0])
327 
332 #define MHD_itc_set_invalid_(itc) ((itc).sk[0] = (itc).sk[1] = MHD_INVALID_SOCKET)
333 
334 #ifndef MHD_socket_pair_nblk_
335 # define MHD_itc_nonblocking_(pip) (MHD_socket_nonblocking_((pip).sk[0]) && MHD_socket_nonblocking_((pip).sk[1]))
336 #endif /* ! MHD_socket_pair_nblk_ */
337 
338 #endif /* _MHD_ITC_SOCKETPAIR */
339 
345 #define MHD_itc_destroy_chk_(itc) do { \
346  if (!MHD_itc_destroy_(itc)) \
347  MHD_PANIC(_("Failed to destroy ITC.\n")); \
348  } while(0)
349 
359 #define MHD_ITC_IS_INVALID_(itc) (! MHD_ITC_IS_VALID_(itc))
360 
361 #endif /* MHD_ITC_H */
Types for platform-independent inter-thread communication.