tst-aio2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Test for notification mechanism in lio_listio.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <aio.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <pthread.h>
  22. static pthread_barrier_t b;
  23. static void
  24. thrfct (sigval_t arg)
  25. {
  26. int e = pthread_barrier_wait (&b);
  27. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  28. {
  29. puts ("thread: barrier_wait failed");
  30. exit (1);
  31. }
  32. }
  33. static int
  34. do_test (int argc, char *argv[])
  35. {
  36. char name[] = "/tmp/aio2.XXXXXX";
  37. int fd;
  38. struct aiocb *arr[1];
  39. struct aiocb cb;
  40. static const char buf[] = "Hello World\n";
  41. fd = mkstemp (name);
  42. if (fd == -1)
  43. {
  44. printf ("cannot open temp name: %m\n");
  45. return 1;
  46. }
  47. unlink (name);
  48. if (pthread_barrier_init (&b, NULL, 2) != 0)
  49. {
  50. puts ("barrier_init failed");
  51. return 1;
  52. }
  53. arr[0] = &cb;
  54. cb.aio_fildes = fd;
  55. cb.aio_lio_opcode = LIO_WRITE;
  56. cb.aio_reqprio = 0;
  57. cb.aio_buf = (void *) buf;
  58. cb.aio_nbytes = sizeof (buf) - 1;
  59. cb.aio_offset = 0;
  60. cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
  61. cb.aio_sigevent.sigev_notify_function = thrfct;
  62. cb.aio_sigevent.sigev_notify_attributes = NULL;
  63. cb.aio_sigevent.sigev_value.sival_ptr = NULL;
  64. if (lio_listio (LIO_WAIT, arr, 1, NULL) < 0)
  65. {
  66. if (errno == ENOSYS)
  67. {
  68. puts ("no aio support in this configuration");
  69. return 0;
  70. }
  71. printf ("lio_listio failed: %m\n");
  72. return 1;
  73. }
  74. puts ("lio_listio returned");
  75. int e = pthread_barrier_wait (&b);
  76. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  77. {
  78. puts ("barrier_wait failed");
  79. return 1;
  80. }
  81. puts ("all OK");
  82. return 0;
  83. }
  84. #include "../test-skeleton.c"