Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #define _GNU_SOURCE
0004 #include <asm/unistd.h>
0005 #include <linux/time_types.h>
0006 #include <poll.h>
0007 #include <unistd.h>
0008 #include <assert.h>
0009 #include <signal.h>
0010 #include <pthread.h>
0011 #include <sys/epoll.h>
0012 #include <sys/socket.h>
0013 #include <sys/eventfd.h>
0014 #include "../../kselftest_harness.h"
0015 
0016 struct epoll_mtcontext
0017 {
0018     int efd[3];
0019     int sfd[4];
0020     volatile int count;
0021 
0022     pthread_t main;
0023     pthread_t waiter;
0024 };
0025 
0026 #ifndef __NR_epoll_pwait2
0027 #define __NR_epoll_pwait2 -1
0028 #endif
0029 
0030 static inline int sys_epoll_pwait2(int fd, struct epoll_event *events,
0031                    int maxevents,
0032                    const struct __kernel_timespec *timeout,
0033                    const sigset_t *sigset, size_t sigsetsize)
0034 {
0035     return syscall(__NR_epoll_pwait2, fd, events, maxevents, timeout,
0036                sigset, sigsetsize);
0037 }
0038 
0039 static void signal_handler(int signum)
0040 {
0041 }
0042 
0043 static void kill_timeout(struct epoll_mtcontext *ctx)
0044 {
0045     usleep(1000000);
0046     pthread_kill(ctx->main, SIGUSR1);
0047     pthread_kill(ctx->waiter, SIGUSR1);
0048 }
0049 
0050 static void *waiter_entry1a(void *data)
0051 {
0052     struct epoll_event e;
0053     struct epoll_mtcontext *ctx = data;
0054 
0055     if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
0056         __sync_fetch_and_add(&ctx->count, 1);
0057 
0058     return NULL;
0059 }
0060 
0061 static void *waiter_entry1ap(void *data)
0062 {
0063     struct pollfd pfd;
0064     struct epoll_event e;
0065     struct epoll_mtcontext *ctx = data;
0066 
0067     pfd.fd = ctx->efd[0];
0068     pfd.events = POLLIN;
0069     if (poll(&pfd, 1, -1) > 0) {
0070         if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
0071             __sync_fetch_and_add(&ctx->count, 1);
0072     }
0073 
0074     return NULL;
0075 }
0076 
0077 static void *waiter_entry1o(void *data)
0078 {
0079     struct epoll_event e;
0080     struct epoll_mtcontext *ctx = data;
0081 
0082     if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
0083         __sync_fetch_and_or(&ctx->count, 1);
0084 
0085     return NULL;
0086 }
0087 
0088 static void *waiter_entry1op(void *data)
0089 {
0090     struct pollfd pfd;
0091     struct epoll_event e;
0092     struct epoll_mtcontext *ctx = data;
0093 
0094     pfd.fd = ctx->efd[0];
0095     pfd.events = POLLIN;
0096     if (poll(&pfd, 1, -1) > 0) {
0097         if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
0098             __sync_fetch_and_or(&ctx->count, 1);
0099     }
0100 
0101     return NULL;
0102 }
0103 
0104 static void *waiter_entry2a(void *data)
0105 {
0106     struct epoll_event events[2];
0107     struct epoll_mtcontext *ctx = data;
0108 
0109     if (epoll_wait(ctx->efd[0], events, 2, -1) > 0)
0110         __sync_fetch_and_add(&ctx->count, 1);
0111 
0112     return NULL;
0113 }
0114 
0115 static void *waiter_entry2ap(void *data)
0116 {
0117     struct pollfd pfd;
0118     struct epoll_event events[2];
0119     struct epoll_mtcontext *ctx = data;
0120 
0121     pfd.fd = ctx->efd[0];
0122     pfd.events = POLLIN;
0123     if (poll(&pfd, 1, -1) > 0) {
0124         if (epoll_wait(ctx->efd[0], events, 2, 0) > 0)
0125             __sync_fetch_and_add(&ctx->count, 1);
0126     }
0127 
0128     return NULL;
0129 }
0130 
0131 static void *emitter_entry1(void *data)
0132 {
0133     struct epoll_mtcontext *ctx = data;
0134 
0135     usleep(100000);
0136     write(ctx->sfd[1], "w", 1);
0137 
0138     kill_timeout(ctx);
0139 
0140     return NULL;
0141 }
0142 
0143 static void *emitter_entry2(void *data)
0144 {
0145     struct epoll_mtcontext *ctx = data;
0146 
0147     usleep(100000);
0148     write(ctx->sfd[1], "w", 1);
0149     write(ctx->sfd[3], "w", 1);
0150 
0151     kill_timeout(ctx);
0152 
0153     return NULL;
0154 }
0155 
0156 /*
0157  *          t0
0158  *           | (ew)
0159  *          e0
0160  *           | (lt)
0161  *          s0
0162  */
0163 TEST(epoll1)
0164 {
0165     int efd;
0166     int sfd[2];
0167     struct epoll_event e;
0168 
0169     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0170 
0171     efd = epoll_create(1);
0172     ASSERT_GE(efd, 0);
0173 
0174     e.events = EPOLLIN;
0175     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
0176 
0177     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0178 
0179     EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0180     EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0181 
0182     close(efd);
0183     close(sfd[0]);
0184     close(sfd[1]);
0185 }
0186 
0187 /*
0188  *          t0
0189  *           | (ew)
0190  *          e0
0191  *           | (et)
0192  *          s0
0193  */
0194 TEST(epoll2)
0195 {
0196     int efd;
0197     int sfd[2];
0198     struct epoll_event e;
0199 
0200     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0201 
0202     efd = epoll_create(1);
0203     ASSERT_GE(efd, 0);
0204 
0205     e.events = EPOLLIN | EPOLLET;
0206     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
0207 
0208     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0209 
0210     EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0211     EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 0);
0212 
0213     close(efd);
0214     close(sfd[0]);
0215     close(sfd[1]);
0216 }
0217 
0218 /*
0219  *           t0
0220  *            | (ew)
0221  *           e0
0222  *     (lt) /  \ (lt)
0223  *        s0    s2
0224  */
0225 TEST(epoll3)
0226 {
0227     int efd;
0228     int sfd[4];
0229     struct epoll_event events[2];
0230 
0231     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0232     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
0233 
0234     efd = epoll_create(1);
0235     ASSERT_GE(efd, 0);
0236 
0237     events[0].events = EPOLLIN;
0238     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
0239 
0240     events[0].events = EPOLLIN;
0241     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
0242 
0243     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0244     ASSERT_EQ(write(sfd[3], "w", 1), 1);
0245 
0246     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0247     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0248 
0249     close(efd);
0250     close(sfd[0]);
0251     close(sfd[1]);
0252     close(sfd[2]);
0253     close(sfd[3]);
0254 }
0255 
0256 /*
0257  *           t0
0258  *            | (ew)
0259  *           e0
0260  *     (et) /  \ (et)
0261  *        s0    s2
0262  */
0263 TEST(epoll4)
0264 {
0265     int efd;
0266     int sfd[4];
0267     struct epoll_event events[2];
0268 
0269     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0270     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
0271 
0272     efd = epoll_create(1);
0273     ASSERT_GE(efd, 0);
0274 
0275     events[0].events = EPOLLIN | EPOLLET;
0276     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
0277 
0278     events[0].events = EPOLLIN | EPOLLET;
0279     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
0280 
0281     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0282     ASSERT_EQ(write(sfd[3], "w", 1), 1);
0283 
0284     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0285     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
0286 
0287     close(efd);
0288     close(sfd[0]);
0289     close(sfd[1]);
0290     close(sfd[2]);
0291     close(sfd[3]);
0292 }
0293 
0294 /*
0295  *          t0
0296  *           | (p)
0297  *          e0
0298  *           | (lt)
0299  *          s0
0300  */
0301 TEST(epoll5)
0302 {
0303     int efd;
0304     int sfd[2];
0305     struct pollfd pfd;
0306     struct epoll_event e;
0307 
0308     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0309 
0310     efd = epoll_create(1);
0311     ASSERT_GE(efd, 0);
0312 
0313     e.events = EPOLLIN;
0314     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
0315 
0316     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0317 
0318     pfd.fd = efd;
0319     pfd.events = POLLIN;
0320     ASSERT_EQ(poll(&pfd, 1, 0), 1);
0321     ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0322 
0323     pfd.fd = efd;
0324     pfd.events = POLLIN;
0325     ASSERT_EQ(poll(&pfd, 1, 0), 1);
0326     ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0327 
0328     close(efd);
0329     close(sfd[0]);
0330     close(sfd[1]);
0331 }
0332 
0333 /*
0334  *          t0
0335  *           | (p)
0336  *          e0
0337  *           | (et)
0338  *          s0
0339  */
0340 TEST(epoll6)
0341 {
0342     int efd;
0343     int sfd[2];
0344     struct pollfd pfd;
0345     struct epoll_event e;
0346 
0347     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0348 
0349     efd = epoll_create(1);
0350     ASSERT_GE(efd, 0);
0351 
0352     e.events = EPOLLIN | EPOLLET;
0353     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
0354 
0355     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0356 
0357     pfd.fd = efd;
0358     pfd.events = POLLIN;
0359     ASSERT_EQ(poll(&pfd, 1, 0), 1);
0360     ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
0361 
0362     pfd.fd = efd;
0363     pfd.events = POLLIN;
0364     ASSERT_EQ(poll(&pfd, 1, 0), 0);
0365     ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 0);
0366 
0367     close(efd);
0368     close(sfd[0]);
0369     close(sfd[1]);
0370 }
0371 
0372 /*
0373  *           t0
0374  *            | (p)
0375  *           e0
0376  *     (lt) /  \ (lt)
0377  *        s0    s2
0378  */
0379 
0380 TEST(epoll7)
0381 {
0382     int efd;
0383     int sfd[4];
0384     struct pollfd pfd;
0385     struct epoll_event events[2];
0386 
0387     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0388     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
0389 
0390     efd = epoll_create(1);
0391     ASSERT_GE(efd, 0);
0392 
0393     events[0].events = EPOLLIN;
0394     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
0395 
0396     events[0].events = EPOLLIN;
0397     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
0398 
0399     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0400     ASSERT_EQ(write(sfd[3], "w", 1), 1);
0401 
0402     pfd.fd = efd;
0403     pfd.events = POLLIN;
0404     EXPECT_EQ(poll(&pfd, 1, 0), 1);
0405     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0406 
0407     pfd.fd = efd;
0408     pfd.events = POLLIN;
0409     EXPECT_EQ(poll(&pfd, 1, 0), 1);
0410     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0411 
0412     close(efd);
0413     close(sfd[0]);
0414     close(sfd[1]);
0415     close(sfd[2]);
0416     close(sfd[3]);
0417 }
0418 
0419 /*
0420  *           t0
0421  *            | (p)
0422  *           e0
0423  *     (et) /  \ (et)
0424  *        s0    s2
0425  */
0426 TEST(epoll8)
0427 {
0428     int efd;
0429     int sfd[4];
0430     struct pollfd pfd;
0431     struct epoll_event events[2];
0432 
0433     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
0434     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
0435 
0436     efd = epoll_create(1);
0437     ASSERT_GE(efd, 0);
0438 
0439     events[0].events = EPOLLIN | EPOLLET;
0440     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
0441 
0442     events[0].events = EPOLLIN | EPOLLET;
0443     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
0444 
0445     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0446     ASSERT_EQ(write(sfd[3], "w", 1), 1);
0447 
0448     pfd.fd = efd;
0449     pfd.events = POLLIN;
0450     EXPECT_EQ(poll(&pfd, 1, 0), 1);
0451     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
0452 
0453     pfd.fd = efd;
0454     pfd.events = POLLIN;
0455     EXPECT_EQ(poll(&pfd, 1, 0), 0);
0456     EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
0457 
0458     close(efd);
0459     close(sfd[0]);
0460     close(sfd[1]);
0461     close(sfd[2]);
0462     close(sfd[3]);
0463 }
0464 
0465 /*
0466  *        t0    t1
0467  *     (ew) \  / (ew)
0468  *           e0
0469  *            | (lt)
0470  *           s0
0471  */
0472 TEST(epoll9)
0473 {
0474     pthread_t emitter;
0475     struct epoll_event e;
0476     struct epoll_mtcontext ctx = { 0 };
0477 
0478     signal(SIGUSR1, signal_handler);
0479 
0480     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
0481 
0482     ctx.efd[0] = epoll_create(1);
0483     ASSERT_GE(ctx.efd[0], 0);
0484 
0485     e.events = EPOLLIN;
0486     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
0487 
0488     ctx.main = pthread_self();
0489     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
0490     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
0491 
0492     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
0493         __sync_fetch_and_add(&ctx.count, 1);
0494 
0495     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0496     EXPECT_EQ(ctx.count, 2);
0497 
0498     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0499         pthread_kill(emitter, SIGUSR1);
0500         pthread_join(emitter, NULL);
0501     }
0502 
0503     close(ctx.efd[0]);
0504     close(ctx.sfd[0]);
0505     close(ctx.sfd[1]);
0506 }
0507 
0508 /*
0509  *        t0    t1
0510  *     (ew) \  / (ew)
0511  *           e0
0512  *            | (et)
0513  *           s0
0514  */
0515 TEST(epoll10)
0516 {
0517     pthread_t emitter;
0518     struct epoll_event e;
0519     struct epoll_mtcontext ctx = { 0 };
0520 
0521     signal(SIGUSR1, signal_handler);
0522 
0523     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
0524 
0525     ctx.efd[0] = epoll_create(1);
0526     ASSERT_GE(ctx.efd[0], 0);
0527 
0528     e.events = EPOLLIN | EPOLLET;
0529     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
0530 
0531     ctx.main = pthread_self();
0532     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
0533     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
0534 
0535     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
0536         __sync_fetch_and_add(&ctx.count, 1);
0537 
0538     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0539     EXPECT_EQ(ctx.count, 1);
0540 
0541     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0542         pthread_kill(emitter, SIGUSR1);
0543         pthread_join(emitter, NULL);
0544     }
0545 
0546     close(ctx.efd[0]);
0547     close(ctx.sfd[0]);
0548     close(ctx.sfd[1]);
0549 }
0550 
0551 /*
0552  *        t0    t1
0553  *     (ew) \  / (ew)
0554  *           e0
0555  *     (lt) /  \ (lt)
0556  *        s0    s2
0557  */
0558 TEST(epoll11)
0559 {
0560     pthread_t emitter;
0561     struct epoll_event events[2];
0562     struct epoll_mtcontext ctx = { 0 };
0563 
0564     signal(SIGUSR1, signal_handler);
0565 
0566     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
0567     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
0568 
0569     ctx.efd[0] = epoll_create(1);
0570     ASSERT_GE(ctx.efd[0], 0);
0571 
0572     events[0].events = EPOLLIN;
0573     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
0574 
0575     events[0].events = EPOLLIN;
0576     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
0577 
0578     ctx.main = pthread_self();
0579     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2a, &ctx), 0);
0580     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
0581 
0582     if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
0583         __sync_fetch_and_add(&ctx.count, 1);
0584 
0585     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0586     EXPECT_EQ(ctx.count, 2);
0587 
0588     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0589         pthread_kill(emitter, SIGUSR1);
0590         pthread_join(emitter, NULL);
0591     }
0592 
0593     close(ctx.efd[0]);
0594     close(ctx.sfd[0]);
0595     close(ctx.sfd[1]);
0596     close(ctx.sfd[2]);
0597     close(ctx.sfd[3]);
0598 }
0599 
0600 /*
0601  *        t0    t1
0602  *     (ew) \  / (ew)
0603  *           e0
0604  *     (et) /  \ (et)
0605  *        s0    s2
0606  */
0607 TEST(epoll12)
0608 {
0609     pthread_t emitter;
0610     struct epoll_event events[2];
0611     struct epoll_mtcontext ctx = { 0 };
0612 
0613     signal(SIGUSR1, signal_handler);
0614 
0615     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
0616     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
0617 
0618     ctx.efd[0] = epoll_create(1);
0619     ASSERT_GE(ctx.efd[0], 0);
0620 
0621     events[0].events = EPOLLIN | EPOLLET;
0622     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
0623 
0624     events[0].events = EPOLLIN | EPOLLET;
0625     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
0626 
0627     ctx.main = pthread_self();
0628     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
0629     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
0630 
0631     if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
0632         __sync_fetch_and_add(&ctx.count, 1);
0633 
0634     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0635     EXPECT_EQ(ctx.count, 2);
0636 
0637     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0638         pthread_kill(emitter, SIGUSR1);
0639         pthread_join(emitter, NULL);
0640     }
0641 
0642     close(ctx.efd[0]);
0643     close(ctx.sfd[0]);
0644     close(ctx.sfd[1]);
0645     close(ctx.sfd[2]);
0646     close(ctx.sfd[3]);
0647 }
0648 
0649 /*
0650  *        t0    t1
0651  *     (ew) \  / (p)
0652  *           e0
0653  *            | (lt)
0654  *           s0
0655  */
0656 TEST(epoll13)
0657 {
0658     pthread_t emitter;
0659     struct epoll_event e;
0660     struct epoll_mtcontext ctx = { 0 };
0661 
0662     signal(SIGUSR1, signal_handler);
0663 
0664     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
0665 
0666     ctx.efd[0] = epoll_create(1);
0667     ASSERT_GE(ctx.efd[0], 0);
0668 
0669     e.events = EPOLLIN;
0670     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
0671 
0672     ctx.main = pthread_self();
0673     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
0674     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
0675 
0676     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
0677         __sync_fetch_and_add(&ctx.count, 1);
0678 
0679     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0680     EXPECT_EQ(ctx.count, 2);
0681 
0682     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0683         pthread_kill(emitter, SIGUSR1);
0684         pthread_join(emitter, NULL);
0685     }
0686 
0687     close(ctx.efd[0]);
0688     close(ctx.sfd[0]);
0689     close(ctx.sfd[1]);
0690 }
0691 
0692 /*
0693  *        t0    t1
0694  *     (ew) \  / (p)
0695  *           e0
0696  *            | (et)
0697  *           s0
0698  */
0699 TEST(epoll14)
0700 {
0701     pthread_t emitter;
0702     struct epoll_event e;
0703     struct epoll_mtcontext ctx = { 0 };
0704 
0705     signal(SIGUSR1, signal_handler);
0706 
0707     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
0708 
0709     ctx.efd[0] = epoll_create(1);
0710     ASSERT_GE(ctx.efd[0], 0);
0711 
0712     e.events = EPOLLIN | EPOLLET;
0713     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
0714 
0715     ctx.main = pthread_self();
0716     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
0717     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
0718 
0719     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
0720         __sync_fetch_and_add(&ctx.count, 1);
0721 
0722     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0723     EXPECT_EQ(ctx.count, 1);
0724 
0725     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0726         pthread_kill(emitter, SIGUSR1);
0727         pthread_join(emitter, NULL);
0728     }
0729 
0730     close(ctx.efd[0]);
0731     close(ctx.sfd[0]);
0732     close(ctx.sfd[1]);
0733 }
0734 
0735 /*
0736  *        t0    t1
0737  *     (ew) \  / (p)
0738  *           e0
0739  *     (lt) /  \ (lt)
0740  *        s0    s2
0741  */
0742 TEST(epoll15)
0743 {
0744     pthread_t emitter;
0745     struct epoll_event events[2];
0746     struct epoll_mtcontext ctx = { 0 };
0747 
0748     signal(SIGUSR1, signal_handler);
0749 
0750     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
0751     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
0752 
0753     ctx.efd[0] = epoll_create(1);
0754     ASSERT_GE(ctx.efd[0], 0);
0755 
0756     events[0].events = EPOLLIN;
0757     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
0758 
0759     events[0].events = EPOLLIN;
0760     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
0761 
0762     ctx.main = pthread_self();
0763     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2ap, &ctx), 0);
0764     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
0765 
0766     if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
0767         __sync_fetch_and_add(&ctx.count, 1);
0768 
0769     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0770     EXPECT_EQ(ctx.count, 2);
0771 
0772     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0773         pthread_kill(emitter, SIGUSR1);
0774         pthread_join(emitter, NULL);
0775     }
0776 
0777     close(ctx.efd[0]);
0778     close(ctx.sfd[0]);
0779     close(ctx.sfd[1]);
0780     close(ctx.sfd[2]);
0781     close(ctx.sfd[3]);
0782 }
0783 
0784 /*
0785  *        t0    t1
0786  *     (ew) \  / (p)
0787  *           e0
0788  *     (et) /  \ (et)
0789  *        s0    s2
0790  */
0791 TEST(epoll16)
0792 {
0793     pthread_t emitter;
0794     struct epoll_event events[2];
0795     struct epoll_mtcontext ctx = { 0 };
0796 
0797     signal(SIGUSR1, signal_handler);
0798 
0799     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
0800     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
0801 
0802     ctx.efd[0] = epoll_create(1);
0803     ASSERT_GE(ctx.efd[0], 0);
0804 
0805     events[0].events = EPOLLIN | EPOLLET;
0806     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
0807 
0808     events[0].events = EPOLLIN | EPOLLET;
0809     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
0810 
0811     ctx.main = pthread_self();
0812     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
0813     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
0814 
0815     if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
0816         __sync_fetch_and_add(&ctx.count, 1);
0817 
0818     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
0819     EXPECT_EQ(ctx.count, 2);
0820 
0821     if (pthread_tryjoin_np(emitter, NULL) < 0) {
0822         pthread_kill(emitter, SIGUSR1);
0823         pthread_join(emitter, NULL);
0824     }
0825 
0826     close(ctx.efd[0]);
0827     close(ctx.sfd[0]);
0828     close(ctx.sfd[1]);
0829     close(ctx.sfd[2]);
0830     close(ctx.sfd[3]);
0831 }
0832 
0833 /*
0834  *          t0
0835  *           | (ew)
0836  *          e0
0837  *           | (lt)
0838  *          e1
0839  *           | (lt)
0840  *          s0
0841  */
0842 TEST(epoll17)
0843 {
0844     int efd[2];
0845     int sfd[2];
0846     struct epoll_event e;
0847 
0848     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0849 
0850     efd[0] = epoll_create(1);
0851     ASSERT_GE(efd[0], 0);
0852 
0853     efd[1] = epoll_create(1);
0854     ASSERT_GE(efd[1], 0);
0855 
0856     e.events = EPOLLIN;
0857     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
0858 
0859     e.events = EPOLLIN;
0860     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
0861 
0862     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0863 
0864     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0865     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0866 
0867     close(efd[0]);
0868     close(efd[1]);
0869     close(sfd[0]);
0870     close(sfd[1]);
0871 }
0872 
0873 /*
0874  *          t0
0875  *           | (ew)
0876  *          e0
0877  *           | (lt)
0878  *          e1
0879  *           | (et)
0880  *          s0
0881  */
0882 TEST(epoll18)
0883 {
0884     int efd[2];
0885     int sfd[2];
0886     struct epoll_event e;
0887 
0888     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0889 
0890     efd[0] = epoll_create(1);
0891     ASSERT_GE(efd[0], 0);
0892 
0893     efd[1] = epoll_create(1);
0894     ASSERT_GE(efd[1], 0);
0895 
0896     e.events = EPOLLIN | EPOLLET;
0897     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
0898 
0899     e.events = EPOLLIN;
0900     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
0901 
0902     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0903 
0904     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0905     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0906 
0907     close(efd[0]);
0908     close(efd[1]);
0909     close(sfd[0]);
0910     close(sfd[1]);
0911 }
0912 
0913 /*
0914  *           t0
0915  *            | (ew)
0916  *           e0
0917  *            | (et)
0918  *           e1
0919  *            | (lt)
0920  *           s0
0921  */
0922 TEST(epoll19)
0923 {
0924     int efd[2];
0925     int sfd[2];
0926     struct epoll_event e;
0927 
0928     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0929 
0930     efd[0] = epoll_create(1);
0931     ASSERT_GE(efd[0], 0);
0932 
0933     efd[1] = epoll_create(1);
0934     ASSERT_GE(efd[1], 0);
0935 
0936     e.events = EPOLLIN;
0937     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
0938 
0939     e.events = EPOLLIN | EPOLLET;
0940     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
0941 
0942     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0943 
0944     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0945     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
0946 
0947     close(efd[0]);
0948     close(efd[1]);
0949     close(sfd[0]);
0950     close(sfd[1]);
0951 }
0952 
0953 /*
0954  *           t0
0955  *            | (ew)
0956  *           e0
0957  *            | (et)
0958  *           e1
0959  *            | (et)
0960  *           s0
0961  */
0962 TEST(epoll20)
0963 {
0964     int efd[2];
0965     int sfd[2];
0966     struct epoll_event e;
0967 
0968     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
0969 
0970     efd[0] = epoll_create(1);
0971     ASSERT_GE(efd[0], 0);
0972 
0973     efd[1] = epoll_create(1);
0974     ASSERT_GE(efd[1], 0);
0975 
0976     e.events = EPOLLIN | EPOLLET;
0977     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
0978 
0979     e.events = EPOLLIN | EPOLLET;
0980     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
0981 
0982     ASSERT_EQ(write(sfd[1], "w", 1), 1);
0983 
0984     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
0985     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
0986 
0987     close(efd[0]);
0988     close(efd[1]);
0989     close(sfd[0]);
0990     close(sfd[1]);
0991 }
0992 
0993 /*
0994  *          t0
0995  *           | (p)
0996  *          e0
0997  *           | (lt)
0998  *          e1
0999  *           | (lt)
1000  *          s0
1001  */
1002 TEST(epoll21)
1003 {
1004     int efd[2];
1005     int sfd[2];
1006     struct pollfd pfd;
1007     struct epoll_event e;
1008 
1009     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1010 
1011     efd[0] = epoll_create(1);
1012     ASSERT_GE(efd[0], 0);
1013 
1014     efd[1] = epoll_create(1);
1015     ASSERT_GE(efd[1], 0);
1016 
1017     e.events = EPOLLIN;
1018     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1019 
1020     e.events = EPOLLIN;
1021     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1022 
1023     ASSERT_EQ(write(sfd[1], "w", 1), 1);
1024 
1025     pfd.fd = efd[0];
1026     pfd.events = POLLIN;
1027     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1028     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1029 
1030     pfd.fd = efd[0];
1031     pfd.events = POLLIN;
1032     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1033     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1034 
1035     close(efd[0]);
1036     close(efd[1]);
1037     close(sfd[0]);
1038     close(sfd[1]);
1039 }
1040 
1041 /*
1042  *          t0
1043  *           | (p)
1044  *          e0
1045  *           | (lt)
1046  *          e1
1047  *           | (et)
1048  *          s0
1049  */
1050 TEST(epoll22)
1051 {
1052     int efd[2];
1053     int sfd[2];
1054     struct pollfd pfd;
1055     struct epoll_event e;
1056 
1057     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1058 
1059     efd[0] = epoll_create(1);
1060     ASSERT_GE(efd[0], 0);
1061 
1062     efd[1] = epoll_create(1);
1063     ASSERT_GE(efd[1], 0);
1064 
1065     e.events = EPOLLIN | EPOLLET;
1066     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1067 
1068     e.events = EPOLLIN;
1069     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1070 
1071     ASSERT_EQ(write(sfd[1], "w", 1), 1);
1072 
1073     pfd.fd = efd[0];
1074     pfd.events = POLLIN;
1075     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1076     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1077 
1078     pfd.fd = efd[0];
1079     pfd.events = POLLIN;
1080     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1081     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1082 
1083     close(efd[0]);
1084     close(efd[1]);
1085     close(sfd[0]);
1086     close(sfd[1]);
1087 }
1088 
1089 /*
1090  *          t0
1091  *           | (p)
1092  *          e0
1093  *           | (et)
1094  *          e1
1095  *           | (lt)
1096  *          s0
1097  */
1098 TEST(epoll23)
1099 {
1100     int efd[2];
1101     int sfd[2];
1102     struct pollfd pfd;
1103     struct epoll_event e;
1104 
1105     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1106 
1107     efd[0] = epoll_create(1);
1108     ASSERT_GE(efd[0], 0);
1109 
1110     efd[1] = epoll_create(1);
1111     ASSERT_GE(efd[1], 0);
1112 
1113     e.events = EPOLLIN;
1114     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1115 
1116     e.events = EPOLLIN | EPOLLET;
1117     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1118 
1119     ASSERT_EQ(write(sfd[1], "w", 1), 1);
1120 
1121     pfd.fd = efd[0];
1122     pfd.events = POLLIN;
1123     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1124     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1125 
1126     pfd.fd = efd[0];
1127     pfd.events = POLLIN;
1128     EXPECT_EQ(poll(&pfd, 1, 0), 0);
1129     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1130 
1131     close(efd[0]);
1132     close(efd[1]);
1133     close(sfd[0]);
1134     close(sfd[1]);
1135 }
1136 
1137 /*
1138  *          t0
1139  *           | (p)
1140  *          e0
1141  *           | (et)
1142  *          e1
1143  *           | (et)
1144  *          s0
1145  */
1146 TEST(epoll24)
1147 {
1148     int efd[2];
1149     int sfd[2];
1150     struct pollfd pfd;
1151     struct epoll_event e;
1152 
1153     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
1154 
1155     efd[0] = epoll_create(1);
1156     ASSERT_GE(efd[0], 0);
1157 
1158     efd[1] = epoll_create(1);
1159     ASSERT_GE(efd[1], 0);
1160 
1161     e.events = EPOLLIN | EPOLLET;
1162     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
1163 
1164     e.events = EPOLLIN | EPOLLET;
1165     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
1166 
1167     ASSERT_EQ(write(sfd[1], "w", 1), 1);
1168 
1169     pfd.fd = efd[0];
1170     pfd.events = POLLIN;
1171     EXPECT_EQ(poll(&pfd, 1, 0), 1);
1172     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
1173 
1174     pfd.fd = efd[0];
1175     pfd.events = POLLIN;
1176     EXPECT_EQ(poll(&pfd, 1, 0), 0);
1177     EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
1178 
1179     close(efd[0]);
1180     close(efd[1]);
1181     close(sfd[0]);
1182     close(sfd[1]);
1183 }
1184 
1185 /*
1186  *        t0    t1
1187  *     (ew) \  / (ew)
1188  *           e0
1189  *            | (lt)
1190  *           e1
1191  *            | (lt)
1192  *           s0
1193  */
1194 TEST(epoll25)
1195 {
1196     pthread_t emitter;
1197     struct epoll_event e;
1198     struct epoll_mtcontext ctx = { 0 };
1199 
1200     signal(SIGUSR1, signal_handler);
1201 
1202     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1203 
1204     ctx.efd[0] = epoll_create(1);
1205     ASSERT_GE(ctx.efd[0], 0);
1206 
1207     ctx.efd[1] = epoll_create(1);
1208     ASSERT_GE(ctx.efd[1], 0);
1209 
1210     e.events = EPOLLIN;
1211     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1212 
1213     e.events = EPOLLIN;
1214     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1215 
1216     ctx.main = pthread_self();
1217     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1218     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1219 
1220     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1221         __sync_fetch_and_add(&ctx.count, 1);
1222 
1223     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1224     EXPECT_EQ(ctx.count, 2);
1225 
1226     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1227         pthread_kill(emitter, SIGUSR1);
1228         pthread_join(emitter, NULL);
1229     }
1230 
1231     close(ctx.efd[0]);
1232     close(ctx.efd[1]);
1233     close(ctx.sfd[0]);
1234     close(ctx.sfd[1]);
1235 }
1236 
1237 /*
1238  *        t0    t1
1239  *     (ew) \  / (ew)
1240  *           e0
1241  *            | (lt)
1242  *           e1
1243  *            | (et)
1244  *           s0
1245  */
1246 TEST(epoll26)
1247 {
1248     pthread_t emitter;
1249     struct epoll_event e;
1250     struct epoll_mtcontext ctx = { 0 };
1251 
1252     signal(SIGUSR1, signal_handler);
1253 
1254     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1255 
1256     ctx.efd[0] = epoll_create(1);
1257     ASSERT_GE(ctx.efd[0], 0);
1258 
1259     ctx.efd[1] = epoll_create(1);
1260     ASSERT_GE(ctx.efd[1], 0);
1261 
1262     e.events = EPOLLIN | EPOLLET;
1263     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1264 
1265     e.events = EPOLLIN;
1266     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1267 
1268     ctx.main = pthread_self();
1269     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1270     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1271 
1272     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1273         __sync_fetch_and_add(&ctx.count, 1);
1274 
1275     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1276     EXPECT_EQ(ctx.count, 2);
1277 
1278     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1279         pthread_kill(emitter, SIGUSR1);
1280         pthread_join(emitter, NULL);
1281     }
1282 
1283     close(ctx.efd[0]);
1284     close(ctx.efd[1]);
1285     close(ctx.sfd[0]);
1286     close(ctx.sfd[1]);
1287 }
1288 
1289 /*
1290  *        t0    t1
1291  *     (ew) \  / (ew)
1292  *           e0
1293  *            | (et)
1294  *           e1
1295  *            | (lt)
1296  *           s0
1297  */
1298 TEST(epoll27)
1299 {
1300     pthread_t emitter;
1301     struct epoll_event e;
1302     struct epoll_mtcontext ctx = { 0 };
1303 
1304     signal(SIGUSR1, signal_handler);
1305 
1306     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1307 
1308     ctx.efd[0] = epoll_create(1);
1309     ASSERT_GE(ctx.efd[0], 0);
1310 
1311     ctx.efd[1] = epoll_create(1);
1312     ASSERT_GE(ctx.efd[1], 0);
1313 
1314     e.events = EPOLLIN;
1315     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1316 
1317     e.events = EPOLLIN | EPOLLET;
1318     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1319 
1320     ctx.main = pthread_self();
1321     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1322     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1323 
1324     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1325         __sync_fetch_and_add(&ctx.count, 1);
1326 
1327     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1328     EXPECT_EQ(ctx.count, 1);
1329 
1330     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1331         pthread_kill(emitter, SIGUSR1);
1332         pthread_join(emitter, NULL);
1333     }
1334 
1335     close(ctx.efd[0]);
1336     close(ctx.efd[1]);
1337     close(ctx.sfd[0]);
1338     close(ctx.sfd[1]);
1339 }
1340 
1341 /*
1342  *        t0    t1
1343  *     (ew) \  / (ew)
1344  *           e0
1345  *            | (et)
1346  *           e1
1347  *            | (et)
1348  *           s0
1349  */
1350 TEST(epoll28)
1351 {
1352     pthread_t emitter;
1353     struct epoll_event e;
1354     struct epoll_mtcontext ctx = { 0 };
1355 
1356     signal(SIGUSR1, signal_handler);
1357 
1358     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1359 
1360     ctx.efd[0] = epoll_create(1);
1361     ASSERT_GE(ctx.efd[0], 0);
1362 
1363     ctx.efd[1] = epoll_create(1);
1364     ASSERT_GE(ctx.efd[1], 0);
1365 
1366     e.events = EPOLLIN | EPOLLET;
1367     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1368 
1369     e.events = EPOLLIN | EPOLLET;
1370     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1371 
1372     ctx.main = pthread_self();
1373     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1374     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1375 
1376     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1377         __sync_fetch_and_add(&ctx.count, 1);
1378 
1379     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1380     EXPECT_EQ(ctx.count, 1);
1381 
1382     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1383         pthread_kill(emitter, SIGUSR1);
1384         pthread_join(emitter, NULL);
1385     }
1386 
1387     close(ctx.efd[0]);
1388     close(ctx.efd[1]);
1389     close(ctx.sfd[0]);
1390     close(ctx.sfd[1]);
1391 }
1392 
1393 /*
1394  *        t0    t1
1395  *     (ew) \  / (p)
1396  *           e0
1397  *            | (lt)
1398  *           e1
1399  *            | (lt)
1400  *           s0
1401  */
1402 TEST(epoll29)
1403 {
1404     pthread_t emitter;
1405     struct epoll_event e;
1406     struct epoll_mtcontext ctx = { 0 };
1407 
1408     signal(SIGUSR1, signal_handler);
1409 
1410     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1411 
1412     ctx.efd[0] = epoll_create(1);
1413     ASSERT_GE(ctx.efd[0], 0);
1414 
1415     ctx.efd[1] = epoll_create(1);
1416     ASSERT_GE(ctx.efd[1], 0);
1417 
1418     e.events = EPOLLIN;
1419     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1420 
1421     e.events = EPOLLIN;
1422     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1423 
1424     ctx.main = pthread_self();
1425     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1426     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1427 
1428     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1429         __sync_fetch_and_add(&ctx.count, 1);
1430 
1431     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1432     EXPECT_EQ(ctx.count, 2);
1433 
1434     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1435         pthread_kill(emitter, SIGUSR1);
1436         pthread_join(emitter, NULL);
1437     }
1438 
1439     close(ctx.efd[0]);
1440     close(ctx.sfd[0]);
1441     close(ctx.sfd[1]);
1442 }
1443 
1444 /*
1445  *        t0    t1
1446  *     (ew) \  / (p)
1447  *           e0
1448  *            | (lt)
1449  *           e1
1450  *            | (et)
1451  *           s0
1452  */
1453 TEST(epoll30)
1454 {
1455     pthread_t emitter;
1456     struct epoll_event e;
1457     struct epoll_mtcontext ctx = { 0 };
1458 
1459     signal(SIGUSR1, signal_handler);
1460 
1461     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1462 
1463     ctx.efd[0] = epoll_create(1);
1464     ASSERT_GE(ctx.efd[0], 0);
1465 
1466     ctx.efd[1] = epoll_create(1);
1467     ASSERT_GE(ctx.efd[1], 0);
1468 
1469     e.events = EPOLLIN | EPOLLET;
1470     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1471 
1472     e.events = EPOLLIN;
1473     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1474 
1475     ctx.main = pthread_self();
1476     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1477     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1478 
1479     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1480         __sync_fetch_and_add(&ctx.count, 1);
1481 
1482     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1483     EXPECT_EQ(ctx.count, 2);
1484 
1485     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1486         pthread_kill(emitter, SIGUSR1);
1487         pthread_join(emitter, NULL);
1488     }
1489 
1490     close(ctx.efd[0]);
1491     close(ctx.sfd[0]);
1492     close(ctx.sfd[1]);
1493 }
1494 
1495 /*
1496  *        t0    t1
1497  *     (ew) \  / (p)
1498  *           e0
1499  *            | (et)
1500  *           e1
1501  *            | (lt)
1502  *           s0
1503  */
1504 TEST(epoll31)
1505 {
1506     pthread_t emitter;
1507     struct epoll_event e;
1508     struct epoll_mtcontext ctx = { 0 };
1509 
1510     signal(SIGUSR1, signal_handler);
1511 
1512     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1513 
1514     ctx.efd[0] = epoll_create(1);
1515     ASSERT_GE(ctx.efd[0], 0);
1516 
1517     ctx.efd[1] = epoll_create(1);
1518     ASSERT_GE(ctx.efd[1], 0);
1519 
1520     e.events = EPOLLIN;
1521     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1522 
1523     e.events = EPOLLIN | EPOLLET;
1524     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1525 
1526     ctx.main = pthread_self();
1527     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1528     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1529 
1530     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1531         __sync_fetch_and_add(&ctx.count, 1);
1532 
1533     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1534     EXPECT_EQ(ctx.count, 1);
1535 
1536     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1537         pthread_kill(emitter, SIGUSR1);
1538         pthread_join(emitter, NULL);
1539     }
1540 
1541     close(ctx.efd[0]);
1542     close(ctx.sfd[0]);
1543     close(ctx.sfd[1]);
1544 }
1545 
1546 /*
1547  *        t0    t1
1548  *     (ew) \  / (p)
1549  *           e0
1550  *            | (et)
1551  *           e1
1552  *            | (et)
1553  *           s0
1554  */
1555 TEST(epoll32)
1556 {
1557     pthread_t emitter;
1558     struct epoll_event e;
1559     struct epoll_mtcontext ctx = { 0 };
1560 
1561     signal(SIGUSR1, signal_handler);
1562 
1563     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1564 
1565     ctx.efd[0] = epoll_create(1);
1566     ASSERT_GE(ctx.efd[0], 0);
1567 
1568     ctx.efd[1] = epoll_create(1);
1569     ASSERT_GE(ctx.efd[1], 0);
1570 
1571     e.events = EPOLLIN | EPOLLET;
1572     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1573 
1574     e.events = EPOLLIN | EPOLLET;
1575     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1576 
1577     ctx.main = pthread_self();
1578     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
1579     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1580 
1581     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
1582         __sync_fetch_and_add(&ctx.count, 1);
1583 
1584     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1585     EXPECT_EQ(ctx.count, 1);
1586 
1587     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1588         pthread_kill(emitter, SIGUSR1);
1589         pthread_join(emitter, NULL);
1590     }
1591 
1592     close(ctx.efd[0]);
1593     close(ctx.sfd[0]);
1594     close(ctx.sfd[1]);
1595 }
1596 
1597 /*
1598  *        t0   t1
1599  *    (ew) |    | (ew)
1600  *         |   e0
1601  *          \  / (lt)
1602  *           e1
1603  *            | (lt)
1604  *           s0
1605  */
1606 TEST(epoll33)
1607 {
1608     pthread_t emitter;
1609     struct epoll_event e;
1610     struct epoll_mtcontext ctx = { 0 };
1611 
1612     signal(SIGUSR1, signal_handler);
1613 
1614     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1615 
1616     ctx.efd[0] = epoll_create(1);
1617     ASSERT_GE(ctx.efd[0], 0);
1618 
1619     ctx.efd[1] = epoll_create(1);
1620     ASSERT_GE(ctx.efd[1], 0);
1621 
1622     e.events = EPOLLIN;
1623     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1624 
1625     e.events = EPOLLIN;
1626     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1627 
1628     ctx.main = pthread_self();
1629     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1630     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1631 
1632     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1633         __sync_fetch_and_add(&ctx.count, 1);
1634 
1635     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1636     EXPECT_EQ(ctx.count, 2);
1637 
1638     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1639         pthread_kill(emitter, SIGUSR1);
1640         pthread_join(emitter, NULL);
1641     }
1642 
1643     close(ctx.efd[0]);
1644     close(ctx.efd[1]);
1645     close(ctx.sfd[0]);
1646     close(ctx.sfd[1]);
1647 }
1648 
1649 /*
1650  *        t0   t1
1651  *    (ew) |    | (ew)
1652  *         |   e0
1653  *          \  / (lt)
1654  *           e1
1655  *            | (et)
1656  *           s0
1657  */
1658 TEST(epoll34)
1659 {
1660     pthread_t emitter;
1661     struct epoll_event e;
1662     struct epoll_mtcontext ctx = { 0 };
1663 
1664     signal(SIGUSR1, signal_handler);
1665 
1666     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1667 
1668     ctx.efd[0] = epoll_create(1);
1669     ASSERT_GE(ctx.efd[0], 0);
1670 
1671     ctx.efd[1] = epoll_create(1);
1672     ASSERT_GE(ctx.efd[1], 0);
1673 
1674     e.events = EPOLLIN | EPOLLET;
1675     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1676 
1677     e.events = EPOLLIN;
1678     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1679 
1680     ctx.main = pthread_self();
1681     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1682     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1683 
1684     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1685         __sync_fetch_and_or(&ctx.count, 2);
1686 
1687     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1688     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1689 
1690     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1691         pthread_kill(emitter, SIGUSR1);
1692         pthread_join(emitter, NULL);
1693     }
1694 
1695     close(ctx.efd[0]);
1696     close(ctx.efd[1]);
1697     close(ctx.sfd[0]);
1698     close(ctx.sfd[1]);
1699 }
1700 
1701 /*
1702  *        t0   t1
1703  *    (ew) |    | (ew)
1704  *         |   e0
1705  *          \  / (et)
1706  *           e1
1707  *            | (lt)
1708  *           s0
1709  */
1710 TEST(epoll35)
1711 {
1712     pthread_t emitter;
1713     struct epoll_event e;
1714     struct epoll_mtcontext ctx = { 0 };
1715 
1716     signal(SIGUSR1, signal_handler);
1717 
1718     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1719 
1720     ctx.efd[0] = epoll_create(1);
1721     ASSERT_GE(ctx.efd[0], 0);
1722 
1723     ctx.efd[1] = epoll_create(1);
1724     ASSERT_GE(ctx.efd[1], 0);
1725 
1726     e.events = EPOLLIN;
1727     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1728 
1729     e.events = EPOLLIN | EPOLLET;
1730     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1731 
1732     ctx.main = pthread_self();
1733     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1734     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1735 
1736     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1737         __sync_fetch_and_add(&ctx.count, 1);
1738 
1739     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1740     EXPECT_EQ(ctx.count, 2);
1741 
1742     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1743         pthread_kill(emitter, SIGUSR1);
1744         pthread_join(emitter, NULL);
1745     }
1746 
1747     close(ctx.efd[0]);
1748     close(ctx.efd[1]);
1749     close(ctx.sfd[0]);
1750     close(ctx.sfd[1]);
1751 }
1752 
1753 /*
1754  *        t0   t1
1755  *    (ew) |    | (ew)
1756  *         |   e0
1757  *          \  / (et)
1758  *           e1
1759  *            | (et)
1760  *           s0
1761  */
1762 TEST(epoll36)
1763 {
1764     pthread_t emitter;
1765     struct epoll_event e;
1766     struct epoll_mtcontext ctx = { 0 };
1767 
1768     signal(SIGUSR1, signal_handler);
1769 
1770     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1771 
1772     ctx.efd[0] = epoll_create(1);
1773     ASSERT_GE(ctx.efd[0], 0);
1774 
1775     ctx.efd[1] = epoll_create(1);
1776     ASSERT_GE(ctx.efd[1], 0);
1777 
1778     e.events = EPOLLIN | EPOLLET;
1779     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1780 
1781     e.events = EPOLLIN | EPOLLET;
1782     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1783 
1784     ctx.main = pthread_self();
1785     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1786     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1787 
1788     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
1789         __sync_fetch_and_or(&ctx.count, 2);
1790 
1791     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1792     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1793 
1794     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1795         pthread_kill(emitter, SIGUSR1);
1796         pthread_join(emitter, NULL);
1797     }
1798 
1799     close(ctx.efd[0]);
1800     close(ctx.efd[1]);
1801     close(ctx.sfd[0]);
1802     close(ctx.sfd[1]);
1803 }
1804 
1805 /*
1806  *        t0   t1
1807  *     (p) |    | (ew)
1808  *         |   e0
1809  *          \  / (lt)
1810  *           e1
1811  *            | (lt)
1812  *           s0
1813  */
1814 TEST(epoll37)
1815 {
1816     pthread_t emitter;
1817     struct pollfd pfd;
1818     struct epoll_event e;
1819     struct epoll_mtcontext ctx = { 0 };
1820 
1821     signal(SIGUSR1, signal_handler);
1822 
1823     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1824 
1825     ctx.efd[0] = epoll_create(1);
1826     ASSERT_GE(ctx.efd[0], 0);
1827 
1828     ctx.efd[1] = epoll_create(1);
1829     ASSERT_GE(ctx.efd[1], 0);
1830 
1831     e.events = EPOLLIN;
1832     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1833 
1834     e.events = EPOLLIN;
1835     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1836 
1837     ctx.main = pthread_self();
1838     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1839     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1840 
1841     pfd.fd = ctx.efd[1];
1842     pfd.events = POLLIN;
1843     if (poll(&pfd, 1, -1) > 0) {
1844         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1845             __sync_fetch_and_add(&ctx.count, 1);
1846     }
1847 
1848     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1849     EXPECT_EQ(ctx.count, 2);
1850 
1851     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1852         pthread_kill(emitter, SIGUSR1);
1853         pthread_join(emitter, NULL);
1854     }
1855 
1856     close(ctx.efd[0]);
1857     close(ctx.efd[1]);
1858     close(ctx.sfd[0]);
1859     close(ctx.sfd[1]);
1860 }
1861 
1862 /*
1863  *        t0   t1
1864  *     (p) |    | (ew)
1865  *         |   e0
1866  *          \  / (lt)
1867  *           e1
1868  *            | (et)
1869  *           s0
1870  */
1871 TEST(epoll38)
1872 {
1873     pthread_t emitter;
1874     struct pollfd pfd;
1875     struct epoll_event e;
1876     struct epoll_mtcontext ctx = { 0 };
1877 
1878     signal(SIGUSR1, signal_handler);
1879 
1880     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1881 
1882     ctx.efd[0] = epoll_create(1);
1883     ASSERT_GE(ctx.efd[0], 0);
1884 
1885     ctx.efd[1] = epoll_create(1);
1886     ASSERT_GE(ctx.efd[1], 0);
1887 
1888     e.events = EPOLLIN | EPOLLET;
1889     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1890 
1891     e.events = EPOLLIN;
1892     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1893 
1894     ctx.main = pthread_self();
1895     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
1896     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1897 
1898     pfd.fd = ctx.efd[1];
1899     pfd.events = POLLIN;
1900     if (poll(&pfd, 1, -1) > 0) {
1901         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1902             __sync_fetch_and_or(&ctx.count, 2);
1903     }
1904 
1905     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1906     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
1907 
1908     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1909         pthread_kill(emitter, SIGUSR1);
1910         pthread_join(emitter, NULL);
1911     }
1912 
1913     close(ctx.efd[0]);
1914     close(ctx.efd[1]);
1915     close(ctx.sfd[0]);
1916     close(ctx.sfd[1]);
1917 }
1918 
1919 /*
1920  *        t0   t1
1921  *     (p) |    | (ew)
1922  *         |   e0
1923  *          \  / (et)
1924  *           e1
1925  *            | (lt)
1926  *           s0
1927  */
1928 TEST(epoll39)
1929 {
1930     pthread_t emitter;
1931     struct pollfd pfd;
1932     struct epoll_event e;
1933     struct epoll_mtcontext ctx = { 0 };
1934 
1935     signal(SIGUSR1, signal_handler);
1936 
1937     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1938 
1939     ctx.efd[0] = epoll_create(1);
1940     ASSERT_GE(ctx.efd[0], 0);
1941 
1942     ctx.efd[1] = epoll_create(1);
1943     ASSERT_GE(ctx.efd[1], 0);
1944 
1945     e.events = EPOLLIN;
1946     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
1947 
1948     e.events = EPOLLIN | EPOLLET;
1949     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
1950 
1951     ctx.main = pthread_self();
1952     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
1953     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
1954 
1955     pfd.fd = ctx.efd[1];
1956     pfd.events = POLLIN;
1957     if (poll(&pfd, 1, -1) > 0) {
1958         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
1959             __sync_fetch_and_add(&ctx.count, 1);
1960     }
1961 
1962     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
1963     EXPECT_EQ(ctx.count, 2);
1964 
1965     if (pthread_tryjoin_np(emitter, NULL) < 0) {
1966         pthread_kill(emitter, SIGUSR1);
1967         pthread_join(emitter, NULL);
1968     }
1969 
1970     close(ctx.efd[0]);
1971     close(ctx.efd[1]);
1972     close(ctx.sfd[0]);
1973     close(ctx.sfd[1]);
1974 }
1975 
1976 /*
1977  *        t0   t1
1978  *     (p) |    | (ew)
1979  *         |   e0
1980  *          \  / (et)
1981  *           e1
1982  *            | (et)
1983  *           s0
1984  */
1985 TEST(epoll40)
1986 {
1987     pthread_t emitter;
1988     struct pollfd pfd;
1989     struct epoll_event e;
1990     struct epoll_mtcontext ctx = { 0 };
1991 
1992     signal(SIGUSR1, signal_handler);
1993 
1994     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
1995 
1996     ctx.efd[0] = epoll_create(1);
1997     ASSERT_GE(ctx.efd[0], 0);
1998 
1999     ctx.efd[1] = epoll_create(1);
2000     ASSERT_GE(ctx.efd[1], 0);
2001 
2002     e.events = EPOLLIN | EPOLLET;
2003     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2004 
2005     e.events = EPOLLIN | EPOLLET;
2006     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2007 
2008     ctx.main = pthread_self();
2009     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
2010     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2011 
2012     pfd.fd = ctx.efd[1];
2013     pfd.events = POLLIN;
2014     if (poll(&pfd, 1, -1) > 0) {
2015         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2016             __sync_fetch_and_or(&ctx.count, 2);
2017     }
2018 
2019     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2020     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2021 
2022     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2023         pthread_kill(emitter, SIGUSR1);
2024         pthread_join(emitter, NULL);
2025     }
2026 
2027     close(ctx.efd[0]);
2028     close(ctx.efd[1]);
2029     close(ctx.sfd[0]);
2030     close(ctx.sfd[1]);
2031 }
2032 
2033 /*
2034  *        t0   t1
2035  *    (ew) |    | (p)
2036  *         |   e0
2037  *          \  / (lt)
2038  *           e1
2039  *            | (lt)
2040  *           s0
2041  */
2042 TEST(epoll41)
2043 {
2044     pthread_t emitter;
2045     struct epoll_event e;
2046     struct epoll_mtcontext ctx = { 0 };
2047 
2048     signal(SIGUSR1, signal_handler);
2049 
2050     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2051 
2052     ctx.efd[0] = epoll_create(1);
2053     ASSERT_GE(ctx.efd[0], 0);
2054 
2055     ctx.efd[1] = epoll_create(1);
2056     ASSERT_GE(ctx.efd[1], 0);
2057 
2058     e.events = EPOLLIN;
2059     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2060 
2061     e.events = EPOLLIN;
2062     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2063 
2064     ctx.main = pthread_self();
2065     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2066     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2067 
2068     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2069         __sync_fetch_and_add(&ctx.count, 1);
2070 
2071     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2072     EXPECT_EQ(ctx.count, 2);
2073 
2074     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2075         pthread_kill(emitter, SIGUSR1);
2076         pthread_join(emitter, NULL);
2077     }
2078 
2079     close(ctx.efd[0]);
2080     close(ctx.efd[1]);
2081     close(ctx.sfd[0]);
2082     close(ctx.sfd[1]);
2083 }
2084 
2085 /*
2086  *        t0   t1
2087  *    (ew) |    | (p)
2088  *         |   e0
2089  *          \  / (lt)
2090  *           e1
2091  *            | (et)
2092  *           s0
2093  */
2094 TEST(epoll42)
2095 {
2096     pthread_t emitter;
2097     struct epoll_event e;
2098     struct epoll_mtcontext ctx = { 0 };
2099 
2100     signal(SIGUSR1, signal_handler);
2101 
2102     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2103 
2104     ctx.efd[0] = epoll_create(1);
2105     ASSERT_GE(ctx.efd[0], 0);
2106 
2107     ctx.efd[1] = epoll_create(1);
2108     ASSERT_GE(ctx.efd[1], 0);
2109 
2110     e.events = EPOLLIN | EPOLLET;
2111     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2112 
2113     e.events = EPOLLIN;
2114     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2115 
2116     ctx.main = pthread_self();
2117     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2118     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2119 
2120     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2121         __sync_fetch_and_or(&ctx.count, 2);
2122 
2123     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2124     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2125 
2126     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2127         pthread_kill(emitter, SIGUSR1);
2128         pthread_join(emitter, NULL);
2129     }
2130 
2131     close(ctx.efd[0]);
2132     close(ctx.efd[1]);
2133     close(ctx.sfd[0]);
2134     close(ctx.sfd[1]);
2135 }
2136 
2137 /*
2138  *        t0   t1
2139  *    (ew) |    | (p)
2140  *         |   e0
2141  *          \  / (et)
2142  *           e1
2143  *            | (lt)
2144  *           s0
2145  */
2146 TEST(epoll43)
2147 {
2148     pthread_t emitter;
2149     struct epoll_event e;
2150     struct epoll_mtcontext ctx = { 0 };
2151 
2152     signal(SIGUSR1, signal_handler);
2153 
2154     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2155 
2156     ctx.efd[0] = epoll_create(1);
2157     ASSERT_GE(ctx.efd[0], 0);
2158 
2159     ctx.efd[1] = epoll_create(1);
2160     ASSERT_GE(ctx.efd[1], 0);
2161 
2162     e.events = EPOLLIN;
2163     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2164 
2165     e.events = EPOLLIN | EPOLLET;
2166     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2167 
2168     ctx.main = pthread_self();
2169     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2170     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2171 
2172     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2173         __sync_fetch_and_add(&ctx.count, 1);
2174 
2175     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2176     EXPECT_EQ(ctx.count, 2);
2177 
2178     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2179         pthread_kill(emitter, SIGUSR1);
2180         pthread_join(emitter, NULL);
2181     }
2182 
2183     close(ctx.efd[0]);
2184     close(ctx.efd[1]);
2185     close(ctx.sfd[0]);
2186     close(ctx.sfd[1]);
2187 }
2188 
2189 /*
2190  *        t0   t1
2191  *    (ew) |    | (p)
2192  *         |   e0
2193  *          \  / (et)
2194  *           e1
2195  *            | (et)
2196  *           s0
2197  */
2198 TEST(epoll44)
2199 {
2200     pthread_t emitter;
2201     struct epoll_event e;
2202     struct epoll_mtcontext ctx = { 0 };
2203 
2204     signal(SIGUSR1, signal_handler);
2205 
2206     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2207 
2208     ctx.efd[0] = epoll_create(1);
2209     ASSERT_GE(ctx.efd[0], 0);
2210 
2211     ctx.efd[1] = epoll_create(1);
2212     ASSERT_GE(ctx.efd[1], 0);
2213 
2214     e.events = EPOLLIN | EPOLLET;
2215     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2216 
2217     e.events = EPOLLIN | EPOLLET;
2218     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2219 
2220     ctx.main = pthread_self();
2221     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2222     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2223 
2224     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2225         __sync_fetch_and_or(&ctx.count, 2);
2226 
2227     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2228     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2229 
2230     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2231         pthread_kill(emitter, SIGUSR1);
2232         pthread_join(emitter, NULL);
2233     }
2234 
2235     close(ctx.efd[0]);
2236     close(ctx.efd[1]);
2237     close(ctx.sfd[0]);
2238     close(ctx.sfd[1]);
2239 }
2240 
2241 /*
2242  *        t0   t1
2243  *     (p) |    | (p)
2244  *         |   e0
2245  *          \  / (lt)
2246  *           e1
2247  *            | (lt)
2248  *           s0
2249  */
2250 TEST(epoll45)
2251 {
2252     pthread_t emitter;
2253     struct pollfd pfd;
2254     struct epoll_event e;
2255     struct epoll_mtcontext ctx = { 0 };
2256 
2257     signal(SIGUSR1, signal_handler);
2258 
2259     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2260 
2261     ctx.efd[0] = epoll_create(1);
2262     ASSERT_GE(ctx.efd[0], 0);
2263 
2264     ctx.efd[1] = epoll_create(1);
2265     ASSERT_GE(ctx.efd[1], 0);
2266 
2267     e.events = EPOLLIN;
2268     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2269 
2270     e.events = EPOLLIN;
2271     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2272 
2273     ctx.main = pthread_self();
2274     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2275     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2276 
2277     pfd.fd = ctx.efd[1];
2278     pfd.events = POLLIN;
2279     if (poll(&pfd, 1, -1) > 0) {
2280         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2281             __sync_fetch_and_add(&ctx.count, 1);
2282     }
2283 
2284     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2285     EXPECT_EQ(ctx.count, 2);
2286 
2287     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2288         pthread_kill(emitter, SIGUSR1);
2289         pthread_join(emitter, NULL);
2290     }
2291 
2292     close(ctx.efd[0]);
2293     close(ctx.efd[1]);
2294     close(ctx.sfd[0]);
2295     close(ctx.sfd[1]);
2296 }
2297 
2298 /*
2299  *        t0   t1
2300  *     (p) |    | (p)
2301  *         |   e0
2302  *          \  / (lt)
2303  *           e1
2304  *            | (et)
2305  *           s0
2306  */
2307 TEST(epoll46)
2308 {
2309     pthread_t emitter;
2310     struct epoll_event e;
2311     struct epoll_mtcontext ctx = { 0 };
2312 
2313     signal(SIGUSR1, signal_handler);
2314 
2315     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2316 
2317     ctx.efd[0] = epoll_create(1);
2318     ASSERT_GE(ctx.efd[0], 0);
2319 
2320     ctx.efd[1] = epoll_create(1);
2321     ASSERT_GE(ctx.efd[1], 0);
2322 
2323     e.events = EPOLLIN | EPOLLET;
2324     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2325 
2326     e.events = EPOLLIN;
2327     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2328 
2329     ctx.main = pthread_self();
2330     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2331     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2332 
2333     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2334         __sync_fetch_and_or(&ctx.count, 2);
2335 
2336     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2337     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2338 
2339     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2340         pthread_kill(emitter, SIGUSR1);
2341         pthread_join(emitter, NULL);
2342     }
2343 
2344     close(ctx.efd[0]);
2345     close(ctx.efd[1]);
2346     close(ctx.sfd[0]);
2347     close(ctx.sfd[1]);
2348 }
2349 
2350 /*
2351  *        t0   t1
2352  *     (p) |    | (p)
2353  *         |   e0
2354  *          \  / (et)
2355  *           e1
2356  *            | (lt)
2357  *           s0
2358  */
2359 TEST(epoll47)
2360 {
2361     pthread_t emitter;
2362     struct pollfd pfd;
2363     struct epoll_event e;
2364     struct epoll_mtcontext ctx = { 0 };
2365 
2366     signal(SIGUSR1, signal_handler);
2367 
2368     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2369 
2370     ctx.efd[0] = epoll_create(1);
2371     ASSERT_GE(ctx.efd[0], 0);
2372 
2373     ctx.efd[1] = epoll_create(1);
2374     ASSERT_GE(ctx.efd[1], 0);
2375 
2376     e.events = EPOLLIN;
2377     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2378 
2379     e.events = EPOLLIN | EPOLLET;
2380     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2381 
2382     ctx.main = pthread_self();
2383     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2384     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2385 
2386     pfd.fd = ctx.efd[1];
2387     pfd.events = POLLIN;
2388     if (poll(&pfd, 1, -1) > 0) {
2389         if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
2390             __sync_fetch_and_add(&ctx.count, 1);
2391     }
2392 
2393     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2394     EXPECT_EQ(ctx.count, 2);
2395 
2396     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2397         pthread_kill(emitter, SIGUSR1);
2398         pthread_join(emitter, NULL);
2399     }
2400 
2401     close(ctx.efd[0]);
2402     close(ctx.efd[1]);
2403     close(ctx.sfd[0]);
2404     close(ctx.sfd[1]);
2405 }
2406 
2407 /*
2408  *        t0   t1
2409  *     (p) |    | (p)
2410  *         |   e0
2411  *          \  / (et)
2412  *           e1
2413  *            | (et)
2414  *           s0
2415  */
2416 TEST(epoll48)
2417 {
2418     pthread_t emitter;
2419     struct epoll_event e;
2420     struct epoll_mtcontext ctx = { 0 };
2421 
2422     signal(SIGUSR1, signal_handler);
2423 
2424     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
2425 
2426     ctx.efd[0] = epoll_create(1);
2427     ASSERT_GE(ctx.efd[0], 0);
2428 
2429     ctx.efd[1] = epoll_create(1);
2430     ASSERT_GE(ctx.efd[1], 0);
2431 
2432     e.events = EPOLLIN | EPOLLET;
2433     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2434 
2435     e.events = EPOLLIN | EPOLLET;
2436     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2437 
2438     ctx.main = pthread_self();
2439     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
2440     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
2441 
2442     if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
2443         __sync_fetch_and_or(&ctx.count, 2);
2444 
2445     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2446     EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
2447 
2448     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2449         pthread_kill(emitter, SIGUSR1);
2450         pthread_join(emitter, NULL);
2451     }
2452 
2453     close(ctx.efd[0]);
2454     close(ctx.efd[1]);
2455     close(ctx.sfd[0]);
2456     close(ctx.sfd[1]);
2457 }
2458 
2459 /*
2460  *           t0
2461  *            | (ew)
2462  *           e0
2463  *     (lt) /  \ (lt)
2464  *        e1    e2
2465  *    (lt) |     | (lt)
2466  *        s0    s2
2467  */
2468 TEST(epoll49)
2469 {
2470     int efd[3];
2471     int sfd[4];
2472     struct epoll_event events[2];
2473 
2474     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2475     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2476 
2477     efd[0] = epoll_create(1);
2478     ASSERT_GE(efd[0], 0);
2479 
2480     efd[1] = epoll_create(1);
2481     ASSERT_GE(efd[1], 0);
2482 
2483     efd[2] = epoll_create(1);
2484     ASSERT_GE(efd[2], 0);
2485 
2486     events[0].events = EPOLLIN;
2487     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2488 
2489     events[0].events = EPOLLIN;
2490     ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2491 
2492     events[0].events = EPOLLIN;
2493     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2494 
2495     events[0].events = EPOLLIN;
2496     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2497 
2498     ASSERT_EQ(write(sfd[1], "w", 1), 1);
2499     ASSERT_EQ(write(sfd[3], "w", 1), 1);
2500 
2501     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2502     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2503 
2504     close(efd[0]);
2505     close(efd[1]);
2506     close(efd[2]);
2507     close(sfd[0]);
2508     close(sfd[1]);
2509     close(sfd[2]);
2510     close(sfd[3]);
2511 }
2512 
2513 /*
2514  *           t0
2515  *            | (ew)
2516  *           e0
2517  *     (et) /  \ (et)
2518  *        e1    e2
2519  *    (lt) |     | (lt)
2520  *        s0    s2
2521  */
2522 TEST(epoll50)
2523 {
2524     int efd[3];
2525     int sfd[4];
2526     struct epoll_event events[2];
2527 
2528     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2529     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2530 
2531     efd[0] = epoll_create(1);
2532     ASSERT_GE(efd[0], 0);
2533 
2534     efd[1] = epoll_create(1);
2535     ASSERT_GE(efd[1], 0);
2536 
2537     efd[2] = epoll_create(1);
2538     ASSERT_GE(efd[2], 0);
2539 
2540     events[0].events = EPOLLIN;
2541     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2542 
2543     events[0].events = EPOLLIN;
2544     ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2545 
2546     events[0].events = EPOLLIN | EPOLLET;
2547     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2548 
2549     events[0].events = EPOLLIN | EPOLLET;
2550     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2551 
2552     ASSERT_EQ(write(sfd[1], "w", 1), 1);
2553     ASSERT_EQ(write(sfd[3], "w", 1), 1);
2554 
2555     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2556     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2557 
2558     close(efd[0]);
2559     close(efd[1]);
2560     close(efd[2]);
2561     close(sfd[0]);
2562     close(sfd[1]);
2563     close(sfd[2]);
2564     close(sfd[3]);
2565 }
2566 
2567 /*
2568  *           t0
2569  *            | (p)
2570  *           e0
2571  *     (lt) /  \ (lt)
2572  *        e1    e2
2573  *    (lt) |     | (lt)
2574  *        s0    s2
2575  */
2576 TEST(epoll51)
2577 {
2578     int efd[3];
2579     int sfd[4];
2580     struct pollfd pfd;
2581     struct epoll_event events[2];
2582 
2583     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2584     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2585 
2586     efd[0] = epoll_create(1);
2587     ASSERT_GE(efd[0], 0);
2588 
2589     efd[1] = epoll_create(1);
2590     ASSERT_GE(efd[1], 0);
2591 
2592     efd[2] = epoll_create(1);
2593     ASSERT_GE(efd[2], 0);
2594 
2595     events[0].events = EPOLLIN;
2596     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2597 
2598     events[0].events = EPOLLIN;
2599     ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2600 
2601     events[0].events = EPOLLIN;
2602     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2603 
2604     events[0].events = EPOLLIN;
2605     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2606 
2607     ASSERT_EQ(write(sfd[1], "w", 1), 1);
2608     ASSERT_EQ(write(sfd[3], "w", 1), 1);
2609 
2610     pfd.fd = efd[0];
2611     pfd.events = POLLIN;
2612     EXPECT_EQ(poll(&pfd, 1, 0), 1);
2613     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2614 
2615     pfd.fd = efd[0];
2616     pfd.events = POLLIN;
2617     EXPECT_EQ(poll(&pfd, 1, 0), 1);
2618     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2619 
2620     close(efd[0]);
2621     close(efd[1]);
2622     close(efd[2]);
2623     close(sfd[0]);
2624     close(sfd[1]);
2625     close(sfd[2]);
2626     close(sfd[3]);
2627 }
2628 
2629 /*
2630  *           t0
2631  *            | (p)
2632  *           e0
2633  *     (et) /  \ (et)
2634  *        e1    e2
2635  *    (lt) |     | (lt)
2636  *        s0    s2
2637  */
2638 TEST(epoll52)
2639 {
2640     int efd[3];
2641     int sfd[4];
2642     struct pollfd pfd;
2643     struct epoll_event events[2];
2644 
2645     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
2646     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
2647 
2648     efd[0] = epoll_create(1);
2649     ASSERT_GE(efd[0], 0);
2650 
2651     efd[1] = epoll_create(1);
2652     ASSERT_GE(efd[1], 0);
2653 
2654     efd[2] = epoll_create(1);
2655     ASSERT_GE(efd[2], 0);
2656 
2657     events[0].events = EPOLLIN;
2658     ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
2659 
2660     events[0].events = EPOLLIN;
2661     ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
2662 
2663     events[0].events = EPOLLIN | EPOLLET;
2664     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
2665 
2666     events[0].events = EPOLLIN | EPOLLET;
2667     ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
2668 
2669     ASSERT_EQ(write(sfd[1], "w", 1), 1);
2670     ASSERT_EQ(write(sfd[3], "w", 1), 1);
2671 
2672     pfd.fd = efd[0];
2673     pfd.events = POLLIN;
2674     EXPECT_EQ(poll(&pfd, 1, 0), 1);
2675     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
2676 
2677     pfd.fd = efd[0];
2678     pfd.events = POLLIN;
2679     EXPECT_EQ(poll(&pfd, 1, 0), 0);
2680     EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
2681 
2682     close(efd[0]);
2683     close(efd[1]);
2684     close(efd[2]);
2685     close(sfd[0]);
2686     close(sfd[1]);
2687     close(sfd[2]);
2688     close(sfd[3]);
2689 }
2690 
2691 /*
2692  *        t0    t1
2693  *     (ew) \  / (ew)
2694  *           e0
2695  *     (lt) /  \ (lt)
2696  *        e1    e2
2697  *    (lt) |     | (lt)
2698  *        s0    s2
2699  */
2700 TEST(epoll53)
2701 {
2702     pthread_t emitter;
2703     struct epoll_event e;
2704     struct epoll_mtcontext ctx = { 0 };
2705 
2706     signal(SIGUSR1, signal_handler);
2707 
2708     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2709     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2710 
2711     ctx.efd[0] = epoll_create(1);
2712     ASSERT_GE(ctx.efd[0], 0);
2713 
2714     ctx.efd[1] = epoll_create(1);
2715     ASSERT_GE(ctx.efd[1], 0);
2716 
2717     ctx.efd[2] = epoll_create(1);
2718     ASSERT_GE(ctx.efd[2], 0);
2719 
2720     e.events = EPOLLIN;
2721     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2722 
2723     e.events = EPOLLIN;
2724     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2725 
2726     e.events = EPOLLIN;
2727     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2728 
2729     e.events = EPOLLIN;
2730     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2731 
2732     ctx.main = pthread_self();
2733     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2734     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2735 
2736     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2737         __sync_fetch_and_add(&ctx.count, 1);
2738 
2739     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2740     EXPECT_EQ(ctx.count, 2);
2741 
2742     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2743         pthread_kill(emitter, SIGUSR1);
2744         pthread_join(emitter, NULL);
2745     }
2746 
2747     close(ctx.efd[0]);
2748     close(ctx.efd[1]);
2749     close(ctx.efd[2]);
2750     close(ctx.sfd[0]);
2751     close(ctx.sfd[1]);
2752     close(ctx.sfd[2]);
2753     close(ctx.sfd[3]);
2754 }
2755 
2756 /*
2757  *        t0    t1
2758  *     (ew) \  / (ew)
2759  *           e0
2760  *     (et) /  \ (et)
2761  *        e1    e2
2762  *    (lt) |     | (lt)
2763  *        s0    s2
2764  */
2765 TEST(epoll54)
2766 {
2767     pthread_t emitter;
2768     struct epoll_event e;
2769     struct epoll_mtcontext ctx = { 0 };
2770 
2771     signal(SIGUSR1, signal_handler);
2772 
2773     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2774     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2775 
2776     ctx.efd[0] = epoll_create(1);
2777     ASSERT_GE(ctx.efd[0], 0);
2778 
2779     ctx.efd[1] = epoll_create(1);
2780     ASSERT_GE(ctx.efd[1], 0);
2781 
2782     ctx.efd[2] = epoll_create(1);
2783     ASSERT_GE(ctx.efd[2], 0);
2784 
2785     e.events = EPOLLIN;
2786     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2787 
2788     e.events = EPOLLIN;
2789     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2790 
2791     e.events = EPOLLIN | EPOLLET;
2792     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2793 
2794     e.events = EPOLLIN | EPOLLET;
2795     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2796 
2797     ctx.main = pthread_self();
2798     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
2799     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2800 
2801     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2802         __sync_fetch_and_add(&ctx.count, 1);
2803 
2804     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2805     EXPECT_EQ(ctx.count, 2);
2806 
2807     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2808         pthread_kill(emitter, SIGUSR1);
2809         pthread_join(emitter, NULL);
2810     }
2811 
2812     close(ctx.efd[0]);
2813     close(ctx.efd[1]);
2814     close(ctx.efd[2]);
2815     close(ctx.sfd[0]);
2816     close(ctx.sfd[1]);
2817     close(ctx.sfd[2]);
2818     close(ctx.sfd[3]);
2819 }
2820 
2821 /*
2822  *        t0    t1
2823  *     (ew) \  / (p)
2824  *           e0
2825  *     (lt) /  \ (lt)
2826  *        e1    e2
2827  *    (lt) |     | (lt)
2828  *        s0    s2
2829  */
2830 TEST(epoll55)
2831 {
2832     pthread_t emitter;
2833     struct epoll_event e;
2834     struct epoll_mtcontext ctx = { 0 };
2835 
2836     signal(SIGUSR1, signal_handler);
2837 
2838     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2839     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2840 
2841     ctx.efd[0] = epoll_create(1);
2842     ASSERT_GE(ctx.efd[0], 0);
2843 
2844     ctx.efd[1] = epoll_create(1);
2845     ASSERT_GE(ctx.efd[1], 0);
2846 
2847     ctx.efd[2] = epoll_create(1);
2848     ASSERT_GE(ctx.efd[2], 0);
2849 
2850     e.events = EPOLLIN;
2851     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2852 
2853     e.events = EPOLLIN;
2854     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2855 
2856     e.events = EPOLLIN;
2857     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2858 
2859     e.events = EPOLLIN;
2860     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2861 
2862     ctx.main = pthread_self();
2863     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2864     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2865 
2866     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2867         __sync_fetch_and_add(&ctx.count, 1);
2868 
2869     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2870     EXPECT_EQ(ctx.count, 2);
2871 
2872     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2873         pthread_kill(emitter, SIGUSR1);
2874         pthread_join(emitter, NULL);
2875     }
2876 
2877     close(ctx.efd[0]);
2878     close(ctx.efd[1]);
2879     close(ctx.efd[2]);
2880     close(ctx.sfd[0]);
2881     close(ctx.sfd[1]);
2882     close(ctx.sfd[2]);
2883     close(ctx.sfd[3]);
2884 }
2885 
2886 /*
2887  *        t0    t1
2888  *     (ew) \  / (p)
2889  *           e0
2890  *     (et) /  \ (et)
2891  *        e1    e2
2892  *    (lt) |     | (lt)
2893  *        s0    s2
2894  */
2895 TEST(epoll56)
2896 {
2897     pthread_t emitter;
2898     struct epoll_event e;
2899     struct epoll_mtcontext ctx = { 0 };
2900 
2901     signal(SIGUSR1, signal_handler);
2902 
2903     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2904     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2905 
2906     ctx.efd[0] = epoll_create(1);
2907     ASSERT_GE(ctx.efd[0], 0);
2908 
2909     ctx.efd[1] = epoll_create(1);
2910     ASSERT_GE(ctx.efd[1], 0);
2911 
2912     ctx.efd[2] = epoll_create(1);
2913     ASSERT_GE(ctx.efd[2], 0);
2914 
2915     e.events = EPOLLIN;
2916     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2917 
2918     e.events = EPOLLIN;
2919     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2920 
2921     e.events = EPOLLIN | EPOLLET;
2922     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2923 
2924     e.events = EPOLLIN | EPOLLET;
2925     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2926 
2927     ctx.main = pthread_self();
2928     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2929     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2930 
2931     if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
2932         __sync_fetch_and_add(&ctx.count, 1);
2933 
2934     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
2935     EXPECT_EQ(ctx.count, 2);
2936 
2937     if (pthread_tryjoin_np(emitter, NULL) < 0) {
2938         pthread_kill(emitter, SIGUSR1);
2939         pthread_join(emitter, NULL);
2940     }
2941 
2942     close(ctx.efd[0]);
2943     close(ctx.efd[1]);
2944     close(ctx.efd[2]);
2945     close(ctx.sfd[0]);
2946     close(ctx.sfd[1]);
2947     close(ctx.sfd[2]);
2948     close(ctx.sfd[3]);
2949 }
2950 
2951 /*
2952  *        t0    t1
2953  *      (p) \  / (p)
2954  *           e0
2955  *     (lt) /  \ (lt)
2956  *        e1    e2
2957  *    (lt) |     | (lt)
2958  *        s0    s2
2959  */
2960 TEST(epoll57)
2961 {
2962     pthread_t emitter;
2963     struct pollfd pfd;
2964     struct epoll_event e;
2965     struct epoll_mtcontext ctx = { 0 };
2966 
2967     signal(SIGUSR1, signal_handler);
2968 
2969     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
2970     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
2971 
2972     ctx.efd[0] = epoll_create(1);
2973     ASSERT_GE(ctx.efd[0], 0);
2974 
2975     ctx.efd[1] = epoll_create(1);
2976     ASSERT_GE(ctx.efd[1], 0);
2977 
2978     ctx.efd[2] = epoll_create(1);
2979     ASSERT_GE(ctx.efd[2], 0);
2980 
2981     e.events = EPOLLIN;
2982     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
2983 
2984     e.events = EPOLLIN;
2985     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
2986 
2987     e.events = EPOLLIN;
2988     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
2989 
2990     e.events = EPOLLIN;
2991     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
2992 
2993     ctx.main = pthread_self();
2994     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
2995     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
2996 
2997     pfd.fd = ctx.efd[0];
2998     pfd.events = POLLIN;
2999     if (poll(&pfd, 1, -1) > 0) {
3000         if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
3001             __sync_fetch_and_add(&ctx.count, 1);
3002     }
3003 
3004     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
3005     EXPECT_EQ(ctx.count, 2);
3006 
3007     if (pthread_tryjoin_np(emitter, NULL) < 0) {
3008         pthread_kill(emitter, SIGUSR1);
3009         pthread_join(emitter, NULL);
3010     }
3011 
3012     close(ctx.efd[0]);
3013     close(ctx.efd[1]);
3014     close(ctx.efd[2]);
3015     close(ctx.sfd[0]);
3016     close(ctx.sfd[1]);
3017     close(ctx.sfd[2]);
3018     close(ctx.sfd[3]);
3019 }
3020 
3021 /*
3022  *        t0    t1
3023  *      (p) \  / (p)
3024  *           e0
3025  *     (et) /  \ (et)
3026  *        e1    e2
3027  *    (lt) |     | (lt)
3028  *        s0    s2
3029  */
3030 TEST(epoll58)
3031 {
3032     pthread_t emitter;
3033     struct pollfd pfd;
3034     struct epoll_event e;
3035     struct epoll_mtcontext ctx = { 0 };
3036 
3037     signal(SIGUSR1, signal_handler);
3038 
3039     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
3040     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
3041 
3042     ctx.efd[0] = epoll_create(1);
3043     ASSERT_GE(ctx.efd[0], 0);
3044 
3045     ctx.efd[1] = epoll_create(1);
3046     ASSERT_GE(ctx.efd[1], 0);
3047 
3048     ctx.efd[2] = epoll_create(1);
3049     ASSERT_GE(ctx.efd[2], 0);
3050 
3051     e.events = EPOLLIN;
3052     ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3053 
3054     e.events = EPOLLIN;
3055     ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
3056 
3057     e.events = EPOLLIN | EPOLLET;
3058     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
3059 
3060     e.events = EPOLLIN | EPOLLET;
3061     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
3062 
3063     ctx.main = pthread_self();
3064     ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
3065     ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
3066 
3067     pfd.fd = ctx.efd[0];
3068     pfd.events = POLLIN;
3069     if (poll(&pfd, 1, -1) > 0) {
3070         if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
3071             __sync_fetch_and_add(&ctx.count, 1);
3072     }
3073 
3074     ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
3075     EXPECT_EQ(ctx.count, 2);
3076 
3077     if (pthread_tryjoin_np(emitter, NULL) < 0) {
3078         pthread_kill(emitter, SIGUSR1);
3079         pthread_join(emitter, NULL);
3080     }
3081 
3082     close(ctx.efd[0]);
3083     close(ctx.efd[1]);
3084     close(ctx.efd[2]);
3085     close(ctx.sfd[0]);
3086     close(ctx.sfd[1]);
3087     close(ctx.sfd[2]);
3088     close(ctx.sfd[3]);
3089 }
3090 
3091 static void *epoll59_thread(void *ctx_)
3092 {
3093     struct epoll_mtcontext *ctx = ctx_;
3094     struct epoll_event e;
3095     int i;
3096 
3097     for (i = 0; i < 100000; i++) {
3098         while (ctx->count == 0)
3099             ;
3100 
3101         e.events = EPOLLIN | EPOLLERR | EPOLLET;
3102         epoll_ctl(ctx->efd[0], EPOLL_CTL_MOD, ctx->sfd[0], &e);
3103         ctx->count = 0;
3104     }
3105 
3106     return NULL;
3107 }
3108 
3109 /*
3110  *        t0
3111  *      (p) \
3112  *           e0
3113  *     (et) /
3114  *        e0
3115  *
3116  * Based on https://bugzilla.kernel.org/show_bug.cgi?id=205933
3117  */
3118 TEST(epoll59)
3119 {
3120     pthread_t emitter;
3121     struct pollfd pfd;
3122     struct epoll_event e;
3123     struct epoll_mtcontext ctx = { 0 };
3124     int i, ret;
3125 
3126     signal(SIGUSR1, signal_handler);
3127 
3128     ctx.efd[0] = epoll_create1(0);
3129     ASSERT_GE(ctx.efd[0], 0);
3130 
3131     ctx.sfd[0] = eventfd(1, 0);
3132     ASSERT_GE(ctx.sfd[0], 0);
3133 
3134     e.events = EPOLLIN | EPOLLERR | EPOLLET;
3135     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3136 
3137     ASSERT_EQ(pthread_create(&emitter, NULL, epoll59_thread, &ctx), 0);
3138 
3139     for (i = 0; i < 100000; i++) {
3140         ret = epoll_wait(ctx.efd[0], &e, 1, 1000);
3141         ASSERT_GT(ret, 0);
3142 
3143         while (ctx.count != 0)
3144             ;
3145         ctx.count = 1;
3146     }
3147     if (pthread_tryjoin_np(emitter, NULL) < 0) {
3148         pthread_kill(emitter, SIGUSR1);
3149         pthread_join(emitter, NULL);
3150     }
3151     close(ctx.efd[0]);
3152     close(ctx.sfd[0]);
3153 }
3154 
3155 enum {
3156     EPOLL60_EVENTS_NR = 10,
3157 };
3158 
3159 struct epoll60_ctx {
3160     volatile int stopped;
3161     int ready;
3162     int waiters;
3163     int epfd;
3164     int evfd[EPOLL60_EVENTS_NR];
3165 };
3166 
3167 static void *epoll60_wait_thread(void *ctx_)
3168 {
3169     struct epoll60_ctx *ctx = ctx_;
3170     struct epoll_event e;
3171     sigset_t sigmask;
3172     uint64_t v;
3173     int ret;
3174 
3175     /* Block SIGUSR1 */
3176     sigemptyset(&sigmask);
3177     sigaddset(&sigmask, SIGUSR1);
3178     sigprocmask(SIG_SETMASK, &sigmask, NULL);
3179 
3180     /* Prepare empty mask for epoll_pwait() */
3181     sigemptyset(&sigmask);
3182 
3183     while (!ctx->stopped) {
3184         /* Mark we are ready */
3185         __atomic_fetch_add(&ctx->ready, 1, __ATOMIC_ACQUIRE);
3186 
3187         /* Start when all are ready */
3188         while (__atomic_load_n(&ctx->ready, __ATOMIC_ACQUIRE) &&
3189                !ctx->stopped);
3190 
3191         /* Account this waiter */
3192         __atomic_fetch_add(&ctx->waiters, 1, __ATOMIC_ACQUIRE);
3193 
3194         ret = epoll_pwait(ctx->epfd, &e, 1, 2000, &sigmask);
3195         if (ret != 1) {
3196             /* We expect only signal delivery on stop */
3197             assert(ret < 0 && errno == EINTR && "Lost wakeup!\n");
3198             assert(ctx->stopped);
3199             break;
3200         }
3201 
3202         ret = read(e.data.fd, &v, sizeof(v));
3203         /* Since we are on ET mode, thus each thread gets its own fd. */
3204         assert(ret == sizeof(v));
3205 
3206         __atomic_fetch_sub(&ctx->waiters, 1, __ATOMIC_RELEASE);
3207     }
3208 
3209     return NULL;
3210 }
3211 
3212 static inline unsigned long long msecs(void)
3213 {
3214     struct timespec ts;
3215     unsigned long long msecs;
3216 
3217     clock_gettime(CLOCK_REALTIME, &ts);
3218     msecs = ts.tv_sec * 1000ull;
3219     msecs += ts.tv_nsec / 1000000ull;
3220 
3221     return msecs;
3222 }
3223 
3224 static inline int count_waiters(struct epoll60_ctx *ctx)
3225 {
3226     return __atomic_load_n(&ctx->waiters, __ATOMIC_ACQUIRE);
3227 }
3228 
3229 TEST(epoll60)
3230 {
3231     struct epoll60_ctx ctx = { 0 };
3232     pthread_t waiters[ARRAY_SIZE(ctx.evfd)];
3233     struct epoll_event e;
3234     int i, n, ret;
3235 
3236     signal(SIGUSR1, signal_handler);
3237 
3238     ctx.epfd = epoll_create1(0);
3239     ASSERT_GE(ctx.epfd, 0);
3240 
3241     /* Create event fds */
3242     for (i = 0; i < ARRAY_SIZE(ctx.evfd); i++) {
3243         ctx.evfd[i] = eventfd(0, EFD_NONBLOCK);
3244         ASSERT_GE(ctx.evfd[i], 0);
3245 
3246         e.events = EPOLLIN | EPOLLET;
3247         e.data.fd = ctx.evfd[i];
3248         ASSERT_EQ(epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, ctx.evfd[i], &e), 0);
3249     }
3250 
3251     /* Create waiter threads */
3252     for (i = 0; i < ARRAY_SIZE(waiters); i++)
3253         ASSERT_EQ(pthread_create(&waiters[i], NULL,
3254                      epoll60_wait_thread, &ctx), 0);
3255 
3256     for (i = 0; i < 300; i++) {
3257         uint64_t v = 1, ms;
3258 
3259         /* Wait for all to be ready */
3260         while (__atomic_load_n(&ctx.ready, __ATOMIC_ACQUIRE) !=
3261                ARRAY_SIZE(ctx.evfd))
3262             ;
3263 
3264         /* Steady, go */
3265         __atomic_fetch_sub(&ctx.ready, ARRAY_SIZE(ctx.evfd),
3266                    __ATOMIC_ACQUIRE);
3267 
3268         /* Wait all have gone to kernel */
3269         while (count_waiters(&ctx) != ARRAY_SIZE(ctx.evfd))
3270             ;
3271 
3272         /* 1ms should be enough to schedule away */
3273         usleep(1000);
3274 
3275         /* Quickly signal all handles at once */
3276         for (n = 0; n < ARRAY_SIZE(ctx.evfd); n++) {
3277             ret = write(ctx.evfd[n], &v, sizeof(v));
3278             ASSERT_EQ(ret, sizeof(v));
3279         }
3280 
3281         /* Busy loop for 1s and wait for all waiters to wake up */
3282         ms = msecs();
3283         while (count_waiters(&ctx) && msecs() < ms + 1000)
3284             ;
3285 
3286         ASSERT_EQ(count_waiters(&ctx), 0);
3287     }
3288     ctx.stopped = 1;
3289     /* Stop waiters */
3290     for (i = 0; i < ARRAY_SIZE(waiters); i++)
3291         ret = pthread_kill(waiters[i], SIGUSR1);
3292     for (i = 0; i < ARRAY_SIZE(waiters); i++)
3293         pthread_join(waiters[i], NULL);
3294 
3295     for (i = 0; i < ARRAY_SIZE(waiters); i++)
3296         close(ctx.evfd[i]);
3297     close(ctx.epfd);
3298 }
3299 
3300 struct epoll61_ctx {
3301     int epfd;
3302     int evfd;
3303 };
3304 
3305 static void *epoll61_write_eventfd(void *ctx_)
3306 {
3307     struct epoll61_ctx *ctx = ctx_;
3308     int64_t l = 1;
3309 
3310     usleep(10950);
3311     write(ctx->evfd, &l, sizeof(l));
3312     return NULL;
3313 }
3314 
3315 static void *epoll61_epoll_with_timeout(void *ctx_)
3316 {
3317     struct epoll61_ctx *ctx = ctx_;
3318     struct epoll_event events[1];
3319     int n;
3320 
3321     n = epoll_wait(ctx->epfd, events, 1, 11);
3322     /*
3323      * If epoll returned the eventfd, write on the eventfd to wake up the
3324      * blocking poller.
3325      */
3326     if (n == 1) {
3327         int64_t l = 1;
3328 
3329         write(ctx->evfd, &l, sizeof(l));
3330     }
3331     return NULL;
3332 }
3333 
3334 static void *epoll61_blocking_epoll(void *ctx_)
3335 {
3336     struct epoll61_ctx *ctx = ctx_;
3337     struct epoll_event events[1];
3338 
3339     epoll_wait(ctx->epfd, events, 1, -1);
3340     return NULL;
3341 }
3342 
3343 TEST(epoll61)
3344 {
3345     struct epoll61_ctx ctx;
3346     struct epoll_event ev;
3347     int i, r;
3348 
3349     ctx.epfd = epoll_create1(0);
3350     ASSERT_GE(ctx.epfd, 0);
3351     ctx.evfd = eventfd(0, EFD_NONBLOCK);
3352     ASSERT_GE(ctx.evfd, 0);
3353 
3354     ev.events = EPOLLIN | EPOLLET | EPOLLERR | EPOLLHUP;
3355     ev.data.ptr = NULL;
3356     r = epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, ctx.evfd, &ev);
3357     ASSERT_EQ(r, 0);
3358 
3359     /*
3360      * We are testing a race.  Repeat the test case 1000 times to make it
3361      * more likely to fail in case of a bug.
3362      */
3363     for (i = 0; i < 1000; i++) {
3364         pthread_t threads[3];
3365         int n;
3366 
3367         /*
3368          * Start 3 threads:
3369          * Thread 1 sleeps for 10.9ms and writes to the evenfd.
3370          * Thread 2 calls epoll with a timeout of 11ms.
3371          * Thread 3 calls epoll with a timeout of -1.
3372          *
3373          * The eventfd write by Thread 1 should either wakeup Thread 2
3374          * or Thread 3.  If it wakes up Thread 2, Thread 2 writes on the
3375          * eventfd to wake up Thread 3.
3376          *
3377          * If no events are missed, all three threads should eventually
3378          * be joinable.
3379          */
3380         ASSERT_EQ(pthread_create(&threads[0], NULL,
3381                      epoll61_write_eventfd, &ctx), 0);
3382         ASSERT_EQ(pthread_create(&threads[1], NULL,
3383                      epoll61_epoll_with_timeout, &ctx), 0);
3384         ASSERT_EQ(pthread_create(&threads[2], NULL,
3385                      epoll61_blocking_epoll, &ctx), 0);
3386 
3387         for (n = 0; n < ARRAY_SIZE(threads); ++n)
3388             ASSERT_EQ(pthread_join(threads[n], NULL), 0);
3389     }
3390 
3391     close(ctx.epfd);
3392     close(ctx.evfd);
3393 }
3394 
3395 /* Equivalent to basic test epoll1, but exercising epoll_pwait2. */
3396 TEST(epoll62)
3397 {
3398     int efd;
3399     int sfd[2];
3400     struct epoll_event e;
3401 
3402     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
3403 
3404     efd = epoll_create(1);
3405     ASSERT_GE(efd, 0);
3406 
3407     e.events = EPOLLIN;
3408     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
3409 
3410     ASSERT_EQ(write(sfd[1], "w", 1), 1);
3411 
3412     EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL, 0), 1);
3413     EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL, 0), 1);
3414 
3415     close(efd);
3416     close(sfd[0]);
3417     close(sfd[1]);
3418 }
3419 
3420 /* Epoll_pwait2 basic timeout test. */
3421 TEST(epoll63)
3422 {
3423     const int cfg_delay_ms = 10;
3424     unsigned long long tdiff;
3425     struct __kernel_timespec ts;
3426     int efd;
3427     int sfd[2];
3428     struct epoll_event e;
3429 
3430     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
3431 
3432     efd = epoll_create(1);
3433     ASSERT_GE(efd, 0);
3434 
3435     e.events = EPOLLIN;
3436     ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
3437 
3438     ts.tv_sec = 0;
3439     ts.tv_nsec = cfg_delay_ms * 1000 * 1000;
3440 
3441     tdiff = msecs();
3442     EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, &ts, NULL, 0), 0);
3443     tdiff = msecs() - tdiff;
3444 
3445     EXPECT_GE(tdiff, cfg_delay_ms);
3446 
3447     close(efd);
3448     close(sfd[0]);
3449     close(sfd[1]);
3450 }
3451 
3452 /*
3453  *        t0    t1
3454  *     (ew) \  / (ew)
3455  *           e0
3456  *            | (lt)
3457  *           s0
3458  */
3459 TEST(epoll64)
3460 {
3461     pthread_t waiter[2];
3462     struct epoll_event e;
3463     struct epoll_mtcontext ctx = { 0 };
3464 
3465     signal(SIGUSR1, signal_handler);
3466 
3467     ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
3468 
3469     ctx.efd[0] = epoll_create(1);
3470     ASSERT_GE(ctx.efd[0], 0);
3471 
3472     e.events = EPOLLIN;
3473     ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
3474 
3475     /*
3476      * main will act as the emitter once both waiter threads are
3477      * blocked and expects to both be awoken upon the ready event.
3478      */
3479     ctx.main = pthread_self();
3480     ASSERT_EQ(pthread_create(&waiter[0], NULL, waiter_entry1a, &ctx), 0);
3481     ASSERT_EQ(pthread_create(&waiter[1], NULL, waiter_entry1a, &ctx), 0);
3482 
3483     usleep(100000);
3484     ASSERT_EQ(write(ctx.sfd[1], "w", 1), 1);
3485 
3486     ASSERT_EQ(pthread_join(waiter[0], NULL), 0);
3487     ASSERT_EQ(pthread_join(waiter[1], NULL), 0);
3488 
3489     EXPECT_EQ(ctx.count, 2);
3490 
3491     close(ctx.efd[0]);
3492     close(ctx.sfd[0]);
3493     close(ctx.sfd[1]);
3494 }
3495 
3496 TEST_HARNESS_MAIN