1 // SPDX-License-Identifier: GPL-2.0
4 #include <asm/unistd.h>
5 #include <linux/time_types.h>
11 #include <sys/epoll.h>
12 #include <sys/socket.h>
13 #include <sys/eventfd.h>
14 #include "../../kselftest_harness.h"
16 struct epoll_mtcontext
26 #ifndef __NR_epoll_pwait2
27 #define __NR_epoll_pwait2 -1
30 static inline int sys_epoll_pwait2(int fd, struct epoll_event *events,
32 const struct __kernel_timespec *timeout,
33 const sigset_t *sigset, size_t sigsetsize)
35 return syscall(__NR_epoll_pwait2, fd, events, maxevents, timeout,
39 static void signal_handler(int signum)
43 static void kill_timeout(struct epoll_mtcontext *ctx)
46 pthread_kill(ctx->main, SIGUSR1);
47 pthread_kill(ctx->waiter, SIGUSR1);
50 static void *waiter_entry1a(void *data)
53 struct epoll_mtcontext *ctx = data;
55 if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
56 __sync_fetch_and_add(&ctx->count, 1);
61 static void *waiter_entry1ap(void *data)
65 struct epoll_mtcontext *ctx = data;
69 if (poll(&pfd, 1, -1) > 0) {
70 if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
71 __sync_fetch_and_add(&ctx->count, 1);
77 static void *waiter_entry1o(void *data)
80 struct epoll_mtcontext *ctx = data;
82 if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
83 __sync_fetch_and_or(&ctx->count, 1);
88 static void *waiter_entry1op(void *data)
92 struct epoll_mtcontext *ctx = data;
96 if (poll(&pfd, 1, -1) > 0) {
97 if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
98 __sync_fetch_and_or(&ctx->count, 1);
104 static void *waiter_entry2a(void *data)
106 struct epoll_event events[2];
107 struct epoll_mtcontext *ctx = data;
109 if (epoll_wait(ctx->efd[0], events, 2, -1) > 0)
110 __sync_fetch_and_add(&ctx->count, 1);
115 static void *waiter_entry2ap(void *data)
118 struct epoll_event events[2];
119 struct epoll_mtcontext *ctx = data;
121 pfd.fd = ctx->efd[0];
123 if (poll(&pfd, 1, -1) > 0) {
124 if (epoll_wait(ctx->efd[0], events, 2, 0) > 0)
125 __sync_fetch_and_add(&ctx->count, 1);
131 static void *emitter_entry1(void *data)
133 struct epoll_mtcontext *ctx = data;
136 write(ctx->sfd[1], "w", 1);
143 static void *emitter_entry2(void *data)
145 struct epoll_mtcontext *ctx = data;
148 write(ctx->sfd[1], "w", 1);
149 write(ctx->sfd[3], "w", 1);
167 struct epoll_event e;
169 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
171 efd = epoll_create(1);
175 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
177 ASSERT_EQ(write(sfd[1], "w", 1), 1);
179 EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
180 EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
198 struct epoll_event e;
200 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
202 efd = epoll_create(1);
205 e.events = EPOLLIN | EPOLLET;
206 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
208 ASSERT_EQ(write(sfd[1], "w", 1), 1);
210 EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
211 EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 0);
229 struct epoll_event events[2];
231 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
232 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
234 efd = epoll_create(1);
237 events[0].events = EPOLLIN;
238 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
240 events[0].events = EPOLLIN;
241 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
243 ASSERT_EQ(write(sfd[1], "w", 1), 1);
244 ASSERT_EQ(write(sfd[3], "w", 1), 1);
246 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
247 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
267 struct epoll_event events[2];
269 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
270 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
272 efd = epoll_create(1);
275 events[0].events = EPOLLIN | EPOLLET;
276 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
278 events[0].events = EPOLLIN | EPOLLET;
279 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
281 ASSERT_EQ(write(sfd[1], "w", 1), 1);
282 ASSERT_EQ(write(sfd[3], "w", 1), 1);
284 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
285 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
306 struct epoll_event e;
308 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
310 efd = epoll_create(1);
314 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
316 ASSERT_EQ(write(sfd[1], "w", 1), 1);
320 ASSERT_EQ(poll(&pfd, 1, 0), 1);
321 ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
325 ASSERT_EQ(poll(&pfd, 1, 0), 1);
326 ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
345 struct epoll_event e;
347 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
349 efd = epoll_create(1);
352 e.events = EPOLLIN | EPOLLET;
353 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
355 ASSERT_EQ(write(sfd[1], "w", 1), 1);
359 ASSERT_EQ(poll(&pfd, 1, 0), 1);
360 ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
364 ASSERT_EQ(poll(&pfd, 1, 0), 0);
365 ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 0);
385 struct epoll_event events[2];
387 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
388 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
390 efd = epoll_create(1);
393 events[0].events = EPOLLIN;
394 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
396 events[0].events = EPOLLIN;
397 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
399 ASSERT_EQ(write(sfd[1], "w", 1), 1);
400 ASSERT_EQ(write(sfd[3], "w", 1), 1);
404 EXPECT_EQ(poll(&pfd, 1, 0), 1);
405 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
409 EXPECT_EQ(poll(&pfd, 1, 0), 1);
410 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
431 struct epoll_event events[2];
433 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
434 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
436 efd = epoll_create(1);
439 events[0].events = EPOLLIN | EPOLLET;
440 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
442 events[0].events = EPOLLIN | EPOLLET;
443 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
445 ASSERT_EQ(write(sfd[1], "w", 1), 1);
446 ASSERT_EQ(write(sfd[3], "w", 1), 1);
450 EXPECT_EQ(poll(&pfd, 1, 0), 1);
451 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
455 EXPECT_EQ(poll(&pfd, 1, 0), 0);
456 EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
475 struct epoll_event e;
476 struct epoll_mtcontext ctx = { 0 };
478 signal(SIGUSR1, signal_handler);
480 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
482 ctx.efd[0] = epoll_create(1);
483 ASSERT_GE(ctx.efd[0], 0);
486 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
488 ctx.main = pthread_self();
489 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
490 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
492 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
493 __sync_fetch_and_add(&ctx.count, 1);
495 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
496 EXPECT_EQ(ctx.count, 2);
498 if (pthread_tryjoin_np(emitter, NULL) < 0) {
499 pthread_kill(emitter, SIGUSR1);
500 pthread_join(emitter, NULL);
518 struct epoll_event e;
519 struct epoll_mtcontext ctx = { 0 };
521 signal(SIGUSR1, signal_handler);
523 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
525 ctx.efd[0] = epoll_create(1);
526 ASSERT_GE(ctx.efd[0], 0);
528 e.events = EPOLLIN | EPOLLET;
529 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
531 ctx.main = pthread_self();
532 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
533 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
535 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
536 __sync_fetch_and_add(&ctx.count, 1);
538 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
539 EXPECT_EQ(ctx.count, 1);
541 if (pthread_tryjoin_np(emitter, NULL) < 0) {
542 pthread_kill(emitter, SIGUSR1);
543 pthread_join(emitter, NULL);
561 struct epoll_event events[2];
562 struct epoll_mtcontext ctx = { 0 };
564 signal(SIGUSR1, signal_handler);
566 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
567 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
569 ctx.efd[0] = epoll_create(1);
570 ASSERT_GE(ctx.efd[0], 0);
572 events[0].events = EPOLLIN;
573 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
575 events[0].events = EPOLLIN;
576 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
578 ctx.main = pthread_self();
579 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2a, &ctx), 0);
580 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
582 if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
583 __sync_fetch_and_add(&ctx.count, 1);
585 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
586 EXPECT_EQ(ctx.count, 2);
588 if (pthread_tryjoin_np(emitter, NULL) < 0) {
589 pthread_kill(emitter, SIGUSR1);
590 pthread_join(emitter, NULL);
610 struct epoll_event events[2];
611 struct epoll_mtcontext ctx = { 0 };
613 signal(SIGUSR1, signal_handler);
615 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
616 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
618 ctx.efd[0] = epoll_create(1);
619 ASSERT_GE(ctx.efd[0], 0);
621 events[0].events = EPOLLIN | EPOLLET;
622 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
624 events[0].events = EPOLLIN | EPOLLET;
625 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
627 ctx.main = pthread_self();
628 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
629 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
631 if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
632 __sync_fetch_and_add(&ctx.count, 1);
634 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
635 EXPECT_EQ(ctx.count, 2);
637 if (pthread_tryjoin_np(emitter, NULL) < 0) {
638 pthread_kill(emitter, SIGUSR1);
639 pthread_join(emitter, NULL);
659 struct epoll_event e;
660 struct epoll_mtcontext ctx = { 0 };
662 signal(SIGUSR1, signal_handler);
664 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
666 ctx.efd[0] = epoll_create(1);
667 ASSERT_GE(ctx.efd[0], 0);
670 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
672 ctx.main = pthread_self();
673 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
674 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
676 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
677 __sync_fetch_and_add(&ctx.count, 1);
679 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
680 EXPECT_EQ(ctx.count, 2);
682 if (pthread_tryjoin_np(emitter, NULL) < 0) {
683 pthread_kill(emitter, SIGUSR1);
684 pthread_join(emitter, NULL);
702 struct epoll_event e;
703 struct epoll_mtcontext ctx = { 0 };
705 signal(SIGUSR1, signal_handler);
707 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
709 ctx.efd[0] = epoll_create(1);
710 ASSERT_GE(ctx.efd[0], 0);
712 e.events = EPOLLIN | EPOLLET;
713 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
715 ctx.main = pthread_self();
716 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
717 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
719 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
720 __sync_fetch_and_add(&ctx.count, 1);
722 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
723 EXPECT_EQ(ctx.count, 1);
725 if (pthread_tryjoin_np(emitter, NULL) < 0) {
726 pthread_kill(emitter, SIGUSR1);
727 pthread_join(emitter, NULL);
745 struct epoll_event events[2];
746 struct epoll_mtcontext ctx = { 0 };
748 signal(SIGUSR1, signal_handler);
750 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
751 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
753 ctx.efd[0] = epoll_create(1);
754 ASSERT_GE(ctx.efd[0], 0);
756 events[0].events = EPOLLIN;
757 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
759 events[0].events = EPOLLIN;
760 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
762 ctx.main = pthread_self();
763 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2ap, &ctx), 0);
764 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
766 if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
767 __sync_fetch_and_add(&ctx.count, 1);
769 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
770 EXPECT_EQ(ctx.count, 2);
772 if (pthread_tryjoin_np(emitter, NULL) < 0) {
773 pthread_kill(emitter, SIGUSR1);
774 pthread_join(emitter, NULL);
794 struct epoll_event events[2];
795 struct epoll_mtcontext ctx = { 0 };
797 signal(SIGUSR1, signal_handler);
799 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
800 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
802 ctx.efd[0] = epoll_create(1);
803 ASSERT_GE(ctx.efd[0], 0);
805 events[0].events = EPOLLIN | EPOLLET;
806 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
808 events[0].events = EPOLLIN | EPOLLET;
809 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
811 ctx.main = pthread_self();
812 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
813 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
815 if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
816 __sync_fetch_and_add(&ctx.count, 1);
818 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
819 EXPECT_EQ(ctx.count, 2);
821 if (pthread_tryjoin_np(emitter, NULL) < 0) {
822 pthread_kill(emitter, SIGUSR1);
823 pthread_join(emitter, NULL);
846 struct epoll_event e;
848 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
850 efd[0] = epoll_create(1);
851 ASSERT_GE(efd[0], 0);
853 efd[1] = epoll_create(1);
854 ASSERT_GE(efd[1], 0);
857 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
860 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
862 ASSERT_EQ(write(sfd[1], "w", 1), 1);
864 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
865 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
886 struct epoll_event e;
888 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
890 efd[0] = epoll_create(1);
891 ASSERT_GE(efd[0], 0);
893 efd[1] = epoll_create(1);
894 ASSERT_GE(efd[1], 0);
896 e.events = EPOLLIN | EPOLLET;
897 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
900 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
902 ASSERT_EQ(write(sfd[1], "w", 1), 1);
904 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
905 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
926 struct epoll_event e;
928 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
930 efd[0] = epoll_create(1);
931 ASSERT_GE(efd[0], 0);
933 efd[1] = epoll_create(1);
934 ASSERT_GE(efd[1], 0);
937 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
939 e.events = EPOLLIN | EPOLLET;
940 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
942 ASSERT_EQ(write(sfd[1], "w", 1), 1);
944 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
945 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
966 struct epoll_event e;
968 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
970 efd[0] = epoll_create(1);
971 ASSERT_GE(efd[0], 0);
973 efd[1] = epoll_create(1);
974 ASSERT_GE(efd[1], 0);
976 e.events = EPOLLIN | EPOLLET;
977 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
979 e.events = EPOLLIN | EPOLLET;
980 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
982 ASSERT_EQ(write(sfd[1], "w", 1), 1);
984 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
985 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1007 struct epoll_event e;
1009 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1011 efd[0] = epoll_create(1);
1012 ASSERT_GE(efd[0], 0);
1014 efd[1] = epoll_create(1);
1015 ASSERT_GE(efd[1], 0);
1018 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1021 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1023 ASSERT_EQ(write(sfd[1], "w", 1), 1);
1026 pfd.events = POLLIN;
1027 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1028 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1031 pfd.events = POLLIN;
1032 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1033 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1055 struct epoll_event e;
1057 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1059 efd[0] = epoll_create(1);
1060 ASSERT_GE(efd[0], 0);
1062 efd[1] = epoll_create(1);
1063 ASSERT_GE(efd[1], 0);
1065 e.events = EPOLLIN | EPOLLET;
1066 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1069 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1071 ASSERT_EQ(write(sfd[1], "w", 1), 1);
1074 pfd.events = POLLIN;
1075 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1076 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1079 pfd.events = POLLIN;
1080 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1081 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1103 struct epoll_event e;
1105 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1107 efd[0] = epoll_create(1);
1108 ASSERT_GE(efd[0], 0);
1110 efd[1] = epoll_create(1);
1111 ASSERT_GE(efd[1], 0);
1114 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1116 e.events = EPOLLIN | EPOLLET;
1117 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1119 ASSERT_EQ(write(sfd[1], "w", 1), 1);
1122 pfd.events = POLLIN;
1123 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1124 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1127 pfd.events = POLLIN;
1128 EXPECT_EQ(poll(&pfd, 1, 0), 0);
1129 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1151 struct epoll_event e;
1153 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1155 efd[0] = epoll_create(1);
1156 ASSERT_GE(efd[0], 0);
1158 efd[1] = epoll_create(1);
1159 ASSERT_GE(efd[1], 0);
1161 e.events = EPOLLIN | EPOLLET;
1162 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1164 e.events = EPOLLIN | EPOLLET;
1165 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1167 ASSERT_EQ(write(sfd[1], "w", 1), 1);
1170 pfd.events = POLLIN;
1171 EXPECT_EQ(poll(&pfd, 1, 0), 1);
1172 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1175 pfd.events = POLLIN;
1176 EXPECT_EQ(poll(&pfd, 1, 0), 0);
1177 EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1197 struct epoll_event e;
1198 struct epoll_mtcontext ctx = { 0 };
1200 signal(SIGUSR1, signal_handler);
1202 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1204 ctx.efd[0] = epoll_create(1);
1205 ASSERT_GE(ctx.efd[0], 0);
1207 ctx.efd[1] = epoll_create(1);
1208 ASSERT_GE(ctx.efd[1], 0);
1211 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1214 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1216 ctx.main = pthread_self();
1217 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1218 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1220 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1221 __sync_fetch_and_add(&ctx.count, 1);
1223 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1224 EXPECT_EQ(ctx.count, 2);
1226 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1227 pthread_kill(emitter, SIGUSR1);
1228 pthread_join(emitter, NULL);
1249 struct epoll_event e;
1250 struct epoll_mtcontext ctx = { 0 };
1252 signal(SIGUSR1, signal_handler);
1254 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1256 ctx.efd[0] = epoll_create(1);
1257 ASSERT_GE(ctx.efd[0], 0);
1259 ctx.efd[1] = epoll_create(1);
1260 ASSERT_GE(ctx.efd[1], 0);
1262 e.events = EPOLLIN | EPOLLET;
1263 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1266 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1268 ctx.main = pthread_self();
1269 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1270 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1272 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1273 __sync_fetch_and_add(&ctx.count, 1);
1275 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1276 EXPECT_EQ(ctx.count, 2);
1278 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1279 pthread_kill(emitter, SIGUSR1);
1280 pthread_join(emitter, NULL);
1301 struct epoll_event e;
1302 struct epoll_mtcontext ctx = { 0 };
1304 signal(SIGUSR1, signal_handler);
1306 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1308 ctx.efd[0] = epoll_create(1);
1309 ASSERT_GE(ctx.efd[0], 0);
1311 ctx.efd[1] = epoll_create(1);
1312 ASSERT_GE(ctx.efd[1], 0);
1315 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1317 e.events = EPOLLIN | EPOLLET;
1318 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1320 ctx.main = pthread_self();
1321 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1322 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1324 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1325 __sync_fetch_and_add(&ctx.count, 1);
1327 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1328 EXPECT_EQ(ctx.count, 1);
1330 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1331 pthread_kill(emitter, SIGUSR1);
1332 pthread_join(emitter, NULL);
1353 struct epoll_event e;
1354 struct epoll_mtcontext ctx = { 0 };
1356 signal(SIGUSR1, signal_handler);
1358 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1360 ctx.efd[0] = epoll_create(1);
1361 ASSERT_GE(ctx.efd[0], 0);
1363 ctx.efd[1] = epoll_create(1);
1364 ASSERT_GE(ctx.efd[1], 0);
1366 e.events = EPOLLIN | EPOLLET;
1367 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1369 e.events = EPOLLIN | EPOLLET;
1370 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1372 ctx.main = pthread_self();
1373 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1374 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1376 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1377 __sync_fetch_and_add(&ctx.count, 1);
1379 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1380 EXPECT_EQ(ctx.count, 1);
1382 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1383 pthread_kill(emitter, SIGUSR1);
1384 pthread_join(emitter, NULL);
1405 struct epoll_event e;
1406 struct epoll_mtcontext ctx = { 0 };
1408 signal(SIGUSR1, signal_handler);
1410 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1412 ctx.efd[0] = epoll_create(1);
1413 ASSERT_GE(ctx.efd[0], 0);
1415 ctx.efd[1] = epoll_create(1);
1416 ASSERT_GE(ctx.efd[1], 0);
1419 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1422 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1424 ctx.main = pthread_self();
1425 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1426 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1428 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1429 __sync_fetch_and_add(&ctx.count, 1);
1431 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1432 EXPECT_EQ(ctx.count, 2);
1434 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1435 pthread_kill(emitter, SIGUSR1);
1436 pthread_join(emitter, NULL);
1456 struct epoll_event e;
1457 struct epoll_mtcontext ctx = { 0 };
1459 signal(SIGUSR1, signal_handler);
1461 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1463 ctx.efd[0] = epoll_create(1);
1464 ASSERT_GE(ctx.efd[0], 0);
1466 ctx.efd[1] = epoll_create(1);
1467 ASSERT_GE(ctx.efd[1], 0);
1469 e.events = EPOLLIN | EPOLLET;
1470 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1473 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1475 ctx.main = pthread_self();
1476 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1477 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1479 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1480 __sync_fetch_and_add(&ctx.count, 1);
1482 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1483 EXPECT_EQ(ctx.count, 2);
1485 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1486 pthread_kill(emitter, SIGUSR1);
1487 pthread_join(emitter, NULL);
1507 struct epoll_event e;
1508 struct epoll_mtcontext ctx = { 0 };
1510 signal(SIGUSR1, signal_handler);
1512 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1514 ctx.efd[0] = epoll_create(1);
1515 ASSERT_GE(ctx.efd[0], 0);
1517 ctx.efd[1] = epoll_create(1);
1518 ASSERT_GE(ctx.efd[1], 0);
1521 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1523 e.events = EPOLLIN | EPOLLET;
1524 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1526 ctx.main = pthread_self();
1527 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1528 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1530 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1531 __sync_fetch_and_add(&ctx.count, 1);
1533 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1534 EXPECT_EQ(ctx.count, 1);
1536 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1537 pthread_kill(emitter, SIGUSR1);
1538 pthread_join(emitter, NULL);
1558 struct epoll_event e;
1559 struct epoll_mtcontext ctx = { 0 };
1561 signal(SIGUSR1, signal_handler);
1563 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1565 ctx.efd[0] = epoll_create(1);
1566 ASSERT_GE(ctx.efd[0], 0);
1568 ctx.efd[1] = epoll_create(1);
1569 ASSERT_GE(ctx.efd[1], 0);
1571 e.events = EPOLLIN | EPOLLET;
1572 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1574 e.events = EPOLLIN | EPOLLET;
1575 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1577 ctx.main = pthread_self();
1578 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1579 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1581 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1582 __sync_fetch_and_add(&ctx.count, 1);
1584 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1585 EXPECT_EQ(ctx.count, 1);
1587 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1588 pthread_kill(emitter, SIGUSR1);
1589 pthread_join(emitter, NULL);
1609 struct epoll_event e;
1610 struct epoll_mtcontext ctx = { 0 };
1612 signal(SIGUSR1, signal_handler);
1614 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1616 ctx.efd[0] = epoll_create(1);
1617 ASSERT_GE(ctx.efd[0], 0);
1619 ctx.efd[1] = epoll_create(1);
1620 ASSERT_GE(ctx.efd[1], 0);
1623 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1626 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1628 ctx.main = pthread_self();
1629 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1630 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1632 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1633 __sync_fetch_and_add(&ctx.count, 1);
1635 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1636 EXPECT_EQ(ctx.count, 2);
1638 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1639 pthread_kill(emitter, SIGUSR1);
1640 pthread_join(emitter, NULL);
1661 struct epoll_event e;
1662 struct epoll_mtcontext ctx = { 0 };
1664 signal(SIGUSR1, signal_handler);
1666 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1668 ctx.efd[0] = epoll_create(1);
1669 ASSERT_GE(ctx.efd[0], 0);
1671 ctx.efd[1] = epoll_create(1);
1672 ASSERT_GE(ctx.efd[1], 0);
1674 e.events = EPOLLIN | EPOLLET;
1675 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1678 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1680 ctx.main = pthread_self();
1681 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1682 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1684 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1685 __sync_fetch_and_or(&ctx.count, 2);
1687 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1688 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1690 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1691 pthread_kill(emitter, SIGUSR1);
1692 pthread_join(emitter, NULL);
1713 struct epoll_event e;
1714 struct epoll_mtcontext ctx = { 0 };
1716 signal(SIGUSR1, signal_handler);
1718 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1720 ctx.efd[0] = epoll_create(1);
1721 ASSERT_GE(ctx.efd[0], 0);
1723 ctx.efd[1] = epoll_create(1);
1724 ASSERT_GE(ctx.efd[1], 0);
1727 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1729 e.events = EPOLLIN | EPOLLET;
1730 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1732 ctx.main = pthread_self();
1733 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1734 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1736 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1737 __sync_fetch_and_add(&ctx.count, 1);
1739 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1740 EXPECT_EQ(ctx.count, 2);
1742 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1743 pthread_kill(emitter, SIGUSR1);
1744 pthread_join(emitter, NULL);
1765 struct epoll_event e;
1766 struct epoll_mtcontext ctx = { 0 };
1768 signal(SIGUSR1, signal_handler);
1770 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1772 ctx.efd[0] = epoll_create(1);
1773 ASSERT_GE(ctx.efd[0], 0);
1775 ctx.efd[1] = epoll_create(1);
1776 ASSERT_GE(ctx.efd[1], 0);
1778 e.events = EPOLLIN | EPOLLET;
1779 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1781 e.events = EPOLLIN | EPOLLET;
1782 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1784 ctx.main = pthread_self();
1785 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1786 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1788 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1789 __sync_fetch_and_or(&ctx.count, 2);
1791 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1792 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1794 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1795 pthread_kill(emitter, SIGUSR1);
1796 pthread_join(emitter, NULL);
1818 struct epoll_event e;
1819 struct epoll_mtcontext ctx = { 0 };
1821 signal(SIGUSR1, signal_handler);
1823 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1825 ctx.efd[0] = epoll_create(1);
1826 ASSERT_GE(ctx.efd[0], 0);
1828 ctx.efd[1] = epoll_create(1);
1829 ASSERT_GE(ctx.efd[1], 0);
1832 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1835 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1837 ctx.main = pthread_self();
1838 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1839 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1841 pfd.fd = ctx.efd[1];
1842 pfd.events = POLLIN;
1843 if (poll(&pfd, 1, -1) > 0) {
1844 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1845 __sync_fetch_and_add(&ctx.count, 1);
1848 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1849 EXPECT_EQ(ctx.count, 2);
1851 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1852 pthread_kill(emitter, SIGUSR1);
1853 pthread_join(emitter, NULL);
1875 struct epoll_event e;
1876 struct epoll_mtcontext ctx = { 0 };
1878 signal(SIGUSR1, signal_handler);
1880 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1882 ctx.efd[0] = epoll_create(1);
1883 ASSERT_GE(ctx.efd[0], 0);
1885 ctx.efd[1] = epoll_create(1);
1886 ASSERT_GE(ctx.efd[1], 0);
1888 e.events = EPOLLIN | EPOLLET;
1889 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1892 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1894 ctx.main = pthread_self();
1895 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1896 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1898 pfd.fd = ctx.efd[1];
1899 pfd.events = POLLIN;
1900 if (poll(&pfd, 1, -1) > 0) {
1901 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1902 __sync_fetch_and_or(&ctx.count, 2);
1905 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1906 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1908 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1909 pthread_kill(emitter, SIGUSR1);
1910 pthread_join(emitter, NULL);
1932 struct epoll_event e;
1933 struct epoll_mtcontext ctx = { 0 };
1935 signal(SIGUSR1, signal_handler);
1937 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1939 ctx.efd[0] = epoll_create(1);
1940 ASSERT_GE(ctx.efd[0], 0);
1942 ctx.efd[1] = epoll_create(1);
1943 ASSERT_GE(ctx.efd[1], 0);
1946 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1948 e.events = EPOLLIN | EPOLLET;
1949 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1951 ctx.main = pthread_self();
1952 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1953 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1955 pfd.fd = ctx.efd[1];
1956 pfd.events = POLLIN;
1957 if (poll(&pfd, 1, -1) > 0) {
1958 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1959 __sync_fetch_and_add(&ctx.count, 1);
1962 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1963 EXPECT_EQ(ctx.count, 2);
1965 if (pthread_tryjoin_np(emitter, NULL) < 0) {
1966 pthread_kill(emitter, SIGUSR1);
1967 pthread_join(emitter, NULL);
1989 struct epoll_event e;
1990 struct epoll_mtcontext ctx = { 0 };
1992 signal(SIGUSR1, signal_handler);
1994 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1996 ctx.efd[0] = epoll_create(1);
1997 ASSERT_GE(ctx.efd[0], 0);
1999 ctx.efd[1] = epoll_create(1);
2000 ASSERT_GE(ctx.efd[1], 0);
2002 e.events = EPOLLIN | EPOLLET;
2003 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2005 e.events = EPOLLIN | EPOLLET;
2006 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2008 ctx.main = pthread_self();
2009 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
2010 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2012 pfd.fd = ctx.efd[1];
2013 pfd.events = POLLIN;
2014 if (poll(&pfd, 1, -1) > 0) {
2015 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2016 __sync_fetch_and_or(&ctx.count, 2);
2019 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2020 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2022 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2023 pthread_kill(emitter, SIGUSR1);
2024 pthread_join(emitter, NULL);
2045 struct epoll_event e;
2046 struct epoll_mtcontext ctx = { 0 };
2048 signal(SIGUSR1, signal_handler);
2050 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2052 ctx.efd[0] = epoll_create(1);
2053 ASSERT_GE(ctx.efd[0], 0);
2055 ctx.efd[1] = epoll_create(1);
2056 ASSERT_GE(ctx.efd[1], 0);
2059 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2062 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2064 ctx.main = pthread_self();
2065 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2066 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2068 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2069 __sync_fetch_and_add(&ctx.count, 1);
2071 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2072 EXPECT_EQ(ctx.count, 2);
2074 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2075 pthread_kill(emitter, SIGUSR1);
2076 pthread_join(emitter, NULL);
2097 struct epoll_event e;
2098 struct epoll_mtcontext ctx = { 0 };
2100 signal(SIGUSR1, signal_handler);
2102 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2104 ctx.efd[0] = epoll_create(1);
2105 ASSERT_GE(ctx.efd[0], 0);
2107 ctx.efd[1] = epoll_create(1);
2108 ASSERT_GE(ctx.efd[1], 0);
2110 e.events = EPOLLIN | EPOLLET;
2111 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2114 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2116 ctx.main = pthread_self();
2117 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2118 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2120 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2121 __sync_fetch_and_or(&ctx.count, 2);
2123 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2124 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2126 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2127 pthread_kill(emitter, SIGUSR1);
2128 pthread_join(emitter, NULL);
2149 struct epoll_event e;
2150 struct epoll_mtcontext ctx = { 0 };
2152 signal(SIGUSR1, signal_handler);
2154 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2156 ctx.efd[0] = epoll_create(1);
2157 ASSERT_GE(ctx.efd[0], 0);
2159 ctx.efd[1] = epoll_create(1);
2160 ASSERT_GE(ctx.efd[1], 0);
2163 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2165 e.events = EPOLLIN | EPOLLET;
2166 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2168 ctx.main = pthread_self();
2169 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2170 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2172 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2173 __sync_fetch_and_add(&ctx.count, 1);
2175 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2176 EXPECT_EQ(ctx.count, 2);
2178 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2179 pthread_kill(emitter, SIGUSR1);
2180 pthread_join(emitter, NULL);
2201 struct epoll_event e;
2202 struct epoll_mtcontext ctx = { 0 };
2204 signal(SIGUSR1, signal_handler);
2206 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2208 ctx.efd[0] = epoll_create(1);
2209 ASSERT_GE(ctx.efd[0], 0);
2211 ctx.efd[1] = epoll_create(1);
2212 ASSERT_GE(ctx.efd[1], 0);
2214 e.events = EPOLLIN | EPOLLET;
2215 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2217 e.events = EPOLLIN | EPOLLET;
2218 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2220 ctx.main = pthread_self();
2221 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2222 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2224 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2225 __sync_fetch_and_or(&ctx.count, 2);
2227 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2228 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2230 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2231 pthread_kill(emitter, SIGUSR1);
2232 pthread_join(emitter, NULL);
2254 struct epoll_event e;
2255 struct epoll_mtcontext ctx = { 0 };
2257 signal(SIGUSR1, signal_handler);
2259 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2261 ctx.efd[0] = epoll_create(1);
2262 ASSERT_GE(ctx.efd[0], 0);
2264 ctx.efd[1] = epoll_create(1);
2265 ASSERT_GE(ctx.efd[1], 0);
2268 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2271 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2273 ctx.main = pthread_self();
2274 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2275 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2277 pfd.fd = ctx.efd[1];
2278 pfd.events = POLLIN;
2279 if (poll(&pfd, 1, -1) > 0) {
2280 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2281 __sync_fetch_and_add(&ctx.count, 1);
2284 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2285 EXPECT_EQ(ctx.count, 2);
2287 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2288 pthread_kill(emitter, SIGUSR1);
2289 pthread_join(emitter, NULL);
2310 struct epoll_event e;
2311 struct epoll_mtcontext ctx = { 0 };
2313 signal(SIGUSR1, signal_handler);
2315 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2317 ctx.efd[0] = epoll_create(1);
2318 ASSERT_GE(ctx.efd[0], 0);
2320 ctx.efd[1] = epoll_create(1);
2321 ASSERT_GE(ctx.efd[1], 0);
2323 e.events = EPOLLIN | EPOLLET;
2324 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2327 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2329 ctx.main = pthread_self();
2330 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2331 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2333 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2334 __sync_fetch_and_or(&ctx.count, 2);
2336 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2337 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2339 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2340 pthread_kill(emitter, SIGUSR1);
2341 pthread_join(emitter, NULL);
2363 struct epoll_event e;
2364 struct epoll_mtcontext ctx = { 0 };
2366 signal(SIGUSR1, signal_handler);
2368 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2370 ctx.efd[0] = epoll_create(1);
2371 ASSERT_GE(ctx.efd[0], 0);
2373 ctx.efd[1] = epoll_create(1);
2374 ASSERT_GE(ctx.efd[1], 0);
2377 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2379 e.events = EPOLLIN | EPOLLET;
2380 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2382 ctx.main = pthread_self();
2383 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2384 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2386 pfd.fd = ctx.efd[1];
2387 pfd.events = POLLIN;
2388 if (poll(&pfd, 1, -1) > 0) {
2389 if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2390 __sync_fetch_and_add(&ctx.count, 1);
2393 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2394 EXPECT_EQ(ctx.count, 2);
2396 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2397 pthread_kill(emitter, SIGUSR1);
2398 pthread_join(emitter, NULL);
2419 struct epoll_event e;
2420 struct epoll_mtcontext ctx = { 0 };
2422 signal(SIGUSR1, signal_handler);
2424 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2426 ctx.efd[0] = epoll_create(1);
2427 ASSERT_GE(ctx.efd[0], 0);
2429 ctx.efd[1] = epoll_create(1);
2430 ASSERT_GE(ctx.efd[1], 0);
2432 e.events = EPOLLIN | EPOLLET;
2433 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2435 e.events = EPOLLIN | EPOLLET;
2436 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2438 ctx.main = pthread_self();
2439 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2440 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2442 if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2443 __sync_fetch_and_or(&ctx.count, 2);
2445 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2446 EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2448 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2449 pthread_kill(emitter, SIGUSR1);
2450 pthread_join(emitter, NULL);
2472 struct epoll_event events[2];
2474 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2475 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2477 efd[0] = epoll_create(1);
2478 ASSERT_GE(efd[0], 0);
2480 efd[1] = epoll_create(1);
2481 ASSERT_GE(efd[1], 0);
2483 efd[2] = epoll_create(1);
2484 ASSERT_GE(efd[2], 0);
2486 events[0].events = EPOLLIN;
2487 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2489 events[0].events = EPOLLIN;
2490 ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2492 events[0].events = EPOLLIN;
2493 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2495 events[0].events = EPOLLIN;
2496 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2498 ASSERT_EQ(write(sfd[1], "w", 1), 1);
2499 ASSERT_EQ(write(sfd[3], "w", 1), 1);
2501 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2502 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2526 struct epoll_event events[2];
2528 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2529 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2531 efd[0] = epoll_create(1);
2532 ASSERT_GE(efd[0], 0);
2534 efd[1] = epoll_create(1);
2535 ASSERT_GE(efd[1], 0);
2537 efd[2] = epoll_create(1);
2538 ASSERT_GE(efd[2], 0);
2540 events[0].events = EPOLLIN;
2541 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2543 events[0].events = EPOLLIN;
2544 ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2546 events[0].events = EPOLLIN | EPOLLET;
2547 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2549 events[0].events = EPOLLIN | EPOLLET;
2550 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2552 ASSERT_EQ(write(sfd[1], "w", 1), 1);
2553 ASSERT_EQ(write(sfd[3], "w", 1), 1);
2555 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2556 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2581 struct epoll_event events[2];
2583 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2584 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2586 efd[0] = epoll_create(1);
2587 ASSERT_GE(efd[0], 0);
2589 efd[1] = epoll_create(1);
2590 ASSERT_GE(efd[1], 0);
2592 efd[2] = epoll_create(1);
2593 ASSERT_GE(efd[2], 0);
2595 events[0].events = EPOLLIN;
2596 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2598 events[0].events = EPOLLIN;
2599 ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2601 events[0].events = EPOLLIN;
2602 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2604 events[0].events = EPOLLIN;
2605 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2607 ASSERT_EQ(write(sfd[1], "w", 1), 1);
2608 ASSERT_EQ(write(sfd[3], "w", 1), 1);
2611 pfd.events = POLLIN;
2612 EXPECT_EQ(poll(&pfd, 1, 0), 1);
2613 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2616 pfd.events = POLLIN;
2617 EXPECT_EQ(poll(&pfd, 1, 0), 1);
2618 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2643 struct epoll_event events[2];
2645 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2646 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2648 efd[0] = epoll_create(1);
2649 ASSERT_GE(efd[0], 0);
2651 efd[1] = epoll_create(1);
2652 ASSERT_GE(efd[1], 0);
2654 efd[2] = epoll_create(1);
2655 ASSERT_GE(efd[2], 0);
2657 events[0].events = EPOLLIN;
2658 ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2660 events[0].events = EPOLLIN;
2661 ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2663 events[0].events = EPOLLIN | EPOLLET;
2664 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2666 events[0].events = EPOLLIN | EPOLLET;
2667 ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2669 ASSERT_EQ(write(sfd[1], "w", 1), 1);
2670 ASSERT_EQ(write(sfd[3], "w", 1), 1);
2673 pfd.events = POLLIN;
2674 EXPECT_EQ(poll(&pfd, 1, 0), 1);
2675 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2678 pfd.events = POLLIN;
2679 EXPECT_EQ(poll(&pfd, 1, 0), 0);
2680 EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2703 struct epoll_event e;
2704 struct epoll_mtcontext ctx = { 0 };
2706 signal(SIGUSR1, signal_handler);
2708 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2709 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2711 ctx.efd[0] = epoll_create(1);
2712 ASSERT_GE(ctx.efd[0], 0);
2714 ctx.efd[1] = epoll_create(1);
2715 ASSERT_GE(ctx.efd[1], 0);
2717 ctx.efd[2] = epoll_create(1);
2718 ASSERT_GE(ctx.efd[2], 0);
2721 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2724 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2727 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2730 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2732 ctx.main = pthread_self();
2733 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2734 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2736 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2737 __sync_fetch_and_add(&ctx.count, 1);
2739 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2740 EXPECT_EQ(ctx.count, 2);
2742 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2743 pthread_kill(emitter, SIGUSR1);
2744 pthread_join(emitter, NULL);
2768 struct epoll_event e;
2769 struct epoll_mtcontext ctx = { 0 };
2771 signal(SIGUSR1, signal_handler);
2773 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2774 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2776 ctx.efd[0] = epoll_create(1);
2777 ASSERT_GE(ctx.efd[0], 0);
2779 ctx.efd[1] = epoll_create(1);
2780 ASSERT_GE(ctx.efd[1], 0);
2782 ctx.efd[2] = epoll_create(1);
2783 ASSERT_GE(ctx.efd[2], 0);
2786 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2789 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2791 e.events = EPOLLIN | EPOLLET;
2792 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2794 e.events = EPOLLIN | EPOLLET;
2795 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2797 ctx.main = pthread_self();
2798 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2799 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2801 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2802 __sync_fetch_and_add(&ctx.count, 1);
2804 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2805 EXPECT_EQ(ctx.count, 2);
2807 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2808 pthread_kill(emitter, SIGUSR1);
2809 pthread_join(emitter, NULL);
2833 struct epoll_event e;
2834 struct epoll_mtcontext ctx = { 0 };
2836 signal(SIGUSR1, signal_handler);
2838 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2839 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2841 ctx.efd[0] = epoll_create(1);
2842 ASSERT_GE(ctx.efd[0], 0);
2844 ctx.efd[1] = epoll_create(1);
2845 ASSERT_GE(ctx.efd[1], 0);
2847 ctx.efd[2] = epoll_create(1);
2848 ASSERT_GE(ctx.efd[2], 0);
2851 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2854 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2857 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2860 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2862 ctx.main = pthread_self();
2863 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2864 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2866 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2867 __sync_fetch_and_add(&ctx.count, 1);
2869 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2870 EXPECT_EQ(ctx.count, 2);
2872 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2873 pthread_kill(emitter, SIGUSR1);
2874 pthread_join(emitter, NULL);
2898 struct epoll_event e;
2899 struct epoll_mtcontext ctx = { 0 };
2901 signal(SIGUSR1, signal_handler);
2903 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2904 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2906 ctx.efd[0] = epoll_create(1);
2907 ASSERT_GE(ctx.efd[0], 0);
2909 ctx.efd[1] = epoll_create(1);
2910 ASSERT_GE(ctx.efd[1], 0);
2912 ctx.efd[2] = epoll_create(1);
2913 ASSERT_GE(ctx.efd[2], 0);
2916 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2919 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2921 e.events = EPOLLIN | EPOLLET;
2922 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2924 e.events = EPOLLIN | EPOLLET;
2925 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2927 ctx.main = pthread_self();
2928 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2929 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2931 if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2932 __sync_fetch_and_add(&ctx.count, 1);
2934 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2935 EXPECT_EQ(ctx.count, 2);
2937 if (pthread_tryjoin_np(emitter, NULL) < 0) {
2938 pthread_kill(emitter, SIGUSR1);
2939 pthread_join(emitter, NULL);
2964 struct epoll_event e;
2965 struct epoll_mtcontext ctx = { 0 };
2967 signal(SIGUSR1, signal_handler);
2969 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2970 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2972 ctx.efd[0] = epoll_create(1);
2973 ASSERT_GE(ctx.efd[0], 0);
2975 ctx.efd[1] = epoll_create(1);
2976 ASSERT_GE(ctx.efd[1], 0);
2978 ctx.efd[2] = epoll_create(1);
2979 ASSERT_GE(ctx.efd[2], 0);
2982 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2985 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2988 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2991 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2993 ctx.main = pthread_self();
2994 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2995 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2997 pfd.fd = ctx.efd[0];
2998 pfd.events = POLLIN;
2999 if (poll(&pfd, 1, -1) > 0) {
3000 if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
3001 __sync_fetch_and_add(&ctx.count, 1);
3004 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
3005 EXPECT_EQ(ctx.count, 2);
3007 if (pthread_tryjoin_np(emitter, NULL) < 0) {
3008 pthread_kill(emitter, SIGUSR1);
3009 pthread_join(emitter, NULL);
3034 struct epoll_event e;
3035 struct epoll_mtcontext ctx = { 0 };
3037 signal(SIGUSR1, signal_handler);
3039 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
3040 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
3042 ctx.efd[0] = epoll_create(1);
3043 ASSERT_GE(ctx.efd[0], 0);
3045 ctx.efd[1] = epoll_create(1);
3046 ASSERT_GE(ctx.efd[1], 0);
3048 ctx.efd[2] = epoll_create(1);
3049 ASSERT_GE(ctx.efd[2], 0);
3052 ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3055 ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
3057 e.events = EPOLLIN | EPOLLET;
3058 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
3060 e.events = EPOLLIN | EPOLLET;
3061 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
3063 ctx.main = pthread_self();
3064 ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
3065 ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
3067 pfd.fd = ctx.efd[0];
3068 pfd.events = POLLIN;
3069 if (poll(&pfd, 1, -1) > 0) {
3070 if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
3071 __sync_fetch_and_add(&ctx.count, 1);
3074 ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
3075 EXPECT_EQ(ctx.count, 2);
3077 if (pthread_tryjoin_np(emitter, NULL) < 0) {
3078 pthread_kill(emitter, SIGUSR1);
3079 pthread_join(emitter, NULL);
3091 static void *epoll59_thread(void *ctx_)
3093 struct epoll_mtcontext *ctx = ctx_;
3094 struct epoll_event e;
3097 for (i = 0; i < 100000; i++) {
3098 while (ctx->count == 0)
3101 e.events = EPOLLIN | EPOLLERR | EPOLLET;
3102 epoll_ctl(ctx->efd[0], EPOLL_CTL_MOD, ctx->sfd[0], &e);
3116 * Based on https://bugzilla.kernel.org/show_bug.cgi?id=205933
3122 struct epoll_event e;
3123 struct epoll_mtcontext ctx = { 0 };
3126 signal(SIGUSR1, signal_handler);
3128 ctx.efd[0] = epoll_create1(0);
3129 ASSERT_GE(ctx.efd[0], 0);
3131 ctx.sfd[0] = eventfd(1, 0);
3132 ASSERT_GE(ctx.sfd[0], 0);
3134 e.events = EPOLLIN | EPOLLERR | EPOLLET;
3135 ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3137 ASSERT_EQ(pthread_create(&emitter, NULL, epoll59_thread, &ctx), 0);
3139 for (i = 0; i < 100000; i++) {
3140 ret = epoll_wait(ctx.efd[0], &e, 1, 1000);
3143 while (ctx.count != 0)
3147 if (pthread_tryjoin_np(emitter, NULL) < 0) {
3148 pthread_kill(emitter, SIGUSR1);
3149 pthread_join(emitter, NULL);
3156 EPOLL60_EVENTS_NR = 10,
3159 struct epoll60_ctx {
3160 volatile int stopped;
3164 int evfd[EPOLL60_EVENTS_NR];
3167 static void *epoll60_wait_thread(void *ctx_)
3169 struct epoll60_ctx *ctx = ctx_;
3170 struct epoll_event e;
3176 sigemptyset(&sigmask);
3177 sigaddset(&sigmask, SIGUSR1);
3178 sigprocmask(SIG_SETMASK, &sigmask, NULL);
3180 /* Prepare empty mask for epoll_pwait() */
3181 sigemptyset(&sigmask);
3183 while (!ctx->stopped) {
3184 /* Mark we are ready */
3185 __atomic_fetch_add(&ctx->ready, 1, __ATOMIC_ACQUIRE);
3187 /* Start when all are ready */
3188 while (__atomic_load_n(&ctx->ready, __ATOMIC_ACQUIRE) &&
3191 /* Account this waiter */
3192 __atomic_fetch_add(&ctx->waiters, 1, __ATOMIC_ACQUIRE);
3194 ret = epoll_pwait(ctx->epfd, &e, 1, 2000, &sigmask);
3196 /* We expect only signal delivery on stop */
3197 assert(ret < 0 && errno == EINTR && "Lost wakeup!\n");
3198 assert(ctx->stopped);
3202 ret = read(e.data.fd, &v, sizeof(v));
3203 /* Since we are on ET mode, thus each thread gets its own fd. */
3204 assert(ret == sizeof(v));
3206 __atomic_fetch_sub(&ctx->waiters, 1, __ATOMIC_RELEASE);
3212 static inline unsigned long long msecs(void)
3215 unsigned long long msecs;
3217 clock_gettime(CLOCK_REALTIME, &ts);
3218 msecs = ts.tv_sec * 1000ull;
3219 msecs += ts.tv_nsec / 1000000ull;
3224 static inline int count_waiters(struct epoll60_ctx *ctx)
3226 return __atomic_load_n(&ctx->waiters, __ATOMIC_ACQUIRE);
3231 struct epoll60_ctx ctx = { 0 };
3232 pthread_t waiters[ARRAY_SIZE(ctx.evfd)];
3233 struct epoll_event e;
3236 signal(SIGUSR1, signal_handler);
3238 ctx.epfd = epoll_create1(0);
3239 ASSERT_GE(ctx.epfd, 0);
3241 /* Create event fds */
3242 for (i = 0; i < ARRAY_SIZE(ctx.evfd); i++) {
3243 ctx.evfd[i] = eventfd(0, EFD_NONBLOCK);
3244 ASSERT_GE(ctx.evfd[i], 0);
3246 e.events = EPOLLIN | EPOLLET;
3247 e.data.fd = ctx.evfd[i];
3248 ASSERT_EQ(epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, ctx.evfd[i], &e), 0);
3251 /* Create waiter threads */
3252 for (i = 0; i < ARRAY_SIZE(waiters); i++)
3253 ASSERT_EQ(pthread_create(&waiters[i], NULL,
3254 epoll60_wait_thread, &ctx), 0);
3256 for (i = 0; i < 300; i++) {
3259 /* Wait for all to be ready */
3260 while (__atomic_load_n(&ctx.ready, __ATOMIC_ACQUIRE) !=
3261 ARRAY_SIZE(ctx.evfd))
3265 __atomic_fetch_sub(&ctx.ready, ARRAY_SIZE(ctx.evfd),
3268 /* Wait all have gone to kernel */
3269 while (count_waiters(&ctx) != ARRAY_SIZE(ctx.evfd))
3272 /* 1ms should be enough to schedule away */
3275 /* Quickly signal all handles at once */
3276 for (n = 0; n < ARRAY_SIZE(ctx.evfd); n++) {
3277 ret = write(ctx.evfd[n], &v, sizeof(v));
3278 ASSERT_EQ(ret, sizeof(v));
3281 /* Busy loop for 1s and wait for all waiters to wake up */
3283 while (count_waiters(&ctx) && msecs() < ms + 1000)
3286 ASSERT_EQ(count_waiters(&ctx), 0);
3290 for (i = 0; i < ARRAY_SIZE(waiters); i++)
3291 ret = pthread_kill(waiters[i], SIGUSR1);
3292 for (i = 0; i < ARRAY_SIZE(waiters); i++)
3293 pthread_join(waiters[i], NULL);
3295 for (i = 0; i < ARRAY_SIZE(waiters); i++)
3300 struct epoll61_ctx {
3305 static void *epoll61_write_eventfd(void *ctx_)
3307 struct epoll61_ctx *ctx = ctx_;
3311 write(ctx->evfd, &l, sizeof(l));
3315 static void *epoll61_epoll_with_timeout(void *ctx_)
3317 struct epoll61_ctx *ctx = ctx_;
3318 struct epoll_event events[1];
3321 n = epoll_wait(ctx->epfd, events, 1, 11);
3323 * If epoll returned the eventfd, write on the eventfd to wake up the
3329 write(ctx->evfd, &l, sizeof(l));
3334 static void *epoll61_blocking_epoll(void *ctx_)
3336 struct epoll61_ctx *ctx = ctx_;
3337 struct epoll_event events[1];
3339 epoll_wait(ctx->epfd, events, 1, -1);
3345 struct epoll61_ctx ctx;
3346 struct epoll_event ev;
3349 ctx.epfd = epoll_create1(0);
3350 ASSERT_GE(ctx.epfd, 0);
3351 ctx.evfd = eventfd(0, EFD_NONBLOCK);
3352 ASSERT_GE(ctx.evfd, 0);
3354 ev.events = EPOLLIN | EPOLLET | EPOLLERR | EPOLLHUP;
3356 r = epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, ctx.evfd, &ev);
3360 * We are testing a race. Repeat the test case 1000 times to make it
3361 * more likely to fail in case of a bug.
3363 for (i = 0; i < 1000; i++) {
3364 pthread_t threads[3];
3369 * Thread 1 sleeps for 10.9ms and writes to the evenfd.
3370 * Thread 2 calls epoll with a timeout of 11ms.
3371 * Thread 3 calls epoll with a timeout of -1.
3373 * The eventfd write by Thread 1 should either wakeup Thread 2
3374 * or Thread 3. If it wakes up Thread 2, Thread 2 writes on the
3375 * eventfd to wake up Thread 3.
3377 * If no events are missed, all three threads should eventually
3380 ASSERT_EQ(pthread_create(&threads[0], NULL,
3381 epoll61_write_eventfd, &ctx), 0);
3382 ASSERT_EQ(pthread_create(&threads[1], NULL,
3383 epoll61_epoll_with_timeout, &ctx), 0);
3384 ASSERT_EQ(pthread_create(&threads[2], NULL,
3385 epoll61_blocking_epoll, &ctx), 0);
3387 for (n = 0; n < ARRAY_SIZE(threads); ++n)
3388 ASSERT_EQ(pthread_join(threads[n], NULL), 0);
3395 /* Equivalent to basic test epoll1, but exercising epoll_pwait2. */
3400 struct epoll_event e;
3402 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
3404 efd = epoll_create(1);
3408 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
3410 ASSERT_EQ(write(sfd[1], "w", 1), 1);
3412 EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL, 0), 1);
3413 EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL, 0), 1);
3420 /* Epoll_pwait2 basic timeout test. */
3423 const int cfg_delay_ms = 10;
3424 unsigned long long tdiff;
3425 struct __kernel_timespec ts;
3428 struct epoll_event e;
3430 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
3432 efd = epoll_create(1);
3436 ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
3439 ts.tv_nsec = cfg_delay_ms * 1000 * 1000;
3442 EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, &ts, NULL, 0), 0);
3443 tdiff = msecs() - tdiff;
3445 EXPECT_GE(tdiff, cfg_delay_ms);