Back to index

glibc  2.9
tst-aio9.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_reqprio = 0;
00079   req.aio_offset = 0;
00080   req.aio_buf = buf;
00081   req.aio_nbytes = sizeof (buf);
00082   req.aio_sigevent.sigev_notify = SIGEV_NONE;
00083 
00084   if (aio_read (&req) != 0)
00085     {
00086       puts ("aio_read failed");
00087       return 1;
00088     }
00089 
00090   pthread_t th;
00091   if (pthread_create (&th, NULL, tf, NULL) != 0)
00092     {
00093       puts ("create failed");
00094       return 1;
00095     }
00096 
00097   int e = pthread_barrier_wait (&b);
00098   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00099     {
00100       puts ("parent: barrier_wait failed");
00101       exit (1);
00102     }
00103 
00104   const struct aiocb *list[1];
00105   list[0] = &req;
00106 
00107   e = aio_suspend (list, 1, NULL);
00108   if (e != -1)
00109     {
00110       puts ("aio_suspend succeeded");
00111       return 1;
00112     }
00113   if (errno != EINTR)
00114     {
00115       puts ("aio_suspend did not return EINTR");
00116       return 1;
00117     }
00118 
00119   return 0;
00120 }
00121 
00122 #define TEST_FUNCTION do_test ()
00123 #define TIMEOUT 5
00124 #include "../test-skeleton.c"