task.h revision 1.1.1.1 1 1.1 christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 1.1 christos *
3 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy
4 1.1 christos * of this software and associated documentation files (the "Software"), to
5 1.1 christos * deal in the Software without restriction, including without limitation the
6 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is
8 1.1 christos * furnished to do so, subject to the following conditions:
9 1.1 christos *
10 1.1 christos * The above copyright notice and this permission notice shall be included in
11 1.1 christos * all copies or substantial portions of the Software.
12 1.1 christos *
13 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 1.1 christos * IN THE SOFTWARE.
20 1.1 christos */
21 1.1 christos
22 1.1 christos #ifndef TASK_H_
23 1.1 christos #define TASK_H_
24 1.1 christos
25 1.1 christos #include "uv.h"
26 1.1 christos
27 1.1 christos #include <stdio.h>
28 1.1 christos #include <stddef.h>
29 1.1 christos #include <stdlib.h>
30 1.1 christos #include <string.h>
31 1.1 christos #include <inttypes.h>
32 1.1 christos
33 1.1 christos #if defined(_MSC_VER) && _MSC_VER < 1600
34 1.1 christos # include "uv/stdint-msvc2008.h"
35 1.1 christos #else
36 1.1 christos # include <stdint.h>
37 1.1 christos #endif
38 1.1 christos
39 1.1 christos #if !defined(_WIN32)
40 1.1 christos # include <sys/time.h>
41 1.1 christos # include <sys/resource.h> /* setrlimit() */
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos #ifdef __clang__
45 1.1 christos # pragma clang diagnostic ignored "-Wvariadic-macros"
46 1.1 christos # pragma clang diagnostic ignored "-Wc99-extensions"
47 1.1 christos #endif
48 1.1 christos
49 1.1 christos #ifdef __GNUC__
50 1.1 christos # pragma GCC diagnostic ignored "-Wvariadic-macros"
51 1.1 christos #endif
52 1.1 christos
53 1.1 christos #define TEST_PORT 9123
54 1.1 christos #define TEST_PORT_2 9124
55 1.1 christos
56 1.1 christos #ifdef _WIN32
57 1.1 christos # define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
58 1.1 christos # define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
59 1.1 christos # define TEST_PIPENAME_3 "\\\\?\\pipe\\uv-test3"
60 1.1 christos #else
61 1.1 christos # define TEST_PIPENAME "/tmp/uv-test-sock"
62 1.1 christos # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
63 1.1 christos # define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
64 1.1 christos #endif
65 1.1 christos
66 1.1 christos #ifdef _WIN32
67 1.1 christos # include <io.h>
68 1.1 christos # ifndef S_IRUSR
69 1.1 christos # define S_IRUSR _S_IREAD
70 1.1 christos # endif
71 1.1 christos # ifndef S_IWUSR
72 1.1 christos # define S_IWUSR _S_IWRITE
73 1.1 christos # endif
74 1.1 christos #endif
75 1.1 christos
76 1.1 christos #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77 1.1 christos
78 1.1 christos #define container_of(ptr, type, member) \
79 1.1 christos ((type *) ((char *) (ptr) - offsetof(type, member)))
80 1.1 christos
81 1.1 christos typedef enum {
82 1.1 christos TCP = 0,
83 1.1 christos UDP,
84 1.1 christos PIPE
85 1.1 christos } stream_type;
86 1.1 christos
87 1.1 christos /* Die with fatal error. */
88 1.1 christos #define FATAL(msg) \
89 1.1 christos do { \
90 1.1 christos fprintf(stderr, \
91 1.1 christos "Fatal error in %s on line %d: %s\n", \
92 1.1 christos __FILE__, \
93 1.1 christos __LINE__, \
94 1.1 christos msg); \
95 1.1 christos fflush(stderr); \
96 1.1 christos abort(); \
97 1.1 christos } while (0)
98 1.1 christos
99 1.1 christos /* Have our own assert, so we are sure it does not get optimized away in
100 1.1 christos * a release build.
101 1.1 christos */
102 1.1 christos #define ASSERT(expr) \
103 1.1 christos do { \
104 1.1 christos if (!(expr)) { \
105 1.1 christos fprintf(stderr, \
106 1.1 christos "Assertion failed in %s on line %d: %s\n", \
107 1.1 christos __FILE__, \
108 1.1 christos __LINE__, \
109 1.1 christos #expr); \
110 1.1 christos abort(); \
111 1.1 christos } \
112 1.1 christos } while (0)
113 1.1 christos
114 1.1 christos #define ASSERT_BASE(expr, a, operator, b, type, conv) \
115 1.1 christos do { \
116 1.1 christos if (!(expr)) { \
117 1.1 christos fprintf(stderr, \
118 1.1 christos "Assertion failed in %s on line %d: `%s %s %s` " \
119 1.1 christos "(%"conv" %s %"conv")\n", \
120 1.1 christos __FILE__, \
121 1.1 christos __LINE__, \
122 1.1 christos #a, \
123 1.1 christos #operator, \
124 1.1 christos #b, \
125 1.1 christos (type)a, \
126 1.1 christos #operator, \
127 1.1 christos (type)b); \
128 1.1 christos abort(); \
129 1.1 christos } \
130 1.1 christos } while (0)
131 1.1 christos
132 1.1 christos #define ASSERT_BASE_LEN(expr, a, operator, b, conv, len) \
133 1.1 christos do { \
134 1.1 christos if (!(expr)) { \
135 1.1 christos fprintf(stderr, \
136 1.1 christos "Assertion failed in %s on line %d: `%s %s %s` " \
137 1.1 christos "(%.*"#conv" %s %.*"#conv")\n", \
138 1.1 christos __FILE__, \
139 1.1 christos __LINE__, \
140 1.1 christos #a, \
141 1.1 christos #operator, \
142 1.1 christos #b, \
143 1.1 christos (int)len, \
144 1.1 christos a, \
145 1.1 christos #operator, \
146 1.1 christos (int)len, \
147 1.1 christos b); \
148 1.1 christos abort(); \
149 1.1 christos } \
150 1.1 christos } while (0)
151 1.1 christos
152 1.1 christos #define ASSERT_BASE_HEX(expr, a, operator, b, size) \
153 1.1 christos do { \
154 1.1 christos if (!(expr)) { \
155 1.1 christos int i; \
156 1.1 christos unsigned char* a_ = (unsigned char*)a; \
157 1.1 christos unsigned char* b_ = (unsigned char*)b; \
158 1.1 christos fprintf(stderr, \
159 1.1 christos "Assertion failed in %s on line %d: `%s %s %s` (", \
160 1.1 christos __FILE__, \
161 1.1 christos __LINE__, \
162 1.1 christos #a, \
163 1.1 christos #operator, \
164 1.1 christos #b); \
165 1.1 christos for (i = 0; i < size; ++i) { \
166 1.1 christos if (i > 0) fprintf(stderr, ":"); \
167 1.1 christos fprintf(stderr, "%02X", a_[i]); \
168 1.1 christos } \
169 1.1 christos fprintf(stderr, " %s ", #operator); \
170 1.1 christos for (i = 0; i < size; ++i) { \
171 1.1 christos if (i > 0) fprintf(stderr, ":"); \
172 1.1 christos fprintf(stderr, "%02X", b_[i]); \
173 1.1 christos } \
174 1.1 christos fprintf(stderr, ")\n"); \
175 1.1 christos abort(); \
176 1.1 christos } \
177 1.1 christos } while (0)
178 1.1 christos
179 1.1 christos #define ASSERT_INT_BASE(a, operator, b, type, conv) \
180 1.1 christos ASSERT_BASE(a operator b, a, operator, b, type, conv)
181 1.1 christos
182 1.1 christos #define ASSERT_EQ(a, b) ASSERT_INT_BASE(a, ==, b, int64_t, PRId64)
183 1.1 christos #define ASSERT_GE(a, b) ASSERT_INT_BASE(a, >=, b, int64_t, PRId64)
184 1.1 christos #define ASSERT_GT(a, b) ASSERT_INT_BASE(a, >, b, int64_t, PRId64)
185 1.1 christos #define ASSERT_LE(a, b) ASSERT_INT_BASE(a, <=, b, int64_t, PRId64)
186 1.1 christos #define ASSERT_LT(a, b) ASSERT_INT_BASE(a, <, b, int64_t, PRId64)
187 1.1 christos #define ASSERT_NE(a, b) ASSERT_INT_BASE(a, !=, b, int64_t, PRId64)
188 1.1 christos
189 1.1 christos #define ASSERT_UINT64_EQ(a, b) ASSERT_INT_BASE(a, ==, b, uint64_t, PRIu64)
190 1.1 christos #define ASSERT_UINT64_GE(a, b) ASSERT_INT_BASE(a, >=, b, uint64_t, PRIu64)
191 1.1 christos #define ASSERT_UINT64_GT(a, b) ASSERT_INT_BASE(a, >, b, uint64_t, PRIu64)
192 1.1 christos #define ASSERT_UINT64_LE(a, b) ASSERT_INT_BASE(a, <=, b, uint64_t, PRIu64)
193 1.1 christos #define ASSERT_UINT64_LT(a, b) ASSERT_INT_BASE(a, <, b, uint64_t, PRIu64)
194 1.1 christos #define ASSERT_UINT64_NE(a, b) ASSERT_INT_BASE(a, !=, b, uint64_t, PRIu64)
195 1.1 christos
196 1.1 christos #define ASSERT_STR_EQ(a, b) \
197 1.1 christos ASSERT_BASE(strcmp(a, b) == 0, a, ==, b, char*, "s")
198 1.1 christos
199 1.1 christos #define ASSERT_STR_NE(a, b) \
200 1.1 christos ASSERT_BASE(strcmp(a, b) != 0, a, !=, b, char*, "s")
201 1.1 christos
202 1.1 christos #define ASSERT_MEM_EQ(a, b, size) \
203 1.1 christos ASSERT_BASE_LEN(memcmp(a, b, size) == 0, a, ==, b, s, size)
204 1.1 christos
205 1.1 christos #define ASSERT_MEM_NE(a, b, size) \
206 1.1 christos ASSERT_BASE_LEN(memcmp(a, b, size) != 0, a, !=, b, s, size)
207 1.1 christos
208 1.1 christos #define ASSERT_MEM_HEX_EQ(a, b, size) \
209 1.1 christos ASSERT_BASE_HEX(memcmp(a, b, size) == 0, a, ==, b, size)
210 1.1 christos
211 1.1 christos #define ASSERT_MEM_HEX_NE(a, b, size) \
212 1.1 christos ASSERT_BASE_HEX(memcmp(a, b, size) != 0, a, !=, b, size)
213 1.1 christos
214 1.1 christos #define ASSERT_NULL(a) \
215 1.1 christos ASSERT_BASE(a == NULL, a, ==, NULL, void*, "p")
216 1.1 christos
217 1.1 christos #define ASSERT_NOT_NULL(a) \
218 1.1 christos ASSERT_BASE(a != NULL, a, !=, NULL, void*, "p")
219 1.1 christos
220 1.1 christos #define ASSERT_PTR_EQ(a, b) \
221 1.1 christos ASSERT_BASE((void*)a == (void*)b, a, ==, b, void*, "p")
222 1.1 christos
223 1.1 christos #define ASSERT_PTR_NE(a, b) \
224 1.1 christos ASSERT_BASE((void*)a != (void*)b, a, !=, b, void*, "p")
225 1.1 christos
226 1.1 christos /* This macro cleans up the main loop. This is used to avoid valgrind
227 1.1 christos * warnings about memory being "leaked" by the main event loop.
228 1.1 christos */
229 1.1 christos #define MAKE_VALGRIND_HAPPY() \
230 1.1 christos do { \
231 1.1 christos close_loop(uv_default_loop()); \
232 1.1 christos ASSERT(0 == uv_loop_close(uv_default_loop())); \
233 1.1 christos uv_library_shutdown(); \
234 1.1 christos } while (0)
235 1.1 christos
236 1.1 christos /* Just sugar for wrapping the main() for a task or helper. */
237 1.1 christos #define TEST_IMPL(name) \
238 1.1 christos int run_test_##name(void); \
239 1.1 christos int run_test_##name(void)
240 1.1 christos
241 1.1 christos #define BENCHMARK_IMPL(name) \
242 1.1 christos int run_benchmark_##name(void); \
243 1.1 christos int run_benchmark_##name(void)
244 1.1 christos
245 1.1 christos #define HELPER_IMPL(name) \
246 1.1 christos int run_helper_##name(void); \
247 1.1 christos int run_helper_##name(void)
248 1.1 christos
249 1.1 christos /* Format big numbers nicely. WARNING: leaks memory. */
250 1.1 christos const char* fmt(double d);
251 1.1 christos
252 1.1 christos /* Reserved test exit codes. */
253 1.1 christos enum test_status {
254 1.1 christos TEST_OK = 0,
255 1.1 christos TEST_SKIP
256 1.1 christos };
257 1.1 christos
258 1.1 christos #define RETURN_OK() \
259 1.1 christos do { \
260 1.1 christos return TEST_OK; \
261 1.1 christos } while (0)
262 1.1 christos
263 1.1 christos #define RETURN_SKIP(explanation) \
264 1.1 christos do { \
265 1.1 christos fprintf(stderr, "%s\n", explanation); \
266 1.1 christos fflush(stderr); \
267 1.1 christos return TEST_SKIP; \
268 1.1 christos } while (0)
269 1.1 christos
270 1.1 christos #if !defined(_WIN32)
271 1.1 christos
272 1.1 christos # define TEST_FILE_LIMIT(num) \
273 1.1 christos do { \
274 1.1 christos struct rlimit lim; \
275 1.1 christos lim.rlim_cur = (num); \
276 1.1 christos lim.rlim_max = lim.rlim_cur; \
277 1.1 christos if (setrlimit(RLIMIT_NOFILE, &lim)) \
278 1.1 christos RETURN_SKIP("File descriptor limit too low."); \
279 1.1 christos } while (0)
280 1.1 christos
281 1.1 christos #else /* defined(_WIN32) */
282 1.1 christos
283 1.1 christos # define TEST_FILE_LIMIT(num) do {} while (0)
284 1.1 christos
285 1.1 christos #endif
286 1.1 christos
287 1.1 christos #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
288 1.1 christos extern int snprintf(char*, size_t, const char*, ...);
289 1.1 christos #endif
290 1.1 christos
291 1.1 christos #if defined(__clang__) || \
292 1.1 christos defined(__GNUC__) || \
293 1.1 christos defined(__INTEL_COMPILER)
294 1.1 christos # define UNUSED __attribute__((unused))
295 1.1 christos #else
296 1.1 christos # define UNUSED
297 1.1 christos #endif
298 1.1 christos
299 1.1 christos #if defined(_WIN32)
300 1.1 christos #define notify_parent_process() ((void) 0)
301 1.1 christos #else
302 1.1 christos extern void notify_parent_process(void);
303 1.1 christos #endif
304 1.1 christos
305 1.1 christos /* Fully close a loop */
306 1.1 christos static void close_walk_cb(uv_handle_t* handle, void* arg) {
307 1.1 christos if (!uv_is_closing(handle))
308 1.1 christos uv_close(handle, NULL);
309 1.1 christos }
310 1.1 christos
311 1.1 christos UNUSED static void close_loop(uv_loop_t* loop) {
312 1.1 christos uv_walk(loop, close_walk_cb, NULL);
313 1.1 christos uv_run(loop, UV_RUN_DEFAULT);
314 1.1 christos }
315 1.1 christos
316 1.1 christos UNUSED static int can_ipv6(void) {
317 1.1 christos uv_interface_address_t* addr;
318 1.1 christos int supported;
319 1.1 christos int count;
320 1.1 christos int i;
321 1.1 christos
322 1.1 christos if (uv_interface_addresses(&addr, &count))
323 1.1 christos return 0; /* Assume no IPv6 support on failure. */
324 1.1 christos
325 1.1 christos supported = 0;
326 1.1 christos for (i = 0; supported == 0 && i < count; i += 1)
327 1.1 christos supported = (AF_INET6 == addr[i].address.address6.sin6_family);
328 1.1 christos
329 1.1 christos uv_free_interface_addresses(addr, count);
330 1.1 christos return supported;
331 1.1 christos }
332 1.1 christos
333 1.1 christos #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
334 1.1 christos # define NO_FS_EVENTS "Filesystem watching not supported on this platform."
335 1.1 christos #endif
336 1.1 christos
337 1.1 christos #if defined(__MSYS__)
338 1.1 christos # define NO_SEND_HANDLE_ON_PIPE \
339 1.1 christos "MSYS2 runtime does not support sending handles on pipes."
340 1.1 christos #elif defined(__CYGWIN__)
341 1.1 christos # define NO_SEND_HANDLE_ON_PIPE \
342 1.1 christos "Cygwin runtime does not support sending handles on pipes."
343 1.1 christos #endif
344 1.1 christos
345 1.1 christos #if defined(__MSYS__)
346 1.1 christos # define NO_SELF_CONNECT \
347 1.1 christos "MSYS2 runtime hangs on listen+connect in same process."
348 1.1 christos #elif defined(__CYGWIN__)
349 1.1 christos # define NO_SELF_CONNECT \
350 1.1 christos "Cygwin runtime hangs on listen+connect in same process."
351 1.1 christos #endif
352 1.1 christos
353 1.1 christos #endif /* TASK_H_ */
354