wakeup-hang.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <sys/eventfd.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9. #include <fcntl.h>
  10. #include <poll.h>
  11. #include <sys/time.h>
  12. #include "liburing.h"
  13. struct thread_data {
  14. struct io_uring *ring;
  15. int write_fd;
  16. };
  17. static void error_exit(char *message)
  18. {
  19. perror(message);
  20. exit(1);
  21. }
  22. static void *listener_thread(void *data)
  23. {
  24. struct thread_data *td = data;
  25. struct io_uring_cqe *cqe;
  26. int ret;
  27. ret = io_uring_wait_cqe(td->ring, &cqe);
  28. if (ret < 0) {
  29. fprintf(stderr, "Error waiting for completion: %s\n",
  30. strerror(-ret));
  31. goto err;
  32. }
  33. if (cqe->res < 0) {
  34. fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
  35. goto err;
  36. }
  37. io_uring_cqe_seen(td->ring, cqe);
  38. return NULL;
  39. err:
  40. return (void *) 1;
  41. }
  42. static void *wakeup_io_uring(void *data)
  43. {
  44. struct thread_data *td = data;
  45. int res;
  46. res = eventfd_write(td->write_fd, (eventfd_t) 1L);
  47. if (res < 0) {
  48. perror("eventfd_write");
  49. return (void *) 1;
  50. }
  51. return NULL;
  52. }
  53. static int test_pipes(void)
  54. {
  55. struct io_uring_sqe *sqe;
  56. struct thread_data td;
  57. struct io_uring ring;
  58. pthread_t t1, t2;
  59. int ret, fds[2];
  60. void *pret;
  61. if (pipe(fds) < 0)
  62. error_exit("eventfd");
  63. ret = io_uring_queue_init(8, &ring, 0);
  64. if (ret) {
  65. fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret));
  66. return 1;
  67. }
  68. td.write_fd = fds[1];
  69. td.ring = &ring;
  70. sqe = io_uring_get_sqe(&ring);
  71. io_uring_prep_poll_add(sqe, fds[0], POLLIN);
  72. sqe->user_data = 2;
  73. ret = io_uring_submit(&ring);
  74. if (ret != 1) {
  75. fprintf(stderr, "ring_submit=%d\n", ret);
  76. return 1;
  77. }
  78. pthread_create(&t1, NULL, listener_thread, &td);
  79. sleep(1);
  80. pthread_create(&t2, NULL, wakeup_io_uring, &td);
  81. pthread_join(t1, &pret);
  82. io_uring_queue_exit(&ring);
  83. return pret != NULL;
  84. }
  85. static int test_eventfd(void)
  86. {
  87. struct io_uring_sqe *sqe;
  88. struct thread_data td;
  89. struct io_uring ring;
  90. pthread_t t1, t2;
  91. int efd, ret;
  92. void *pret;
  93. efd = eventfd(0, 0);
  94. if (efd < 0)
  95. error_exit("eventfd");
  96. ret = io_uring_queue_init(8, &ring, 0);
  97. if (ret) {
  98. fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret));
  99. return 1;
  100. }
  101. td.write_fd = efd;
  102. td.ring = &ring;
  103. sqe = io_uring_get_sqe(&ring);
  104. io_uring_prep_poll_add(sqe, efd, POLLIN);
  105. sqe->user_data = 2;
  106. ret = io_uring_submit(&ring);
  107. if (ret != 1) {
  108. fprintf(stderr, "ring_submit=%d\n", ret);
  109. return 1;
  110. }
  111. pthread_create(&t1, NULL, listener_thread, &td);
  112. sleep(1);
  113. pthread_create(&t2, NULL, wakeup_io_uring, &td);
  114. pthread_join(t1, &pret);
  115. io_uring_queue_exit(&ring);
  116. return pret != NULL;
  117. }
  118. int main(int argc, char *argv[])
  119. {
  120. int ret;
  121. if (argc > 1)
  122. return 0;
  123. ret = test_pipes();
  124. if (ret) {
  125. fprintf(stderr, "test_pipe failed\n");
  126. return ret;
  127. }
  128. ret = test_eventfd();
  129. if (ret) {
  130. fprintf(stderr, "test_eventfd failed\n");
  131. return ret;
  132. }
  133. return 0;
  134. }