[BZ #5995]
[glibc.git] / nptl / tst-cond11.c
blob0de4d56137e7e70e4199e2383910593a94413cd7
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <unistd.h>
27 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
28 static int
29 run_test (clockid_t cl)
31 pthread_condattr_t condattr;
32 pthread_cond_t cond;
33 pthread_mutexattr_t mutattr;
34 pthread_mutex_t mut;
36 printf ("clock = %d\n", (int) cl);
38 if (pthread_condattr_init (&condattr) != 0)
40 puts ("condattr_init failed");
41 return 1;
44 if (pthread_condattr_setclock (&condattr, cl) != 0)
46 puts ("condattr_setclock failed");
47 return 1;
50 clockid_t cl2;
51 if (pthread_condattr_getclock (&condattr, &cl2) != 0)
53 puts ("condattr_getclock failed");
54 return 1;
56 if (cl != cl2)
58 printf ("condattr_getclock returned wrong value: %d, expected %d\n",
59 (int) cl2, (int) cl);
60 return 1;
63 if (pthread_cond_init (&cond, &condattr) != 0)
65 puts ("cond_init failed");
66 return 1;
69 if (pthread_condattr_destroy (&condattr) != 0)
71 puts ("condattr_destroy failed");
72 return 1;
75 if (pthread_mutexattr_init (&mutattr) != 0)
77 puts ("mutexattr_init failed");
78 return 1;
81 if (pthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
83 puts ("mutexattr_settype failed");
84 return 1;
87 if (pthread_mutex_init (&mut, &mutattr) != 0)
89 puts ("mutex_init failed");
90 return 1;
93 if (pthread_mutexattr_destroy (&mutattr) != 0)
95 puts ("mutexattr_destroy failed");
96 return 1;
99 if (pthread_mutex_lock (&mut) != 0)
101 puts ("mutex_lock failed");
102 return 1;
105 if (pthread_mutex_lock (&mut) != EDEADLK)
107 puts ("2nd mutex_lock did not return EDEADLK");
108 return 1;
111 struct timespec ts;
112 if (clock_gettime (cl, &ts) != 0)
114 puts ("clock_gettime failed");
115 return 1;
118 /* Wait one second. */
119 ++ts.tv_sec;
121 int e = pthread_cond_timedwait (&cond, &mut, &ts);
122 if (e == 0)
124 puts ("cond_timedwait succeeded");
125 return 1;
127 else if (e != ETIMEDOUT)
129 puts ("cond_timedwait did not return ETIMEDOUT");
130 return 1;
133 if (pthread_mutex_unlock (&mut) != 0)
135 puts ("mutex_unlock failed");
136 return 1;
139 if (pthread_mutex_destroy (&mut) != 0)
141 puts ("mutex_destroy failed");
142 return 1;
145 if (pthread_cond_destroy (&cond) != 0)
147 puts ("cond_destroy failed");
148 return 1;
151 return 0;
153 #endif
156 static int
157 do_test (void)
159 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
161 puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
162 return 0;
164 #else
166 int res = run_test (CLOCK_REALTIME);
168 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
169 # if _POSIX_MONOTONIC_CLOCK == 0
170 int e = sysconf (_SC_MONOTONIC_CLOCK);
171 if (e < 0)
172 puts ("CLOCK_MONOTONIC not supported");
173 else if (e == 0)
175 puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
176 res = 1;
178 else
179 # endif
180 res |= run_test (CLOCK_MONOTONIC);
181 # else
182 puts ("_POSIX_MONOTONIC_CLOCK not defined");
183 # endif
185 return res;
186 #endif
189 #define TIMEOUT 3
190 #define TEST_FUNCTION do_test ()
191 #include "../test-skeleton.c"