• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 #include <poll.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <pthread.h>
8 #include <sys/epoll.h>
9 #include <sys/socket.h>
10 #include "../../kselftest_harness.h"
11 
12 struct epoll_mtcontext
13 {
14 	int efd[3];
15 	int sfd[4];
16 	int count;
17 
18 	pthread_t main;
19 	pthread_t waiter;
20 };
21 
signal_handler(int signum)22 static void signal_handler(int signum)
23 {
24 }
25 
kill_timeout(struct epoll_mtcontext * ctx)26 static void kill_timeout(struct epoll_mtcontext *ctx)
27 {
28 	usleep(1000000);
29 	pthread_kill(ctx->main, SIGUSR1);
30 	pthread_kill(ctx->waiter, SIGUSR1);
31 }
32 
waiter_entry1a(void * data)33 static void *waiter_entry1a(void *data)
34 {
35 	struct epoll_event e;
36 	struct epoll_mtcontext *ctx = data;
37 
38 	if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
39 		__sync_fetch_and_add(&ctx->count, 1);
40 
41 	return NULL;
42 }
43 
waiter_entry1ap(void * data)44 static void *waiter_entry1ap(void *data)
45 {
46 	struct pollfd pfd;
47 	struct epoll_event e;
48 	struct epoll_mtcontext *ctx = data;
49 
50 	pfd.fd = ctx->efd[0];
51 	pfd.events = POLLIN;
52 	if (poll(&pfd, 1, -1) > 0) {
53 		if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
54 			__sync_fetch_and_add(&ctx->count, 1);
55 	}
56 
57 	return NULL;
58 }
59 
waiter_entry1o(void * data)60 static void *waiter_entry1o(void *data)
61 {
62 	struct epoll_event e;
63 	struct epoll_mtcontext *ctx = data;
64 
65 	if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
66 		__sync_fetch_and_or(&ctx->count, 1);
67 
68 	return NULL;
69 }
70 
waiter_entry1op(void * data)71 static void *waiter_entry1op(void *data)
72 {
73 	struct pollfd pfd;
74 	struct epoll_event e;
75 	struct epoll_mtcontext *ctx = data;
76 
77 	pfd.fd = ctx->efd[0];
78 	pfd.events = POLLIN;
79 	if (poll(&pfd, 1, -1) > 0) {
80 		if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
81 			__sync_fetch_and_or(&ctx->count, 1);
82 	}
83 
84 	return NULL;
85 }
86 
waiter_entry2a(void * data)87 static void *waiter_entry2a(void *data)
88 {
89 	struct epoll_event events[2];
90 	struct epoll_mtcontext *ctx = data;
91 
92 	if (epoll_wait(ctx->efd[0], events, 2, -1) > 0)
93 		__sync_fetch_and_add(&ctx->count, 1);
94 
95 	return NULL;
96 }
97 
waiter_entry2ap(void * data)98 static void *waiter_entry2ap(void *data)
99 {
100 	struct pollfd pfd;
101 	struct epoll_event events[2];
102 	struct epoll_mtcontext *ctx = data;
103 
104 	pfd.fd = ctx->efd[0];
105 	pfd.events = POLLIN;
106 	if (poll(&pfd, 1, -1) > 0) {
107 		if (epoll_wait(ctx->efd[0], events, 2, 0) > 0)
108 			__sync_fetch_and_add(&ctx->count, 1);
109 	}
110 
111 	return NULL;
112 }
113 
emitter_entry1(void * data)114 static void *emitter_entry1(void *data)
115 {
116 	struct epoll_mtcontext *ctx = data;
117 
118 	usleep(100000);
119 	write(ctx->sfd[1], "w", 1);
120 
121 	kill_timeout(ctx);
122 
123 	return NULL;
124 }
125 
emitter_entry2(void * data)126 static void *emitter_entry2(void *data)
127 {
128 	struct epoll_mtcontext *ctx = data;
129 
130 	usleep(100000);
131 	write(ctx->sfd[1], "w", 1);
132 	write(ctx->sfd[3], "w", 1);
133 
134 	kill_timeout(ctx);
135 
136 	return NULL;
137 }
138 
139 /*
140  *          t0
141  *           | (ew)
142  *          e0
143  *           | (lt)
144  *          s0
145  */
TEST(epoll1)146 TEST(epoll1)
147 {
148 	int efd;
149 	int sfd[2];
150 	struct epoll_event e;
151 
152 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
153 
154 	efd = epoll_create(1);
155 	ASSERT_GE(efd, 0);
156 
157 	e.events = EPOLLIN;
158 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
159 
160 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
161 
162 	EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
163 	EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
164 
165 	close(efd);
166 	close(sfd[0]);
167 	close(sfd[1]);
168 }
169 
170 /*
171  *          t0
172  *           | (ew)
173  *          e0
174  *           | (et)
175  *          s0
176  */
TEST(epoll2)177 TEST(epoll2)
178 {
179 	int efd;
180 	int sfd[2];
181 	struct epoll_event e;
182 
183 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
184 
185 	efd = epoll_create(1);
186 	ASSERT_GE(efd, 0);
187 
188 	e.events = EPOLLIN | EPOLLET;
189 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
190 
191 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
192 
193 	EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
194 	EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 0);
195 
196 	close(efd);
197 	close(sfd[0]);
198 	close(sfd[1]);
199 }
200 
201 /*
202  *           t0
203  *            | (ew)
204  *           e0
205  *     (lt) /  \ (lt)
206  *        s0    s2
207  */
TEST(epoll3)208 TEST(epoll3)
209 {
210 	int efd;
211 	int sfd[4];
212 	struct epoll_event events[2];
213 
214 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
215 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
216 
217 	efd = epoll_create(1);
218 	ASSERT_GE(efd, 0);
219 
220 	events[0].events = EPOLLIN;
221 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
222 
223 	events[0].events = EPOLLIN;
224 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
225 
226 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
227 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
228 
229 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
230 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
231 
232 	close(efd);
233 	close(sfd[0]);
234 	close(sfd[1]);
235 	close(sfd[2]);
236 	close(sfd[3]);
237 }
238 
239 /*
240  *           t0
241  *            | (ew)
242  *           e0
243  *     (et) /  \ (et)
244  *        s0    s2
245  */
TEST(epoll4)246 TEST(epoll4)
247 {
248 	int efd;
249 	int sfd[4];
250 	struct epoll_event events[2];
251 
252 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
253 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
254 
255 	efd = epoll_create(1);
256 	ASSERT_GE(efd, 0);
257 
258 	events[0].events = EPOLLIN | EPOLLET;
259 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
260 
261 	events[0].events = EPOLLIN | EPOLLET;
262 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
263 
264 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
265 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
266 
267 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
268 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
269 
270 	close(efd);
271 	close(sfd[0]);
272 	close(sfd[1]);
273 	close(sfd[2]);
274 	close(sfd[3]);
275 }
276 
277 /*
278  *          t0
279  *           | (p)
280  *          e0
281  *           | (lt)
282  *          s0
283  */
TEST(epoll5)284 TEST(epoll5)
285 {
286 	int efd;
287 	int sfd[2];
288 	struct pollfd pfd;
289 	struct epoll_event e;
290 
291 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
292 
293 	efd = epoll_create(1);
294 	ASSERT_GE(efd, 0);
295 
296 	e.events = EPOLLIN;
297 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
298 
299 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
300 
301 	pfd.fd = efd;
302 	pfd.events = POLLIN;
303 	ASSERT_EQ(poll(&pfd, 1, 0), 1);
304 	ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
305 
306 	pfd.fd = efd;
307 	pfd.events = POLLIN;
308 	ASSERT_EQ(poll(&pfd, 1, 0), 1);
309 	ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
310 
311 	close(efd);
312 	close(sfd[0]);
313 	close(sfd[1]);
314 }
315 
316 /*
317  *          t0
318  *           | (p)
319  *          e0
320  *           | (et)
321  *          s0
322  */
TEST(epoll6)323 TEST(epoll6)
324 {
325 	int efd;
326 	int sfd[2];
327 	struct pollfd pfd;
328 	struct epoll_event e;
329 
330 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
331 
332 	efd = epoll_create(1);
333 	ASSERT_GE(efd, 0);
334 
335 	e.events = EPOLLIN | EPOLLET;
336 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
337 
338 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
339 
340 	pfd.fd = efd;
341 	pfd.events = POLLIN;
342 	ASSERT_EQ(poll(&pfd, 1, 0), 1);
343 	ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
344 
345 	pfd.fd = efd;
346 	pfd.events = POLLIN;
347 	ASSERT_EQ(poll(&pfd, 1, 0), 0);
348 	ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 0);
349 
350 	close(efd);
351 	close(sfd[0]);
352 	close(sfd[1]);
353 }
354 
355 /*
356  *           t0
357  *            | (p)
358  *           e0
359  *     (lt) /  \ (lt)
360  *        s0    s2
361  */
362 
TEST(epoll7)363 TEST(epoll7)
364 {
365 	int efd;
366 	int sfd[4];
367 	struct pollfd pfd;
368 	struct epoll_event events[2];
369 
370 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
371 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
372 
373 	efd = epoll_create(1);
374 	ASSERT_GE(efd, 0);
375 
376 	events[0].events = EPOLLIN;
377 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
378 
379 	events[0].events = EPOLLIN;
380 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
381 
382 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
383 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
384 
385 	pfd.fd = efd;
386 	pfd.events = POLLIN;
387 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
388 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
389 
390 	pfd.fd = efd;
391 	pfd.events = POLLIN;
392 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
393 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
394 
395 	close(efd);
396 	close(sfd[0]);
397 	close(sfd[1]);
398 	close(sfd[2]);
399 	close(sfd[3]);
400 }
401 
402 /*
403  *           t0
404  *            | (p)
405  *           e0
406  *     (et) /  \ (et)
407  *        s0    s2
408  */
TEST(epoll8)409 TEST(epoll8)
410 {
411 	int efd;
412 	int sfd[4];
413 	struct pollfd pfd;
414 	struct epoll_event events[2];
415 
416 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
417 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
418 
419 	efd = epoll_create(1);
420 	ASSERT_GE(efd, 0);
421 
422 	events[0].events = EPOLLIN | EPOLLET;
423 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
424 
425 	events[0].events = EPOLLIN | EPOLLET;
426 	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
427 
428 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
429 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
430 
431 	pfd.fd = efd;
432 	pfd.events = POLLIN;
433 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
434 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
435 
436 	pfd.fd = efd;
437 	pfd.events = POLLIN;
438 	EXPECT_EQ(poll(&pfd, 1, 0), 0);
439 	EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
440 
441 	close(efd);
442 	close(sfd[0]);
443 	close(sfd[1]);
444 	close(sfd[2]);
445 	close(sfd[3]);
446 }
447 
448 /*
449  *        t0    t1
450  *     (ew) \  / (ew)
451  *           e0
452  *            | (lt)
453  *           s0
454  */
TEST(epoll9)455 TEST(epoll9)
456 {
457 	pthread_t emitter;
458 	struct epoll_event e;
459 	struct epoll_mtcontext ctx = { 0 };
460 
461 	signal(SIGUSR1, signal_handler);
462 
463 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
464 
465 	ctx.efd[0] = epoll_create(1);
466 	ASSERT_GE(ctx.efd[0], 0);
467 
468 	e.events = EPOLLIN;
469 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
470 
471 	ctx.main = pthread_self();
472 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
473 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
474 
475 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
476 		__sync_fetch_and_add(&ctx.count, 1);
477 
478 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
479 	EXPECT_EQ(ctx.count, 2);
480 
481 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
482 		pthread_kill(emitter, SIGUSR1);
483 		pthread_join(emitter, NULL);
484 	}
485 
486 	close(ctx.efd[0]);
487 	close(ctx.sfd[0]);
488 	close(ctx.sfd[1]);
489 }
490 
491 /*
492  *        t0    t1
493  *     (ew) \  / (ew)
494  *           e0
495  *            | (et)
496  *           s0
497  */
TEST(epoll10)498 TEST(epoll10)
499 {
500 	pthread_t emitter;
501 	struct epoll_event e;
502 	struct epoll_mtcontext ctx = { 0 };
503 
504 	signal(SIGUSR1, signal_handler);
505 
506 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
507 
508 	ctx.efd[0] = epoll_create(1);
509 	ASSERT_GE(ctx.efd[0], 0);
510 
511 	e.events = EPOLLIN | EPOLLET;
512 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
513 
514 	ctx.main = pthread_self();
515 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
516 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
517 
518 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
519 		__sync_fetch_and_add(&ctx.count, 1);
520 
521 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
522 	EXPECT_EQ(ctx.count, 1);
523 
524 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
525 		pthread_kill(emitter, SIGUSR1);
526 		pthread_join(emitter, NULL);
527 	}
528 
529 	close(ctx.efd[0]);
530 	close(ctx.sfd[0]);
531 	close(ctx.sfd[1]);
532 }
533 
534 /*
535  *        t0    t1
536  *     (ew) \  / (ew)
537  *           e0
538  *     (lt) /  \ (lt)
539  *        s0    s2
540  */
TEST(epoll11)541 TEST(epoll11)
542 {
543 	pthread_t emitter;
544 	struct epoll_event events[2];
545 	struct epoll_mtcontext ctx = { 0 };
546 
547 	signal(SIGUSR1, signal_handler);
548 
549 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
550 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
551 
552 	ctx.efd[0] = epoll_create(1);
553 	ASSERT_GE(ctx.efd[0], 0);
554 
555 	events[0].events = EPOLLIN;
556 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
557 
558 	events[0].events = EPOLLIN;
559 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
560 
561 	ctx.main = pthread_self();
562 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2a, &ctx), 0);
563 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
564 
565 	if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
566 		__sync_fetch_and_add(&ctx.count, 1);
567 
568 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
569 	EXPECT_EQ(ctx.count, 2);
570 
571 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
572 		pthread_kill(emitter, SIGUSR1);
573 		pthread_join(emitter, NULL);
574 	}
575 
576 	close(ctx.efd[0]);
577 	close(ctx.sfd[0]);
578 	close(ctx.sfd[1]);
579 	close(ctx.sfd[2]);
580 	close(ctx.sfd[3]);
581 }
582 
583 /*
584  *        t0    t1
585  *     (ew) \  / (ew)
586  *           e0
587  *     (et) /  \ (et)
588  *        s0    s2
589  */
TEST(epoll12)590 TEST(epoll12)
591 {
592 	pthread_t emitter;
593 	struct epoll_event events[2];
594 	struct epoll_mtcontext ctx = { 0 };
595 
596 	signal(SIGUSR1, signal_handler);
597 
598 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
599 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
600 
601 	ctx.efd[0] = epoll_create(1);
602 	ASSERT_GE(ctx.efd[0], 0);
603 
604 	events[0].events = EPOLLIN | EPOLLET;
605 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
606 
607 	events[0].events = EPOLLIN | EPOLLET;
608 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
609 
610 	ctx.main = pthread_self();
611 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
612 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
613 
614 	if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
615 		__sync_fetch_and_add(&ctx.count, 1);
616 
617 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
618 	EXPECT_EQ(ctx.count, 2);
619 
620 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
621 		pthread_kill(emitter, SIGUSR1);
622 		pthread_join(emitter, NULL);
623 	}
624 
625 	close(ctx.efd[0]);
626 	close(ctx.sfd[0]);
627 	close(ctx.sfd[1]);
628 	close(ctx.sfd[2]);
629 	close(ctx.sfd[3]);
630 }
631 
632 /*
633  *        t0    t1
634  *     (ew) \  / (p)
635  *           e0
636  *            | (lt)
637  *           s0
638  */
TEST(epoll13)639 TEST(epoll13)
640 {
641 	pthread_t emitter;
642 	struct epoll_event e;
643 	struct epoll_mtcontext ctx = { 0 };
644 
645 	signal(SIGUSR1, signal_handler);
646 
647 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
648 
649 	ctx.efd[0] = epoll_create(1);
650 	ASSERT_GE(ctx.efd[0], 0);
651 
652 	e.events = EPOLLIN;
653 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
654 
655 	ctx.main = pthread_self();
656 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
657 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
658 
659 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
660 		__sync_fetch_and_add(&ctx.count, 1);
661 
662 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
663 	EXPECT_EQ(ctx.count, 2);
664 
665 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
666 		pthread_kill(emitter, SIGUSR1);
667 		pthread_join(emitter, NULL);
668 	}
669 
670 	close(ctx.efd[0]);
671 	close(ctx.sfd[0]);
672 	close(ctx.sfd[1]);
673 }
674 
675 /*
676  *        t0    t1
677  *     (ew) \  / (p)
678  *           e0
679  *            | (et)
680  *           s0
681  */
TEST(epoll14)682 TEST(epoll14)
683 {
684 	pthread_t emitter;
685 	struct epoll_event e;
686 	struct epoll_mtcontext ctx = { 0 };
687 
688 	signal(SIGUSR1, signal_handler);
689 
690 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
691 
692 	ctx.efd[0] = epoll_create(1);
693 	ASSERT_GE(ctx.efd[0], 0);
694 
695 	e.events = EPOLLIN | EPOLLET;
696 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
697 
698 	ctx.main = pthread_self();
699 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
700 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
701 
702 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
703 		__sync_fetch_and_add(&ctx.count, 1);
704 
705 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
706 	EXPECT_EQ(ctx.count, 1);
707 
708 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
709 		pthread_kill(emitter, SIGUSR1);
710 		pthread_join(emitter, NULL);
711 	}
712 
713 	close(ctx.efd[0]);
714 	close(ctx.sfd[0]);
715 	close(ctx.sfd[1]);
716 }
717 
718 /*
719  *        t0    t1
720  *     (ew) \  / (p)
721  *           e0
722  *     (lt) /  \ (lt)
723  *        s0    s2
724  */
TEST(epoll15)725 TEST(epoll15)
726 {
727 	pthread_t emitter;
728 	struct epoll_event events[2];
729 	struct epoll_mtcontext ctx = { 0 };
730 
731 	signal(SIGUSR1, signal_handler);
732 
733 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
734 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
735 
736 	ctx.efd[0] = epoll_create(1);
737 	ASSERT_GE(ctx.efd[0], 0);
738 
739 	events[0].events = EPOLLIN;
740 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
741 
742 	events[0].events = EPOLLIN;
743 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
744 
745 	ctx.main = pthread_self();
746 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2ap, &ctx), 0);
747 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
748 
749 	if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
750 		__sync_fetch_and_add(&ctx.count, 1);
751 
752 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
753 	EXPECT_EQ(ctx.count, 2);
754 
755 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
756 		pthread_kill(emitter, SIGUSR1);
757 		pthread_join(emitter, NULL);
758 	}
759 
760 	close(ctx.efd[0]);
761 	close(ctx.sfd[0]);
762 	close(ctx.sfd[1]);
763 	close(ctx.sfd[2]);
764 	close(ctx.sfd[3]);
765 }
766 
767 /*
768  *        t0    t1
769  *     (ew) \  / (p)
770  *           e0
771  *     (et) /  \ (et)
772  *        s0    s2
773  */
TEST(epoll16)774 TEST(epoll16)
775 {
776 	pthread_t emitter;
777 	struct epoll_event events[2];
778 	struct epoll_mtcontext ctx = { 0 };
779 
780 	signal(SIGUSR1, signal_handler);
781 
782 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
783 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
784 
785 	ctx.efd[0] = epoll_create(1);
786 	ASSERT_GE(ctx.efd[0], 0);
787 
788 	events[0].events = EPOLLIN | EPOLLET;
789 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
790 
791 	events[0].events = EPOLLIN | EPOLLET;
792 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
793 
794 	ctx.main = pthread_self();
795 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
796 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
797 
798 	if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
799 		__sync_fetch_and_add(&ctx.count, 1);
800 
801 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
802 	EXPECT_EQ(ctx.count, 2);
803 
804 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
805 		pthread_kill(emitter, SIGUSR1);
806 		pthread_join(emitter, NULL);
807 	}
808 
809 	close(ctx.efd[0]);
810 	close(ctx.sfd[0]);
811 	close(ctx.sfd[1]);
812 	close(ctx.sfd[2]);
813 	close(ctx.sfd[3]);
814 }
815 
816 /*
817  *          t0
818  *           | (ew)
819  *          e0
820  *           | (lt)
821  *          e1
822  *           | (lt)
823  *          s0
824  */
TEST(epoll17)825 TEST(epoll17)
826 {
827 	int efd[2];
828 	int sfd[2];
829 	struct epoll_event e;
830 
831 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
832 
833 	efd[0] = epoll_create(1);
834 	ASSERT_GE(efd[0], 0);
835 
836 	efd[1] = epoll_create(1);
837 	ASSERT_GE(efd[1], 0);
838 
839 	e.events = EPOLLIN;
840 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
841 
842 	e.events = EPOLLIN;
843 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
844 
845 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
846 
847 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
848 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
849 
850 	close(efd[0]);
851 	close(efd[1]);
852 	close(sfd[0]);
853 	close(sfd[1]);
854 }
855 
856 /*
857  *          t0
858  *           | (ew)
859  *          e0
860  *           | (lt)
861  *          e1
862  *           | (et)
863  *          s0
864  */
TEST(epoll18)865 TEST(epoll18)
866 {
867 	int efd[2];
868 	int sfd[2];
869 	struct epoll_event e;
870 
871 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
872 
873 	efd[0] = epoll_create(1);
874 	ASSERT_GE(efd[0], 0);
875 
876 	efd[1] = epoll_create(1);
877 	ASSERT_GE(efd[1], 0);
878 
879 	e.events = EPOLLIN | EPOLLET;
880 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
881 
882 	e.events = EPOLLIN;
883 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
884 
885 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
886 
887 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
888 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
889 
890 	close(efd[0]);
891 	close(efd[1]);
892 	close(sfd[0]);
893 	close(sfd[1]);
894 }
895 
896 /*
897  *           t0
898  *            | (ew)
899  *           e0
900  *            | (et)
901  *           e1
902  *            | (lt)
903  *           s0
904  */
TEST(epoll19)905 TEST(epoll19)
906 {
907 	int efd[2];
908 	int sfd[2];
909 	struct epoll_event e;
910 
911 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
912 
913 	efd[0] = epoll_create(1);
914 	ASSERT_GE(efd[0], 0);
915 
916 	efd[1] = epoll_create(1);
917 	ASSERT_GE(efd[1], 0);
918 
919 	e.events = EPOLLIN;
920 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
921 
922 	e.events = EPOLLIN | EPOLLET;
923 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
924 
925 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
926 
927 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
928 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
929 
930 	close(efd[0]);
931 	close(efd[1]);
932 	close(sfd[0]);
933 	close(sfd[1]);
934 }
935 
936 /*
937  *           t0
938  *            | (ew)
939  *           e0
940  *            | (et)
941  *           e1
942  *            | (et)
943  *           s0
944  */
TEST(epoll20)945 TEST(epoll20)
946 {
947 	int efd[2];
948 	int sfd[2];
949 	struct epoll_event e;
950 
951 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
952 
953 	efd[0] = epoll_create(1);
954 	ASSERT_GE(efd[0], 0);
955 
956 	efd[1] = epoll_create(1);
957 	ASSERT_GE(efd[1], 0);
958 
959 	e.events = EPOLLIN | EPOLLET;
960 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
961 
962 	e.events = EPOLLIN | EPOLLET;
963 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
964 
965 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
966 
967 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
968 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
969 
970 	close(efd[0]);
971 	close(efd[1]);
972 	close(sfd[0]);
973 	close(sfd[1]);
974 }
975 
976 /*
977  *          t0
978  *           | (p)
979  *          e0
980  *           | (lt)
981  *          e1
982  *           | (lt)
983  *          s0
984  */
TEST(epoll21)985 TEST(epoll21)
986 {
987 	int efd[2];
988 	int sfd[2];
989 	struct pollfd pfd;
990 	struct epoll_event e;
991 
992 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
993 
994 	efd[0] = epoll_create(1);
995 	ASSERT_GE(efd[0], 0);
996 
997 	efd[1] = epoll_create(1);
998 	ASSERT_GE(efd[1], 0);
999 
1000 	e.events = EPOLLIN;
1001 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1002 
1003 	e.events = EPOLLIN;
1004 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1005 
1006 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
1007 
1008 	pfd.fd = efd[0];
1009 	pfd.events = POLLIN;
1010 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1011 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1012 
1013 	pfd.fd = efd[0];
1014 	pfd.events = POLLIN;
1015 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1016 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1017 
1018 	close(efd[0]);
1019 	close(efd[1]);
1020 	close(sfd[0]);
1021 	close(sfd[1]);
1022 }
1023 
1024 /*
1025  *          t0
1026  *           | (p)
1027  *          e0
1028  *           | (lt)
1029  *          e1
1030  *           | (et)
1031  *          s0
1032  */
TEST(epoll22)1033 TEST(epoll22)
1034 {
1035 	int efd[2];
1036 	int sfd[2];
1037 	struct pollfd pfd;
1038 	struct epoll_event e;
1039 
1040 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1041 
1042 	efd[0] = epoll_create(1);
1043 	ASSERT_GE(efd[0], 0);
1044 
1045 	efd[1] = epoll_create(1);
1046 	ASSERT_GE(efd[1], 0);
1047 
1048 	e.events = EPOLLIN | EPOLLET;
1049 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1050 
1051 	e.events = EPOLLIN;
1052 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1053 
1054 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
1055 
1056 	pfd.fd = efd[0];
1057 	pfd.events = POLLIN;
1058 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1059 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1060 
1061 	pfd.fd = efd[0];
1062 	pfd.events = POLLIN;
1063 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1064 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1065 
1066 	close(efd[0]);
1067 	close(efd[1]);
1068 	close(sfd[0]);
1069 	close(sfd[1]);
1070 }
1071 
1072 /*
1073  *          t0
1074  *           | (p)
1075  *          e0
1076  *           | (et)
1077  *          e1
1078  *           | (lt)
1079  *          s0
1080  */
TEST(epoll23)1081 TEST(epoll23)
1082 {
1083 	int efd[2];
1084 	int sfd[2];
1085 	struct pollfd pfd;
1086 	struct epoll_event e;
1087 
1088 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1089 
1090 	efd[0] = epoll_create(1);
1091 	ASSERT_GE(efd[0], 0);
1092 
1093 	efd[1] = epoll_create(1);
1094 	ASSERT_GE(efd[1], 0);
1095 
1096 	e.events = EPOLLIN;
1097 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1098 
1099 	e.events = EPOLLIN | EPOLLET;
1100 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1101 
1102 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
1103 
1104 	pfd.fd = efd[0];
1105 	pfd.events = POLLIN;
1106 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1107 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1108 
1109 	pfd.fd = efd[0];
1110 	pfd.events = POLLIN;
1111 	EXPECT_EQ(poll(&pfd, 1, 0), 0);
1112 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1113 
1114 	close(efd[0]);
1115 	close(efd[1]);
1116 	close(sfd[0]);
1117 	close(sfd[1]);
1118 }
1119 
1120 /*
1121  *          t0
1122  *           | (p)
1123  *          e0
1124  *           | (et)
1125  *          e1
1126  *           | (et)
1127  *          s0
1128  */
TEST(epoll24)1129 TEST(epoll24)
1130 {
1131 	int efd[2];
1132 	int sfd[2];
1133 	struct pollfd pfd;
1134 	struct epoll_event e;
1135 
1136 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1137 
1138 	efd[0] = epoll_create(1);
1139 	ASSERT_GE(efd[0], 0);
1140 
1141 	efd[1] = epoll_create(1);
1142 	ASSERT_GE(efd[1], 0);
1143 
1144 	e.events = EPOLLIN | EPOLLET;
1145 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1146 
1147 	e.events = EPOLLIN | EPOLLET;
1148 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1149 
1150 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
1151 
1152 	pfd.fd = efd[0];
1153 	pfd.events = POLLIN;
1154 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
1155 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1156 
1157 	pfd.fd = efd[0];
1158 	pfd.events = POLLIN;
1159 	EXPECT_EQ(poll(&pfd, 1, 0), 0);
1160 	EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1161 
1162 	close(efd[0]);
1163 	close(efd[1]);
1164 	close(sfd[0]);
1165 	close(sfd[1]);
1166 }
1167 
1168 /*
1169  *        t0    t1
1170  *     (ew) \  / (ew)
1171  *           e0
1172  *            | (lt)
1173  *           e1
1174  *            | (lt)
1175  *           s0
1176  */
TEST(epoll25)1177 TEST(epoll25)
1178 {
1179 	pthread_t emitter;
1180 	struct epoll_event e;
1181 	struct epoll_mtcontext ctx = { 0 };
1182 
1183 	signal(SIGUSR1, signal_handler);
1184 
1185 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1186 
1187 	ctx.efd[0] = epoll_create(1);
1188 	ASSERT_GE(ctx.efd[0], 0);
1189 
1190 	ctx.efd[1] = epoll_create(1);
1191 	ASSERT_GE(ctx.efd[1], 0);
1192 
1193 	e.events = EPOLLIN;
1194 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1195 
1196 	e.events = EPOLLIN;
1197 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1198 
1199 	ctx.main = pthread_self();
1200 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1201 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1202 
1203 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1204 		__sync_fetch_and_add(&ctx.count, 1);
1205 
1206 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1207 	EXPECT_EQ(ctx.count, 2);
1208 
1209 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1210 		pthread_kill(emitter, SIGUSR1);
1211 		pthread_join(emitter, NULL);
1212 	}
1213 
1214 	close(ctx.efd[0]);
1215 	close(ctx.efd[1]);
1216 	close(ctx.sfd[0]);
1217 	close(ctx.sfd[1]);
1218 }
1219 
1220 /*
1221  *        t0    t1
1222  *     (ew) \  / (ew)
1223  *           e0
1224  *            | (lt)
1225  *           e1
1226  *            | (et)
1227  *           s0
1228  */
TEST(epoll26)1229 TEST(epoll26)
1230 {
1231 	pthread_t emitter;
1232 	struct epoll_event e;
1233 	struct epoll_mtcontext ctx = { 0 };
1234 
1235 	signal(SIGUSR1, signal_handler);
1236 
1237 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1238 
1239 	ctx.efd[0] = epoll_create(1);
1240 	ASSERT_GE(ctx.efd[0], 0);
1241 
1242 	ctx.efd[1] = epoll_create(1);
1243 	ASSERT_GE(ctx.efd[1], 0);
1244 
1245 	e.events = EPOLLIN | EPOLLET;
1246 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1247 
1248 	e.events = EPOLLIN;
1249 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1250 
1251 	ctx.main = pthread_self();
1252 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1253 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1254 
1255 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1256 		__sync_fetch_and_add(&ctx.count, 1);
1257 
1258 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1259 	EXPECT_EQ(ctx.count, 2);
1260 
1261 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1262 		pthread_kill(emitter, SIGUSR1);
1263 		pthread_join(emitter, NULL);
1264 	}
1265 
1266 	close(ctx.efd[0]);
1267 	close(ctx.efd[1]);
1268 	close(ctx.sfd[0]);
1269 	close(ctx.sfd[1]);
1270 }
1271 
1272 /*
1273  *        t0    t1
1274  *     (ew) \  / (ew)
1275  *           e0
1276  *            | (et)
1277  *           e1
1278  *            | (lt)
1279  *           s0
1280  */
TEST(epoll27)1281 TEST(epoll27)
1282 {
1283 	pthread_t emitter;
1284 	struct epoll_event e;
1285 	struct epoll_mtcontext ctx = { 0 };
1286 
1287 	signal(SIGUSR1, signal_handler);
1288 
1289 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1290 
1291 	ctx.efd[0] = epoll_create(1);
1292 	ASSERT_GE(ctx.efd[0], 0);
1293 
1294 	ctx.efd[1] = epoll_create(1);
1295 	ASSERT_GE(ctx.efd[1], 0);
1296 
1297 	e.events = EPOLLIN;
1298 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1299 
1300 	e.events = EPOLLIN | EPOLLET;
1301 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1302 
1303 	ctx.main = pthread_self();
1304 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1305 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1306 
1307 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1308 		__sync_fetch_and_add(&ctx.count, 1);
1309 
1310 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1311 	EXPECT_EQ(ctx.count, 1);
1312 
1313 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1314 		pthread_kill(emitter, SIGUSR1);
1315 		pthread_join(emitter, NULL);
1316 	}
1317 
1318 	close(ctx.efd[0]);
1319 	close(ctx.efd[1]);
1320 	close(ctx.sfd[0]);
1321 	close(ctx.sfd[1]);
1322 }
1323 
1324 /*
1325  *        t0    t1
1326  *     (ew) \  / (ew)
1327  *           e0
1328  *            | (et)
1329  *           e1
1330  *            | (et)
1331  *           s0
1332  */
TEST(epoll28)1333 TEST(epoll28)
1334 {
1335 	pthread_t emitter;
1336 	struct epoll_event e;
1337 	struct epoll_mtcontext ctx = { 0 };
1338 
1339 	signal(SIGUSR1, signal_handler);
1340 
1341 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1342 
1343 	ctx.efd[0] = epoll_create(1);
1344 	ASSERT_GE(ctx.efd[0], 0);
1345 
1346 	ctx.efd[1] = epoll_create(1);
1347 	ASSERT_GE(ctx.efd[1], 0);
1348 
1349 	e.events = EPOLLIN | EPOLLET;
1350 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1351 
1352 	e.events = EPOLLIN | EPOLLET;
1353 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1354 
1355 	ctx.main = pthread_self();
1356 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1357 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1358 
1359 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1360 		__sync_fetch_and_add(&ctx.count, 1);
1361 
1362 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1363 	EXPECT_EQ(ctx.count, 1);
1364 
1365 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1366 		pthread_kill(emitter, SIGUSR1);
1367 		pthread_join(emitter, NULL);
1368 	}
1369 
1370 	close(ctx.efd[0]);
1371 	close(ctx.efd[1]);
1372 	close(ctx.sfd[0]);
1373 	close(ctx.sfd[1]);
1374 }
1375 
1376 /*
1377  *        t0    t1
1378  *     (ew) \  / (p)
1379  *           e0
1380  *            | (lt)
1381  *           e1
1382  *            | (lt)
1383  *           s0
1384  */
TEST(epoll29)1385 TEST(epoll29)
1386 {
1387 	pthread_t emitter;
1388 	struct epoll_event e;
1389 	struct epoll_mtcontext ctx = { 0 };
1390 
1391 	signal(SIGUSR1, signal_handler);
1392 
1393 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1394 
1395 	ctx.efd[0] = epoll_create(1);
1396 	ASSERT_GE(ctx.efd[0], 0);
1397 
1398 	ctx.efd[1] = epoll_create(1);
1399 	ASSERT_GE(ctx.efd[1], 0);
1400 
1401 	e.events = EPOLLIN;
1402 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1403 
1404 	e.events = EPOLLIN;
1405 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1406 
1407 	ctx.main = pthread_self();
1408 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1409 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1410 
1411 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1412 		__sync_fetch_and_add(&ctx.count, 1);
1413 
1414 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1415 	EXPECT_EQ(ctx.count, 2);
1416 
1417 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1418 		pthread_kill(emitter, SIGUSR1);
1419 		pthread_join(emitter, NULL);
1420 	}
1421 
1422 	close(ctx.efd[0]);
1423 	close(ctx.sfd[0]);
1424 	close(ctx.sfd[1]);
1425 }
1426 
1427 /*
1428  *        t0    t1
1429  *     (ew) \  / (p)
1430  *           e0
1431  *            | (lt)
1432  *           e1
1433  *            | (et)
1434  *           s0
1435  */
TEST(epoll30)1436 TEST(epoll30)
1437 {
1438 	pthread_t emitter;
1439 	struct epoll_event e;
1440 	struct epoll_mtcontext ctx = { 0 };
1441 
1442 	signal(SIGUSR1, signal_handler);
1443 
1444 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1445 
1446 	ctx.efd[0] = epoll_create(1);
1447 	ASSERT_GE(ctx.efd[0], 0);
1448 
1449 	ctx.efd[1] = epoll_create(1);
1450 	ASSERT_GE(ctx.efd[1], 0);
1451 
1452 	e.events = EPOLLIN | EPOLLET;
1453 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1454 
1455 	e.events = EPOLLIN;
1456 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1457 
1458 	ctx.main = pthread_self();
1459 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1460 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1461 
1462 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1463 		__sync_fetch_and_add(&ctx.count, 1);
1464 
1465 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1466 	EXPECT_EQ(ctx.count, 2);
1467 
1468 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1469 		pthread_kill(emitter, SIGUSR1);
1470 		pthread_join(emitter, NULL);
1471 	}
1472 
1473 	close(ctx.efd[0]);
1474 	close(ctx.sfd[0]);
1475 	close(ctx.sfd[1]);
1476 }
1477 
1478 /*
1479  *        t0    t1
1480  *     (ew) \  / (p)
1481  *           e0
1482  *            | (et)
1483  *           e1
1484  *            | (lt)
1485  *           s0
1486  */
TEST(epoll31)1487 TEST(epoll31)
1488 {
1489 	pthread_t emitter;
1490 	struct epoll_event e;
1491 	struct epoll_mtcontext ctx = { 0 };
1492 
1493 	signal(SIGUSR1, signal_handler);
1494 
1495 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1496 
1497 	ctx.efd[0] = epoll_create(1);
1498 	ASSERT_GE(ctx.efd[0], 0);
1499 
1500 	ctx.efd[1] = epoll_create(1);
1501 	ASSERT_GE(ctx.efd[1], 0);
1502 
1503 	e.events = EPOLLIN;
1504 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1505 
1506 	e.events = EPOLLIN | EPOLLET;
1507 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1508 
1509 	ctx.main = pthread_self();
1510 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1511 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1512 
1513 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1514 		__sync_fetch_and_add(&ctx.count, 1);
1515 
1516 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1517 	EXPECT_EQ(ctx.count, 1);
1518 
1519 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1520 		pthread_kill(emitter, SIGUSR1);
1521 		pthread_join(emitter, NULL);
1522 	}
1523 
1524 	close(ctx.efd[0]);
1525 	close(ctx.sfd[0]);
1526 	close(ctx.sfd[1]);
1527 }
1528 
1529 /*
1530  *        t0    t1
1531  *     (ew) \  / (p)
1532  *           e0
1533  *            | (et)
1534  *           e1
1535  *            | (et)
1536  *           s0
1537  */
TEST(epoll32)1538 TEST(epoll32)
1539 {
1540 	pthread_t emitter;
1541 	struct epoll_event e;
1542 	struct epoll_mtcontext ctx = { 0 };
1543 
1544 	signal(SIGUSR1, signal_handler);
1545 
1546 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1547 
1548 	ctx.efd[0] = epoll_create(1);
1549 	ASSERT_GE(ctx.efd[0], 0);
1550 
1551 	ctx.efd[1] = epoll_create(1);
1552 	ASSERT_GE(ctx.efd[1], 0);
1553 
1554 	e.events = EPOLLIN | EPOLLET;
1555 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1556 
1557 	e.events = EPOLLIN | EPOLLET;
1558 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1559 
1560 	ctx.main = pthread_self();
1561 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1562 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1563 
1564 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1565 		__sync_fetch_and_add(&ctx.count, 1);
1566 
1567 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1568 	EXPECT_EQ(ctx.count, 1);
1569 
1570 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1571 		pthread_kill(emitter, SIGUSR1);
1572 		pthread_join(emitter, NULL);
1573 	}
1574 
1575 	close(ctx.efd[0]);
1576 	close(ctx.sfd[0]);
1577 	close(ctx.sfd[1]);
1578 }
1579 
1580 /*
1581  *        t0   t1
1582  *    (ew) |    | (ew)
1583  *         |   e0
1584  *          \  / (lt)
1585  *           e1
1586  *            | (lt)
1587  *           s0
1588  */
TEST(epoll33)1589 TEST(epoll33)
1590 {
1591 	pthread_t emitter;
1592 	struct epoll_event e;
1593 	struct epoll_mtcontext ctx = { 0 };
1594 
1595 	signal(SIGUSR1, signal_handler);
1596 
1597 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1598 
1599 	ctx.efd[0] = epoll_create(1);
1600 	ASSERT_GE(ctx.efd[0], 0);
1601 
1602 	ctx.efd[1] = epoll_create(1);
1603 	ASSERT_GE(ctx.efd[1], 0);
1604 
1605 	e.events = EPOLLIN;
1606 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1607 
1608 	e.events = EPOLLIN;
1609 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1610 
1611 	ctx.main = pthread_self();
1612 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1613 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1614 
1615 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1616 		__sync_fetch_and_add(&ctx.count, 1);
1617 
1618 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1619 	EXPECT_EQ(ctx.count, 2);
1620 
1621 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1622 		pthread_kill(emitter, SIGUSR1);
1623 		pthread_join(emitter, NULL);
1624 	}
1625 
1626 	close(ctx.efd[0]);
1627 	close(ctx.efd[1]);
1628 	close(ctx.sfd[0]);
1629 	close(ctx.sfd[1]);
1630 }
1631 
1632 /*
1633  *        t0   t1
1634  *    (ew) |    | (ew)
1635  *         |   e0
1636  *          \  / (lt)
1637  *           e1
1638  *            | (et)
1639  *           s0
1640  */
TEST(epoll34)1641 TEST(epoll34)
1642 {
1643 	pthread_t emitter;
1644 	struct epoll_event e;
1645 	struct epoll_mtcontext ctx = { 0 };
1646 
1647 	signal(SIGUSR1, signal_handler);
1648 
1649 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1650 
1651 	ctx.efd[0] = epoll_create(1);
1652 	ASSERT_GE(ctx.efd[0], 0);
1653 
1654 	ctx.efd[1] = epoll_create(1);
1655 	ASSERT_GE(ctx.efd[1], 0);
1656 
1657 	e.events = EPOLLIN | EPOLLET;
1658 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1659 
1660 	e.events = EPOLLIN;
1661 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1662 
1663 	ctx.main = pthread_self();
1664 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1665 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1666 
1667 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1668 		__sync_fetch_and_or(&ctx.count, 2);
1669 
1670 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1671 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1672 
1673 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1674 		pthread_kill(emitter, SIGUSR1);
1675 		pthread_join(emitter, NULL);
1676 	}
1677 
1678 	close(ctx.efd[0]);
1679 	close(ctx.efd[1]);
1680 	close(ctx.sfd[0]);
1681 	close(ctx.sfd[1]);
1682 }
1683 
1684 /*
1685  *        t0   t1
1686  *    (ew) |    | (ew)
1687  *         |   e0
1688  *          \  / (et)
1689  *           e1
1690  *            | (lt)
1691  *           s0
1692  */
TEST(epoll35)1693 TEST(epoll35)
1694 {
1695 	pthread_t emitter;
1696 	struct epoll_event e;
1697 	struct epoll_mtcontext ctx = { 0 };
1698 
1699 	signal(SIGUSR1, signal_handler);
1700 
1701 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1702 
1703 	ctx.efd[0] = epoll_create(1);
1704 	ASSERT_GE(ctx.efd[0], 0);
1705 
1706 	ctx.efd[1] = epoll_create(1);
1707 	ASSERT_GE(ctx.efd[1], 0);
1708 
1709 	e.events = EPOLLIN;
1710 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1711 
1712 	e.events = EPOLLIN | EPOLLET;
1713 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1714 
1715 	ctx.main = pthread_self();
1716 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1717 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1718 
1719 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1720 		__sync_fetch_and_add(&ctx.count, 1);
1721 
1722 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1723 	EXPECT_EQ(ctx.count, 2);
1724 
1725 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1726 		pthread_kill(emitter, SIGUSR1);
1727 		pthread_join(emitter, NULL);
1728 	}
1729 
1730 	close(ctx.efd[0]);
1731 	close(ctx.efd[1]);
1732 	close(ctx.sfd[0]);
1733 	close(ctx.sfd[1]);
1734 }
1735 
1736 /*
1737  *        t0   t1
1738  *    (ew) |    | (ew)
1739  *         |   e0
1740  *          \  / (et)
1741  *           e1
1742  *            | (et)
1743  *           s0
1744  */
TEST(epoll36)1745 TEST(epoll36)
1746 {
1747 	pthread_t emitter;
1748 	struct epoll_event e;
1749 	struct epoll_mtcontext ctx = { 0 };
1750 
1751 	signal(SIGUSR1, signal_handler);
1752 
1753 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1754 
1755 	ctx.efd[0] = epoll_create(1);
1756 	ASSERT_GE(ctx.efd[0], 0);
1757 
1758 	ctx.efd[1] = epoll_create(1);
1759 	ASSERT_GE(ctx.efd[1], 0);
1760 
1761 	e.events = EPOLLIN | EPOLLET;
1762 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1763 
1764 	e.events = EPOLLIN | EPOLLET;
1765 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1766 
1767 	ctx.main = pthread_self();
1768 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1769 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1770 
1771 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1772 		__sync_fetch_and_or(&ctx.count, 2);
1773 
1774 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1775 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1776 
1777 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1778 		pthread_kill(emitter, SIGUSR1);
1779 		pthread_join(emitter, NULL);
1780 	}
1781 
1782 	close(ctx.efd[0]);
1783 	close(ctx.efd[1]);
1784 	close(ctx.sfd[0]);
1785 	close(ctx.sfd[1]);
1786 }
1787 
1788 /*
1789  *        t0   t1
1790  *     (p) |    | (ew)
1791  *         |   e0
1792  *          \  / (lt)
1793  *           e1
1794  *            | (lt)
1795  *           s0
1796  */
TEST(epoll37)1797 TEST(epoll37)
1798 {
1799 	pthread_t emitter;
1800 	struct pollfd pfd;
1801 	struct epoll_event e;
1802 	struct epoll_mtcontext ctx = { 0 };
1803 
1804 	signal(SIGUSR1, signal_handler);
1805 
1806 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1807 
1808 	ctx.efd[0] = epoll_create(1);
1809 	ASSERT_GE(ctx.efd[0], 0);
1810 
1811 	ctx.efd[1] = epoll_create(1);
1812 	ASSERT_GE(ctx.efd[1], 0);
1813 
1814 	e.events = EPOLLIN;
1815 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1816 
1817 	e.events = EPOLLIN;
1818 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1819 
1820 	ctx.main = pthread_self();
1821 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1822 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1823 
1824 	pfd.fd = ctx.efd[1];
1825 	pfd.events = POLLIN;
1826 	if (poll(&pfd, 1, -1) > 0) {
1827 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1828 			__sync_fetch_and_add(&ctx.count, 1);
1829 	}
1830 
1831 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1832 	EXPECT_EQ(ctx.count, 2);
1833 
1834 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1835 		pthread_kill(emitter, SIGUSR1);
1836 		pthread_join(emitter, NULL);
1837 	}
1838 
1839 	close(ctx.efd[0]);
1840 	close(ctx.efd[1]);
1841 	close(ctx.sfd[0]);
1842 	close(ctx.sfd[1]);
1843 }
1844 
1845 /*
1846  *        t0   t1
1847  *     (p) |    | (ew)
1848  *         |   e0
1849  *          \  / (lt)
1850  *           e1
1851  *            | (et)
1852  *           s0
1853  */
TEST(epoll38)1854 TEST(epoll38)
1855 {
1856 	pthread_t emitter;
1857 	struct pollfd pfd;
1858 	struct epoll_event e;
1859 	struct epoll_mtcontext ctx = { 0 };
1860 
1861 	signal(SIGUSR1, signal_handler);
1862 
1863 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1864 
1865 	ctx.efd[0] = epoll_create(1);
1866 	ASSERT_GE(ctx.efd[0], 0);
1867 
1868 	ctx.efd[1] = epoll_create(1);
1869 	ASSERT_GE(ctx.efd[1], 0);
1870 
1871 	e.events = EPOLLIN | EPOLLET;
1872 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1873 
1874 	e.events = EPOLLIN;
1875 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1876 
1877 	ctx.main = pthread_self();
1878 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1879 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1880 
1881 	pfd.fd = ctx.efd[1];
1882 	pfd.events = POLLIN;
1883 	if (poll(&pfd, 1, -1) > 0) {
1884 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1885 			__sync_fetch_and_or(&ctx.count, 2);
1886 	}
1887 
1888 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1889 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1890 
1891 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1892 		pthread_kill(emitter, SIGUSR1);
1893 		pthread_join(emitter, NULL);
1894 	}
1895 
1896 	close(ctx.efd[0]);
1897 	close(ctx.efd[1]);
1898 	close(ctx.sfd[0]);
1899 	close(ctx.sfd[1]);
1900 }
1901 
1902 /*
1903  *        t0   t1
1904  *     (p) |    | (ew)
1905  *         |   e0
1906  *          \  / (et)
1907  *           e1
1908  *            | (lt)
1909  *           s0
1910  */
TEST(epoll39)1911 TEST(epoll39)
1912 {
1913 	pthread_t emitter;
1914 	struct pollfd pfd;
1915 	struct epoll_event e;
1916 	struct epoll_mtcontext ctx = { 0 };
1917 
1918 	signal(SIGUSR1, signal_handler);
1919 
1920 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1921 
1922 	ctx.efd[0] = epoll_create(1);
1923 	ASSERT_GE(ctx.efd[0], 0);
1924 
1925 	ctx.efd[1] = epoll_create(1);
1926 	ASSERT_GE(ctx.efd[1], 0);
1927 
1928 	e.events = EPOLLIN;
1929 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1930 
1931 	e.events = EPOLLIN | EPOLLET;
1932 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1933 
1934 	ctx.main = pthread_self();
1935 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1936 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1937 
1938 	pfd.fd = ctx.efd[1];
1939 	pfd.events = POLLIN;
1940 	if (poll(&pfd, 1, -1) > 0) {
1941 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1942 			__sync_fetch_and_add(&ctx.count, 1);
1943 	}
1944 
1945 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1946 	EXPECT_EQ(ctx.count, 2);
1947 
1948 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
1949 		pthread_kill(emitter, SIGUSR1);
1950 		pthread_join(emitter, NULL);
1951 	}
1952 
1953 	close(ctx.efd[0]);
1954 	close(ctx.efd[1]);
1955 	close(ctx.sfd[0]);
1956 	close(ctx.sfd[1]);
1957 }
1958 
1959 /*
1960  *        t0   t1
1961  *     (p) |    | (ew)
1962  *         |   e0
1963  *          \  / (et)
1964  *           e1
1965  *            | (et)
1966  *           s0
1967  */
TEST(epoll40)1968 TEST(epoll40)
1969 {
1970 	pthread_t emitter;
1971 	struct pollfd pfd;
1972 	struct epoll_event e;
1973 	struct epoll_mtcontext ctx = { 0 };
1974 
1975 	signal(SIGUSR1, signal_handler);
1976 
1977 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1978 
1979 	ctx.efd[0] = epoll_create(1);
1980 	ASSERT_GE(ctx.efd[0], 0);
1981 
1982 	ctx.efd[1] = epoll_create(1);
1983 	ASSERT_GE(ctx.efd[1], 0);
1984 
1985 	e.events = EPOLLIN | EPOLLET;
1986 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1987 
1988 	e.events = EPOLLIN | EPOLLET;
1989 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1990 
1991 	ctx.main = pthread_self();
1992 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1993 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1994 
1995 	pfd.fd = ctx.efd[1];
1996 	pfd.events = POLLIN;
1997 	if (poll(&pfd, 1, -1) > 0) {
1998 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1999 			__sync_fetch_and_or(&ctx.count, 2);
2000 	}
2001 
2002 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2003 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2004 
2005 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2006 		pthread_kill(emitter, SIGUSR1);
2007 		pthread_join(emitter, NULL);
2008 	}
2009 
2010 	close(ctx.efd[0]);
2011 	close(ctx.efd[1]);
2012 	close(ctx.sfd[0]);
2013 	close(ctx.sfd[1]);
2014 }
2015 
2016 /*
2017  *        t0   t1
2018  *    (ew) |    | (p)
2019  *         |   e0
2020  *          \  / (lt)
2021  *           e1
2022  *            | (lt)
2023  *           s0
2024  */
TEST(epoll41)2025 TEST(epoll41)
2026 {
2027 	pthread_t emitter;
2028 	struct epoll_event e;
2029 	struct epoll_mtcontext ctx = { 0 };
2030 
2031 	signal(SIGUSR1, signal_handler);
2032 
2033 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2034 
2035 	ctx.efd[0] = epoll_create(1);
2036 	ASSERT_GE(ctx.efd[0], 0);
2037 
2038 	ctx.efd[1] = epoll_create(1);
2039 	ASSERT_GE(ctx.efd[1], 0);
2040 
2041 	e.events = EPOLLIN;
2042 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2043 
2044 	e.events = EPOLLIN;
2045 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2046 
2047 	ctx.main = pthread_self();
2048 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2049 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2050 
2051 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2052 		__sync_fetch_and_add(&ctx.count, 1);
2053 
2054 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2055 	EXPECT_EQ(ctx.count, 2);
2056 
2057 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2058 		pthread_kill(emitter, SIGUSR1);
2059 		pthread_join(emitter, NULL);
2060 	}
2061 
2062 	close(ctx.efd[0]);
2063 	close(ctx.efd[1]);
2064 	close(ctx.sfd[0]);
2065 	close(ctx.sfd[1]);
2066 }
2067 
2068 /*
2069  *        t0   t1
2070  *    (ew) |    | (p)
2071  *         |   e0
2072  *          \  / (lt)
2073  *           e1
2074  *            | (et)
2075  *           s0
2076  */
TEST(epoll42)2077 TEST(epoll42)
2078 {
2079 	pthread_t emitter;
2080 	struct epoll_event e;
2081 	struct epoll_mtcontext ctx = { 0 };
2082 
2083 	signal(SIGUSR1, signal_handler);
2084 
2085 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2086 
2087 	ctx.efd[0] = epoll_create(1);
2088 	ASSERT_GE(ctx.efd[0], 0);
2089 
2090 	ctx.efd[1] = epoll_create(1);
2091 	ASSERT_GE(ctx.efd[1], 0);
2092 
2093 	e.events = EPOLLIN | EPOLLET;
2094 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2095 
2096 	e.events = EPOLLIN;
2097 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2098 
2099 	ctx.main = pthread_self();
2100 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2101 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2102 
2103 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2104 		__sync_fetch_and_or(&ctx.count, 2);
2105 
2106 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2107 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2108 
2109 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2110 		pthread_kill(emitter, SIGUSR1);
2111 		pthread_join(emitter, NULL);
2112 	}
2113 
2114 	close(ctx.efd[0]);
2115 	close(ctx.efd[1]);
2116 	close(ctx.sfd[0]);
2117 	close(ctx.sfd[1]);
2118 }
2119 
2120 /*
2121  *        t0   t1
2122  *    (ew) |    | (p)
2123  *         |   e0
2124  *          \  / (et)
2125  *           e1
2126  *            | (lt)
2127  *           s0
2128  */
TEST(epoll43)2129 TEST(epoll43)
2130 {
2131 	pthread_t emitter;
2132 	struct epoll_event e;
2133 	struct epoll_mtcontext ctx = { 0 };
2134 
2135 	signal(SIGUSR1, signal_handler);
2136 
2137 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2138 
2139 	ctx.efd[0] = epoll_create(1);
2140 	ASSERT_GE(ctx.efd[0], 0);
2141 
2142 	ctx.efd[1] = epoll_create(1);
2143 	ASSERT_GE(ctx.efd[1], 0);
2144 
2145 	e.events = EPOLLIN;
2146 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2147 
2148 	e.events = EPOLLIN | EPOLLET;
2149 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2150 
2151 	ctx.main = pthread_self();
2152 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2153 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2154 
2155 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2156 		__sync_fetch_and_add(&ctx.count, 1);
2157 
2158 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2159 	EXPECT_EQ(ctx.count, 2);
2160 
2161 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2162 		pthread_kill(emitter, SIGUSR1);
2163 		pthread_join(emitter, NULL);
2164 	}
2165 
2166 	close(ctx.efd[0]);
2167 	close(ctx.efd[1]);
2168 	close(ctx.sfd[0]);
2169 	close(ctx.sfd[1]);
2170 }
2171 
2172 /*
2173  *        t0   t1
2174  *    (ew) |    | (p)
2175  *         |   e0
2176  *          \  / (et)
2177  *           e1
2178  *            | (et)
2179  *           s0
2180  */
TEST(epoll44)2181 TEST(epoll44)
2182 {
2183 	pthread_t emitter;
2184 	struct epoll_event e;
2185 	struct epoll_mtcontext ctx = { 0 };
2186 
2187 	signal(SIGUSR1, signal_handler);
2188 
2189 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2190 
2191 	ctx.efd[0] = epoll_create(1);
2192 	ASSERT_GE(ctx.efd[0], 0);
2193 
2194 	ctx.efd[1] = epoll_create(1);
2195 	ASSERT_GE(ctx.efd[1], 0);
2196 
2197 	e.events = EPOLLIN | EPOLLET;
2198 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2199 
2200 	e.events = EPOLLIN | EPOLLET;
2201 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2202 
2203 	ctx.main = pthread_self();
2204 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2205 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2206 
2207 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2208 		__sync_fetch_and_or(&ctx.count, 2);
2209 
2210 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2211 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2212 
2213 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2214 		pthread_kill(emitter, SIGUSR1);
2215 		pthread_join(emitter, NULL);
2216 	}
2217 
2218 	close(ctx.efd[0]);
2219 	close(ctx.efd[1]);
2220 	close(ctx.sfd[0]);
2221 	close(ctx.sfd[1]);
2222 }
2223 
2224 /*
2225  *        t0   t1
2226  *     (p) |    | (p)
2227  *         |   e0
2228  *          \  / (lt)
2229  *           e1
2230  *            | (lt)
2231  *           s0
2232  */
TEST(epoll45)2233 TEST(epoll45)
2234 {
2235 	pthread_t emitter;
2236 	struct pollfd pfd;
2237 	struct epoll_event e;
2238 	struct epoll_mtcontext ctx = { 0 };
2239 
2240 	signal(SIGUSR1, signal_handler);
2241 
2242 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2243 
2244 	ctx.efd[0] = epoll_create(1);
2245 	ASSERT_GE(ctx.efd[0], 0);
2246 
2247 	ctx.efd[1] = epoll_create(1);
2248 	ASSERT_GE(ctx.efd[1], 0);
2249 
2250 	e.events = EPOLLIN;
2251 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2252 
2253 	e.events = EPOLLIN;
2254 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2255 
2256 	ctx.main = pthread_self();
2257 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2258 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2259 
2260 	pfd.fd = ctx.efd[1];
2261 	pfd.events = POLLIN;
2262 	if (poll(&pfd, 1, -1) > 0) {
2263 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2264 			__sync_fetch_and_add(&ctx.count, 1);
2265 	}
2266 
2267 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2268 	EXPECT_EQ(ctx.count, 2);
2269 
2270 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2271 		pthread_kill(emitter, SIGUSR1);
2272 		pthread_join(emitter, NULL);
2273 	}
2274 
2275 	close(ctx.efd[0]);
2276 	close(ctx.efd[1]);
2277 	close(ctx.sfd[0]);
2278 	close(ctx.sfd[1]);
2279 }
2280 
2281 /*
2282  *        t0   t1
2283  *     (p) |    | (p)
2284  *         |   e0
2285  *          \  / (lt)
2286  *           e1
2287  *            | (et)
2288  *           s0
2289  */
TEST(epoll46)2290 TEST(epoll46)
2291 {
2292 	pthread_t emitter;
2293 	struct epoll_event e;
2294 	struct epoll_mtcontext ctx = { 0 };
2295 
2296 	signal(SIGUSR1, signal_handler);
2297 
2298 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2299 
2300 	ctx.efd[0] = epoll_create(1);
2301 	ASSERT_GE(ctx.efd[0], 0);
2302 
2303 	ctx.efd[1] = epoll_create(1);
2304 	ASSERT_GE(ctx.efd[1], 0);
2305 
2306 	e.events = EPOLLIN | EPOLLET;
2307 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2308 
2309 	e.events = EPOLLIN;
2310 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2311 
2312 	ctx.main = pthread_self();
2313 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2314 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2315 
2316 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2317 		__sync_fetch_and_or(&ctx.count, 2);
2318 
2319 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2320 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2321 
2322 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2323 		pthread_kill(emitter, SIGUSR1);
2324 		pthread_join(emitter, NULL);
2325 	}
2326 
2327 	close(ctx.efd[0]);
2328 	close(ctx.efd[1]);
2329 	close(ctx.sfd[0]);
2330 	close(ctx.sfd[1]);
2331 }
2332 
2333 /*
2334  *        t0   t1
2335  *     (p) |    | (p)
2336  *         |   e0
2337  *          \  / (et)
2338  *           e1
2339  *            | (lt)
2340  *           s0
2341  */
TEST(epoll47)2342 TEST(epoll47)
2343 {
2344 	pthread_t emitter;
2345 	struct pollfd pfd;
2346 	struct epoll_event e;
2347 	struct epoll_mtcontext ctx = { 0 };
2348 
2349 	signal(SIGUSR1, signal_handler);
2350 
2351 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2352 
2353 	ctx.efd[0] = epoll_create(1);
2354 	ASSERT_GE(ctx.efd[0], 0);
2355 
2356 	ctx.efd[1] = epoll_create(1);
2357 	ASSERT_GE(ctx.efd[1], 0);
2358 
2359 	e.events = EPOLLIN;
2360 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2361 
2362 	e.events = EPOLLIN | EPOLLET;
2363 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2364 
2365 	ctx.main = pthread_self();
2366 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2367 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2368 
2369 	pfd.fd = ctx.efd[1];
2370 	pfd.events = POLLIN;
2371 	if (poll(&pfd, 1, -1) > 0) {
2372 		if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2373 			__sync_fetch_and_add(&ctx.count, 1);
2374 	}
2375 
2376 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2377 	EXPECT_EQ(ctx.count, 2);
2378 
2379 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2380 		pthread_kill(emitter, SIGUSR1);
2381 		pthread_join(emitter, NULL);
2382 	}
2383 
2384 	close(ctx.efd[0]);
2385 	close(ctx.efd[1]);
2386 	close(ctx.sfd[0]);
2387 	close(ctx.sfd[1]);
2388 }
2389 
2390 /*
2391  *        t0   t1
2392  *     (p) |    | (p)
2393  *         |   e0
2394  *          \  / (et)
2395  *           e1
2396  *            | (et)
2397  *           s0
2398  */
TEST(epoll48)2399 TEST(epoll48)
2400 {
2401 	pthread_t emitter;
2402 	struct epoll_event e;
2403 	struct epoll_mtcontext ctx = { 0 };
2404 
2405 	signal(SIGUSR1, signal_handler);
2406 
2407 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2408 
2409 	ctx.efd[0] = epoll_create(1);
2410 	ASSERT_GE(ctx.efd[0], 0);
2411 
2412 	ctx.efd[1] = epoll_create(1);
2413 	ASSERT_GE(ctx.efd[1], 0);
2414 
2415 	e.events = EPOLLIN | EPOLLET;
2416 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2417 
2418 	e.events = EPOLLIN | EPOLLET;
2419 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2420 
2421 	ctx.main = pthread_self();
2422 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2423 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2424 
2425 	if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2426 		__sync_fetch_and_or(&ctx.count, 2);
2427 
2428 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2429 	EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2430 
2431 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2432 		pthread_kill(emitter, SIGUSR1);
2433 		pthread_join(emitter, NULL);
2434 	}
2435 
2436 	close(ctx.efd[0]);
2437 	close(ctx.efd[1]);
2438 	close(ctx.sfd[0]);
2439 	close(ctx.sfd[1]);
2440 }
2441 
2442 /*
2443  *           t0
2444  *            | (ew)
2445  *           e0
2446  *     (lt) /  \ (lt)
2447  *        e1    e2
2448  *    (lt) |     | (lt)
2449  *        s0    s2
2450  */
TEST(epoll49)2451 TEST(epoll49)
2452 {
2453 	int efd[3];
2454 	int sfd[4];
2455 	struct epoll_event events[2];
2456 
2457 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2458 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2459 
2460 	efd[0] = epoll_create(1);
2461 	ASSERT_GE(efd[0], 0);
2462 
2463 	efd[1] = epoll_create(1);
2464 	ASSERT_GE(efd[1], 0);
2465 
2466 	efd[2] = epoll_create(1);
2467 	ASSERT_GE(efd[2], 0);
2468 
2469 	events[0].events = EPOLLIN;
2470 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2471 
2472 	events[0].events = EPOLLIN;
2473 	ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2474 
2475 	events[0].events = EPOLLIN;
2476 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2477 
2478 	events[0].events = EPOLLIN;
2479 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2480 
2481 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
2482 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
2483 
2484 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2485 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2486 
2487 	close(efd[0]);
2488 	close(efd[1]);
2489 	close(efd[2]);
2490 	close(sfd[0]);
2491 	close(sfd[1]);
2492 	close(sfd[2]);
2493 	close(sfd[3]);
2494 }
2495 
2496 /*
2497  *           t0
2498  *            | (ew)
2499  *           e0
2500  *     (et) /  \ (et)
2501  *        e1    e2
2502  *    (lt) |     | (lt)
2503  *        s0    s2
2504  */
TEST(epoll50)2505 TEST(epoll50)
2506 {
2507 	int efd[3];
2508 	int sfd[4];
2509 	struct epoll_event events[2];
2510 
2511 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2512 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2513 
2514 	efd[0] = epoll_create(1);
2515 	ASSERT_GE(efd[0], 0);
2516 
2517 	efd[1] = epoll_create(1);
2518 	ASSERT_GE(efd[1], 0);
2519 
2520 	efd[2] = epoll_create(1);
2521 	ASSERT_GE(efd[2], 0);
2522 
2523 	events[0].events = EPOLLIN;
2524 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2525 
2526 	events[0].events = EPOLLIN;
2527 	ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2528 
2529 	events[0].events = EPOLLIN | EPOLLET;
2530 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2531 
2532 	events[0].events = EPOLLIN | EPOLLET;
2533 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2534 
2535 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
2536 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
2537 
2538 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2539 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2540 
2541 	close(efd[0]);
2542 	close(efd[1]);
2543 	close(efd[2]);
2544 	close(sfd[0]);
2545 	close(sfd[1]);
2546 	close(sfd[2]);
2547 	close(sfd[3]);
2548 }
2549 
2550 /*
2551  *           t0
2552  *            | (p)
2553  *           e0
2554  *     (lt) /  \ (lt)
2555  *        e1    e2
2556  *    (lt) |     | (lt)
2557  *        s0    s2
2558  */
TEST(epoll51)2559 TEST(epoll51)
2560 {
2561 	int efd[3];
2562 	int sfd[4];
2563 	struct pollfd pfd;
2564 	struct epoll_event events[2];
2565 
2566 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2567 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2568 
2569 	efd[0] = epoll_create(1);
2570 	ASSERT_GE(efd[0], 0);
2571 
2572 	efd[1] = epoll_create(1);
2573 	ASSERT_GE(efd[1], 0);
2574 
2575 	efd[2] = epoll_create(1);
2576 	ASSERT_GE(efd[2], 0);
2577 
2578 	events[0].events = EPOLLIN;
2579 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2580 
2581 	events[0].events = EPOLLIN;
2582 	ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2583 
2584 	events[0].events = EPOLLIN;
2585 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2586 
2587 	events[0].events = EPOLLIN;
2588 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2589 
2590 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
2591 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
2592 
2593 	pfd.fd = efd[0];
2594 	pfd.events = POLLIN;
2595 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
2596 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2597 
2598 	pfd.fd = efd[0];
2599 	pfd.events = POLLIN;
2600 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
2601 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2602 
2603 	close(efd[0]);
2604 	close(efd[1]);
2605 	close(efd[2]);
2606 	close(sfd[0]);
2607 	close(sfd[1]);
2608 	close(sfd[2]);
2609 	close(sfd[3]);
2610 }
2611 
2612 /*
2613  *           t0
2614  *            | (p)
2615  *           e0
2616  *     (et) /  \ (et)
2617  *        e1    e2
2618  *    (lt) |     | (lt)
2619  *        s0    s2
2620  */
TEST(epoll52)2621 TEST(epoll52)
2622 {
2623 	int efd[3];
2624 	int sfd[4];
2625 	struct pollfd pfd;
2626 	struct epoll_event events[2];
2627 
2628 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2629 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2630 
2631 	efd[0] = epoll_create(1);
2632 	ASSERT_GE(efd[0], 0);
2633 
2634 	efd[1] = epoll_create(1);
2635 	ASSERT_GE(efd[1], 0);
2636 
2637 	efd[2] = epoll_create(1);
2638 	ASSERT_GE(efd[2], 0);
2639 
2640 	events[0].events = EPOLLIN;
2641 	ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2642 
2643 	events[0].events = EPOLLIN;
2644 	ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2645 
2646 	events[0].events = EPOLLIN | EPOLLET;
2647 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2648 
2649 	events[0].events = EPOLLIN | EPOLLET;
2650 	ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2651 
2652 	ASSERT_EQ(write(sfd[1], "w", 1), 1);
2653 	ASSERT_EQ(write(sfd[3], "w", 1), 1);
2654 
2655 	pfd.fd = efd[0];
2656 	pfd.events = POLLIN;
2657 	EXPECT_EQ(poll(&pfd, 1, 0), 1);
2658 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2659 
2660 	pfd.fd = efd[0];
2661 	pfd.events = POLLIN;
2662 	EXPECT_EQ(poll(&pfd, 1, 0), 0);
2663 	EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2664 
2665 	close(efd[0]);
2666 	close(efd[1]);
2667 	close(efd[2]);
2668 	close(sfd[0]);
2669 	close(sfd[1]);
2670 	close(sfd[2]);
2671 	close(sfd[3]);
2672 }
2673 
2674 /*
2675  *        t0    t1
2676  *     (ew) \  / (ew)
2677  *           e0
2678  *     (lt) /  \ (lt)
2679  *        e1    e2
2680  *    (lt) |     | (lt)
2681  *        s0    s2
2682  */
TEST(epoll53)2683 TEST(epoll53)
2684 {
2685 	pthread_t emitter;
2686 	struct epoll_event e;
2687 	struct epoll_mtcontext ctx = { 0 };
2688 
2689 	signal(SIGUSR1, signal_handler);
2690 
2691 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2692 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2693 
2694 	ctx.efd[0] = epoll_create(1);
2695 	ASSERT_GE(ctx.efd[0], 0);
2696 
2697 	ctx.efd[1] = epoll_create(1);
2698 	ASSERT_GE(ctx.efd[1], 0);
2699 
2700 	ctx.efd[2] = epoll_create(1);
2701 	ASSERT_GE(ctx.efd[2], 0);
2702 
2703 	e.events = EPOLLIN;
2704 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2705 
2706 	e.events = EPOLLIN;
2707 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2708 
2709 	e.events = EPOLLIN;
2710 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2711 
2712 	e.events = EPOLLIN;
2713 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2714 
2715 	ctx.main = pthread_self();
2716 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2717 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2718 
2719 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2720 		__sync_fetch_and_add(&ctx.count, 1);
2721 
2722 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2723 	EXPECT_EQ(ctx.count, 2);
2724 
2725 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2726 		pthread_kill(emitter, SIGUSR1);
2727 		pthread_join(emitter, NULL);
2728 	}
2729 
2730 	close(ctx.efd[0]);
2731 	close(ctx.efd[1]);
2732 	close(ctx.efd[2]);
2733 	close(ctx.sfd[0]);
2734 	close(ctx.sfd[1]);
2735 	close(ctx.sfd[2]);
2736 	close(ctx.sfd[3]);
2737 }
2738 
2739 /*
2740  *        t0    t1
2741  *     (ew) \  / (ew)
2742  *           e0
2743  *     (et) /  \ (et)
2744  *        e1    e2
2745  *    (lt) |     | (lt)
2746  *        s0    s2
2747  */
TEST(epoll54)2748 TEST(epoll54)
2749 {
2750 	pthread_t emitter;
2751 	struct epoll_event e;
2752 	struct epoll_mtcontext ctx = { 0 };
2753 
2754 	signal(SIGUSR1, signal_handler);
2755 
2756 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2757 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2758 
2759 	ctx.efd[0] = epoll_create(1);
2760 	ASSERT_GE(ctx.efd[0], 0);
2761 
2762 	ctx.efd[1] = epoll_create(1);
2763 	ASSERT_GE(ctx.efd[1], 0);
2764 
2765 	ctx.efd[2] = epoll_create(1);
2766 	ASSERT_GE(ctx.efd[2], 0);
2767 
2768 	e.events = EPOLLIN;
2769 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2770 
2771 	e.events = EPOLLIN;
2772 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2773 
2774 	e.events = EPOLLIN | EPOLLET;
2775 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2776 
2777 	e.events = EPOLLIN | EPOLLET;
2778 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2779 
2780 	ctx.main = pthread_self();
2781 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2782 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2783 
2784 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2785 		__sync_fetch_and_add(&ctx.count, 1);
2786 
2787 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2788 	EXPECT_EQ(ctx.count, 2);
2789 
2790 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2791 		pthread_kill(emitter, SIGUSR1);
2792 		pthread_join(emitter, NULL);
2793 	}
2794 
2795 	close(ctx.efd[0]);
2796 	close(ctx.efd[1]);
2797 	close(ctx.efd[2]);
2798 	close(ctx.sfd[0]);
2799 	close(ctx.sfd[1]);
2800 	close(ctx.sfd[2]);
2801 	close(ctx.sfd[3]);
2802 }
2803 
2804 /*
2805  *        t0    t1
2806  *     (ew) \  / (p)
2807  *           e0
2808  *     (lt) /  \ (lt)
2809  *        e1    e2
2810  *    (lt) |     | (lt)
2811  *        s0    s2
2812  */
TEST(epoll55)2813 TEST(epoll55)
2814 {
2815 	pthread_t emitter;
2816 	struct epoll_event e;
2817 	struct epoll_mtcontext ctx = { 0 };
2818 
2819 	signal(SIGUSR1, signal_handler);
2820 
2821 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2822 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2823 
2824 	ctx.efd[0] = epoll_create(1);
2825 	ASSERT_GE(ctx.efd[0], 0);
2826 
2827 	ctx.efd[1] = epoll_create(1);
2828 	ASSERT_GE(ctx.efd[1], 0);
2829 
2830 	ctx.efd[2] = epoll_create(1);
2831 	ASSERT_GE(ctx.efd[2], 0);
2832 
2833 	e.events = EPOLLIN;
2834 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2835 
2836 	e.events = EPOLLIN;
2837 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2838 
2839 	e.events = EPOLLIN;
2840 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2841 
2842 	e.events = EPOLLIN;
2843 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2844 
2845 	ctx.main = pthread_self();
2846 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2847 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2848 
2849 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2850 		__sync_fetch_and_add(&ctx.count, 1);
2851 
2852 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2853 	EXPECT_EQ(ctx.count, 2);
2854 
2855 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2856 		pthread_kill(emitter, SIGUSR1);
2857 		pthread_join(emitter, NULL);
2858 	}
2859 
2860 	close(ctx.efd[0]);
2861 	close(ctx.efd[1]);
2862 	close(ctx.efd[2]);
2863 	close(ctx.sfd[0]);
2864 	close(ctx.sfd[1]);
2865 	close(ctx.sfd[2]);
2866 	close(ctx.sfd[3]);
2867 }
2868 
2869 /*
2870  *        t0    t1
2871  *     (ew) \  / (p)
2872  *           e0
2873  *     (et) /  \ (et)
2874  *        e1    e2
2875  *    (lt) |     | (lt)
2876  *        s0    s2
2877  */
TEST(epoll56)2878 TEST(epoll56)
2879 {
2880 	pthread_t emitter;
2881 	struct epoll_event e;
2882 	struct epoll_mtcontext ctx = { 0 };
2883 
2884 	signal(SIGUSR1, signal_handler);
2885 
2886 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2887 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2888 
2889 	ctx.efd[0] = epoll_create(1);
2890 	ASSERT_GE(ctx.efd[0], 0);
2891 
2892 	ctx.efd[1] = epoll_create(1);
2893 	ASSERT_GE(ctx.efd[1], 0);
2894 
2895 	ctx.efd[2] = epoll_create(1);
2896 	ASSERT_GE(ctx.efd[2], 0);
2897 
2898 	e.events = EPOLLIN;
2899 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2900 
2901 	e.events = EPOLLIN;
2902 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2903 
2904 	e.events = EPOLLIN | EPOLLET;
2905 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2906 
2907 	e.events = EPOLLIN | EPOLLET;
2908 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2909 
2910 	ctx.main = pthread_self();
2911 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2912 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2913 
2914 	if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2915 		__sync_fetch_and_add(&ctx.count, 1);
2916 
2917 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2918 	EXPECT_EQ(ctx.count, 2);
2919 
2920 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2921 		pthread_kill(emitter, SIGUSR1);
2922 		pthread_join(emitter, NULL);
2923 	}
2924 
2925 	close(ctx.efd[0]);
2926 	close(ctx.efd[1]);
2927 	close(ctx.efd[2]);
2928 	close(ctx.sfd[0]);
2929 	close(ctx.sfd[1]);
2930 	close(ctx.sfd[2]);
2931 	close(ctx.sfd[3]);
2932 }
2933 
2934 /*
2935  *        t0    t1
2936  *      (p) \  / (p)
2937  *           e0
2938  *     (lt) /  \ (lt)
2939  *        e1    e2
2940  *    (lt) |     | (lt)
2941  *        s0    s2
2942  */
TEST(epoll57)2943 TEST(epoll57)
2944 {
2945 	pthread_t emitter;
2946 	struct pollfd pfd;
2947 	struct epoll_event e;
2948 	struct epoll_mtcontext ctx = { 0 };
2949 
2950 	signal(SIGUSR1, signal_handler);
2951 
2952 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2953 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2954 
2955 	ctx.efd[0] = epoll_create(1);
2956 	ASSERT_GE(ctx.efd[0], 0);
2957 
2958 	ctx.efd[1] = epoll_create(1);
2959 	ASSERT_GE(ctx.efd[1], 0);
2960 
2961 	ctx.efd[2] = epoll_create(1);
2962 	ASSERT_GE(ctx.efd[2], 0);
2963 
2964 	e.events = EPOLLIN;
2965 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2966 
2967 	e.events = EPOLLIN;
2968 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2969 
2970 	e.events = EPOLLIN;
2971 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2972 
2973 	e.events = EPOLLIN;
2974 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2975 
2976 	ctx.main = pthread_self();
2977 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2978 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2979 
2980 	pfd.fd = ctx.efd[0];
2981 	pfd.events = POLLIN;
2982 	if (poll(&pfd, 1, -1) > 0) {
2983 		if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
2984 			__sync_fetch_and_add(&ctx.count, 1);
2985 	}
2986 
2987 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2988 	EXPECT_EQ(ctx.count, 2);
2989 
2990 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
2991 		pthread_kill(emitter, SIGUSR1);
2992 		pthread_join(emitter, NULL);
2993 	}
2994 
2995 	close(ctx.efd[0]);
2996 	close(ctx.efd[1]);
2997 	close(ctx.efd[2]);
2998 	close(ctx.sfd[0]);
2999 	close(ctx.sfd[1]);
3000 	close(ctx.sfd[2]);
3001 	close(ctx.sfd[3]);
3002 }
3003 
3004 /*
3005  *        t0    t1
3006  *      (p) \  / (p)
3007  *           e0
3008  *     (et) /  \ (et)
3009  *        e1    e2
3010  *    (lt) |     | (lt)
3011  *        s0    s2
3012  */
TEST(epoll58)3013 TEST(epoll58)
3014 {
3015 	pthread_t emitter;
3016 	struct pollfd pfd;
3017 	struct epoll_event e;
3018 	struct epoll_mtcontext ctx = { 0 };
3019 
3020 	signal(SIGUSR1, signal_handler);
3021 
3022 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
3023 	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
3024 
3025 	ctx.efd[0] = epoll_create(1);
3026 	ASSERT_GE(ctx.efd[0], 0);
3027 
3028 	ctx.efd[1] = epoll_create(1);
3029 	ASSERT_GE(ctx.efd[1], 0);
3030 
3031 	ctx.efd[2] = epoll_create(1);
3032 	ASSERT_GE(ctx.efd[2], 0);
3033 
3034 	e.events = EPOLLIN;
3035 	ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3036 
3037 	e.events = EPOLLIN;
3038 	ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
3039 
3040 	e.events = EPOLLIN | EPOLLET;
3041 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
3042 
3043 	e.events = EPOLLIN | EPOLLET;
3044 	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
3045 
3046 	ctx.main = pthread_self();
3047 	ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
3048 	ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
3049 
3050 	pfd.fd = ctx.efd[0];
3051 	pfd.events = POLLIN;
3052 	if (poll(&pfd, 1, -1) > 0) {
3053 		if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
3054 			__sync_fetch_and_add(&ctx.count, 1);
3055 	}
3056 
3057 	ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
3058 	EXPECT_EQ(ctx.count, 2);
3059 
3060 	if (pthread_tryjoin_np(emitter, NULL) < 0) {
3061 		pthread_kill(emitter, SIGUSR1);
3062 		pthread_join(emitter, NULL);
3063 	}
3064 
3065 	close(ctx.efd[0]);
3066 	close(ctx.efd[1]);
3067 	close(ctx.efd[2]);
3068 	close(ctx.sfd[0]);
3069 	close(ctx.sfd[1]);
3070 	close(ctx.sfd[2]);
3071 	close(ctx.sfd[3]);
3072 }
3073 
3074 TEST_HARNESS_MAIN
3075