tst-aio9.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <aio.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <unistd.h>
  8. static pthread_barrier_t b;
  9. static pthread_t main_thread;
  10. static int flag;
  11. static void *
  12. tf (void *arg)
  13. {
  14. int e = pthread_barrier_wait (&b);
  15. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  16. {
  17. puts ("child: barrier_wait failed");
  18. exit (1);
  19. }
  20. /* There is unfortunately no other way to try to make sure the other
  21. thread reached the aio_suspend call. This test could fail on
  22. highly loaded machines. */
  23. sleep (2);
  24. pthread_kill (main_thread, SIGUSR1);
  25. while (1)
  26. sleep (1000);
  27. return NULL;
  28. }
  29. static void
  30. sh (int sig)
  31. {
  32. flag = 1;
  33. }
  34. static int
  35. do_test (void)
  36. {
  37. main_thread = pthread_self ();
  38. struct sigaction sa;
  39. sa.sa_handler = sh;
  40. sa.sa_flags = 0;
  41. sigemptyset (&sa.sa_mask);
  42. if (sigaction (SIGUSR1, &sa, NULL) != 0)
  43. {
  44. puts ("sigaction failed");
  45. return 1;
  46. }
  47. if (pthread_barrier_init (&b, NULL, 2) != 0)
  48. {
  49. puts ("barrier_init");
  50. return 1;
  51. }
  52. int fds[2];
  53. if (pipe (fds) != 0)
  54. {
  55. puts ("pipe failed");
  56. return 1;
  57. }
  58. char buf[42];
  59. struct aiocb req;
  60. req.aio_fildes = fds[0];
  61. req.aio_reqprio = 0;
  62. req.aio_offset = 0;
  63. req.aio_buf = buf;
  64. req.aio_nbytes = sizeof (buf);
  65. req.aio_sigevent.sigev_notify = SIGEV_NONE;
  66. if (aio_read (&req) != 0)
  67. {
  68. puts ("aio_read failed");
  69. return 1;
  70. }
  71. pthread_t th;
  72. if (pthread_create (&th, NULL, tf, NULL) != 0)
  73. {
  74. puts ("create failed");
  75. return 1;
  76. }
  77. int e = pthread_barrier_wait (&b);
  78. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  79. {
  80. puts ("parent: barrier_wait failed");
  81. exit (1);
  82. }
  83. const struct aiocb *list[1];
  84. list[0] = &req;
  85. e = aio_suspend (list, 1, NULL);
  86. if (e != -1)
  87. {
  88. puts ("aio_suspend succeeded");
  89. return 1;
  90. }
  91. if (errno != EINTR)
  92. {
  93. puts ("aio_suspend did not return EINTR");
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. #define TEST_FUNCTION do_test ()
  99. #include "../test-skeleton.c"