Back to index

glibc  2.9
tst-aio2.c
Go to the documentation of this file.
00001 /* Test for notification mechanism in lio_listio.
00002    Copyright (C) 2000, 2002, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <aio.h>
00022 #include <signal.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 #include <errno.h>
00027 
00028 
00029 static pthread_barrier_t b;
00030 
00031 
00032 static void
00033 thrfct (sigval_t arg)
00034 {
00035   int e = pthread_barrier_wait (&b);
00036   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00037     {
00038       puts ("thread: barrier_wait failed");
00039       exit (1);
00040     }
00041 }
00042 
00043 
00044 static int
00045 do_test (int argc, char *argv[])
00046 {
00047   char name[] = "/tmp/aio2.XXXXXX";
00048   int fd;
00049   struct aiocb *arr[1];
00050   struct aiocb cb;
00051   static const char buf[] = "Hello World\n";
00052 
00053   fd = mkstemp (name);
00054   if (fd == -1)
00055     {
00056       printf ("cannot open temp name: %m\n");
00057       return 1;
00058     }
00059 
00060   unlink (name);
00061 
00062   if (pthread_barrier_init (&b, NULL, 2) != 0)
00063     {
00064       puts ("barrier_init failed");
00065       return 1;
00066     }
00067 
00068   arr[0] = &cb;
00069 
00070   cb.aio_fildes = fd;
00071   cb.aio_lio_opcode = LIO_WRITE;
00072   cb.aio_reqprio = 0;
00073   cb.aio_buf = (void *) buf;
00074   cb.aio_nbytes = sizeof (buf) - 1;
00075   cb.aio_offset = 0;
00076   cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
00077   cb.aio_sigevent.sigev_notify_function = thrfct;
00078   cb.aio_sigevent.sigev_notify_attributes = NULL;
00079   cb.aio_sigevent.sigev_value.sival_ptr = NULL;
00080 
00081   if (lio_listio (LIO_WAIT, arr, 1, NULL) < 0)
00082     {
00083       if (errno == ENOSYS)
00084        {
00085          puts ("no aio support in this configuration");
00086          return 0;
00087        }
00088       printf ("lio_listio failed: %m\n");
00089       return 1;
00090     }
00091 
00092   puts ("lio_listio returned");
00093 
00094   int e = pthread_barrier_wait (&b);
00095   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00096     {
00097       puts ("barrier_wait failed");
00098       return 1;
00099     }
00100 
00101   puts ("all OK");
00102 
00103   return 0;
00104 }
00105 
00106 #include "../test-skeleton.c"