aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_itc.h
blob: 0f10a2bc33efada66193df131d24665d92565414 (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
/*
  This file is part of libmicrohttpd
  Copyright (C) 2016 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, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
 * @file microhttpd/mhd_itc.h
 * @brief  Header for platform-independent inter-thread communication
 * @author Karlson2k (Evgeny Grin)
 * @author Christian Grothoff
 *
 * Provides basic abstraction for inter-thread communication.
 * Any functions can be implemented as macro on some platforms
 * unless explicitly marked otherwise.
 * Any function argument can be skipped in macro, so avoid
 * variable modification in function parameters.
 */
#ifndef MHD_ITC_H
#define MHD_ITC_H 1
#include "mhd_options.h"

/* Force socketpair on native W32 */
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(_MHD_ITC_SOCKETPAIR)
#error _MHD_ITC_SOCKETPAIR is not defined on naitive W32 platform
#endif /* _WIN32 && !__CYGWIN__ && !_MHD_ITC_SOCKETPAIR */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <fcntl.h>

#if defined(_MHD_ITC_EVENTFD)
#include <sys/eventfd.h>

/* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */

/**
 * Data type for a MHD ITC.
 */
typedef int MHD_itc_;

/**
 * Initialise ITC by generating eventFD
 * @param itc the itc to initialise
 * @return non-zero if succeeded, zero otherwise
 */
#define MHD_itc_init_(itc) (-1 != ((itc) = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK)))

/***
 * Get description string of last errno for pipe operations.
 */
#define MHD_pipe_last_strerror_() strerror(errno)

/**
 * write data to real pipe
 */
int
MHD_pipe_write_ (MHD_itc_ pip,
                 const void *ptr,
                 size_t sz);

#define MHD_pipe_get_read_fd_(pip) (pip)

#define MHD_pipe_get_write_fd_(pip) (pip)

/**
 * drain data from real pipe
 */
#define MHD_pipe_drain_(pip) do { \
   uint64_t tmp; \
   read (pip, &tmp, sizeof (tmp)); \
 } while (0)

/**
 * Close any FDs of the pipe (non-W32)
 */
#define MHD_pipe_close_(pip) do { \
    if ( (0 != close (pip)) && \
         (EBADF == errno) )             \
      MHD_PANIC (_("close failed"));    \
  } while (0)

/**
 * Check if we have an uninitialized pipe
 */
#define MHD_INVALID_PIPE_(pip)  (-1 == pip)

/**
 * Setup uninitialized @a pip data structure.
 */
#define MHD_make_invalid_pipe_(pip) do { \
    pip = -1;        \
  } while (0)


/**
 * Change itc FD options to be non-blocking.  As we already did this
 * on eventfd creation, this always succeeds.
 *
 * @param fd the FD to manipulate
 * @return non-zero if succeeded, zero otherwise
 */
#define MHD_itc_nonblocking_(pip) (!0)


#elif defined(_MHD_ITC_PIPE)

/* **************** Standard UNIX ITC implementation by pipe ********** */

#  ifdef HAVE_STRING_H
#    include <string.h> /* for strerror() */
#  endif

/**
 * Data type for a MHD ITC.
 */
struct MHD_Itc
{
  int fd[2];
};
typedef struct MHD_Itc MHD_itc_;

/**
 * Initialise ITC by generating pipe
 * @param itc the itc to initialise
 * @return non-zero if succeeded, zero otherwise
 */
#define MHD_itc_init_(itc) (!pipe((itc).fd))

/***
 * Get description string of last errno for pipe operations.
 */
#define MHD_pipe_last_strerror_() strerror(errno)

/**
 * write data to real pipe
 */
#define MHD_pipe_write_(pip, ptr, sz) write((pip).fd[1], (const void*)(ptr), (sz))


#define MHD_pipe_get_read_fd_(pip) ((pip).fd[0])

#define MHD_pipe_get_write_fd_(pip) ((pip).fd[1])

/**
 * drain data from real pipe
 */
#define MHD_pipe_drain_(pip) do { \
   long tmp; \
   while (0 < read((pip).fd[0], (void*)&tmp, sizeof (tmp))) ; \
 } while (0)

/**
 * Close any FDs of the pipe (non-W32)
 */
#define MHD_pipe_close_(pip) do { \
    if ( (0 != close ((pip).fd[0])) && \
         (EBADF == errno) )             \
      MHD_PANIC (_("close failed"));    \
    if ( (0 != close ((pip).fd[1])) && \
         (EBADF == errno) )             \
      MHD_PANIC (_("close failed"));    \
  } while (0)

/**
 * Check if we have an uninitialized pipe
 */
#define MHD_INVALID_PIPE_(pip)  (-1 == (pip).fd[0])

/**
 * Setup uninitialized @a pip data structure.
 */
#define MHD_make_invalid_pipe_(pip) do { \
    (pip).fd[0] = (pip).fd[1] = -1; \
  } while (0)

/**
 * Change itc FD options to be non-blocking.
 *
 * @param fd the FD to manipulate
 * @return non-zero if succeeded, zero otherwise
 */
int
MHD_itc_nonblocking_ (MHD_itc_ itc);


#elif defined(_MHD_ITC_SOCKETPAIR)

/* **************** ITC implementation by socket pair ********** */

#include "mhd_sockets.h"

/**
 * Data type for a MHD pipe.
 */
struct MHD_Itc
{
  MHD_socket fd[2];
};
typedef struct MHD_Itc MHD_itc_;

/**
 * Initialise ITC by generating socketpair
 * @param itc the itc to initialise
 * @return non-zero if succeeded, zero otherwise
 */
#define MHD_itc_init_(itc) MHD_socket_pair_((itc).fd)

/**
 * Get description string of last pipe error
 */
#define MHD_pipe_last_strerror_() MHD_socket_last_strerr_()

/**
 * Write data to emulated pipe
 */
#define MHD_pipe_write_(pip, ptr, sz) send((pip.fd[1]), (const char*)(ptr), (sz), 0)

#define MHD_pipe_get_read_fd_(pip) (pip.fd[0])

#define MHD_pipe_get_write_fd_(pip) (pip.fd[1])

/**
 * Drain data from emulated pipe
 */
#define MHD_pipe_drain_(pip) do { long tmp; while (0 < recv((pip.fd[0]), (void*)&tmp, sizeof (tmp), 0)) ; } while (0)


/**
 * Close emulated pipe FDs
 */
#define MHD_pipe_close_(pip) do { \
   MHD_socket_close_chk_ ((pip).fd[0]); \
   MHD_socket_close_chk_ ((pip).fd[1]); \
} while (0)

/**
 * Check for uninitialized pipe @a pip
 */
#define MHD_INVALID_PIPE_(pip)  (MHD_INVALID_SOCKET == pip.fd[0])

/**
 * Setup uninitialized @a pip data structure.
 */
#define MHD_make_invalid_pipe_(pip) do { \
    pip.fd[0] = pip.fd[1] = MHD_INVALID_SOCKET; \
  } while (0)


#define MHD_itc_nonblocking_(pip) (MHD_socket_nonblocking_((pip.fd[0])) && MHD_socket_nonblocking_((pip.fd[1])))

#endif /* _MHD_ITC_SOCKETPAIR */

#endif /* MHD_ITC_H */