Fix cproj implmentation.
[glibc.git] / nptl / tst-cond11.c
blob4d0c7dd225b53c23fa42efe30968e39c543d1387
1 /* Copyright (C) 2003, 2004, 2009 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 struct timespec ts2;
134 if (clock_gettime (cl, &ts2) != 0)
136 puts ("second clock_gettime failed");
137 return 1;
140 if (ts2.tv_sec < ts.tv_sec
141 || (ts2.tv_sec == ts.tv_sec && ts2.tv_nsec < ts.tv_nsec))
143 puts ("timeout too short");
144 return 1;
147 if (pthread_mutex_unlock (&mut) != 0)
149 puts ("mutex_unlock failed");
150 return 1;
153 if (pthread_mutex_destroy (&mut) != 0)
155 puts ("mutex_destroy failed");
156 return 1;
159 if (pthread_cond_destroy (&cond) != 0)
161 puts ("cond_destroy failed");
162 return 1;
165 return 0;
167 #endif
170 static int
171 do_test (void)
173 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
175 puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
176 return 0;
178 #else
180 int res = run_test (CLOCK_REALTIME);
182 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
183 # if _POSIX_MONOTONIC_CLOCK == 0
184 int e = sysconf (_SC_MONOTONIC_CLOCK);
185 if (e < 0)
186 puts ("CLOCK_MONOTONIC not supported");
187 else if (e == 0)
189 puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
190 res = 1;
192 else
193 # endif
194 res |= run_test (CLOCK_MONOTONIC);
195 # else
196 puts ("_POSIX_MONOTONIC_CLOCK not defined");
197 # endif
199 return res;
200 #endif
203 #define TIMEOUT 3
204 #define TEST_FUNCTION do_test ()
205 #include "../test-skeleton.c"