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