2010-05-17 22:18:00 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-17 22:18:00 +00:00
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting LAyer Tests (SPLAT) Condition Variable Tests.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2012-11-02 22:29:44 +00:00
|
|
|
#include <sys/condvar.h>
|
2014-12-08 18:04:42 +00:00
|
|
|
#include <sys/timer.h>
|
|
|
|
#include <sys/thread.h>
|
2008-02-27 23:42:31 +00:00
|
|
|
#include "splat-internal.h"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_NAME "condvar"
|
|
|
|
#define SPLAT_CONDVAR_DESC "Kernel Condition Variable Tests"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST1_ID 0x0501
|
|
|
|
#define SPLAT_CONDVAR_TEST1_NAME "signal1"
|
|
|
|
#define SPLAT_CONDVAR_TEST1_DESC "Wake a single thread, cv_wait()/cv_signal()"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST2_ID 0x0502
|
|
|
|
#define SPLAT_CONDVAR_TEST2_NAME "broadcast1"
|
|
|
|
#define SPLAT_CONDVAR_TEST2_DESC "Wake all threads, cv_wait()/cv_broadcast()"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST3_ID 0x0503
|
|
|
|
#define SPLAT_CONDVAR_TEST3_NAME "signal2"
|
|
|
|
#define SPLAT_CONDVAR_TEST3_DESC "Wake a single thread, cv_wait_timeout()/cv_signal()"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST4_ID 0x0504
|
|
|
|
#define SPLAT_CONDVAR_TEST4_NAME "broadcast2"
|
|
|
|
#define SPLAT_CONDVAR_TEST4_DESC "Wake all threads, cv_wait_timeout()/cv_broadcast()"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST5_ID 0x0505
|
|
|
|
#define SPLAT_CONDVAR_TEST5_NAME "timeout"
|
|
|
|
#define SPLAT_CONDVAR_TEST5_DESC "Timeout thread, cv_wait_timeout()"
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST_MAGIC 0x115599DDUL
|
2012-11-30 23:46:30 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST_NAME "condvar"
|
2008-02-27 23:42:31 +00:00
|
|
|
#define SPLAT_CONDVAR_TEST_COUNT 8
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
typedef struct condvar_priv {
|
2012-11-30 23:46:30 +00:00
|
|
|
unsigned long cv_magic;
|
|
|
|
struct file *cv_file;
|
2008-02-26 20:36:04 +00:00
|
|
|
kcondvar_t cv_condvar;
|
|
|
|
kmutex_t cv_mtx;
|
|
|
|
} condvar_priv_t;
|
|
|
|
|
|
|
|
typedef struct condvar_thr {
|
|
|
|
const char *ct_name;
|
|
|
|
condvar_priv_t *ct_cvp;
|
2012-11-30 23:46:30 +00:00
|
|
|
struct task_struct *ct_thread;
|
2008-02-26 20:36:04 +00:00
|
|
|
int ct_rc;
|
|
|
|
} condvar_thr_t;
|
|
|
|
|
|
|
|
int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test12_thread(void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
condvar_thr_t *ct = (condvar_thr_t *)arg;
|
|
|
|
condvar_priv_t *cv = ct->ct_cvp;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
mutex_enter(&cv->cv_mtx);
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(cv->cv_file, ct->ct_name,
|
2012-11-30 23:46:30 +00:00
|
|
|
"%s thread sleeping with %d waiters\n",
|
|
|
|
ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
|
2008-02-26 20:36:04 +00:00
|
|
|
cv_wait(&cv->cv_condvar, &cv->cv_mtx);
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(cv->cv_file, ct->ct_name,
|
2012-11-30 23:46:30 +00:00
|
|
|
"%s thread woken %d waiters remain\n",
|
|
|
|
ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
|
2008-02-26 20:36:04 +00:00
|
|
|
mutex_exit(&cv->cv_mtx);
|
|
|
|
|
2016-01-07 03:05:24 +00:00
|
|
|
/* wait for main thread reap us */
|
|
|
|
while (!kthread_should_stop())
|
|
|
|
schedule();
|
2008-02-26 20:36:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test1(struct file *file, void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
int i, count = 0, rc = 0;
|
2008-02-27 23:42:31 +00:00
|
|
|
condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
|
2008-02-26 20:36:04 +00:00
|
|
|
condvar_priv_t cv;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
|
2008-02-26 20:36:04 +00:00
|
|
|
cv.cv_file = file;
|
2008-02-27 23:42:31 +00:00
|
|
|
mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
|
2012-04-06 18:29:23 +00:00
|
|
|
cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
/* Create some threads, the exact number isn't important just as
|
|
|
|
* long as we know how many we managed to create and should expect. */
|
2008-02-27 23:42:31 +00:00
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_cvp = &cv;
|
2008-02-27 23:42:31 +00:00
|
|
|
ct[i].ct_name = SPLAT_CONDVAR_TEST1_NAME;
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_rc = 0;
|
2014-03-26 13:29:24 +00:00
|
|
|
ct[i].ct_thread = spl_kthread_create(splat_condvar_test12_thread,
|
2012-11-30 23:46:30 +00:00
|
|
|
&ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-11-30 23:46:30 +00:00
|
|
|
if (!IS_ERR(ct[i].ct_thread)) {
|
|
|
|
wake_up_process(ct[i].ct_thread);
|
2008-02-26 20:36:04 +00:00
|
|
|
count++;
|
2012-11-30 23:46:30 +00:00
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until all threads are waiting on the condition variable */
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake a single thread at a time, wait until it exits */
|
|
|
|
for (i = 1; i <= count; i++) {
|
|
|
|
cv_signal(&cv.cv_condvar);
|
|
|
|
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Correct behavior 1 thread woken */
|
|
|
|
if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
|
|
|
|
continue;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Attempted to "
|
2008-02-26 20:36:04 +00:00
|
|
|
"wake %d thread but work %d threads woke\n",
|
|
|
|
1, count - atomic_read(&cv.cv_condvar.cv_waiters));
|
|
|
|
rc = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST1_NAME, "Correctly woke "
|
2008-02-26 20:36:04 +00:00
|
|
|
"%d sleeping threads %d at a time\n", count, 1);
|
|
|
|
|
|
|
|
/* Wait until that last nutex is dropped */
|
|
|
|
while (mutex_owner(&cv.cv_mtx))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake everything for the failure case */
|
|
|
|
cv_broadcast(&cv.cv_condvar);
|
|
|
|
cv_destroy(&cv.cv_condvar);
|
2016-01-07 03:05:24 +00:00
|
|
|
|
|
|
|
/* wait for threads to exit */
|
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
|
|
|
if (!IS_ERR(ct[i].ct_thread))
|
|
|
|
kthread_stop(ct[i].ct_thread);
|
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
mutex_destroy(&cv.cv_mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test2(struct file *file, void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
int i, count = 0, rc = 0;
|
2008-02-27 23:42:31 +00:00
|
|
|
condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
|
2008-02-26 20:36:04 +00:00
|
|
|
condvar_priv_t cv;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
|
2008-02-26 20:36:04 +00:00
|
|
|
cv.cv_file = file;
|
2008-02-27 23:42:31 +00:00
|
|
|
mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
|
2012-04-06 18:29:23 +00:00
|
|
|
cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
/* Create some threads, the exact number isn't important just as
|
|
|
|
* long as we know how many we managed to create and should expect. */
|
2008-02-27 23:42:31 +00:00
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_cvp = &cv;
|
2008-02-27 23:42:31 +00:00
|
|
|
ct[i].ct_name = SPLAT_CONDVAR_TEST2_NAME;
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_rc = 0;
|
2014-03-26 13:29:24 +00:00
|
|
|
ct[i].ct_thread = spl_kthread_create(splat_condvar_test12_thread,
|
2012-11-30 23:46:30 +00:00
|
|
|
&ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-11-30 23:46:30 +00:00
|
|
|
if (!IS_ERR(ct[i].ct_thread)) {
|
|
|
|
wake_up_process(ct[i].ct_thread);
|
2008-02-26 20:36:04 +00:00
|
|
|
count++;
|
2012-11-30 23:46:30 +00:00
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until all threads are waiting on the condition variable */
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake all threads waiting on the condition variable */
|
|
|
|
cv_broadcast(&cv.cv_condvar);
|
|
|
|
|
|
|
|
/* Wait until all threads have exited */
|
|
|
|
while ((atomic_read(&cv.cv_condvar.cv_waiters) > 0) || mutex_owner(&cv.cv_mtx))
|
|
|
|
schedule();
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST2_NAME, "Correctly woke all "
|
2008-02-26 20:36:04 +00:00
|
|
|
"%d sleeping threads at once\n", count);
|
|
|
|
|
|
|
|
/* Wake everything for the failure case */
|
|
|
|
cv_destroy(&cv.cv_condvar);
|
2016-01-07 03:05:24 +00:00
|
|
|
|
|
|
|
/* wait for threads to exit */
|
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
|
|
|
if (!IS_ERR(ct[i].ct_thread))
|
|
|
|
kthread_stop(ct[i].ct_thread);
|
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
mutex_destroy(&cv.cv_mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test34_thread(void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
condvar_thr_t *ct = (condvar_thr_t *)arg;
|
|
|
|
condvar_priv_t *cv = ct->ct_cvp;
|
|
|
|
clock_t rc;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
ASSERT(cv->cv_magic == SPLAT_CONDVAR_TEST_MAGIC);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
mutex_enter(&cv->cv_mtx);
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(cv->cv_file, ct->ct_name,
|
2012-11-30 23:46:30 +00:00
|
|
|
"%s thread sleeping with %d waiters\n",
|
|
|
|
ct->ct_thread->comm, atomic_read(&cv->cv_condvar.cv_waiters));
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
/* Sleep no longer than 3 seconds, for this test we should
|
|
|
|
* actually never sleep that long without being woken up. */
|
|
|
|
rc = cv_timedwait(&cv->cv_condvar, &cv->cv_mtx, lbolt + HZ * 3);
|
|
|
|
if (rc == -1) {
|
|
|
|
ct->ct_rc = -ETIMEDOUT;
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(cv->cv_file, ct->ct_name, "%s thread timed out, "
|
2012-11-30 23:46:30 +00:00
|
|
|
"should have been woken\n", ct->ct_thread->comm);
|
2008-02-26 20:36:04 +00:00
|
|
|
} else {
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(cv->cv_file, ct->ct_name,
|
2012-11-30 23:46:30 +00:00
|
|
|
"%s thread woken %d waiters remain\n",
|
|
|
|
ct->ct_thread->comm,
|
|
|
|
atomic_read(&cv->cv_condvar.cv_waiters));
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_exit(&cv->cv_mtx);
|
|
|
|
|
2016-01-07 03:05:24 +00:00
|
|
|
/* wait for main thread reap us */
|
|
|
|
while (!kthread_should_stop())
|
|
|
|
schedule();
|
2008-02-26 20:36:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test3(struct file *file, void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
int i, count = 0, rc = 0;
|
2008-02-27 23:42:31 +00:00
|
|
|
condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
|
2008-02-26 20:36:04 +00:00
|
|
|
condvar_priv_t cv;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
|
2008-02-26 20:36:04 +00:00
|
|
|
cv.cv_file = file;
|
2008-02-27 23:42:31 +00:00
|
|
|
mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
|
2012-04-06 18:29:23 +00:00
|
|
|
cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
/* Create some threads, the exact number isn't important just as
|
|
|
|
* long as we know how many we managed to create and should expect. */
|
2008-02-27 23:42:31 +00:00
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_cvp = &cv;
|
2008-02-27 23:42:31 +00:00
|
|
|
ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME;
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_rc = 0;
|
2014-03-26 13:29:24 +00:00
|
|
|
ct[i].ct_thread = spl_kthread_create(splat_condvar_test34_thread,
|
2012-11-30 23:46:30 +00:00
|
|
|
&ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-11-30 23:46:30 +00:00
|
|
|
if (!IS_ERR(ct[i].ct_thread)) {
|
|
|
|
wake_up_process(ct[i].ct_thread);
|
2008-02-26 20:36:04 +00:00
|
|
|
count++;
|
2012-11-30 23:46:30 +00:00
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until all threads are waiting on the condition variable */
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake a single thread at a time, wait until it exits */
|
|
|
|
for (i = 1; i <= count; i++) {
|
|
|
|
cv_signal(&cv.cv_condvar);
|
|
|
|
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Correct behavior 1 thread woken */
|
|
|
|
if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
|
|
|
|
continue;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to "
|
2008-02-26 20:36:04 +00:00
|
|
|
"wake %d thread but work %d threads woke\n",
|
|
|
|
1, count - atomic_read(&cv.cv_condvar.cv_waiters));
|
|
|
|
rc = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate no waiting thread timed out early */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if (ct[i].ct_rc)
|
|
|
|
rc = ct[i].ct_rc;
|
|
|
|
|
|
|
|
if (!rc)
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke "
|
2008-02-26 20:36:04 +00:00
|
|
|
"%d sleeping threads %d at a time\n", count, 1);
|
|
|
|
|
|
|
|
/* Wait until that last nutex is dropped */
|
|
|
|
while (mutex_owner(&cv.cv_mtx))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake everything for the failure case */
|
|
|
|
cv_broadcast(&cv.cv_condvar);
|
|
|
|
cv_destroy(&cv.cv_condvar);
|
2016-01-07 03:05:24 +00:00
|
|
|
|
|
|
|
/* wait for threads to exit */
|
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
|
|
|
if (!IS_ERR(ct[i].ct_thread))
|
|
|
|
kthread_stop(ct[i].ct_thread);
|
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
mutex_destroy(&cv.cv_mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test4(struct file *file, void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
int i, count = 0, rc = 0;
|
2008-02-27 23:42:31 +00:00
|
|
|
condvar_thr_t ct[SPLAT_CONDVAR_TEST_COUNT];
|
2008-02-26 20:36:04 +00:00
|
|
|
condvar_priv_t cv;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
cv.cv_magic = SPLAT_CONDVAR_TEST_MAGIC;
|
2008-02-26 20:36:04 +00:00
|
|
|
cv.cv_file = file;
|
2008-02-27 23:42:31 +00:00
|
|
|
mutex_init(&cv.cv_mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
|
2012-04-06 18:29:23 +00:00
|
|
|
cv_init(&cv.cv_condvar, NULL, CV_DEFAULT, NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
/* Create some threads, the exact number isn't important just as
|
|
|
|
* long as we know how many we managed to create and should expect. */
|
2008-02-27 23:42:31 +00:00
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_cvp = &cv;
|
2008-02-27 23:42:31 +00:00
|
|
|
ct[i].ct_name = SPLAT_CONDVAR_TEST3_NAME;
|
2008-02-26 20:36:04 +00:00
|
|
|
ct[i].ct_rc = 0;
|
2014-03-26 13:29:24 +00:00
|
|
|
ct[i].ct_thread = spl_kthread_create(splat_condvar_test34_thread,
|
2012-11-30 23:46:30 +00:00
|
|
|
&ct[i], "%s/%d", SPLAT_CONDVAR_TEST_NAME, i);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-11-30 23:46:30 +00:00
|
|
|
if (!IS_ERR(ct[i].ct_thread)) {
|
|
|
|
wake_up_process(ct[i].ct_thread);
|
2008-02-26 20:36:04 +00:00
|
|
|
count++;
|
2012-11-30 23:46:30 +00:00
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until all threads are waiting on the condition variable */
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) != count)
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake a single thread at a time, wait until it exits */
|
|
|
|
for (i = 1; i <= count; i++) {
|
|
|
|
cv_signal(&cv.cv_condvar);
|
|
|
|
|
|
|
|
while (atomic_read(&cv.cv_condvar.cv_waiters) > (count - i))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Correct behavior 1 thread woken */
|
|
|
|
if (atomic_read(&cv.cv_condvar.cv_waiters) == (count - i))
|
|
|
|
continue;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Attempted to "
|
2008-02-26 20:36:04 +00:00
|
|
|
"wake %d thread but work %d threads woke\n",
|
|
|
|
1, count - atomic_read(&cv.cv_condvar.cv_waiters));
|
|
|
|
rc = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate no waiting thread timed out early */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if (ct[i].ct_rc)
|
|
|
|
rc = ct[i].ct_rc;
|
|
|
|
|
|
|
|
if (!rc)
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST3_NAME, "Correctly woke "
|
2008-02-26 20:36:04 +00:00
|
|
|
"%d sleeping threads %d at a time\n", count, 1);
|
|
|
|
|
|
|
|
/* Wait until that last nutex is dropped */
|
|
|
|
while (mutex_owner(&cv.cv_mtx))
|
|
|
|
schedule();
|
|
|
|
|
|
|
|
/* Wake everything for the failure case */
|
|
|
|
cv_broadcast(&cv.cv_condvar);
|
|
|
|
cv_destroy(&cv.cv_condvar);
|
2016-01-07 03:05:24 +00:00
|
|
|
|
|
|
|
/* wait for threads to exit */
|
|
|
|
for (i = 0; i < SPLAT_CONDVAR_TEST_COUNT; i++) {
|
|
|
|
if (!IS_ERR(ct[i].ct_thread))
|
|
|
|
kthread_stop(ct[i].ct_thread);
|
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
mutex_destroy(&cv.cv_mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_test5(struct file *file, void *arg)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
kcondvar_t condvar;
|
|
|
|
kmutex_t mtx;
|
|
|
|
clock_t time_left, time_before, time_after, time_delta;
|
2015-12-22 17:26:10 +00:00
|
|
|
uint64_t whole_delta;
|
|
|
|
uint32_t remain_delta;
|
2008-02-26 20:36:04 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
mutex_init(&mtx, SPLAT_CONDVAR_TEST_NAME, MUTEX_DEFAULT, NULL);
|
2012-04-06 18:29:23 +00:00
|
|
|
cv_init(&condvar, NULL, CV_DEFAULT, NULL);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME, "Thread going to sleep for "
|
2008-02-26 20:36:04 +00:00
|
|
|
"%d second and expecting to be woken by timeout\n", 1);
|
|
|
|
|
|
|
|
/* Allow a 1 second timeout, plenty long to validate correctness. */
|
|
|
|
time_before = lbolt;
|
|
|
|
mutex_enter(&mtx);
|
|
|
|
time_left = cv_timedwait(&condvar, &mtx, lbolt + HZ);
|
|
|
|
mutex_exit(&mtx);
|
|
|
|
time_after = lbolt;
|
|
|
|
time_delta = time_after - time_before; /* XXX - Handle jiffie wrap */
|
|
|
|
whole_delta = time_delta;
|
|
|
|
remain_delta = do_div(whole_delta, HZ);
|
|
|
|
|
|
|
|
if (time_left == -1) {
|
|
|
|
if (time_delta >= HZ) {
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
|
2008-02-26 20:36:04 +00:00
|
|
|
"Thread correctly timed out and was asleep "
|
|
|
|
"for %d.%d seconds (%d second min)\n",
|
2015-12-22 17:26:10 +00:00
|
|
|
(int)whole_delta, (int)remain_delta, 1);
|
2008-02-26 20:36:04 +00:00
|
|
|
} else {
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
|
2008-02-26 20:36:04 +00:00
|
|
|
"Thread correctly timed out but was only "
|
|
|
|
"asleep for %d.%d seconds (%d second "
|
2015-12-22 17:26:10 +00:00
|
|
|
"min)\n", (int)whole_delta,
|
|
|
|
(int)remain_delta, 1);
|
2008-02-26 20:36:04 +00:00
|
|
|
rc = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
} else {
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_vprint(file, SPLAT_CONDVAR_TEST5_NAME,
|
2008-02-26 20:36:04 +00:00
|
|
|
"Thread exited after only %d.%d seconds, it "
|
|
|
|
"did not hit the %d second timeout\n",
|
2015-12-22 17:26:10 +00:00
|
|
|
(int)whole_delta, (int)remain_delta, 1);
|
2008-02-26 20:36:04 +00:00
|
|
|
rc = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
cv_destroy(&condvar);
|
|
|
|
mutex_destroy(&mtx);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_subsystem_t *
|
|
|
|
splat_condvar_init(void)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_subsystem_t *sub;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
sub = kmalloc(sizeof(*sub), GFP_KERNEL);
|
|
|
|
if (sub == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(sub, 0, sizeof(*sub));
|
2008-02-27 23:42:31 +00:00
|
|
|
strncpy(sub->desc.name, SPLAT_CONDVAR_NAME, SPLAT_NAME_SIZE);
|
|
|
|
strncpy(sub->desc.desc, SPLAT_CONDVAR_DESC, SPLAT_DESC_SIZE);
|
2008-02-26 20:36:04 +00:00
|
|
|
INIT_LIST_HEAD(&sub->subsystem_list);
|
|
|
|
INIT_LIST_HEAD(&sub->test_list);
|
|
|
|
spin_lock_init(&sub->test_lock);
|
2008-02-27 23:42:31 +00:00
|
|
|
sub->desc.id = SPLAT_SUBSYSTEM_CONDVAR;
|
|
|
|
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_init(sub, SPLAT_CONDVAR_TEST1_NAME, SPLAT_CONDVAR_TEST1_DESC,
|
2008-02-27 23:42:31 +00:00
|
|
|
SPLAT_CONDVAR_TEST1_ID, splat_condvar_test1);
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_init(sub, SPLAT_CONDVAR_TEST2_NAME, SPLAT_CONDVAR_TEST2_DESC,
|
2008-02-27 23:42:31 +00:00
|
|
|
SPLAT_CONDVAR_TEST2_ID, splat_condvar_test2);
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_init(sub, SPLAT_CONDVAR_TEST3_NAME, SPLAT_CONDVAR_TEST3_DESC,
|
2008-02-27 23:42:31 +00:00
|
|
|
SPLAT_CONDVAR_TEST3_ID, splat_condvar_test3);
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_init(sub, SPLAT_CONDVAR_TEST4_NAME, SPLAT_CONDVAR_TEST4_DESC,
|
2008-02-27 23:42:31 +00:00
|
|
|
SPLAT_CONDVAR_TEST4_ID, splat_condvar_test4);
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_init(sub, SPLAT_CONDVAR_TEST5_NAME, SPLAT_CONDVAR_TEST5_DESC,
|
2008-02-27 23:42:31 +00:00
|
|
|
SPLAT_CONDVAR_TEST5_ID, splat_condvar_test5);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_fini(splat_subsystem_t *sub)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
|
|
|
ASSERT(sub);
|
2016-12-15 02:24:47 +00:00
|
|
|
splat_test_fini(sub, SPLAT_CONDVAR_TEST5_ID);
|
|
|
|
splat_test_fini(sub, SPLAT_CONDVAR_TEST4_ID);
|
|
|
|
splat_test_fini(sub, SPLAT_CONDVAR_TEST3_ID);
|
|
|
|
splat_test_fini(sub, SPLAT_CONDVAR_TEST2_ID);
|
|
|
|
splat_test_fini(sub, SPLAT_CONDVAR_TEST1_ID);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
|
|
|
kfree(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-02-27 23:42:31 +00:00
|
|
|
splat_condvar_id(void) {
|
|
|
|
return SPLAT_SUBSYSTEM_CONDVAR;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|