Back to index

glibc  2.9
tst-aio5.c
Go to the documentation of this file.
00001 /* Test for completion thread handling.
00002    Copyright (C) 2000,02 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 /* We might need a bit longer timeout.  */
00028 #define TIMEOUT 10 /* sec */
00029 
00030 #define MY_SIVAL 27
00031 
00032 volatile sig_atomic_t flag;
00033 
00034 
00035 static void
00036 callback (sigval_t s)
00037 {
00038   flag = s.sival_int;
00039 }
00040 
00041 static int
00042 wait_flag (void)
00043 {
00044   while (flag == 0)
00045     {
00046       puts ("Sleeping...");
00047       sleep (1);
00048     }
00049 
00050   if (flag != MY_SIVAL)
00051     {
00052       printf ("signal handler received wrong signal, flag is %d\n", flag);
00053       return 1;
00054     }
00055 
00056   return 0;
00057 }
00058 
00059 
00060 static int
00061 do_test (int argc, char *argv[])
00062 {
00063   char name[] = "/tmp/aio5.XXXXXX";
00064   int fd;
00065   struct aiocb *arr[1];
00066   struct aiocb cb;
00067   static const char buf[] = "Hello World\n";
00068   struct sigevent ev;
00069 
00070   fd = mkstemp (name);
00071   if (fd == -1)
00072     {
00073       printf ("cannot open temp name: %m\n");
00074       return 1;
00075     }
00076 
00077   unlink (name);
00078 
00079   arr[0] = &cb;
00080 
00081   cb.aio_fildes = fd;
00082   cb.aio_lio_opcode = LIO_WRITE;
00083   cb.aio_reqprio = 0;
00084   cb.aio_buf = (void *) buf;
00085   cb.aio_nbytes = sizeof (buf) - 1;
00086   cb.aio_offset = 0;
00087   cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
00088   cb.aio_sigevent.sigev_notify_function = callback;
00089   cb.aio_sigevent.sigev_notify_attributes = NULL;
00090   cb.aio_sigevent.sigev_value.sival_int = MY_SIVAL;
00091 
00092   ev.sigev_notify = SIGEV_THREAD;
00093   ev.sigev_notify_function = callback;
00094   ev.sigev_notify_attributes = NULL;
00095   ev.sigev_value.sival_int = MY_SIVAL;
00096 
00097   /* First use aio_write.  */
00098   if (aio_write (arr[0]) < 0)
00099     {
00100       if (errno == ENOSYS)
00101        {
00102          puts ("no aio support in this configuration");
00103          return 0;
00104        }
00105       printf ("aio_write failed: %m\n");
00106       return 1;
00107     }
00108 
00109   if (wait_flag ())
00110     return 1;
00111 
00112   puts ("aio_write OK");
00113 
00114   flag = 0;
00115   /* Again with lio_listio.  */
00116   if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
00117     {
00118       printf ("lio_listio failed: %m\n");
00119       return 1;
00120     }
00121 
00122   if (wait_flag ())
00123     return 1;
00124 
00125   puts ("all OK");
00126 
00127   return 0;
00128 }
00129 
00130 #include "../test-skeleton.c"