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.
|
---|
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, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <errno.h>
|
---|
21 | #include <pthread.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <time.h>
|
---|
24 | #include <unistd.h>
|
---|
25 |
|
---|
26 |
|
---|
27 | #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
|
---|
28 | static int
|
---|
29 | run_test (clockid_t cl)
|
---|
30 | {
|
---|
31 | pthread_condattr_t condattr;
|
---|
32 | pthread_cond_t cond;
|
---|
33 | pthread_mutexattr_t mutattr;
|
---|
34 | pthread_mutex_t mut;
|
---|
35 |
|
---|
36 | printf ("clock = %d\n", (int) cl);
|
---|
37 |
|
---|
38 | if (pthread_condattr_init (&condattr) != 0)
|
---|
39 | {
|
---|
40 | puts ("condattr_init failed");
|
---|
41 | return 1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (pthread_condattr_setclock (&condattr, cl) != 0)
|
---|
45 | {
|
---|
46 | puts ("condattr_setclock failed");
|
---|
47 | return 1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | clockid_t cl2;
|
---|
51 | if (pthread_condattr_getclock (&condattr, &cl2) != 0)
|
---|
52 | {
|
---|
53 | puts ("condattr_getclock failed");
|
---|
54 | return 1;
|
---|
55 | }
|
---|
56 | if (cl != cl2)
|
---|
57 | {
|
---|
58 | printf ("condattr_getclock returned wrong value: %d, expected %d\n",
|
---|
59 | (int) cl2, (int) cl);
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (pthread_cond_init (&cond, &condattr) != 0)
|
---|
64 | {
|
---|
65 | puts ("cond_init failed");
|
---|
66 | return 1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (pthread_condattr_destroy (&condattr) != 0)
|
---|
70 | {
|
---|
71 | puts ("condattr_destroy failed");
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (pthread_mutexattr_init (&mutattr) != 0)
|
---|
76 | {
|
---|
77 | puts ("mutexattr_init failed");
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (pthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
|
---|
82 | {
|
---|
83 | puts ("mutexattr_settype failed");
|
---|
84 | return 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (pthread_mutex_init (&mut, &mutattr) != 0)
|
---|
88 | {
|
---|
89 | puts ("mutex_init failed");
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (pthread_mutexattr_destroy (&mutattr) != 0)
|
---|
94 | {
|
---|
95 | puts ("mutexattr_destroy failed");
|
---|
96 | return 1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (pthread_mutex_lock (&mut) != 0)
|
---|
100 | {
|
---|
101 | puts ("mutex_lock failed");
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (pthread_mutex_lock (&mut) != EDEADLK)
|
---|
106 | {
|
---|
107 | puts ("2nd mutex_lock did not return EDEADLK");
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | struct timespec ts;
|
---|
112 | if (clock_gettime (cl, &ts) != 0)
|
---|
113 | {
|
---|
114 | puts ("clock_gettime failed");
|
---|
115 | return 1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* Wait one second. */
|
---|
119 | ++ts.tv_sec;
|
---|
120 |
|
---|
121 | int e = pthread_cond_timedwait (&cond, &mut, &ts);
|
---|
122 | if (e == 0)
|
---|
123 | {
|
---|
124 | puts ("cond_timedwait succeeded");
|
---|
125 | return 1;
|
---|
126 | }
|
---|
127 | else if (e != ETIMEDOUT)
|
---|
128 | {
|
---|
129 | puts ("cond_timedwait did not return ETIMEDOUT");
|
---|
130 | return 1;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (pthread_mutex_unlock (&mut) != 0)
|
---|
134 | {
|
---|
135 | puts ("mutex_unlock failed");
|
---|
136 | return 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (pthread_mutex_destroy (&mut) != 0)
|
---|
140 | {
|
---|
141 | puts ("mutex_destroy failed");
|
---|
142 | return 1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (pthread_cond_destroy (&cond) != 0)
|
---|
146 | {
|
---|
147 | puts ("cond_destroy failed");
|
---|
148 | return 1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return 0;
|
---|
152 | }
|
---|
153 | #endif
|
---|
154 |
|
---|
155 |
|
---|
156 | static int
|
---|
157 | do_test (void)
|
---|
158 | {
|
---|
159 | #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
|
---|
160 |
|
---|
161 | puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
|
---|
162 | return 0;
|
---|
163 |
|
---|
164 | #else
|
---|
165 |
|
---|
166 | int res = run_test (CLOCK_REALTIME);
|
---|
167 |
|
---|
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)
|
---|
174 | {
|
---|
175 | puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
|
---|
176 | res = 1;
|
---|
177 | }
|
---|
178 | else
|
---|
179 | # endif
|
---|
180 | res |= run_test (CLOCK_MONOTONIC);
|
---|
181 | # else
|
---|
182 | puts ("_POSIX_MONOTONIC_CLOCK not defined");
|
---|
183 | # endif
|
---|
184 |
|
---|
185 | return res;
|
---|
186 | #endif
|
---|
187 | }
|
---|
188 |
|
---|
189 | #define TIMEOUT 3
|
---|
190 | #define TEST_FUNCTION do_test ()
|
---|
191 | #include "../test-skeleton.c"
|
---|