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