]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 | 6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
3d6af2dd | 9 | * For details, see <http://zfsonlinux.org/>. |
716154c5 BB |
10 | * |
11 | * The SPL is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
715f6251 | 15 | * |
716154c5 | 16 | * The SPL is distributed in the hope that it will be useful, but WITHOUT |
715f6251 | 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | * for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | ***************************************************************************** | |
24 | * Solaris Porting LAyer Tests (SPLAT) Condition Variable Tests. | |
25 | \*****************************************************************************/ | |
715f6251 | 26 | |
df870a69 | 27 | #include <sys/condvar.h> |
10946b02 AX |
28 | #include <sys/timer.h> |
29 | #include <sys/thread.h> | |
7c50328b | 30 | #include "splat-internal.h" |
f1ca4da6 | 31 | |
7c50328b | 32 | #define SPLAT_CONDVAR_NAME "condvar" |
33 | #define SPLAT_CONDVAR_DESC "Kernel Condition Variable Tests" | |
f1ca4da6 | 34 | |
7c50328b | 35 | #define SPLAT_CONDVAR_TEST1_ID 0x0501 |
36 | #define SPLAT_CONDVAR_TEST1_NAME "signal1" | |
37 | #define SPLAT_CONDVAR_TEST1_DESC "Wake a single thread, cv_wait()/cv_signal()" | |
f1ca4da6 | 38 | |
7c50328b | 39 | #define SPLAT_CONDVAR_TEST2_ID 0x0502 |
40 | #define SPLAT_CONDVAR_TEST2_NAME "broadcast1" | |
41 | #define SPLAT_CONDVAR_TEST2_DESC "Wake all threads, cv_wait()/cv_broadcast()" | |
f1ca4da6 | 42 | |
7c50328b | 43 | #define SPLAT_CONDVAR_TEST3_ID 0x0503 |
44 | #define SPLAT_CONDVAR_TEST3_NAME "signal2" | |
45 | #define SPLAT_CONDVAR_TEST3_DESC "Wake a single thread, cv_wait_timeout()/cv_signal()" | |
f1ca4da6 | 46 | |
7c50328b | 47 | #define SPLAT_CONDVAR_TEST4_ID 0x0504 |
48 | #define SPLAT_CONDVAR_TEST4_NAME "broadcast2" | |
49 | #define SPLAT_CONDVAR_TEST4_DESC "Wake all threads, cv_wait_timeout()/cv_broadcast()" | |
f1ca4da6 | 50 | |
7c50328b | 51 | #define SPLAT_CONDVAR_TEST5_ID 0x0505 |
52 | #define SPLAT_CONDVAR_TEST5_NAME "timeout" | |
53 | #define SPLAT_CONDVAR_TEST5_DESC "Timeout thread, cv_wait_timeout()" | |
f1ca4da6 | 54 | |
7c50328b | 55 | #define SPLAT_CONDVAR_TEST_MAGIC 0x115599DDUL |
b84412a6 | 56 | #define SPLAT_CONDVAR_TEST_NAME "condvar" |
7c50328b | 57 | #define SPLAT_CONDVAR_TEST_COUNT 8 |
f1ca4da6 | 58 | |
59 | typedef struct condvar_priv { | |
b84412a6 BB |
60 | unsigned long cv_magic; |
61 | struct file *cv_file; | |
f1ca4da6 | 62 | kcondvar_t cv_condvar; |
63 | kmutex_t cv_mtx; | |
64 | } condvar_priv_t; | |
65 | ||
66 | typedef struct condvar_thr { | |
f1ca4da6 | 67 | const char *ct_name; |
68 | condvar_priv_t *ct_cvp; | |
b84412a6 | 69 | struct task_struct *ct_thread; |
f1ca4da6 | 70 | int ct_rc; |
71 | } condvar_thr_t; | |
72 | ||
73 | int | |
7c50328b | 74 | splat_condvar_test12_thread(void *arg) |
f1ca4da6 | 75 | { |
76 | condvar_thr_t *ct = (condvar_thr_t *)arg; | |
77 | condvar_priv_t *cv = ct->ct_cvp; | |
f1ca4da6 | 78 | |
7c50328b | 79 | ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC); |
f1ca4da6 | 80 | |
81 | mutex_enter(&cv->cv_mtx); | |
7c50328b | 82 | splat_vprint(cv->cv_file, ct->ct_name, |
b84412a6 BB |
83 | "%s thread sleeping with %d waiters\n", |
84 | ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters)); | |
f1ca4da6 | 85 | cv_wait(&cv->cv_condvar, &cv->cv_mtx); |
7c50328b | 86 | splat_vprint(cv->cv_file, ct->ct_name, |
b84412a6 BB |
87 | "%s thread woken %d waiters remain\n", |
88 | ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters)); | |
f1ca4da6 | 89 | mutex_exit(&cv->cv_mtx); |
90 | ||
81dab2ed AX |
91 | /* wait for main thread reap us */ |
92 | while (!kthread_should_stop()) | |
93 | schedule(); | |
f1ca4da6 | 94 | return 0; |
95 | } | |
96 | ||
97 | static int | |
7c50328b | 98 | splat_condvar_test1(struct file *file, void *arg) |
f1ca4da6 | 99 | { |
100 | int i, count = 0, rc = 0; | |
7c50328b | 101 | condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT]; |
f1ca4da6 | 102 | condvar_priv_t cv; |
103 | ||
7c50328b | 104 | cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC; |
f1ca4da6 | 105 | cv.cv_file = file; |
7c50328b | 106 | mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL); |
b29012b9 | 107 | cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL); |
f1ca4da6 | 108 | |
109 | /* Create some threads, the exact number isn't important just as | |
110 | * long as we know how many we managed to create and should expect. */ | |
7c50328b | 111 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { |
f1ca4da6 | 112 | ct[i].ct_cvp = &cv; |
7c50328b | 113 | ct[i].ct_name = SPLAT_CONDVAR_TEST1_NAME; |
f1ca4da6 | 114 | ct[i].ct_rc = 0; |
9e4fb5c2 | 115 | ct[i].ct_thread = spl_kthread_create(splat_condvar_test12_thread, |
b84412a6 | 116 | &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i); |
f1ca4da6 | 117 | |
b84412a6 BB |
118 | if (!IS_ERR(ct[i].ct_thread)) { |
119 | wake_up_process(ct[i].ct_thread); | |
f1ca4da6 | 120 | count++; |
b84412a6 | 121 | } |
f1ca4da6 | 122 | } |
123 | ||
124 | /* Wait until all threads are waiting on the condition variable */ | |
125 | while (atomic_read(&cv.cv_condvar.cv_waiters) != count) | |
126 | schedule(); | |
127 | ||
128 | /* Wake a single thread at a time, wait until it exits */ | |
129 | for (i = 1; i <= count; i++) { | |
130 | cv_signal(&cv.cv_condvar); | |
131 | ||
132 | while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i)) | |
133 | schedule(); | |
134 | ||
135 | /* Correct behavior 1 thread woken */ | |
136 | if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i)) | |
137 | continue; | |
138 | ||
7c50328b | 139 | splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Attempted to " |
f1ca4da6 | 140 | "wake %d thread but work %d threads woke\n", |
141 | 1, count - atomic_read(&cv.cv_condvar.cv_waiters)); | |
142 | rc = -EINVAL; | |
143 | break; | |
144 | } | |
145 | ||
146 | if (!rc) | |
7c50328b | 147 | splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Correctly woke " |
f1ca4da6 | 148 | "%d sleeping threads %d at a time\n", count, 1); |
149 | ||
150 | /* Wait until that last nutex is dropped */ | |
151 | while (mutex_owner(&cv.cv_mtx)) | |
152 | schedule(); | |
153 | ||
154 | /* Wake everything for the failure case */ | |
155 | cv_broadcast(&cv.cv_condvar); | |
156 | cv_destroy(&cv.cv_condvar); | |
81dab2ed AX |
157 | |
158 | /* wait for threads to exit */ | |
159 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { | |
160 | if (!IS_ERR(ct[i].ct_thread)) | |
161 | kthread_stop(ct[i].ct_thread); | |
162 | } | |
f1ca4da6 | 163 | mutex_destroy(&cv.cv_mtx); |
164 | ||
165 | return rc; | |
166 | } | |
167 | ||
168 | static int | |
7c50328b | 169 | splat_condvar_test2(struct file *file, void *arg) |
f1ca4da6 | 170 | { |
171 | int i, count = 0, rc = 0; | |
7c50328b | 172 | condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT]; |
f1ca4da6 | 173 | condvar_priv_t cv; |
174 | ||
7c50328b | 175 | cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC; |
f1ca4da6 | 176 | cv.cv_file = file; |
7c50328b | 177 | mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL); |
b29012b9 | 178 | cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL); |
f1ca4da6 | 179 | |
180 | /* Create some threads, the exact number isn't important just as | |
181 | * long as we know how many we managed to create and should expect. */ | |
7c50328b | 182 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { |
f1ca4da6 | 183 | ct[i].ct_cvp = &cv; |
7c50328b | 184 | ct[i].ct_name = SPLAT_CONDVAR_TEST2_NAME; |
f1ca4da6 | 185 | ct[i].ct_rc = 0; |
9e4fb5c2 | 186 | ct[i].ct_thread = spl_kthread_create(splat_condvar_test12_thread, |
b84412a6 | 187 | &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i); |
f1ca4da6 | 188 | |
b84412a6 BB |
189 | if (!IS_ERR(ct[i].ct_thread)) { |
190 | wake_up_process(ct[i].ct_thread); | |
f1ca4da6 | 191 | count++; |
b84412a6 | 192 | } |
f1ca4da6 | 193 | } |
194 | ||
195 | /* Wait until all threads are waiting on the condition variable */ | |
196 | while (atomic_read(&cv.cv_condvar.cv_waiters) != count) | |
197 | schedule(); | |
198 | ||
199 | /* Wake all threads waiting on the condition variable */ | |
200 | cv_broadcast(&cv.cv_condvar); | |
201 | ||
202 | /* Wait until all threads have exited */ | |
203 | while ((atomic_read(&cv.cv_condvar.cv_waiters) > 0) || mutex_owner(&cv.cv_mtx)) | |
204 | schedule(); | |
205 | ||
7c50328b | 206 | splat_vprint(file, SPLAT_CONDVAR_TEST2_NAME, "Correctly woke all " |
f1ca4da6 | 207 | "%d sleeping threads at once\n", count); |
208 | ||
209 | /* Wake everything for the failure case */ | |
210 | cv_destroy(&cv.cv_condvar); | |
81dab2ed AX |
211 | |
212 | /* wait for threads to exit */ | |
213 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { | |
214 | if (!IS_ERR(ct[i].ct_thread)) | |
215 | kthread_stop(ct[i].ct_thread); | |
216 | } | |
f1ca4da6 | 217 | mutex_destroy(&cv.cv_mtx); |
218 | ||
219 | return rc; | |
220 | } | |
221 | ||
222 | int | |
7c50328b | 223 | splat_condvar_test34_thread(void *arg) |
f1ca4da6 | 224 | { |
225 | condvar_thr_t *ct = (condvar_thr_t *)arg; | |
226 | condvar_priv_t *cv = ct->ct_cvp; | |
f1ca4da6 | 227 | clock_t rc; |
228 | ||
7c50328b | 229 | ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC); |
f1ca4da6 | 230 | |
231 | mutex_enter(&cv->cv_mtx); | |
7c50328b | 232 | splat_vprint(cv->cv_file, ct->ct_name, |
b84412a6 BB |
233 | "%s thread sleeping with %d waiters\n", |
234 | ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters)); | |
f1ca4da6 | 235 | |
236 | /* Sleep no longer than 3 seconds, for this test we should | |
237 | * actually never sleep that long without being woken up. */ | |
238 | rc = cv_timedwait(&cv->cv_condvar, &cv->cv_mtx, lbolt + HZ * 3); | |
239 | if (rc == -1) { | |
240 | ct->ct_rc = -ETIMEDOUT; | |
7c50328b | 241 | splat_vprint(cv->cv_file, ct->ct_name, "%s thread timed out, " |
b84412a6 | 242 | "should have been woken\n", ct->ct_thread->comm); |
f1ca4da6 | 243 | } else { |
7c50328b | 244 | splat_vprint(cv->cv_file, ct->ct_name, |
b84412a6 BB |
245 | "%s thread woken %d waiters remain\n", |
246 | ct->ct_thread->comm, | |
247 | atomic_read(&cv->cv_condvar.cv_waiters)); | |
f1ca4da6 | 248 | } |
249 | ||
250 | mutex_exit(&cv->cv_mtx); | |
251 | ||
81dab2ed AX |
252 | /* wait for main thread reap us */ |
253 | while (!kthread_should_stop()) | |
254 | schedule(); | |
f1ca4da6 | 255 | return 0; |
256 | } | |
257 | ||
258 | static int | |
7c50328b | 259 | splat_condvar_test3(struct file *file, void *arg) |
f1ca4da6 | 260 | { |
261 | int i, count = 0, rc = 0; | |
7c50328b | 262 | condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT]; |
f1ca4da6 | 263 | condvar_priv_t cv; |
264 | ||
7c50328b | 265 | cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC; |
f1ca4da6 | 266 | cv.cv_file = file; |
7c50328b | 267 | mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL); |
b29012b9 | 268 | cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL); |
f1ca4da6 | 269 | |
270 | /* Create some threads, the exact number isn't important just as | |
271 | * long as we know how many we managed to create and should expect. */ | |
7c50328b | 272 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { |
f1ca4da6 | 273 | ct[i].ct_cvp = &cv; |
7c50328b | 274 | ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME; |
f1ca4da6 | 275 | ct[i].ct_rc = 0; |
9e4fb5c2 | 276 | ct[i].ct_thread = spl_kthread_create(splat_condvar_test34_thread, |
b84412a6 | 277 | &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i); |
f1ca4da6 | 278 | |
b84412a6 BB |
279 | if (!IS_ERR(ct[i].ct_thread)) { |
280 | wake_up_process(ct[i].ct_thread); | |
f1ca4da6 | 281 | count++; |
b84412a6 | 282 | } |
f1ca4da6 | 283 | } |
284 | ||
285 | /* Wait until all threads are waiting on the condition variable */ | |
286 | while (atomic_read(&cv.cv_condvar.cv_waiters) != count) | |
287 | schedule(); | |
288 | ||
289 | /* Wake a single thread at a time, wait until it exits */ | |
290 | for (i = 1; i <= count; i++) { | |
291 | cv_signal(&cv.cv_condvar); | |
292 | ||
293 | while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i)) | |
294 | schedule(); | |
295 | ||
296 | /* Correct behavior 1 thread woken */ | |
297 | if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i)) | |
298 | continue; | |
299 | ||
7c50328b | 300 | splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to " |
f1ca4da6 | 301 | "wake %d thread but work %d threads woke\n", |
302 | 1, count - atomic_read(&cv.cv_condvar.cv_waiters)); | |
303 | rc = -EINVAL; | |
304 | break; | |
305 | } | |
306 | ||
307 | /* Validate no waiting thread timed out early */ | |
308 | for (i = 0; i < count; i++) | |
309 | if (ct[i].ct_rc) | |
310 | rc = ct[i].ct_rc; | |
311 | ||
312 | if (!rc) | |
7c50328b | 313 | splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke " |
f1ca4da6 | 314 | "%d sleeping threads %d at a time\n", count, 1); |
315 | ||
316 | /* Wait until that last nutex is dropped */ | |
317 | while (mutex_owner(&cv.cv_mtx)) | |
318 | schedule(); | |
319 | ||
320 | /* Wake everything for the failure case */ | |
321 | cv_broadcast(&cv.cv_condvar); | |
322 | cv_destroy(&cv.cv_condvar); | |
81dab2ed AX |
323 | |
324 | /* wait for threads to exit */ | |
325 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { | |
326 | if (!IS_ERR(ct[i].ct_thread)) | |
327 | kthread_stop(ct[i].ct_thread); | |
328 | } | |
f1ca4da6 | 329 | mutex_destroy(&cv.cv_mtx); |
330 | ||
331 | return rc; | |
332 | } | |
333 | ||
334 | static int | |
7c50328b | 335 | splat_condvar_test4(struct file *file, void *arg) |
f1ca4da6 | 336 | { |
337 | int i, count = 0, rc = 0; | |
7c50328b | 338 | condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT]; |
f1ca4da6 | 339 | condvar_priv_t cv; |
340 | ||
7c50328b | 341 | cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC; |
f1ca4da6 | 342 | cv.cv_file = file; |
7c50328b | 343 | mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL); |
b29012b9 | 344 | cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL); |
f1ca4da6 | 345 | |
346 | /* Create some threads, the exact number isn't important just as | |
347 | * long as we know how many we managed to create and should expect. */ | |
7c50328b | 348 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { |
f1ca4da6 | 349 | ct[i].ct_cvp = &cv; |
7c50328b | 350 | ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME; |
f1ca4da6 | 351 | ct[i].ct_rc = 0; |
9e4fb5c2 | 352 | ct[i].ct_thread = spl_kthread_create(splat_condvar_test34_thread, |
b84412a6 | 353 | &ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i); |
f1ca4da6 | 354 | |
b84412a6 BB |
355 | if (!IS_ERR(ct[i].ct_thread)) { |
356 | wake_up_process(ct[i].ct_thread); | |
f1ca4da6 | 357 | count++; |
b84412a6 | 358 | } |
f1ca4da6 | 359 | } |
360 | ||
361 | /* Wait until all threads are waiting on the condition variable */ | |
362 | while (atomic_read(&cv.cv_condvar.cv_waiters) != count) | |
363 | schedule(); | |
364 | ||
365 | /* Wake a single thread at a time, wait until it exits */ | |
366 | for (i = 1; i <= count; i++) { | |
367 | cv_signal(&cv.cv_condvar); | |
368 | ||
369 | while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i)) | |
370 | schedule(); | |
371 | ||
372 | /* Correct behavior 1 thread woken */ | |
373 | if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i)) | |
374 | continue; | |
375 | ||
7c50328b | 376 | splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to " |
f1ca4da6 | 377 | "wake %d thread but work %d threads woke\n", |
378 | 1, count - atomic_read(&cv.cv_condvar.cv_waiters)); | |
379 | rc = -EINVAL; | |
380 | break; | |
381 | } | |
382 | ||
383 | /* Validate no waiting thread timed out early */ | |
384 | for (i = 0; i < count; i++) | |
385 | if (ct[i].ct_rc) | |
386 | rc = ct[i].ct_rc; | |
387 | ||
388 | if (!rc) | |
7c50328b | 389 | splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke " |
f1ca4da6 | 390 | "%d sleeping threads %d at a time\n", count, 1); |
391 | ||
392 | /* Wait until that last nutex is dropped */ | |
393 | while (mutex_owner(&cv.cv_mtx)) | |
394 | schedule(); | |
395 | ||
396 | /* Wake everything for the failure case */ | |
397 | cv_broadcast(&cv.cv_condvar); | |
398 | cv_destroy(&cv.cv_condvar); | |
81dab2ed AX |
399 | |
400 | /* wait for threads to exit */ | |
401 | for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) { | |
402 | if (!IS_ERR(ct[i].ct_thread)) | |
403 | kthread_stop(ct[i].ct_thread); | |
404 | } | |
f1ca4da6 | 405 | mutex_destroy(&cv.cv_mtx); |
406 | ||
407 | return rc; | |
408 | } | |
409 | ||
410 | static int | |
7c50328b | 411 | splat_condvar_test5(struct file *file, void *arg) |
f1ca4da6 | 412 | { |
413 | kcondvar_t condvar; | |
414 | kmutex_t mtx; | |
415 | clock_t time_left, time_before, time_after, time_delta; | |
c4ed98c5 AX |
416 | uint64_t whole_delta; |
417 | uint32_t remain_delta; | |
f1ca4da6 | 418 | int rc = 0; |
419 | ||
7c50328b | 420 | mutex_init(&mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL); |
b29012b9 | 421 | cv_init(&condvar, NULL, CV_DEFAULT, NULL); |
f1ca4da6 | 422 | |
7c50328b | 423 | splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, "Thread going to sleep for " |
f1ca4da6 | 424 | "%d second and expecting to be woken by timeout\n", 1); |
425 | ||
426 | /* Allow a 1 second timeout, plenty long to validate correctness. */ | |
427 | time_before = lbolt; | |
428 | mutex_enter(&mtx); | |
429 | time_left = cv_timedwait(&condvar, &mtx, lbolt + HZ); | |
430 | mutex_exit(&mtx); | |
431 | time_after = lbolt; | |
432 | time_delta = time_after - time_before; /* XXX - Handle jiffie wrap */ | |
433 | whole_delta = time_delta; | |
434 | remain_delta = do_div(whole_delta, HZ); | |
435 | ||
436 | if (time_left == -1) { | |
437 | if (time_delta >= HZ) { | |
7c50328b | 438 | splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, |
f1ca4da6 | 439 | "Thread correctly timed out and was asleep " |
440 | "for %d.%d seconds (%d second min)\n", | |
c4ed98c5 | 441 | (int)whole_delta, (int)remain_delta, 1); |
f1ca4da6 | 442 | } else { |
7c50328b | 443 | splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, |
f1ca4da6 | 444 | "Thread correctly timed out but was only " |
445 | "asleep for %d.%d seconds (%d second " | |
c4ed98c5 AX |
446 | "min)\n", (int)whole_delta, |
447 | (int)remain_delta, 1); | |
f1ca4da6 | 448 | rc = -ETIMEDOUT; |
449 | } | |
450 | } else { | |
7c50328b | 451 | splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, |
f1ca4da6 | 452 | "Thread exited after only %d.%d seconds, it " |
453 | "did not hit the %d second timeout\n", | |
c4ed98c5 | 454 | (int)whole_delta, (int)remain_delta, 1); |
f1ca4da6 | 455 | rc = -ETIMEDOUT; |
456 | } | |
457 | ||
458 | cv_destroy(&condvar); | |
459 | mutex_destroy(&mtx); | |
460 | ||
461 | return rc; | |
462 | } | |
463 | ||
7c50328b | 464 | splat_subsystem_t * |
465 | splat_condvar_init(void) | |
f1ca4da6 | 466 | { |
7c50328b | 467 | splat_subsystem_t *sub; |
f1ca4da6 | 468 | |
469 | sub = kmalloc(sizeof(*sub), GFP_KERNEL); | |
470 | if (sub == NULL) | |
471 | return NULL; | |
472 | ||
473 | memset(sub, 0, sizeof(*sub)); | |
7c50328b | 474 | strncpy(sub->desc.name, SPLAT_CONDVAR_NAME, SPLAT_NAME_SIZE); |
475 | strncpy(sub->desc.desc, SPLAT_CONDVAR_DESC, SPLAT_DESC_SIZE); | |
f1ca4da6 | 476 | INIT_LIST_HEAD(&sub->subsystem_list); |
477 | INIT_LIST_HEAD(&sub->test_list); | |
478 | spin_lock_init(&sub->test_lock); | |
7c50328b | 479 | sub->desc.id = SPLAT_SUBSYSTEM_CONDVAR; |
480 | ||
ec06701b | 481 | splat_test_init(sub, SPLAT_CONDVAR_TEST1_NAME, SPLAT_CONDVAR_TEST1_DESC, |
7c50328b | 482 | SPLAT_CONDVAR_TEST1_ID, splat_condvar_test1); |
ec06701b | 483 | splat_test_init(sub, SPLAT_CONDVAR_TEST2_NAME, SPLAT_CONDVAR_TEST2_DESC, |
7c50328b | 484 | SPLAT_CONDVAR_TEST2_ID, splat_condvar_test2); |
ec06701b | 485 | splat_test_init(sub, SPLAT_CONDVAR_TEST3_NAME, SPLAT_CONDVAR_TEST3_DESC, |
7c50328b | 486 | SPLAT_CONDVAR_TEST3_ID, splat_condvar_test3); |
ec06701b | 487 | splat_test_init(sub, SPLAT_CONDVAR_TEST4_NAME, SPLAT_CONDVAR_TEST4_DESC, |
7c50328b | 488 | SPLAT_CONDVAR_TEST4_ID, splat_condvar_test4); |
ec06701b | 489 | splat_test_init(sub, SPLAT_CONDVAR_TEST5_NAME, SPLAT_CONDVAR_TEST5_DESC, |
7c50328b | 490 | SPLAT_CONDVAR_TEST5_ID, splat_condvar_test5); |
f1ca4da6 | 491 | |
492 | return sub; | |
493 | } | |
494 | ||
495 | void | |
7c50328b | 496 | splat_condvar_fini(splat_subsystem_t *sub) |
f1ca4da6 | 497 | { |
498 | ASSERT(sub); | |
ec06701b AX |
499 | splat_test_fini(sub, SPLAT_CONDVAR_TEST5_ID); |
500 | splat_test_fini(sub, SPLAT_CONDVAR_TEST4_ID); | |
501 | splat_test_fini(sub, SPLAT_CONDVAR_TEST3_ID); | |
502 | splat_test_fini(sub, SPLAT_CONDVAR_TEST2_ID); | |
503 | splat_test_fini(sub, SPLAT_CONDVAR_TEST1_ID); | |
f1ca4da6 | 504 | |
505 | kfree(sub); | |
506 | } | |
507 | ||
508 | int | |
7c50328b | 509 | splat_condvar_id(void) { |
510 | return SPLAT_SUBSYSTEM_CONDVAR; | |
f1ca4da6 | 511 | } |