]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/test/nptl/tst-cond5.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / test / nptl / tst-cond5.c
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <sys/time.h>
26
27
28 static pthread_mutex_t mut;
29 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
30
31
32 static int
33 do_test (void)
34 {
35   pthread_mutexattr_t ma;
36   int err;
37   struct timespec ts;
38   struct timeval tv;
39
40   if (pthread_mutexattr_init (&ma) != 0)
41     {
42       puts ("mutexattr_init failed");
43       exit (1);
44     }
45
46   if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
47     {
48       puts ("mutexattr_settype failed");
49       exit (1);
50     }
51
52   if (pthread_mutex_init (&mut, &ma) != 0)
53     {
54       puts ("mutex_init failed");
55       exit (1);
56     }
57
58   /* Get the mutex.  */
59   if (pthread_mutex_lock (&mut) != 0)
60     {
61       puts ("mutex_lock failed");
62       exit (1);
63     }
64
65   /* Waiting for the condition will fail.  But we want the timeout here.  */
66   if (gettimeofday (&tv, NULL) != 0)
67     {
68       puts ("gettimeofday failed");
69       exit (1);
70     }
71
72   TIMEVAL_TO_TIMESPEC (&tv, &ts);
73   ts.tv_nsec += 500000000;
74   if (ts.tv_nsec >= 1000000000)
75     {
76       ts.tv_nsec -= 1000000000;
77       ++ts.tv_sec;
78     }
79   err = pthread_cond_timedwait (&cond, &mut, &ts);
80   if (err == 0)
81     {
82       /* This could in theory happen but here without any signal and
83          additional waiter it should not.  */
84       puts ("cond_timedwait succeeded");
85       exit (1);
86     }
87   else if (err != ETIMEDOUT)
88     {
89       printf ("cond_timedwait returned with %s\n", strerror (err));
90       exit (1);
91     }
92
93   err = pthread_mutex_unlock (&mut);
94   if (err != 0)
95     {
96       printf ("mutex_unlock failed: %s\n", strerror (err));
97       exit (1);
98     }
99
100   return 0;
101 }
102
103
104 #define TEST_FUNCTION do_test ()
105 #include "../test-skeleton.c"