Back to index

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