2.10.90-5
[glibc.git] / rtkaio / tst-aiod5.c
blob780f1b60284f56dd57de34311b4232daea33a769
1 /* Test for completion thread handling.
2 Copyright (C) 2000, 2002, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <aio.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include "tst-aiod.h"
28 /* We might need a bit longer timeout. */
29 #define TIMEOUT 10 /* sec */
31 #define MY_SIVAL 27
33 volatile sig_atomic_t flag;
36 static void
37 callback (sigval_t s)
39 flag = s.sival_int;
42 static int
43 wait_flag (void)
45 while (flag == 0)
47 puts ("Sleeping...");
48 sleep (1);
51 if (flag != MY_SIVAL)
53 printf ("signal handler received wrong signal, flag is %d\n", flag);
54 return 1;
57 return 0;
61 static int
62 do_test (int argc, char *argv[])
64 char name[] = "/tmp/aio5.XXXXXX";
65 int fd;
66 struct aiocb *arr[1];
67 struct aiocb cb;
68 static const char buf[] = "Hello World\n";
69 struct sigevent ev;
71 fd = mkstemp (name);
72 if (fd == -1)
74 printf ("cannot open temp name: %m\n");
75 return 1;
78 unlink (name);
80 arr[0] = &cb;
82 void *p;
83 int sz = set_o_direct (fd);
84 if (sz != -1)
86 int err = posix_memalign (&p, sz, sz);
87 if (err)
89 errno = err;
90 printf ("cannot allocate memory: %m\n");
91 return 1;
93 memcpy (p, buf, sizeof (buf) - 1);
94 memset (p + sizeof (buf) - 1, ' ', sz - sizeof (buf) + 1);
95 printf ("Using O_DIRECT with block size %d\n", sz);
97 else
99 p = (void *) buf;
100 sz = sizeof (buf) - 1;
103 cb.aio_fildes = fd;
104 cb.aio_lio_opcode = LIO_WRITE;
105 cb.aio_reqprio = 0;
106 cb.aio_buf = p;
107 cb.aio_nbytes = sz;
108 cb.aio_offset = 0;
109 cb.aio_sigevent.sigev_notify = SIGEV_THREAD;
110 cb.aio_sigevent.sigev_notify_function = callback;
111 cb.aio_sigevent.sigev_notify_attributes = NULL;
112 cb.aio_sigevent.sigev_value.sival_int = MY_SIVAL;
114 ev.sigev_notify = SIGEV_THREAD;
115 ev.sigev_notify_function = callback;
116 ev.sigev_notify_attributes = NULL;
117 ev.sigev_value.sival_int = MY_SIVAL;
119 /* First use aio_write. */
120 if (aio_write (arr[0]) < 0)
122 if (errno == ENOSYS)
124 puts ("no aio support in this configuration");
125 return 0;
127 printf ("aio_write failed: %m\n");
128 return 1;
131 if (wait_flag ())
132 return 1;
134 puts ("aio_write OK");
136 flag = 0;
137 /* Again with lio_listio. */
138 if (lio_listio (LIO_NOWAIT, arr, 1, &ev) < 0)
140 printf ("lio_listio failed: %m\n");
141 return 1;
144 if (wait_flag ())
145 return 1;
147 puts ("all OK");
149 return 0;
152 #include "../test-skeleton.c"