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