PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 1ms 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

Large files files are truncated, but you can click here to view the full file

  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 =

Large files files are truncated, but you can click here to view the full file