tst-aio10.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_lio_opcode = LIO_READ;
  62. req.aio_reqprio = 0;
  63. req.aio_offset = 0;
  64. req.aio_buf = buf;
  65. req.aio_nbytes = sizeof (buf);
  66. req.aio_sigevent.sigev_notify = SIGEV_NONE;
  67. pthread_t th;
  68. if (pthread_create (&th, NULL, tf, NULL) != 0)
  69. {
  70. puts ("create failed");
  71. return 1;
  72. }
  73. int e = pthread_barrier_wait (&b);
  74. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  75. {
  76. puts ("parent: barrier_wait failed");
  77. exit (1);
  78. }
  79. struct aiocb *list[1];
  80. list[0] = &req;
  81. e = lio_listio (LIO_WAIT, list, 1, NULL);
  82. if (e != -1)
  83. {
  84. puts ("lio_listio succeeded");
  85. return 1;
  86. }
  87. if (errno != EINTR)
  88. {
  89. printf ("lio_listio did not return EINTR: %d (%d = %m)\n", e, errno);
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. #define TEST_FUNCTION do_test ()
  95. #include "../test-skeleton.c"