Back to index

glibc  2.9
tst-aio10.c
Go to the documentation of this file.
00001 #include <aio.h>
00002 #include <errno.h>
00003 #include <signal.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <pthread.h>
00007 #include <unistd.h>
00008 
00009 static pthread_barrier_t b;
00010 static pthread_t main_thread;
00011 static int flag;
00012 
00013 
00014 static void *
00015 tf (void *arg)
00016 {
00017   int e = pthread_barrier_wait (&b);
00018   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00019     {
00020       puts ("child: barrier_wait failed");
00021       exit (1);
00022     }
00023 
00024   /* There is unfortunately no other way to try to make sure the other
00025      thread reached the aio_suspend call.  This test could fail on
00026      highly loaded machines.  */
00027   sleep (2);
00028 
00029   pthread_kill (main_thread, SIGUSR1);
00030 
00031   while (1)
00032     sleep (1000);
00033 
00034   return NULL;
00035 }
00036 
00037 
00038 static void
00039 sh (int sig)
00040 {
00041   flag = 1;
00042 }
00043 
00044 
00045 static int
00046 do_test (void)
00047 {
00048   main_thread = pthread_self ();
00049 
00050   struct sigaction sa;
00051 
00052   sa.sa_handler = sh;
00053   sa.sa_flags = 0;
00054   sigemptyset (&sa.sa_mask);
00055 
00056   if (sigaction (SIGUSR1, &sa, NULL) != 0)
00057     {
00058       puts ("sigaction failed");
00059       return 1;
00060     }
00061 
00062   if (pthread_barrier_init (&b, NULL, 2) != 0)
00063     {
00064       puts ("barrier_init");
00065       return 1;
00066     }
00067 
00068   int fds[2];
00069   if (pipe (fds) != 0)
00070     {
00071       puts ("pipe failed");
00072       return 1;
00073     }
00074 
00075   char buf[42];
00076   struct aiocb req;
00077   req.aio_fildes = fds[0];
00078   req.aio_lio_opcode = LIO_READ;
00079   req.aio_reqprio = 0;
00080   req.aio_offset = 0;
00081   req.aio_buf = buf;
00082   req.aio_nbytes = sizeof (buf);
00083   req.aio_sigevent.sigev_notify = SIGEV_NONE;
00084 
00085   pthread_t th;
00086   if (pthread_create (&th, NULL, tf, NULL) != 0)
00087     {
00088       puts ("create failed");
00089       return 1;
00090     }
00091 
00092   int e = pthread_barrier_wait (&b);
00093   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00094     {
00095       puts ("parent: barrier_wait failed");
00096       exit (1);
00097     }
00098 
00099   struct aiocb *list[1];
00100   list[0] = &req;
00101 
00102   e = lio_listio (LIO_WAIT, list, 1, NULL);
00103   if (e != -1)
00104     {
00105       puts ("lio_listio succeeded");
00106       return 1;
00107     }
00108   if (errno != EINTR)
00109     {
00110       printf ("lio_listio did not return EINTR: %d (%d = %m)\n", e, errno);
00111       return 1;
00112     }
00113 
00114   return 0;
00115 }
00116 
00117 #define TEST_FUNCTION do_test ()
00118 #define TIMEOUT 5
00119 #include "../test-skeleton.c"