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