1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
20 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
21 exit to be called more than once. */
31 #include <sys/select.h>
35 /* The following interfaces are defined to be cancellation points but
36 tests are not yet implemented:
38 accept() aio_suspend() clock_nanosleep()
39 close() connect() creat()
40 fcntl() fsync() getmsg()
41 getpmsg() lockf() mq_receive()
42 mq_send() mq_timedreceive() mq_timedsend()
43 msgrcv() msgsnd() msync()
45 pread() pthread_cond_timedwait()
46 pthread_cond_wait() pthread_join() pthread_testcancel()
47 putmsg() putpmsg() pwrite()
50 sem_timedwait() sem_wait() send()
51 sendmsg() sendto() sigpause()
52 sigsuspend() sigtimedwait() sigwait()
53 sigwaitinfo() system()
56 Since STREAMS are not supported in the standard Linux kernel there
57 is no need to test the STREAMS related functions.
60 /* Pipe descriptors. */
63 /* Often used barrier for two threads. */
64 static pthread_barrier_t b2
;
70 int r
= pthread_barrier_wait (&b2
);
71 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
73 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
78 ssize_t s
= read (fds
[0], buf
, sizeof (buf
));
80 printf ("%s: read returns with %zd\n", __FUNCTION__
, s
);
89 int r
= pthread_barrier_wait (&b2
);
90 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
92 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
97 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
98 ssize_t s
= readv (fds
[0], iov
, 1);
100 printf ("%s: readv returns with %zd\n", __FUNCTION__
, s
);
109 int r
= pthread_barrier_wait (&b2
);
110 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
112 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
117 memset (buf
, '\0', sizeof (buf
));
118 ssize_t s
= write (fds
[1], buf
, sizeof (buf
));
120 printf ("%s: write returns with %zd\n", __FUNCTION__
, s
);
127 tf_writev (void *arg
)
129 int r
= pthread_barrier_wait (&b2
);
130 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
132 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
137 memset (buf
, '\0', sizeof (buf
));
138 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
139 ssize_t s
= writev (fds
[1], iov
, 1);
141 printf ("%s: writev returns with %zd\n", __FUNCTION__
, s
);
150 int r
= pthread_barrier_wait (&b2
);
151 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
153 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
159 printf ("%s: sleep returns\n", __FUNCTION__
);
166 tf_usleep (void *arg
)
168 int r
= pthread_barrier_wait (&b2
);
169 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
171 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
175 usleep ((useconds_t
) ULONG_MAX
);
177 printf ("%s: usleep returns\n", __FUNCTION__
);
184 tf_nanosleep (void *arg
)
186 int r
= pthread_barrier_wait (&b2
);
187 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
189 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
193 struct timespec ts
= { .tv_sec
= 10000000, .tv_nsec
= 0 };
194 while (nanosleep (&ts
, &ts
) != 0)
197 printf ("%s: nanosleep returns\n", __FUNCTION__
);
204 tf_select (void *arg
)
206 int r
= pthread_barrier_wait (&b2
);
207 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
209 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
215 FD_SET (fds
[0], &rfs
);
217 int s
= select (fds
[0] + 1, &rfs
, NULL
, NULL
, NULL
);
219 printf ("%s: select returns with %d (%s)\n", __FUNCTION__
, s
,
227 tf_pselect (void *arg
)
229 int r
= pthread_barrier_wait (&b2
);
230 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
232 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
238 FD_SET (fds
[0], &rfs
);
240 int s
= pselect (fds
[0] + 1, &rfs
, NULL
, NULL
, NULL
, NULL
);
242 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__
, s
,
252 int r
= pthread_barrier_wait (&b2
);
253 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
255 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
259 struct pollfd rfs
[1] = { [0] = { .fd
= fds
[0], .events
= POLLIN
} };
261 int s
= poll (rfs
, 1, -1);
263 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__
, s
,
273 int r
= pthread_barrier_wait (&b2
);
274 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
276 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
283 puts ("fork failed");
289 /* Make the program disappear after a while. */
296 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__
, s
,
304 tf_waitpid (void *arg
)
306 int r
= pthread_barrier_wait (&b2
);
307 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
309 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
316 puts ("fork failed");
322 /* Make the program disappear after a while. */
327 int s
= waitpid (-1, NULL
, 0);
329 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__
, s
,
337 tf_waitid (void *arg
)
339 int r
= pthread_barrier_wait (&b2
);
340 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
342 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
349 puts ("fork failed");
355 /* Make the program disappear after a while. */
364 int s
= waitid (P_PID
, pid
, &si
, WEXITED
);
366 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__
, s
,
375 void *(*tf
) (void *);
393 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
401 puts ("pipe failed");
406 for (cnt
= 0; cnt
< ntest_tf
; ++cnt
)
408 printf ("round %d\n", cnt
);
410 if (pthread_barrier_init (&b2
, NULL
, tests
[cnt
].nb
) != 0)
412 puts ("b2 init failed");
417 if (pthread_create (&th
, NULL
, tests
[cnt
].tf
, NULL
) != 0)
419 printf ("create for round %d test failed\n", cnt
);
423 puts ("barrier waits");
425 int r
= pthread_barrier_wait (&b2
);
426 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
428 printf ("%s: barrier_wait failed\n", __FUNCTION__
);
432 puts ("nanosleep delay");
434 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
435 while (nanosleep (&ts
, &ts
) != 0)
438 if (pthread_cancel (th
) != 0)
440 printf ("cancel in round %d failed\n", cnt
);
445 if (pthread_join (th
, &status
) != 0)
447 printf ("join in round %d failed\n", cnt
);
450 if (status
!= PTHREAD_CANCELED
)
452 printf ("thread in round %d not canceled\n", cnt
);
455 printf ("test %d successful\n", cnt
);
457 if (pthread_barrier_destroy (&b2
) != 0)
459 puts ("barrier_destroy failed");
468 #define TEST_FUNCTION do_test ()
469 #include "../test-skeleton.c"