tst-aio2.c 2.5 KB

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