PageRenderTime 60ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/testing/selftests/filesystems/epoll/epoll_wakeup_test.c

http://github.com/mirrors/linux
C | 3285 lines | 2041 code | 728 blank | 516 comment | 144 complexity | 6839c0da1b8dc5f2a08a02ea34b53610 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <poll.h>
  4. #include <unistd.h>
  5. #include <assert.h>
  6. #include <signal.h>
  7. #include <pthread.h>
  8. #include <sys/epoll.h>
  9. #include <sys/socket.h>
  10. #include <sys/eventfd.h>
  11. #include "../../kselftest_harness.h"
  12. struct epoll_mtcontext
  13. {
  14. int efd[3];
  15. int sfd[4];
  16. volatile int count;
  17. pthread_t main;
  18. pthread_t waiter;
  19. };
  20. static void signal_handler(int signum)
  21. {
  22. }
  23. static void kill_timeout(struct epoll_mtcontext *ctx)
  24. {
  25. usleep(1000000);
  26. pthread_kill(ctx->main, SIGUSR1);
  27. pthread_kill(ctx->waiter, SIGUSR1);
  28. }
  29. static void *waiter_entry1a(void *data)
  30. {
  31. struct epoll_event e;
  32. struct epoll_mtcontext *ctx = data;
  33. if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
  34. __sync_fetch_and_add(&ctx->count, 1);
  35. return NULL;
  36. }
  37. static void *waiter_entry1ap(void *data)
  38. {
  39. struct pollfd pfd;
  40. struct epoll_event e;
  41. struct epoll_mtcontext *ctx = data;
  42. pfd.fd = ctx->efd[0];
  43. pfd.events = POLLIN;
  44. if (poll(&pfd, 1, -1) > 0) {
  45. if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
  46. __sync_fetch_and_add(&ctx->count, 1);
  47. }
  48. return NULL;
  49. }
  50. static void *waiter_entry1o(void *data)
  51. {
  52. struct epoll_event e;
  53. struct epoll_mtcontext *ctx = data;
  54. if (epoll_wait(ctx->efd[0], &e, 1, -1) > 0)
  55. __sync_fetch_and_or(&ctx->count, 1);
  56. return NULL;
  57. }
  58. static void *waiter_entry1op(void *data)
  59. {
  60. struct pollfd pfd;
  61. struct epoll_event e;
  62. struct epoll_mtcontext *ctx = data;
  63. pfd.fd = ctx->efd[0];
  64. pfd.events = POLLIN;
  65. if (poll(&pfd, 1, -1) > 0) {
  66. if (epoll_wait(ctx->efd[0], &e, 1, 0) > 0)
  67. __sync_fetch_and_or(&ctx->count, 1);
  68. }
  69. return NULL;
  70. }
  71. static void *waiter_entry2a(void *data)
  72. {
  73. struct epoll_event events[2];
  74. struct epoll_mtcontext *ctx = data;
  75. if (epoll_wait(ctx->efd[0], events, 2, -1) > 0)
  76. __sync_fetch_and_add(&ctx->count, 1);
  77. return NULL;
  78. }
  79. static void *waiter_entry2ap(void *data)
  80. {
  81. struct pollfd pfd;
  82. struct epoll_event events[2];
  83. struct epoll_mtcontext *ctx = data;
  84. pfd.fd = ctx->efd[0];
  85. pfd.events = POLLIN;
  86. if (poll(&pfd, 1, -1) > 0) {
  87. if (epoll_wait(ctx->efd[0], events, 2, 0) > 0)
  88. __sync_fetch_and_add(&ctx->count, 1);
  89. }
  90. return NULL;
  91. }
  92. static void *emitter_entry1(void *data)
  93. {
  94. struct epoll_mtcontext *ctx = data;
  95. usleep(100000);
  96. write(ctx->sfd[1], "w", 1);
  97. kill_timeout(ctx);
  98. return NULL;
  99. }
  100. static void *emitter_entry2(void *data)
  101. {
  102. struct epoll_mtcontext *ctx = data;
  103. usleep(100000);
  104. write(ctx->sfd[1], "w", 1);
  105. write(ctx->sfd[3], "w", 1);
  106. kill_timeout(ctx);
  107. return NULL;
  108. }
  109. /*
  110. * t0
  111. * | (ew)
  112. * e0
  113. * | (lt)
  114. * s0
  115. */
  116. TEST(epoll1)
  117. {
  118. int efd;
  119. int sfd[2];
  120. struct epoll_event e;
  121. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  122. efd = epoll_create(1);
  123. ASSERT_GE(efd, 0);
  124. e.events = EPOLLIN;
  125. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
  126. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  127. EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  128. EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  129. close(efd);
  130. close(sfd[0]);
  131. close(sfd[1]);
  132. }
  133. /*
  134. * t0
  135. * | (ew)
  136. * e0
  137. * | (et)
  138. * s0
  139. */
  140. TEST(epoll2)
  141. {
  142. int efd;
  143. int sfd[2];
  144. struct epoll_event e;
  145. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  146. efd = epoll_create(1);
  147. ASSERT_GE(efd, 0);
  148. e.events = EPOLLIN | EPOLLET;
  149. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
  150. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  151. EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  152. EXPECT_EQ(epoll_wait(efd, &e, 1, 0), 0);
  153. close(efd);
  154. close(sfd[0]);
  155. close(sfd[1]);
  156. }
  157. /*
  158. * t0
  159. * | (ew)
  160. * e0
  161. * (lt) / \ (lt)
  162. * s0 s2
  163. */
  164. TEST(epoll3)
  165. {
  166. int efd;
  167. int sfd[4];
  168. struct epoll_event events[2];
  169. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  170. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  171. efd = epoll_create(1);
  172. ASSERT_GE(efd, 0);
  173. events[0].events = EPOLLIN;
  174. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
  175. events[0].events = EPOLLIN;
  176. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
  177. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  178. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  179. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  180. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  181. close(efd);
  182. close(sfd[0]);
  183. close(sfd[1]);
  184. close(sfd[2]);
  185. close(sfd[3]);
  186. }
  187. /*
  188. * t0
  189. * | (ew)
  190. * e0
  191. * (et) / \ (et)
  192. * s0 s2
  193. */
  194. TEST(epoll4)
  195. {
  196. int efd;
  197. int sfd[4];
  198. struct epoll_event events[2];
  199. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  200. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  201. efd = epoll_create(1);
  202. ASSERT_GE(efd, 0);
  203. events[0].events = EPOLLIN | EPOLLET;
  204. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
  205. events[0].events = EPOLLIN | EPOLLET;
  206. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
  207. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  208. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  209. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  210. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
  211. close(efd);
  212. close(sfd[0]);
  213. close(sfd[1]);
  214. close(sfd[2]);
  215. close(sfd[3]);
  216. }
  217. /*
  218. * t0
  219. * | (p)
  220. * e0
  221. * | (lt)
  222. * s0
  223. */
  224. TEST(epoll5)
  225. {
  226. int efd;
  227. int sfd[2];
  228. struct pollfd pfd;
  229. struct epoll_event e;
  230. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  231. efd = epoll_create(1);
  232. ASSERT_GE(efd, 0);
  233. e.events = EPOLLIN;
  234. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
  235. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  236. pfd.fd = efd;
  237. pfd.events = POLLIN;
  238. ASSERT_EQ(poll(&pfd, 1, 0), 1);
  239. ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  240. pfd.fd = efd;
  241. pfd.events = POLLIN;
  242. ASSERT_EQ(poll(&pfd, 1, 0), 1);
  243. ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  244. close(efd);
  245. close(sfd[0]);
  246. close(sfd[1]);
  247. }
  248. /*
  249. * t0
  250. * | (p)
  251. * e0
  252. * | (et)
  253. * s0
  254. */
  255. TEST(epoll6)
  256. {
  257. int efd;
  258. int sfd[2];
  259. struct pollfd pfd;
  260. struct epoll_event e;
  261. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  262. efd = epoll_create(1);
  263. ASSERT_GE(efd, 0);
  264. e.events = EPOLLIN | EPOLLET;
  265. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
  266. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  267. pfd.fd = efd;
  268. pfd.events = POLLIN;
  269. ASSERT_EQ(poll(&pfd, 1, 0), 1);
  270. ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 1);
  271. pfd.fd = efd;
  272. pfd.events = POLLIN;
  273. ASSERT_EQ(poll(&pfd, 1, 0), 0);
  274. ASSERT_EQ(epoll_wait(efd, &e, 1, 0), 0);
  275. close(efd);
  276. close(sfd[0]);
  277. close(sfd[1]);
  278. }
  279. /*
  280. * t0
  281. * | (p)
  282. * e0
  283. * (lt) / \ (lt)
  284. * s0 s2
  285. */
  286. TEST(epoll7)
  287. {
  288. int efd;
  289. int sfd[4];
  290. struct pollfd pfd;
  291. struct epoll_event events[2];
  292. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  293. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  294. efd = epoll_create(1);
  295. ASSERT_GE(efd, 0);
  296. events[0].events = EPOLLIN;
  297. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
  298. events[0].events = EPOLLIN;
  299. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
  300. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  301. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  302. pfd.fd = efd;
  303. pfd.events = POLLIN;
  304. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  305. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  306. pfd.fd = efd;
  307. pfd.events = POLLIN;
  308. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  309. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  310. close(efd);
  311. close(sfd[0]);
  312. close(sfd[1]);
  313. close(sfd[2]);
  314. close(sfd[3]);
  315. }
  316. /*
  317. * t0
  318. * | (p)
  319. * e0
  320. * (et) / \ (et)
  321. * s0 s2
  322. */
  323. TEST(epoll8)
  324. {
  325. int efd;
  326. int sfd[4];
  327. struct pollfd pfd;
  328. struct epoll_event events[2];
  329. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  330. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  331. efd = epoll_create(1);
  332. ASSERT_GE(efd, 0);
  333. events[0].events = EPOLLIN | EPOLLET;
  334. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], events), 0);
  335. events[0].events = EPOLLIN | EPOLLET;
  336. ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[2], events), 0);
  337. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  338. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  339. pfd.fd = efd;
  340. pfd.events = POLLIN;
  341. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  342. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 2);
  343. pfd.fd = efd;
  344. pfd.events = POLLIN;
  345. EXPECT_EQ(poll(&pfd, 1, 0), 0);
  346. EXPECT_EQ(epoll_wait(efd, events, 2, 0), 0);
  347. close(efd);
  348. close(sfd[0]);
  349. close(sfd[1]);
  350. close(sfd[2]);
  351. close(sfd[3]);
  352. }
  353. /*
  354. * t0 t1
  355. * (ew) \ / (ew)
  356. * e0
  357. * | (lt)
  358. * s0
  359. */
  360. TEST(epoll9)
  361. {
  362. pthread_t emitter;
  363. struct epoll_event e;
  364. struct epoll_mtcontext ctx = { 0 };
  365. signal(SIGUSR1, signal_handler);
  366. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  367. ctx.efd[0] = epoll_create(1);
  368. ASSERT_GE(ctx.efd[0], 0);
  369. e.events = EPOLLIN;
  370. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  371. ctx.main = pthread_self();
  372. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  373. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  374. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  375. __sync_fetch_and_add(&ctx.count, 1);
  376. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  377. EXPECT_EQ(ctx.count, 2);
  378. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  379. pthread_kill(emitter, SIGUSR1);
  380. pthread_join(emitter, NULL);
  381. }
  382. close(ctx.efd[0]);
  383. close(ctx.sfd[0]);
  384. close(ctx.sfd[1]);
  385. }
  386. /*
  387. * t0 t1
  388. * (ew) \ / (ew)
  389. * e0
  390. * | (et)
  391. * s0
  392. */
  393. TEST(epoll10)
  394. {
  395. pthread_t emitter;
  396. struct epoll_event e;
  397. struct epoll_mtcontext ctx = { 0 };
  398. signal(SIGUSR1, signal_handler);
  399. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  400. ctx.efd[0] = epoll_create(1);
  401. ASSERT_GE(ctx.efd[0], 0);
  402. e.events = EPOLLIN | EPOLLET;
  403. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  404. ctx.main = pthread_self();
  405. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  406. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  407. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  408. __sync_fetch_and_add(&ctx.count, 1);
  409. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  410. EXPECT_EQ(ctx.count, 1);
  411. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  412. pthread_kill(emitter, SIGUSR1);
  413. pthread_join(emitter, NULL);
  414. }
  415. close(ctx.efd[0]);
  416. close(ctx.sfd[0]);
  417. close(ctx.sfd[1]);
  418. }
  419. /*
  420. * t0 t1
  421. * (ew) \ / (ew)
  422. * e0
  423. * (lt) / \ (lt)
  424. * s0 s2
  425. */
  426. TEST(epoll11)
  427. {
  428. pthread_t emitter;
  429. struct epoll_event events[2];
  430. struct epoll_mtcontext ctx = { 0 };
  431. signal(SIGUSR1, signal_handler);
  432. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  433. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  434. ctx.efd[0] = epoll_create(1);
  435. ASSERT_GE(ctx.efd[0], 0);
  436. events[0].events = EPOLLIN;
  437. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
  438. events[0].events = EPOLLIN;
  439. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
  440. ctx.main = pthread_self();
  441. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2a, &ctx), 0);
  442. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  443. if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
  444. __sync_fetch_and_add(&ctx.count, 1);
  445. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  446. EXPECT_EQ(ctx.count, 2);
  447. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  448. pthread_kill(emitter, SIGUSR1);
  449. pthread_join(emitter, NULL);
  450. }
  451. close(ctx.efd[0]);
  452. close(ctx.sfd[0]);
  453. close(ctx.sfd[1]);
  454. close(ctx.sfd[2]);
  455. close(ctx.sfd[3]);
  456. }
  457. /*
  458. * t0 t1
  459. * (ew) \ / (ew)
  460. * e0
  461. * (et) / \ (et)
  462. * s0 s2
  463. */
  464. TEST(epoll12)
  465. {
  466. pthread_t emitter;
  467. struct epoll_event events[2];
  468. struct epoll_mtcontext ctx = { 0 };
  469. signal(SIGUSR1, signal_handler);
  470. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  471. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  472. ctx.efd[0] = epoll_create(1);
  473. ASSERT_GE(ctx.efd[0], 0);
  474. events[0].events = EPOLLIN | EPOLLET;
  475. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
  476. events[0].events = EPOLLIN | EPOLLET;
  477. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
  478. ctx.main = pthread_self();
  479. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  480. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  481. if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
  482. __sync_fetch_and_add(&ctx.count, 1);
  483. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  484. EXPECT_EQ(ctx.count, 2);
  485. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  486. pthread_kill(emitter, SIGUSR1);
  487. pthread_join(emitter, NULL);
  488. }
  489. close(ctx.efd[0]);
  490. close(ctx.sfd[0]);
  491. close(ctx.sfd[1]);
  492. close(ctx.sfd[2]);
  493. close(ctx.sfd[3]);
  494. }
  495. /*
  496. * t0 t1
  497. * (ew) \ / (p)
  498. * e0
  499. * | (lt)
  500. * s0
  501. */
  502. TEST(epoll13)
  503. {
  504. pthread_t emitter;
  505. struct epoll_event e;
  506. struct epoll_mtcontext ctx = { 0 };
  507. signal(SIGUSR1, signal_handler);
  508. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  509. ctx.efd[0] = epoll_create(1);
  510. ASSERT_GE(ctx.efd[0], 0);
  511. e.events = EPOLLIN;
  512. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  513. ctx.main = pthread_self();
  514. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  515. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  516. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  517. __sync_fetch_and_add(&ctx.count, 1);
  518. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  519. EXPECT_EQ(ctx.count, 2);
  520. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  521. pthread_kill(emitter, SIGUSR1);
  522. pthread_join(emitter, NULL);
  523. }
  524. close(ctx.efd[0]);
  525. close(ctx.sfd[0]);
  526. close(ctx.sfd[1]);
  527. }
  528. /*
  529. * t0 t1
  530. * (ew) \ / (p)
  531. * e0
  532. * | (et)
  533. * s0
  534. */
  535. TEST(epoll14)
  536. {
  537. pthread_t emitter;
  538. struct epoll_event e;
  539. struct epoll_mtcontext ctx = { 0 };
  540. signal(SIGUSR1, signal_handler);
  541. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  542. ctx.efd[0] = epoll_create(1);
  543. ASSERT_GE(ctx.efd[0], 0);
  544. e.events = EPOLLIN | EPOLLET;
  545. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  546. ctx.main = pthread_self();
  547. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  548. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  549. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  550. __sync_fetch_and_add(&ctx.count, 1);
  551. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  552. EXPECT_EQ(ctx.count, 1);
  553. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  554. pthread_kill(emitter, SIGUSR1);
  555. pthread_join(emitter, NULL);
  556. }
  557. close(ctx.efd[0]);
  558. close(ctx.sfd[0]);
  559. close(ctx.sfd[1]);
  560. }
  561. /*
  562. * t0 t1
  563. * (ew) \ / (p)
  564. * e0
  565. * (lt) / \ (lt)
  566. * s0 s2
  567. */
  568. TEST(epoll15)
  569. {
  570. pthread_t emitter;
  571. struct epoll_event events[2];
  572. struct epoll_mtcontext ctx = { 0 };
  573. signal(SIGUSR1, signal_handler);
  574. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  575. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  576. ctx.efd[0] = epoll_create(1);
  577. ASSERT_GE(ctx.efd[0], 0);
  578. events[0].events = EPOLLIN;
  579. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
  580. events[0].events = EPOLLIN;
  581. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
  582. ctx.main = pthread_self();
  583. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry2ap, &ctx), 0);
  584. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  585. if (epoll_wait(ctx.efd[0], events, 2, -1) > 0)
  586. __sync_fetch_and_add(&ctx.count, 1);
  587. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  588. EXPECT_EQ(ctx.count, 2);
  589. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  590. pthread_kill(emitter, SIGUSR1);
  591. pthread_join(emitter, NULL);
  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. * t0 t1
  601. * (ew) \ / (p)
  602. * e0
  603. * (et) / \ (et)
  604. * s0 s2
  605. */
  606. TEST(epoll16)
  607. {
  608. pthread_t emitter;
  609. struct epoll_event events[2];
  610. struct epoll_mtcontext ctx = { 0 };
  611. signal(SIGUSR1, signal_handler);
  612. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  613. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  614. ctx.efd[0] = epoll_create(1);
  615. ASSERT_GE(ctx.efd[0], 0);
  616. events[0].events = EPOLLIN | EPOLLET;
  617. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], events), 0);
  618. events[0].events = EPOLLIN | EPOLLET;
  619. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[2], events), 0);
  620. ctx.main = pthread_self();
  621. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  622. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  623. if (epoll_wait(ctx.efd[0], events, 1, -1) > 0)
  624. __sync_fetch_and_add(&ctx.count, 1);
  625. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  626. EXPECT_EQ(ctx.count, 2);
  627. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  628. pthread_kill(emitter, SIGUSR1);
  629. pthread_join(emitter, NULL);
  630. }
  631. close(ctx.efd[0]);
  632. close(ctx.sfd[0]);
  633. close(ctx.sfd[1]);
  634. close(ctx.sfd[2]);
  635. close(ctx.sfd[3]);
  636. }
  637. /*
  638. * t0
  639. * | (ew)
  640. * e0
  641. * | (lt)
  642. * e1
  643. * | (lt)
  644. * s0
  645. */
  646. TEST(epoll17)
  647. {
  648. int efd[2];
  649. int sfd[2];
  650. struct epoll_event e;
  651. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  652. efd[0] = epoll_create(1);
  653. ASSERT_GE(efd[0], 0);
  654. efd[1] = epoll_create(1);
  655. ASSERT_GE(efd[1], 0);
  656. e.events = EPOLLIN;
  657. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  658. e.events = EPOLLIN;
  659. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  660. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  661. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  662. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  663. close(efd[0]);
  664. close(efd[1]);
  665. close(sfd[0]);
  666. close(sfd[1]);
  667. }
  668. /*
  669. * t0
  670. * | (ew)
  671. * e0
  672. * | (lt)
  673. * e1
  674. * | (et)
  675. * s0
  676. */
  677. TEST(epoll18)
  678. {
  679. int efd[2];
  680. int sfd[2];
  681. struct epoll_event e;
  682. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  683. efd[0] = epoll_create(1);
  684. ASSERT_GE(efd[0], 0);
  685. efd[1] = epoll_create(1);
  686. ASSERT_GE(efd[1], 0);
  687. e.events = EPOLLIN | EPOLLET;
  688. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  689. e.events = EPOLLIN;
  690. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  691. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  692. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  693. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  694. close(efd[0]);
  695. close(efd[1]);
  696. close(sfd[0]);
  697. close(sfd[1]);
  698. }
  699. /*
  700. * t0
  701. * | (ew)
  702. * e0
  703. * | (et)
  704. * e1
  705. * | (lt)
  706. * s0
  707. */
  708. TEST(epoll19)
  709. {
  710. int efd[2];
  711. int sfd[2];
  712. struct epoll_event e;
  713. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  714. efd[0] = epoll_create(1);
  715. ASSERT_GE(efd[0], 0);
  716. efd[1] = epoll_create(1);
  717. ASSERT_GE(efd[1], 0);
  718. e.events = EPOLLIN;
  719. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  720. e.events = EPOLLIN | EPOLLET;
  721. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  722. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  723. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  724. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
  725. close(efd[0]);
  726. close(efd[1]);
  727. close(sfd[0]);
  728. close(sfd[1]);
  729. }
  730. /*
  731. * t0
  732. * | (ew)
  733. * e0
  734. * | (et)
  735. * e1
  736. * | (et)
  737. * s0
  738. */
  739. TEST(epoll20)
  740. {
  741. int efd[2];
  742. int sfd[2];
  743. struct epoll_event e;
  744. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  745. efd[0] = epoll_create(1);
  746. ASSERT_GE(efd[0], 0);
  747. efd[1] = epoll_create(1);
  748. ASSERT_GE(efd[1], 0);
  749. e.events = EPOLLIN | EPOLLET;
  750. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  751. e.events = EPOLLIN | EPOLLET;
  752. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  753. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  754. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  755. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
  756. close(efd[0]);
  757. close(efd[1]);
  758. close(sfd[0]);
  759. close(sfd[1]);
  760. }
  761. /*
  762. * t0
  763. * | (p)
  764. * e0
  765. * | (lt)
  766. * e1
  767. * | (lt)
  768. * s0
  769. */
  770. TEST(epoll21)
  771. {
  772. int efd[2];
  773. int sfd[2];
  774. struct pollfd pfd;
  775. struct epoll_event e;
  776. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  777. efd[0] = epoll_create(1);
  778. ASSERT_GE(efd[0], 0);
  779. efd[1] = epoll_create(1);
  780. ASSERT_GE(efd[1], 0);
  781. e.events = EPOLLIN;
  782. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  783. e.events = EPOLLIN;
  784. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  785. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  786. pfd.fd = efd[0];
  787. pfd.events = POLLIN;
  788. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  789. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  790. pfd.fd = efd[0];
  791. pfd.events = POLLIN;
  792. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  793. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  794. close(efd[0]);
  795. close(efd[1]);
  796. close(sfd[0]);
  797. close(sfd[1]);
  798. }
  799. /*
  800. * t0
  801. * | (p)
  802. * e0
  803. * | (lt)
  804. * e1
  805. * | (et)
  806. * s0
  807. */
  808. TEST(epoll22)
  809. {
  810. int efd[2];
  811. int sfd[2];
  812. struct pollfd pfd;
  813. struct epoll_event e;
  814. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  815. efd[0] = epoll_create(1);
  816. ASSERT_GE(efd[0], 0);
  817. efd[1] = epoll_create(1);
  818. ASSERT_GE(efd[1], 0);
  819. e.events = EPOLLIN | EPOLLET;
  820. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  821. e.events = EPOLLIN;
  822. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  823. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  824. pfd.fd = efd[0];
  825. pfd.events = POLLIN;
  826. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  827. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  828. pfd.fd = efd[0];
  829. pfd.events = POLLIN;
  830. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  831. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  832. close(efd[0]);
  833. close(efd[1]);
  834. close(sfd[0]);
  835. close(sfd[1]);
  836. }
  837. /*
  838. * t0
  839. * | (p)
  840. * e0
  841. * | (et)
  842. * e1
  843. * | (lt)
  844. * s0
  845. */
  846. TEST(epoll23)
  847. {
  848. int efd[2];
  849. int sfd[2];
  850. struct pollfd pfd;
  851. struct epoll_event e;
  852. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  853. efd[0] = epoll_create(1);
  854. ASSERT_GE(efd[0], 0);
  855. efd[1] = epoll_create(1);
  856. ASSERT_GE(efd[1], 0);
  857. e.events = EPOLLIN;
  858. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  859. e.events = EPOLLIN | EPOLLET;
  860. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  861. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  862. pfd.fd = efd[0];
  863. pfd.events = POLLIN;
  864. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  865. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  866. pfd.fd = efd[0];
  867. pfd.events = POLLIN;
  868. EXPECT_EQ(poll(&pfd, 1, 0), 0);
  869. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
  870. close(efd[0]);
  871. close(efd[1]);
  872. close(sfd[0]);
  873. close(sfd[1]);
  874. }
  875. /*
  876. * t0
  877. * | (p)
  878. * e0
  879. * | (et)
  880. * e1
  881. * | (et)
  882. * s0
  883. */
  884. TEST(epoll24)
  885. {
  886. int efd[2];
  887. int sfd[2];
  888. struct pollfd pfd;
  889. struct epoll_event e;
  890. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
  891. efd[0] = epoll_create(1);
  892. ASSERT_GE(efd[0], 0);
  893. efd[1] = epoll_create(1);
  894. ASSERT_GE(efd[1], 0);
  895. e.events = EPOLLIN | EPOLLET;
  896. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e), 0);
  897. e.events = EPOLLIN | EPOLLET;
  898. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e), 0);
  899. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  900. pfd.fd = efd[0];
  901. pfd.events = POLLIN;
  902. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  903. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 1);
  904. pfd.fd = efd[0];
  905. pfd.events = POLLIN;
  906. EXPECT_EQ(poll(&pfd, 1, 0), 0);
  907. EXPECT_EQ(epoll_wait(efd[0], &e, 1, 0), 0);
  908. close(efd[0]);
  909. close(efd[1]);
  910. close(sfd[0]);
  911. close(sfd[1]);
  912. }
  913. /*
  914. * t0 t1
  915. * (ew) \ / (ew)
  916. * e0
  917. * | (lt)
  918. * e1
  919. * | (lt)
  920. * s0
  921. */
  922. TEST(epoll25)
  923. {
  924. pthread_t emitter;
  925. struct epoll_event e;
  926. struct epoll_mtcontext ctx = { 0 };
  927. signal(SIGUSR1, signal_handler);
  928. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  929. ctx.efd[0] = epoll_create(1);
  930. ASSERT_GE(ctx.efd[0], 0);
  931. ctx.efd[1] = epoll_create(1);
  932. ASSERT_GE(ctx.efd[1], 0);
  933. e.events = EPOLLIN;
  934. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  935. e.events = EPOLLIN;
  936. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  937. ctx.main = pthread_self();
  938. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  939. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  940. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  941. __sync_fetch_and_add(&ctx.count, 1);
  942. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  943. EXPECT_EQ(ctx.count, 2);
  944. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  945. pthread_kill(emitter, SIGUSR1);
  946. pthread_join(emitter, NULL);
  947. }
  948. close(ctx.efd[0]);
  949. close(ctx.efd[1]);
  950. close(ctx.sfd[0]);
  951. close(ctx.sfd[1]);
  952. }
  953. /*
  954. * t0 t1
  955. * (ew) \ / (ew)
  956. * e0
  957. * | (lt)
  958. * e1
  959. * | (et)
  960. * s0
  961. */
  962. TEST(epoll26)
  963. {
  964. pthread_t emitter;
  965. struct epoll_event e;
  966. struct epoll_mtcontext ctx = { 0 };
  967. signal(SIGUSR1, signal_handler);
  968. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  969. ctx.efd[0] = epoll_create(1);
  970. ASSERT_GE(ctx.efd[0], 0);
  971. ctx.efd[1] = epoll_create(1);
  972. ASSERT_GE(ctx.efd[1], 0);
  973. e.events = EPOLLIN | EPOLLET;
  974. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  975. e.events = EPOLLIN;
  976. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  977. ctx.main = pthread_self();
  978. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  979. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  980. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  981. __sync_fetch_and_add(&ctx.count, 1);
  982. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  983. EXPECT_EQ(ctx.count, 2);
  984. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  985. pthread_kill(emitter, SIGUSR1);
  986. pthread_join(emitter, NULL);
  987. }
  988. close(ctx.efd[0]);
  989. close(ctx.efd[1]);
  990. close(ctx.sfd[0]);
  991. close(ctx.sfd[1]);
  992. }
  993. /*
  994. * t0 t1
  995. * (ew) \ / (ew)
  996. * e0
  997. * | (et)
  998. * e1
  999. * | (lt)
  1000. * s0
  1001. */
  1002. TEST(epoll27)
  1003. {
  1004. pthread_t emitter;
  1005. struct epoll_event e;
  1006. struct epoll_mtcontext ctx = { 0 };
  1007. signal(SIGUSR1, signal_handler);
  1008. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1009. ctx.efd[0] = epoll_create(1);
  1010. ASSERT_GE(ctx.efd[0], 0);
  1011. ctx.efd[1] = epoll_create(1);
  1012. ASSERT_GE(ctx.efd[1], 0);
  1013. e.events = EPOLLIN;
  1014. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1015. e.events = EPOLLIN | EPOLLET;
  1016. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1017. ctx.main = pthread_self();
  1018. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  1019. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1020. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1021. __sync_fetch_and_add(&ctx.count, 1);
  1022. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1023. EXPECT_EQ(ctx.count, 1);
  1024. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1025. pthread_kill(emitter, SIGUSR1);
  1026. pthread_join(emitter, NULL);
  1027. }
  1028. close(ctx.efd[0]);
  1029. close(ctx.efd[1]);
  1030. close(ctx.sfd[0]);
  1031. close(ctx.sfd[1]);
  1032. }
  1033. /*
  1034. * t0 t1
  1035. * (ew) \ / (ew)
  1036. * e0
  1037. * | (et)
  1038. * e1
  1039. * | (et)
  1040. * s0
  1041. */
  1042. TEST(epoll28)
  1043. {
  1044. pthread_t emitter;
  1045. struct epoll_event e;
  1046. struct epoll_mtcontext ctx = { 0 };
  1047. signal(SIGUSR1, signal_handler);
  1048. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1049. ctx.efd[0] = epoll_create(1);
  1050. ASSERT_GE(ctx.efd[0], 0);
  1051. ctx.efd[1] = epoll_create(1);
  1052. ASSERT_GE(ctx.efd[1], 0);
  1053. e.events = EPOLLIN | EPOLLET;
  1054. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1055. e.events = EPOLLIN | EPOLLET;
  1056. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1057. ctx.main = pthread_self();
  1058. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  1059. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1060. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1061. __sync_fetch_and_add(&ctx.count, 1);
  1062. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1063. EXPECT_EQ(ctx.count, 1);
  1064. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1065. pthread_kill(emitter, SIGUSR1);
  1066. pthread_join(emitter, NULL);
  1067. }
  1068. close(ctx.efd[0]);
  1069. close(ctx.efd[1]);
  1070. close(ctx.sfd[0]);
  1071. close(ctx.sfd[1]);
  1072. }
  1073. /*
  1074. * t0 t1
  1075. * (ew) \ / (p)
  1076. * e0
  1077. * | (lt)
  1078. * e1
  1079. * | (lt)
  1080. * s0
  1081. */
  1082. TEST(epoll29)
  1083. {
  1084. pthread_t emitter;
  1085. struct epoll_event e;
  1086. struct epoll_mtcontext ctx = { 0 };
  1087. signal(SIGUSR1, signal_handler);
  1088. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1089. ctx.efd[0] = epoll_create(1);
  1090. ASSERT_GE(ctx.efd[0], 0);
  1091. ctx.efd[1] = epoll_create(1);
  1092. ASSERT_GE(ctx.efd[1], 0);
  1093. e.events = EPOLLIN;
  1094. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1095. e.events = EPOLLIN;
  1096. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1097. ctx.main = pthread_self();
  1098. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1099. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1100. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1101. __sync_fetch_and_add(&ctx.count, 1);
  1102. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1103. EXPECT_EQ(ctx.count, 2);
  1104. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1105. pthread_kill(emitter, SIGUSR1);
  1106. pthread_join(emitter, NULL);
  1107. }
  1108. close(ctx.efd[0]);
  1109. close(ctx.sfd[0]);
  1110. close(ctx.sfd[1]);
  1111. }
  1112. /*
  1113. * t0 t1
  1114. * (ew) \ / (p)
  1115. * e0
  1116. * | (lt)
  1117. * e1
  1118. * | (et)
  1119. * s0
  1120. */
  1121. TEST(epoll30)
  1122. {
  1123. pthread_t emitter;
  1124. struct epoll_event e;
  1125. struct epoll_mtcontext ctx = { 0 };
  1126. signal(SIGUSR1, signal_handler);
  1127. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1128. ctx.efd[0] = epoll_create(1);
  1129. ASSERT_GE(ctx.efd[0], 0);
  1130. ctx.efd[1] = epoll_create(1);
  1131. ASSERT_GE(ctx.efd[1], 0);
  1132. e.events = EPOLLIN | EPOLLET;
  1133. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1134. e.events = EPOLLIN;
  1135. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1136. ctx.main = pthread_self();
  1137. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1138. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1139. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1140. __sync_fetch_and_add(&ctx.count, 1);
  1141. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1142. EXPECT_EQ(ctx.count, 2);
  1143. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1144. pthread_kill(emitter, SIGUSR1);
  1145. pthread_join(emitter, NULL);
  1146. }
  1147. close(ctx.efd[0]);
  1148. close(ctx.sfd[0]);
  1149. close(ctx.sfd[1]);
  1150. }
  1151. /*
  1152. * t0 t1
  1153. * (ew) \ / (p)
  1154. * e0
  1155. * | (et)
  1156. * e1
  1157. * | (lt)
  1158. * s0
  1159. */
  1160. TEST(epoll31)
  1161. {
  1162. pthread_t emitter;
  1163. struct epoll_event e;
  1164. struct epoll_mtcontext ctx = { 0 };
  1165. signal(SIGUSR1, signal_handler);
  1166. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1167. ctx.efd[0] = epoll_create(1);
  1168. ASSERT_GE(ctx.efd[0], 0);
  1169. ctx.efd[1] = epoll_create(1);
  1170. ASSERT_GE(ctx.efd[1], 0);
  1171. e.events = EPOLLIN;
  1172. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1173. e.events = EPOLLIN | EPOLLET;
  1174. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1175. ctx.main = pthread_self();
  1176. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1177. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1178. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1179. __sync_fetch_and_add(&ctx.count, 1);
  1180. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1181. EXPECT_EQ(ctx.count, 1);
  1182. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1183. pthread_kill(emitter, SIGUSR1);
  1184. pthread_join(emitter, NULL);
  1185. }
  1186. close(ctx.efd[0]);
  1187. close(ctx.sfd[0]);
  1188. close(ctx.sfd[1]);
  1189. }
  1190. /*
  1191. * t0 t1
  1192. * (ew) \ / (p)
  1193. * e0
  1194. * | (et)
  1195. * e1
  1196. * | (et)
  1197. * s0
  1198. */
  1199. TEST(epoll32)
  1200. {
  1201. pthread_t emitter;
  1202. struct epoll_event e;
  1203. struct epoll_mtcontext ctx = { 0 };
  1204. signal(SIGUSR1, signal_handler);
  1205. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1206. ctx.efd[0] = epoll_create(1);
  1207. ASSERT_GE(ctx.efd[0], 0);
  1208. ctx.efd[1] = epoll_create(1);
  1209. ASSERT_GE(ctx.efd[1], 0);
  1210. e.events = EPOLLIN | EPOLLET;
  1211. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1212. e.events = EPOLLIN | EPOLLET;
  1213. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1214. ctx.main = pthread_self();
  1215. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1216. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1217. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  1218. __sync_fetch_and_add(&ctx.count, 1);
  1219. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1220. EXPECT_EQ(ctx.count, 1);
  1221. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1222. pthread_kill(emitter, SIGUSR1);
  1223. pthread_join(emitter, NULL);
  1224. }
  1225. close(ctx.efd[0]);
  1226. close(ctx.sfd[0]);
  1227. close(ctx.sfd[1]);
  1228. }
  1229. /*
  1230. * t0 t1
  1231. * (ew) | | (ew)
  1232. * | e0
  1233. * \ / (lt)
  1234. * e1
  1235. * | (lt)
  1236. * s0
  1237. */
  1238. TEST(epoll33)
  1239. {
  1240. pthread_t emitter;
  1241. struct epoll_event e;
  1242. struct epoll_mtcontext ctx = { 0 };
  1243. signal(SIGUSR1, signal_handler);
  1244. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1245. ctx.efd[0] = epoll_create(1);
  1246. ASSERT_GE(ctx.efd[0], 0);
  1247. ctx.efd[1] = epoll_create(1);
  1248. ASSERT_GE(ctx.efd[1], 0);
  1249. e.events = EPOLLIN;
  1250. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1251. e.events = EPOLLIN;
  1252. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  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. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1257. __sync_fetch_and_add(&ctx.count, 1);
  1258. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1259. EXPECT_EQ(ctx.count, 2);
  1260. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1261. pthread_kill(emitter, SIGUSR1);
  1262. pthread_join(emitter, NULL);
  1263. }
  1264. close(ctx.efd[0]);
  1265. close(ctx.efd[1]);
  1266. close(ctx.sfd[0]);
  1267. close(ctx.sfd[1]);
  1268. }
  1269. /*
  1270. * t0 t1
  1271. * (ew) | | (ew)
  1272. * | e0
  1273. * \ / (lt)
  1274. * e1
  1275. * | (et)
  1276. * s0
  1277. */
  1278. TEST(epoll34)
  1279. {
  1280. pthread_t emitter;
  1281. struct epoll_event e;
  1282. struct epoll_mtcontext ctx = { 0 };
  1283. signal(SIGUSR1, signal_handler);
  1284. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1285. ctx.efd[0] = epoll_create(1);
  1286. ASSERT_GE(ctx.efd[0], 0);
  1287. ctx.efd[1] = epoll_create(1);
  1288. ASSERT_GE(ctx.efd[1], 0);
  1289. e.events = EPOLLIN | EPOLLET;
  1290. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1291. e.events = EPOLLIN;
  1292. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1293. ctx.main = pthread_self();
  1294. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
  1295. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1296. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1297. __sync_fetch_and_or(&ctx.count, 2);
  1298. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1299. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1300. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1301. pthread_kill(emitter, SIGUSR1);
  1302. pthread_join(emitter, NULL);
  1303. }
  1304. close(ctx.efd[0]);
  1305. close(ctx.efd[1]);
  1306. close(ctx.sfd[0]);
  1307. close(ctx.sfd[1]);
  1308. }
  1309. /*
  1310. * t0 t1
  1311. * (ew) | | (ew)
  1312. * | e0
  1313. * \ / (et)
  1314. * e1
  1315. * | (lt)
  1316. * s0
  1317. */
  1318. TEST(epoll35)
  1319. {
  1320. pthread_t emitter;
  1321. struct epoll_event e;
  1322. struct epoll_mtcontext ctx = { 0 };
  1323. signal(SIGUSR1, signal_handler);
  1324. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1325. ctx.efd[0] = epoll_create(1);
  1326. ASSERT_GE(ctx.efd[0], 0);
  1327. ctx.efd[1] = epoll_create(1);
  1328. ASSERT_GE(ctx.efd[1], 0);
  1329. e.events = EPOLLIN;
  1330. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1331. e.events = EPOLLIN | EPOLLET;
  1332. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1333. ctx.main = pthread_self();
  1334. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  1335. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1336. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1337. __sync_fetch_and_add(&ctx.count, 1);
  1338. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1339. EXPECT_EQ(ctx.count, 2);
  1340. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1341. pthread_kill(emitter, SIGUSR1);
  1342. pthread_join(emitter, NULL);
  1343. }
  1344. close(ctx.efd[0]);
  1345. close(ctx.efd[1]);
  1346. close(ctx.sfd[0]);
  1347. close(ctx.sfd[1]);
  1348. }
  1349. /*
  1350. * t0 t1
  1351. * (ew) | | (ew)
  1352. * | e0
  1353. * \ / (et)
  1354. * e1
  1355. * | (et)
  1356. * s0
  1357. */
  1358. TEST(epoll36)
  1359. {
  1360. pthread_t emitter;
  1361. struct epoll_event e;
  1362. struct epoll_mtcontext ctx = { 0 };
  1363. signal(SIGUSR1, signal_handler);
  1364. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1365. ctx.efd[0] = epoll_create(1);
  1366. ASSERT_GE(ctx.efd[0], 0);
  1367. ctx.efd[1] = epoll_create(1);
  1368. ASSERT_GE(ctx.efd[1], 0);
  1369. e.events = EPOLLIN | EPOLLET;
  1370. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1371. e.events = EPOLLIN | EPOLLET;
  1372. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1373. ctx.main = pthread_self();
  1374. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
  1375. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1376. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1377. __sync_fetch_and_or(&ctx.count, 2);
  1378. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1379. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1380. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1381. pthread_kill(emitter, SIGUSR1);
  1382. pthread_join(emitter, NULL);
  1383. }
  1384. close(ctx.efd[0]);
  1385. close(ctx.efd[1]);
  1386. close(ctx.sfd[0]);
  1387. close(ctx.sfd[1]);
  1388. }
  1389. /*
  1390. * t0 t1
  1391. * (p) | | (ew)
  1392. * | e0
  1393. * \ / (lt)
  1394. * e1
  1395. * | (lt)
  1396. * s0
  1397. */
  1398. TEST(epoll37)
  1399. {
  1400. pthread_t emitter;
  1401. struct pollfd pfd;
  1402. struct epoll_event e;
  1403. struct epoll_mtcontext ctx = { 0 };
  1404. signal(SIGUSR1, signal_handler);
  1405. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1406. ctx.efd[0] = epoll_create(1);
  1407. ASSERT_GE(ctx.efd[0], 0);
  1408. ctx.efd[1] = epoll_create(1);
  1409. ASSERT_GE(ctx.efd[1], 0);
  1410. e.events = EPOLLIN;
  1411. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1412. e.events = EPOLLIN;
  1413. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1414. ctx.main = pthread_self();
  1415. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  1416. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1417. pfd.fd = ctx.efd[1];
  1418. pfd.events = POLLIN;
  1419. if (poll(&pfd, 1, -1) > 0) {
  1420. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1421. __sync_fetch_and_add(&ctx.count, 1);
  1422. }
  1423. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1424. EXPECT_EQ(ctx.count, 2);
  1425. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1426. pthread_kill(emitter, SIGUSR1);
  1427. pthread_join(emitter, NULL);
  1428. }
  1429. close(ctx.efd[0]);
  1430. close(ctx.efd[1]);
  1431. close(ctx.sfd[0]);
  1432. close(ctx.sfd[1]);
  1433. }
  1434. /*
  1435. * t0 t1
  1436. * (p) | | (ew)
  1437. * | e0
  1438. * \ / (lt)
  1439. * e1
  1440. * | (et)
  1441. * s0
  1442. */
  1443. TEST(epoll38)
  1444. {
  1445. pthread_t emitter;
  1446. struct pollfd pfd;
  1447. struct epoll_event e;
  1448. struct epoll_mtcontext ctx = { 0 };
  1449. signal(SIGUSR1, signal_handler);
  1450. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1451. ctx.efd[0] = epoll_create(1);
  1452. ASSERT_GE(ctx.efd[0], 0);
  1453. ctx.efd[1] = epoll_create(1);
  1454. ASSERT_GE(ctx.efd[1], 0);
  1455. e.events = EPOLLIN | EPOLLET;
  1456. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1457. e.events = EPOLLIN;
  1458. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1459. ctx.main = pthread_self();
  1460. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
  1461. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1462. pfd.fd = ctx.efd[1];
  1463. pfd.events = POLLIN;
  1464. if (poll(&pfd, 1, -1) > 0) {
  1465. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1466. __sync_fetch_and_or(&ctx.count, 2);
  1467. }
  1468. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1469. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1470. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1471. pthread_kill(emitter, SIGUSR1);
  1472. pthread_join(emitter, NULL);
  1473. }
  1474. close(ctx.efd[0]);
  1475. close(ctx.efd[1]);
  1476. close(ctx.sfd[0]);
  1477. close(ctx.sfd[1]);
  1478. }
  1479. /*
  1480. * t0 t1
  1481. * (p) | | (ew)
  1482. * | e0
  1483. * \ / (et)
  1484. * e1
  1485. * | (lt)
  1486. * s0
  1487. */
  1488. TEST(epoll39)
  1489. {
  1490. pthread_t emitter;
  1491. struct pollfd pfd;
  1492. struct epoll_event e;
  1493. struct epoll_mtcontext ctx = { 0 };
  1494. signal(SIGUSR1, signal_handler);
  1495. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1496. ctx.efd[0] = epoll_create(1);
  1497. ASSERT_GE(ctx.efd[0], 0);
  1498. ctx.efd[1] = epoll_create(1);
  1499. ASSERT_GE(ctx.efd[1], 0);
  1500. e.events = EPOLLIN;
  1501. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1502. e.events = EPOLLIN | EPOLLET;
  1503. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1504. ctx.main = pthread_self();
  1505. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  1506. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1507. pfd.fd = ctx.efd[1];
  1508. pfd.events = POLLIN;
  1509. if (poll(&pfd, 1, -1) > 0) {
  1510. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1511. __sync_fetch_and_add(&ctx.count, 1);
  1512. }
  1513. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1514. EXPECT_EQ(ctx.count, 2);
  1515. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1516. pthread_kill(emitter, SIGUSR1);
  1517. pthread_join(emitter, NULL);
  1518. }
  1519. close(ctx.efd[0]);
  1520. close(ctx.efd[1]);
  1521. close(ctx.sfd[0]);
  1522. close(ctx.sfd[1]);
  1523. }
  1524. /*
  1525. * t0 t1
  1526. * (p) | | (ew)
  1527. * | e0
  1528. * \ / (et)
  1529. * e1
  1530. * | (et)
  1531. * s0
  1532. */
  1533. TEST(epoll40)
  1534. {
  1535. pthread_t emitter;
  1536. struct pollfd pfd;
  1537. struct epoll_event e;
  1538. struct epoll_mtcontext ctx = { 0 };
  1539. signal(SIGUSR1, signal_handler);
  1540. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1541. ctx.efd[0] = epoll_create(1);
  1542. ASSERT_GE(ctx.efd[0], 0);
  1543. ctx.efd[1] = epoll_create(1);
  1544. ASSERT_GE(ctx.efd[1], 0);
  1545. e.events = EPOLLIN | EPOLLET;
  1546. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1547. e.events = EPOLLIN | EPOLLET;
  1548. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1549. ctx.main = pthread_self();
  1550. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1o, &ctx), 0);
  1551. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1552. pfd.fd = ctx.efd[1];
  1553. pfd.events = POLLIN;
  1554. if (poll(&pfd, 1, -1) > 0) {
  1555. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1556. __sync_fetch_and_or(&ctx.count, 2);
  1557. }
  1558. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1559. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1560. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1561. pthread_kill(emitter, SIGUSR1);
  1562. pthread_join(emitter, NULL);
  1563. }
  1564. close(ctx.efd[0]);
  1565. close(ctx.efd[1]);
  1566. close(ctx.sfd[0]);
  1567. close(ctx.sfd[1]);
  1568. }
  1569. /*
  1570. * t0 t1
  1571. * (ew) | | (p)
  1572. * | e0
  1573. * \ / (lt)
  1574. * e1
  1575. * | (lt)
  1576. * s0
  1577. */
  1578. TEST(epoll41)
  1579. {
  1580. pthread_t emitter;
  1581. struct epoll_event e;
  1582. struct epoll_mtcontext ctx = { 0 };
  1583. signal(SIGUSR1, signal_handler);
  1584. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1585. ctx.efd[0] = epoll_create(1);
  1586. ASSERT_GE(ctx.efd[0], 0);
  1587. ctx.efd[1] = epoll_create(1);
  1588. ASSERT_GE(ctx.efd[1], 0);
  1589. e.events = EPOLLIN;
  1590. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1591. e.events = EPOLLIN;
  1592. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1593. ctx.main = pthread_self();
  1594. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1595. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1596. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1597. __sync_fetch_and_add(&ctx.count, 1);
  1598. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1599. EXPECT_EQ(ctx.count, 2);
  1600. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1601. pthread_kill(emitter, SIGUSR1);
  1602. pthread_join(emitter, NULL);
  1603. }
  1604. close(ctx.efd[0]);
  1605. close(ctx.efd[1]);
  1606. close(ctx.sfd[0]);
  1607. close(ctx.sfd[1]);
  1608. }
  1609. /*
  1610. * t0 t1
  1611. * (ew) | | (p)
  1612. * | e0
  1613. * \ / (lt)
  1614. * e1
  1615. * | (et)
  1616. * s0
  1617. */
  1618. TEST(epoll42)
  1619. {
  1620. pthread_t emitter;
  1621. struct epoll_event e;
  1622. struct epoll_mtcontext ctx = { 0 };
  1623. signal(SIGUSR1, signal_handler);
  1624. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1625. ctx.efd[0] = epoll_create(1);
  1626. ASSERT_GE(ctx.efd[0], 0);
  1627. ctx.efd[1] = epoll_create(1);
  1628. ASSERT_GE(ctx.efd[1], 0);
  1629. e.events = EPOLLIN | EPOLLET;
  1630. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1631. e.events = EPOLLIN;
  1632. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1633. ctx.main = pthread_self();
  1634. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
  1635. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1636. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1637. __sync_fetch_and_or(&ctx.count, 2);
  1638. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1639. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1640. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1641. pthread_kill(emitter, SIGUSR1);
  1642. pthread_join(emitter, NULL);
  1643. }
  1644. close(ctx.efd[0]);
  1645. close(ctx.efd[1]);
  1646. close(ctx.sfd[0]);
  1647. close(ctx.sfd[1]);
  1648. }
  1649. /*
  1650. * t0 t1
  1651. * (ew) | | (p)
  1652. * | e0
  1653. * \ / (et)
  1654. * e1
  1655. * | (lt)
  1656. * s0
  1657. */
  1658. TEST(epoll43)
  1659. {
  1660. pthread_t emitter;
  1661. struct epoll_event e;
  1662. struct epoll_mtcontext ctx = { 0 };
  1663. signal(SIGUSR1, signal_handler);
  1664. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1665. ctx.efd[0] = epoll_create(1);
  1666. ASSERT_GE(ctx.efd[0], 0);
  1667. ctx.efd[1] = epoll_create(1);
  1668. ASSERT_GE(ctx.efd[1], 0);
  1669. e.events = EPOLLIN;
  1670. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1671. e.events = EPOLLIN | EPOLLET;
  1672. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1673. ctx.main = pthread_self();
  1674. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1675. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1676. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1677. __sync_fetch_and_add(&ctx.count, 1);
  1678. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1679. EXPECT_EQ(ctx.count, 2);
  1680. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1681. pthread_kill(emitter, SIGUSR1);
  1682. pthread_join(emitter, NULL);
  1683. }
  1684. close(ctx.efd[0]);
  1685. close(ctx.efd[1]);
  1686. close(ctx.sfd[0]);
  1687. close(ctx.sfd[1]);
  1688. }
  1689. /*
  1690. * t0 t1
  1691. * (ew) | | (p)
  1692. * | e0
  1693. * \ / (et)
  1694. * e1
  1695. * | (et)
  1696. * s0
  1697. */
  1698. TEST(epoll44)
  1699. {
  1700. pthread_t emitter;
  1701. struct epoll_event e;
  1702. struct epoll_mtcontext ctx = { 0 };
  1703. signal(SIGUSR1, signal_handler);
  1704. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1705. ctx.efd[0] = epoll_create(1);
  1706. ASSERT_GE(ctx.efd[0], 0);
  1707. ctx.efd[1] = epoll_create(1);
  1708. ASSERT_GE(ctx.efd[1], 0);
  1709. e.events = EPOLLIN | EPOLLET;
  1710. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1711. e.events = EPOLLIN | EPOLLET;
  1712. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1713. ctx.main = pthread_self();
  1714. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
  1715. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1716. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1717. __sync_fetch_and_or(&ctx.count, 2);
  1718. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1719. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1720. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1721. pthread_kill(emitter, SIGUSR1);
  1722. pthread_join(emitter, NULL);
  1723. }
  1724. close(ctx.efd[0]);
  1725. close(ctx.efd[1]);
  1726. close(ctx.sfd[0]);
  1727. close(ctx.sfd[1]);
  1728. }
  1729. /*
  1730. * t0 t1
  1731. * (p) | | (p)
  1732. * | e0
  1733. * \ / (lt)
  1734. * e1
  1735. * | (lt)
  1736. * s0
  1737. */
  1738. TEST(epoll45)
  1739. {
  1740. pthread_t emitter;
  1741. struct pollfd pfd;
  1742. struct epoll_event e;
  1743. struct epoll_mtcontext ctx = { 0 };
  1744. signal(SIGUSR1, signal_handler);
  1745. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1746. ctx.efd[0] = epoll_create(1);
  1747. ASSERT_GE(ctx.efd[0], 0);
  1748. ctx.efd[1] = epoll_create(1);
  1749. ASSERT_GE(ctx.efd[1], 0);
  1750. e.events = EPOLLIN;
  1751. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1752. e.events = EPOLLIN;
  1753. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1754. ctx.main = pthread_self();
  1755. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1756. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1757. pfd.fd = ctx.efd[1];
  1758. pfd.events = POLLIN;
  1759. if (poll(&pfd, 1, -1) > 0) {
  1760. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1761. __sync_fetch_and_add(&ctx.count, 1);
  1762. }
  1763. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1764. EXPECT_EQ(ctx.count, 2);
  1765. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1766. pthread_kill(emitter, SIGUSR1);
  1767. pthread_join(emitter, NULL);
  1768. }
  1769. close(ctx.efd[0]);
  1770. close(ctx.efd[1]);
  1771. close(ctx.sfd[0]);
  1772. close(ctx.sfd[1]);
  1773. }
  1774. /*
  1775. * t0 t1
  1776. * (p) | | (p)
  1777. * | e0
  1778. * \ / (lt)
  1779. * e1
  1780. * | (et)
  1781. * s0
  1782. */
  1783. TEST(epoll46)
  1784. {
  1785. pthread_t emitter;
  1786. struct epoll_event e;
  1787. struct epoll_mtcontext ctx = { 0 };
  1788. signal(SIGUSR1, signal_handler);
  1789. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1790. ctx.efd[0] = epoll_create(1);
  1791. ASSERT_GE(ctx.efd[0], 0);
  1792. ctx.efd[1] = epoll_create(1);
  1793. ASSERT_GE(ctx.efd[1], 0);
  1794. e.events = EPOLLIN | EPOLLET;
  1795. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1796. e.events = EPOLLIN;
  1797. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1798. ctx.main = pthread_self();
  1799. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
  1800. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1801. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1802. __sync_fetch_and_or(&ctx.count, 2);
  1803. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1804. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1805. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1806. pthread_kill(emitter, SIGUSR1);
  1807. pthread_join(emitter, NULL);
  1808. }
  1809. close(ctx.efd[0]);
  1810. close(ctx.efd[1]);
  1811. close(ctx.sfd[0]);
  1812. close(ctx.sfd[1]);
  1813. }
  1814. /*
  1815. * t0 t1
  1816. * (p) | | (p)
  1817. * | e0
  1818. * \ / (et)
  1819. * e1
  1820. * | (lt)
  1821. * s0
  1822. */
  1823. TEST(epoll47)
  1824. {
  1825. pthread_t emitter;
  1826. struct pollfd pfd;
  1827. struct epoll_event e;
  1828. struct epoll_mtcontext ctx = { 0 };
  1829. signal(SIGUSR1, signal_handler);
  1830. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1831. ctx.efd[0] = epoll_create(1);
  1832. ASSERT_GE(ctx.efd[0], 0);
  1833. ctx.efd[1] = epoll_create(1);
  1834. ASSERT_GE(ctx.efd[1], 0);
  1835. e.events = EPOLLIN;
  1836. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1837. e.events = EPOLLIN | EPOLLET;
  1838. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1839. ctx.main = pthread_self();
  1840. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  1841. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1842. pfd.fd = ctx.efd[1];
  1843. pfd.events = POLLIN;
  1844. if (poll(&pfd, 1, -1) > 0) {
  1845. if (epoll_wait(ctx.efd[1], &e, 1, 0) > 0)
  1846. __sync_fetch_and_add(&ctx.count, 1);
  1847. }
  1848. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1849. EXPECT_EQ(ctx.count, 2);
  1850. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1851. pthread_kill(emitter, SIGUSR1);
  1852. pthread_join(emitter, NULL);
  1853. }
  1854. close(ctx.efd[0]);
  1855. close(ctx.efd[1]);
  1856. close(ctx.sfd[0]);
  1857. close(ctx.sfd[1]);
  1858. }
  1859. /*
  1860. * t0 t1
  1861. * (p) | | (p)
  1862. * | e0
  1863. * \ / (et)
  1864. * e1
  1865. * | (et)
  1866. * s0
  1867. */
  1868. TEST(epoll48)
  1869. {
  1870. pthread_t emitter;
  1871. struct epoll_event e;
  1872. struct epoll_mtcontext ctx = { 0 };
  1873. signal(SIGUSR1, signal_handler);
  1874. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx.sfd), 0);
  1875. ctx.efd[0] = epoll_create(1);
  1876. ASSERT_GE(ctx.efd[0], 0);
  1877. ctx.efd[1] = epoll_create(1);
  1878. ASSERT_GE(ctx.efd[1], 0);
  1879. e.events = EPOLLIN | EPOLLET;
  1880. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  1881. e.events = EPOLLIN | EPOLLET;
  1882. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  1883. ctx.main = pthread_self();
  1884. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1op, &ctx), 0);
  1885. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry1, &ctx), 0);
  1886. if (epoll_wait(ctx.efd[1], &e, 1, -1) > 0)
  1887. __sync_fetch_and_or(&ctx.count, 2);
  1888. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  1889. EXPECT_TRUE((ctx.count == 2) || (ctx.count == 3));
  1890. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  1891. pthread_kill(emitter, SIGUSR1);
  1892. pthread_join(emitter, NULL);
  1893. }
  1894. close(ctx.efd[0]);
  1895. close(ctx.efd[1]);
  1896. close(ctx.sfd[0]);
  1897. close(ctx.sfd[1]);
  1898. }
  1899. /*
  1900. * t0
  1901. * | (ew)
  1902. * e0
  1903. * (lt) / \ (lt)
  1904. * e1 e2
  1905. * (lt) | | (lt)
  1906. * s0 s2
  1907. */
  1908. TEST(epoll49)
  1909. {
  1910. int efd[3];
  1911. int sfd[4];
  1912. struct epoll_event events[2];
  1913. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  1914. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  1915. efd[0] = epoll_create(1);
  1916. ASSERT_GE(efd[0], 0);
  1917. efd[1] = epoll_create(1);
  1918. ASSERT_GE(efd[1], 0);
  1919. efd[2] = epoll_create(1);
  1920. ASSERT_GE(efd[2], 0);
  1921. events[0].events = EPOLLIN;
  1922. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
  1923. events[0].events = EPOLLIN;
  1924. ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
  1925. events[0].events = EPOLLIN;
  1926. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
  1927. events[0].events = EPOLLIN;
  1928. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
  1929. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  1930. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  1931. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  1932. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  1933. close(efd[0]);
  1934. close(efd[1]);
  1935. close(efd[2]);
  1936. close(sfd[0]);
  1937. close(sfd[1]);
  1938. close(sfd[2]);
  1939. close(sfd[3]);
  1940. }
  1941. /*
  1942. * t0
  1943. * | (ew)
  1944. * e0
  1945. * (et) / \ (et)
  1946. * e1 e2
  1947. * (lt) | | (lt)
  1948. * s0 s2
  1949. */
  1950. TEST(epoll50)
  1951. {
  1952. int efd[3];
  1953. int sfd[4];
  1954. struct epoll_event events[2];
  1955. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  1956. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  1957. efd[0] = epoll_create(1);
  1958. ASSERT_GE(efd[0], 0);
  1959. efd[1] = epoll_create(1);
  1960. ASSERT_GE(efd[1], 0);
  1961. efd[2] = epoll_create(1);
  1962. ASSERT_GE(efd[2], 0);
  1963. events[0].events = EPOLLIN;
  1964. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
  1965. events[0].events = EPOLLIN;
  1966. ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
  1967. events[0].events = EPOLLIN | EPOLLET;
  1968. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
  1969. events[0].events = EPOLLIN | EPOLLET;
  1970. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
  1971. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  1972. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  1973. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  1974. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
  1975. close(efd[0]);
  1976. close(efd[1]);
  1977. close(efd[2]);
  1978. close(sfd[0]);
  1979. close(sfd[1]);
  1980. close(sfd[2]);
  1981. close(sfd[3]);
  1982. }
  1983. /*
  1984. * t0
  1985. * | (p)
  1986. * e0
  1987. * (lt) / \ (lt)
  1988. * e1 e2
  1989. * (lt) | | (lt)
  1990. * s0 s2
  1991. */
  1992. TEST(epoll51)
  1993. {
  1994. int efd[3];
  1995. int sfd[4];
  1996. struct pollfd pfd;
  1997. struct epoll_event events[2];
  1998. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  1999. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  2000. efd[0] = epoll_create(1);
  2001. ASSERT_GE(efd[0], 0);
  2002. efd[1] = epoll_create(1);
  2003. ASSERT_GE(efd[1], 0);
  2004. efd[2] = epoll_create(1);
  2005. ASSERT_GE(efd[2], 0);
  2006. events[0].events = EPOLLIN;
  2007. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
  2008. events[0].events = EPOLLIN;
  2009. ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
  2010. events[0].events = EPOLLIN;
  2011. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
  2012. events[0].events = EPOLLIN;
  2013. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
  2014. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  2015. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  2016. pfd.fd = efd[0];
  2017. pfd.events = POLLIN;
  2018. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  2019. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  2020. pfd.fd = efd[0];
  2021. pfd.events = POLLIN;
  2022. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  2023. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  2024. close(efd[0]);
  2025. close(efd[1]);
  2026. close(efd[2]);
  2027. close(sfd[0]);
  2028. close(sfd[1]);
  2029. close(sfd[2]);
  2030. close(sfd[3]);
  2031. }
  2032. /*
  2033. * t0
  2034. * | (p)
  2035. * e0
  2036. * (et) / \ (et)
  2037. * e1 e2
  2038. * (lt) | | (lt)
  2039. * s0 s2
  2040. */
  2041. TEST(epoll52)
  2042. {
  2043. int efd[3];
  2044. int sfd[4];
  2045. struct pollfd pfd;
  2046. struct epoll_event events[2];
  2047. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[0]), 0);
  2048. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &sfd[2]), 0);
  2049. efd[0] = epoll_create(1);
  2050. ASSERT_GE(efd[0], 0);
  2051. efd[1] = epoll_create(1);
  2052. ASSERT_GE(efd[1], 0);
  2053. efd[2] = epoll_create(1);
  2054. ASSERT_GE(efd[2], 0);
  2055. events[0].events = EPOLLIN;
  2056. ASSERT_EQ(epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], events), 0);
  2057. events[0].events = EPOLLIN;
  2058. ASSERT_EQ(epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[2], events), 0);
  2059. events[0].events = EPOLLIN | EPOLLET;
  2060. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], events), 0);
  2061. events[0].events = EPOLLIN | EPOLLET;
  2062. ASSERT_EQ(epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], events), 0);
  2063. ASSERT_EQ(write(sfd[1], "w", 1), 1);
  2064. ASSERT_EQ(write(sfd[3], "w", 1), 1);
  2065. pfd.fd = efd[0];
  2066. pfd.events = POLLIN;
  2067. EXPECT_EQ(poll(&pfd, 1, 0), 1);
  2068. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 2);
  2069. pfd.fd = efd[0];
  2070. pfd.events = POLLIN;
  2071. EXPECT_EQ(poll(&pfd, 1, 0), 0);
  2072. EXPECT_EQ(epoll_wait(efd[0], events, 2, 0), 0);
  2073. close(efd[0]);
  2074. close(efd[1]);
  2075. close(efd[2]);
  2076. close(sfd[0]);
  2077. close(sfd[1]);
  2078. close(sfd[2]);
  2079. close(sfd[3]);
  2080. }
  2081. /*
  2082. * t0 t1
  2083. * (ew) \ / (ew)
  2084. * e0
  2085. * (lt) / \ (lt)
  2086. * e1 e2
  2087. * (lt) | | (lt)
  2088. * s0 s2
  2089. */
  2090. TEST(epoll53)
  2091. {
  2092. pthread_t emitter;
  2093. struct epoll_event e;
  2094. struct epoll_mtcontext ctx = { 0 };
  2095. signal(SIGUSR1, signal_handler);
  2096. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2097. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2098. ctx.efd[0] = epoll_create(1);
  2099. ASSERT_GE(ctx.efd[0], 0);
  2100. ctx.efd[1] = epoll_create(1);
  2101. ASSERT_GE(ctx.efd[1], 0);
  2102. ctx.efd[2] = epoll_create(1);
  2103. ASSERT_GE(ctx.efd[2], 0);
  2104. e.events = EPOLLIN;
  2105. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2106. e.events = EPOLLIN;
  2107. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2108. e.events = EPOLLIN;
  2109. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2110. e.events = EPOLLIN;
  2111. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2112. ctx.main = pthread_self();
  2113. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  2114. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2115. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  2116. __sync_fetch_and_add(&ctx.count, 1);
  2117. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2118. EXPECT_EQ(ctx.count, 2);
  2119. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2120. pthread_kill(emitter, SIGUSR1);
  2121. pthread_join(emitter, NULL);
  2122. }
  2123. close(ctx.efd[0]);
  2124. close(ctx.efd[1]);
  2125. close(ctx.efd[2]);
  2126. close(ctx.sfd[0]);
  2127. close(ctx.sfd[1]);
  2128. close(ctx.sfd[2]);
  2129. close(ctx.sfd[3]);
  2130. }
  2131. /*
  2132. * t0 t1
  2133. * (ew) \ / (ew)
  2134. * e0
  2135. * (et) / \ (et)
  2136. * e1 e2
  2137. * (lt) | | (lt)
  2138. * s0 s2
  2139. */
  2140. TEST(epoll54)
  2141. {
  2142. pthread_t emitter;
  2143. struct epoll_event e;
  2144. struct epoll_mtcontext ctx = { 0 };
  2145. signal(SIGUSR1, signal_handler);
  2146. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2147. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2148. ctx.efd[0] = epoll_create(1);
  2149. ASSERT_GE(ctx.efd[0], 0);
  2150. ctx.efd[1] = epoll_create(1);
  2151. ASSERT_GE(ctx.efd[1], 0);
  2152. ctx.efd[2] = epoll_create(1);
  2153. ASSERT_GE(ctx.efd[2], 0);
  2154. e.events = EPOLLIN;
  2155. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2156. e.events = EPOLLIN;
  2157. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2158. e.events = EPOLLIN | EPOLLET;
  2159. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2160. e.events = EPOLLIN | EPOLLET;
  2161. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2162. ctx.main = pthread_self();
  2163. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1a, &ctx), 0);
  2164. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2165. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  2166. __sync_fetch_and_add(&ctx.count, 1);
  2167. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2168. EXPECT_EQ(ctx.count, 2);
  2169. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2170. pthread_kill(emitter, SIGUSR1);
  2171. pthread_join(emitter, NULL);
  2172. }
  2173. close(ctx.efd[0]);
  2174. close(ctx.efd[1]);
  2175. close(ctx.efd[2]);
  2176. close(ctx.sfd[0]);
  2177. close(ctx.sfd[1]);
  2178. close(ctx.sfd[2]);
  2179. close(ctx.sfd[3]);
  2180. }
  2181. /*
  2182. * t0 t1
  2183. * (ew) \ / (p)
  2184. * e0
  2185. * (lt) / \ (lt)
  2186. * e1 e2
  2187. * (lt) | | (lt)
  2188. * s0 s2
  2189. */
  2190. TEST(epoll55)
  2191. {
  2192. pthread_t emitter;
  2193. struct epoll_event e;
  2194. struct epoll_mtcontext ctx = { 0 };
  2195. signal(SIGUSR1, signal_handler);
  2196. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2197. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2198. ctx.efd[0] = epoll_create(1);
  2199. ASSERT_GE(ctx.efd[0], 0);
  2200. ctx.efd[1] = epoll_create(1);
  2201. ASSERT_GE(ctx.efd[1], 0);
  2202. ctx.efd[2] = epoll_create(1);
  2203. ASSERT_GE(ctx.efd[2], 0);
  2204. e.events = EPOLLIN;
  2205. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2206. e.events = EPOLLIN;
  2207. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2208. e.events = EPOLLIN;
  2209. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2210. e.events = EPOLLIN;
  2211. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2212. ctx.main = pthread_self();
  2213. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  2214. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2215. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  2216. __sync_fetch_and_add(&ctx.count, 1);
  2217. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2218. EXPECT_EQ(ctx.count, 2);
  2219. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2220. pthread_kill(emitter, SIGUSR1);
  2221. pthread_join(emitter, NULL);
  2222. }
  2223. close(ctx.efd[0]);
  2224. close(ctx.efd[1]);
  2225. close(ctx.efd[2]);
  2226. close(ctx.sfd[0]);
  2227. close(ctx.sfd[1]);
  2228. close(ctx.sfd[2]);
  2229. close(ctx.sfd[3]);
  2230. }
  2231. /*
  2232. * t0 t1
  2233. * (ew) \ / (p)
  2234. * e0
  2235. * (et) / \ (et)
  2236. * e1 e2
  2237. * (lt) | | (lt)
  2238. * s0 s2
  2239. */
  2240. TEST(epoll56)
  2241. {
  2242. pthread_t emitter;
  2243. struct epoll_event e;
  2244. struct epoll_mtcontext ctx = { 0 };
  2245. signal(SIGUSR1, signal_handler);
  2246. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2247. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2248. ctx.efd[0] = epoll_create(1);
  2249. ASSERT_GE(ctx.efd[0], 0);
  2250. ctx.efd[1] = epoll_create(1);
  2251. ASSERT_GE(ctx.efd[1], 0);
  2252. ctx.efd[2] = epoll_create(1);
  2253. ASSERT_GE(ctx.efd[2], 0);
  2254. e.events = EPOLLIN;
  2255. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2256. e.events = EPOLLIN;
  2257. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2258. e.events = EPOLLIN | EPOLLET;
  2259. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2260. e.events = EPOLLIN | EPOLLET;
  2261. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2262. ctx.main = pthread_self();
  2263. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  2264. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2265. if (epoll_wait(ctx.efd[0], &e, 1, -1) > 0)
  2266. __sync_fetch_and_add(&ctx.count, 1);
  2267. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2268. EXPECT_EQ(ctx.count, 2);
  2269. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2270. pthread_kill(emitter, SIGUSR1);
  2271. pthread_join(emitter, NULL);
  2272. }
  2273. close(ctx.efd[0]);
  2274. close(ctx.efd[1]);
  2275. close(ctx.efd[2]);
  2276. close(ctx.sfd[0]);
  2277. close(ctx.sfd[1]);
  2278. close(ctx.sfd[2]);
  2279. close(ctx.sfd[3]);
  2280. }
  2281. /*
  2282. * t0 t1
  2283. * (p) \ / (p)
  2284. * e0
  2285. * (lt) / \ (lt)
  2286. * e1 e2
  2287. * (lt) | | (lt)
  2288. * s0 s2
  2289. */
  2290. TEST(epoll57)
  2291. {
  2292. pthread_t emitter;
  2293. struct pollfd pfd;
  2294. struct epoll_event e;
  2295. struct epoll_mtcontext ctx = { 0 };
  2296. signal(SIGUSR1, signal_handler);
  2297. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2298. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2299. ctx.efd[0] = epoll_create(1);
  2300. ASSERT_GE(ctx.efd[0], 0);
  2301. ctx.efd[1] = epoll_create(1);
  2302. ASSERT_GE(ctx.efd[1], 0);
  2303. ctx.efd[2] = epoll_create(1);
  2304. ASSERT_GE(ctx.efd[2], 0);
  2305. e.events = EPOLLIN;
  2306. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2307. e.events = EPOLLIN;
  2308. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2309. e.events = EPOLLIN;
  2310. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2311. e.events = EPOLLIN;
  2312. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2313. ctx.main = pthread_self();
  2314. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  2315. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2316. pfd.fd = ctx.efd[0];
  2317. pfd.events = POLLIN;
  2318. if (poll(&pfd, 1, -1) > 0) {
  2319. if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
  2320. __sync_fetch_and_add(&ctx.count, 1);
  2321. }
  2322. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2323. EXPECT_EQ(ctx.count, 2);
  2324. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2325. pthread_kill(emitter, SIGUSR1);
  2326. pthread_join(emitter, NULL);
  2327. }
  2328. close(ctx.efd[0]);
  2329. close(ctx.efd[1]);
  2330. close(ctx.efd[2]);
  2331. close(ctx.sfd[0]);
  2332. close(ctx.sfd[1]);
  2333. close(ctx.sfd[2]);
  2334. close(ctx.sfd[3]);
  2335. }
  2336. /*
  2337. * t0 t1
  2338. * (p) \ / (p)
  2339. * e0
  2340. * (et) / \ (et)
  2341. * e1 e2
  2342. * (lt) | | (lt)
  2343. * s0 s2
  2344. */
  2345. TEST(epoll58)
  2346. {
  2347. pthread_t emitter;
  2348. struct pollfd pfd;
  2349. struct epoll_event e;
  2350. struct epoll_mtcontext ctx = { 0 };
  2351. signal(SIGUSR1, signal_handler);
  2352. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[0]), 0);
  2353. ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, &ctx.sfd[2]), 0);
  2354. ctx.efd[0] = epoll_create(1);
  2355. ASSERT_GE(ctx.efd[0], 0);
  2356. ctx.efd[1] = epoll_create(1);
  2357. ASSERT_GE(ctx.efd[1], 0);
  2358. ctx.efd[2] = epoll_create(1);
  2359. ASSERT_GE(ctx.efd[2], 0);
  2360. e.events = EPOLLIN;
  2361. ASSERT_EQ(epoll_ctl(ctx.efd[1], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2362. e.events = EPOLLIN;
  2363. ASSERT_EQ(epoll_ctl(ctx.efd[2], EPOLL_CTL_ADD, ctx.sfd[2], &e), 0);
  2364. e.events = EPOLLIN | EPOLLET;
  2365. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[1], &e), 0);
  2366. e.events = EPOLLIN | EPOLLET;
  2367. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.efd[2], &e), 0);
  2368. ctx.main = pthread_self();
  2369. ASSERT_EQ(pthread_create(&ctx.waiter, NULL, waiter_entry1ap, &ctx), 0);
  2370. ASSERT_EQ(pthread_create(&emitter, NULL, emitter_entry2, &ctx), 0);
  2371. pfd.fd = ctx.efd[0];
  2372. pfd.events = POLLIN;
  2373. if (poll(&pfd, 1, -1) > 0) {
  2374. if (epoll_wait(ctx.efd[0], &e, 1, 0) > 0)
  2375. __sync_fetch_and_add(&ctx.count, 1);
  2376. }
  2377. ASSERT_EQ(pthread_join(ctx.waiter, NULL), 0);
  2378. EXPECT_EQ(ctx.count, 2);
  2379. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2380. pthread_kill(emitter, SIGUSR1);
  2381. pthread_join(emitter, NULL);
  2382. }
  2383. close(ctx.efd[0]);
  2384. close(ctx.efd[1]);
  2385. close(ctx.efd[2]);
  2386. close(ctx.sfd[0]);
  2387. close(ctx.sfd[1]);
  2388. close(ctx.sfd[2]);
  2389. close(ctx.sfd[3]);
  2390. }
  2391. static void *epoll59_thread(void *ctx_)
  2392. {
  2393. struct epoll_mtcontext *ctx = ctx_;
  2394. struct epoll_event e;
  2395. int i;
  2396. for (i = 0; i < 100000; i++) {
  2397. while (ctx->count == 0)
  2398. ;
  2399. e.events = EPOLLIN | EPOLLERR | EPOLLET;
  2400. epoll_ctl(ctx->efd[0], EPOLL_CTL_MOD, ctx->sfd[0], &e);
  2401. ctx->count = 0;
  2402. }
  2403. return NULL;
  2404. }
  2405. /*
  2406. * t0
  2407. * (p) \
  2408. * e0
  2409. * (et) /
  2410. * e0
  2411. *
  2412. * Based on https://bugzilla.kernel.org/show_bug.cgi?id=205933
  2413. */
  2414. TEST(epoll59)
  2415. {
  2416. pthread_t emitter;
  2417. struct pollfd pfd;
  2418. struct epoll_event e;
  2419. struct epoll_mtcontext ctx = { 0 };
  2420. int i, ret;
  2421. signal(SIGUSR1, signal_handler);
  2422. ctx.efd[0] = epoll_create1(0);
  2423. ASSERT_GE(ctx.efd[0], 0);
  2424. ctx.sfd[0] = eventfd(1, 0);
  2425. ASSERT_GE(ctx.sfd[0], 0);
  2426. e.events = EPOLLIN | EPOLLERR | EPOLLET;
  2427. ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
  2428. ASSERT_EQ(pthread_create(&emitter, NULL, epoll59_thread, &ctx), 0);
  2429. for (i = 0; i < 100000; i++) {
  2430. ret = epoll_wait(ctx.efd[0], &e, 1, 1000);
  2431. ASSERT_GT(ret, 0);
  2432. while (ctx.count != 0)
  2433. ;
  2434. ctx.count = 1;
  2435. }
  2436. if (pthread_tryjoin_np(emitter, NULL) < 0) {
  2437. pthread_kill(emitter, SIGUSR1);
  2438. pthread_join(emitter, NULL);
  2439. }
  2440. close(ctx.efd[0]);
  2441. close(ctx.sfd[0]);
  2442. }
  2443. enum {
  2444. EPOLL60_EVENTS_NR = 10,
  2445. };
  2446. struct epoll60_ctx {
  2447. volatile int stopped;
  2448. int ready;
  2449. int waiters;
  2450. int epfd;
  2451. int evfd[EPOLL60_EVENTS_NR];
  2452. };
  2453. static void *epoll60_wait_thread(void *ctx_)
  2454. {
  2455. struct epoll60_ctx *ctx = ctx_;
  2456. struct epoll_event e;
  2457. sigset_t sigmask;
  2458. uint64_t v;
  2459. int ret;
  2460. /* Block SIGUSR1 */
  2461. sigemptyset(&sigmask);
  2462. sigaddset(&sigmask, SIGUSR1);
  2463. sigprocmask(SIG_SETMASK, &sigmask, NULL);
  2464. /* Prepare empty mask for epoll_pwait() */
  2465. sigemptyset(&sigmask);
  2466. while (!ctx->stopped) {
  2467. /* Mark we are ready */
  2468. __atomic_fetch_add(&ctx->ready, 1, __ATOMIC_ACQUIRE);
  2469. /* Start when all are ready */
  2470. while (__atomic_load_n(&ctx->ready, __ATOMIC_ACQUIRE) &&
  2471. !ctx->stopped);
  2472. /* Account this waiter */
  2473. __atomic_fetch_add(&ctx->waiters, 1, __ATOMIC_ACQUIRE);
  2474. ret = epoll_pwait(ctx->epfd, &e, 1, 2000, &sigmask);
  2475. if (ret != 1) {
  2476. /* We expect only signal delivery on stop */
  2477. assert(ret < 0 && errno == EINTR && "Lost wakeup!\n");
  2478. assert(ctx->stopped);
  2479. break;
  2480. }
  2481. ret = read(e.data.fd, &v, sizeof(v));
  2482. /* Since we are on ET mode, thus each thread gets its own fd. */
  2483. assert(ret == sizeof(v));
  2484. __atomic_fetch_sub(&ctx->waiters, 1, __ATOMIC_RELEASE);
  2485. }
  2486. return NULL;
  2487. }
  2488. static inline unsigned long long msecs(void)
  2489. {
  2490. struct timespec ts;
  2491. unsigned long long msecs;
  2492. clock_gettime(CLOCK_REALTIME, &ts);
  2493. msecs = ts.tv_sec * 1000ull;
  2494. msecs += ts.tv_nsec / 1000000ull;
  2495. return msecs;
  2496. }
  2497. static inline int count_waiters(struct epoll60_ctx *ctx)
  2498. {
  2499. return __atomic_load_n(&ctx->waiters, __ATOMIC_ACQUIRE);
  2500. }
  2501. TEST(epoll60)
  2502. {
  2503. struct epoll60_ctx ctx = { 0 };
  2504. pthread_t waiters[ARRAY_SIZE(ctx.evfd)];
  2505. struct epoll_event e;
  2506. int i, n, ret;
  2507. signal(SIGUSR1, signal_handler);
  2508. ctx.epfd = epoll_create1(0);
  2509. ASSERT_GE(ctx.epfd, 0);
  2510. /* Create event fds */
  2511. for (i = 0; i < ARRAY_SIZE(ctx.evfd); i++) {
  2512. ctx.evfd[i] = eventfd(0, EFD_NONBLOCK);
  2513. ASSERT_GE(ctx.evfd[i], 0);
  2514. e.events = EPOLLIN | EPOLLET;
  2515. e.data.fd = ctx.evfd[i];
  2516. ASSERT_EQ(epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, ctx.evfd[i], &e), 0);
  2517. }
  2518. /* Create waiter threads */
  2519. for (i = 0; i < ARRAY_SIZE(waiters); i++)
  2520. ASSERT_EQ(pthread_create(&waiters[i], NULL,
  2521. epoll60_wait_thread, &ctx), 0);
  2522. for (i = 0; i < 300; i++) {
  2523. uint64_t v = 1, ms;
  2524. /* Wait for all to be ready */
  2525. while (__atomic_load_n(&ctx.ready, __ATOMIC_ACQUIRE) !=
  2526. ARRAY_SIZE(ctx.evfd))
  2527. ;
  2528. /* Steady, go */
  2529. __atomic_fetch_sub(&ctx.ready, ARRAY_SIZE(ctx.evfd),
  2530. __ATOMIC_ACQUIRE);
  2531. /* Wait all have gone to kernel */
  2532. while (count_waiters(&ctx) != ARRAY_SIZE(ctx.evfd))
  2533. ;
  2534. /* 1ms should be enough to schedule away */
  2535. usleep(1000);
  2536. /* Quickly signal all handles at once */
  2537. for (n = 0; n < ARRAY_SIZE(ctx.evfd); n++) {
  2538. ret = write(ctx.evfd[n], &v, sizeof(v));
  2539. ASSERT_EQ(ret, sizeof(v));
  2540. }
  2541. /* Busy loop for 1s and wait for all waiters to wake up */
  2542. ms = msecs();
  2543. while (count_waiters(&ctx) && msecs() < ms + 1000)
  2544. ;
  2545. ASSERT_EQ(count_waiters(&ctx), 0);
  2546. }
  2547. ctx.stopped = 1;
  2548. /* Stop waiters */
  2549. for (i = 0; i < ARRAY_SIZE(waiters); i++)
  2550. ret = pthread_kill(waiters[i], SIGUSR1);
  2551. for (i = 0; i < ARRAY_SIZE(waiters); i++)
  2552. pthread_join(waiters[i], NULL);
  2553. for (i = 0; i < ARRAY_SIZE(waiters); i++)
  2554. close(ctx.evfd[i]);
  2555. close(ctx.epfd);
  2556. }
  2557. TEST_HARNESS_MAIN