@@ -29,6 +29,9 @@
|
||||
|
||||
/*
|
||||
* Synchronization test code.
|
||||
*
|
||||
* All the contents of this file are overwritten during automated
|
||||
* testing. Please consider this before changing anything in this file.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
@@ -37,185 +40,303 @@
|
||||
#include <thread.h>
|
||||
#include <synch.h>
|
||||
#include <test.h>
|
||||
#include <kern/test161.h>
|
||||
#include <spinlock.h>
|
||||
|
||||
#define CREATELOOPS 8
|
||||
#define NSEMLOOPS 63
|
||||
#define NLOCKLOOPS 120
|
||||
#define NCVLOOPS 5
|
||||
#define NTHREADS 32
|
||||
#define SYNCHTEST_YIELDER_MAX 16
|
||||
|
||||
static volatile unsigned long testval1;
|
||||
static volatile unsigned long testval2;
|
||||
static volatile unsigned long testval3;
|
||||
static struct semaphore *testsem;
|
||||
static struct lock *testlock;
|
||||
static struct cv *testcv;
|
||||
static struct semaphore *donesem;
|
||||
static volatile int32_t testval4;
|
||||
|
||||
static struct semaphore *testsem = NULL;
|
||||
static struct semaphore *testsem2 = NULL;
|
||||
static struct lock *testlock = NULL;
|
||||
static struct lock *testlock2 = NULL;
|
||||
static struct cv *testcv = NULL;
|
||||
static struct semaphore *donesem = NULL;
|
||||
|
||||
struct spinlock status_lock;
|
||||
static bool test_status = TEST161_FAIL;
|
||||
|
||||
static unsigned long semtest_current;
|
||||
|
||||
static
|
||||
void
|
||||
inititems(void)
|
||||
{
|
||||
if (testsem==NULL) {
|
||||
testsem = sem_create("testsem", 2);
|
||||
if (testsem == NULL) {
|
||||
panic("synchtest: sem_create failed\n");
|
||||
}
|
||||
}
|
||||
if (testlock==NULL) {
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("synchtest: lock_create failed\n");
|
||||
}
|
||||
}
|
||||
if (testcv==NULL) {
|
||||
testcv = cv_create("testlock");
|
||||
if (testcv == NULL) {
|
||||
panic("synchtest: cv_create failed\n");
|
||||
}
|
||||
}
|
||||
if (donesem==NULL) {
|
||||
donesem = sem_create("donesem", 0);
|
||||
if (donesem == NULL) {
|
||||
panic("synchtest: sem_create failed\n");
|
||||
}
|
||||
bool
|
||||
failif(bool condition) {
|
||||
if (condition) {
|
||||
spinlock_acquire(&status_lock);
|
||||
test_status = TEST161_FAIL;
|
||||
spinlock_release(&status_lock);
|
||||
}
|
||||
return condition;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
semtestthread(void *junk, unsigned long num)
|
||||
{
|
||||
int i;
|
||||
(void)junk;
|
||||
|
||||
int i;
|
||||
|
||||
random_yielder(4);
|
||||
|
||||
/*
|
||||
* Only one of these should print at a time.
|
||||
*/
|
||||
P(testsem);
|
||||
kprintf("Thread %2lu: ", num);
|
||||
semtest_current = num;
|
||||
|
||||
kprintf_n("Thread %2lu: ", num);
|
||||
for (i=0; i<NSEMLOOPS; i++) {
|
||||
kprintf("%c", (int)num+64);
|
||||
kprintf_t(".");
|
||||
kprintf_n("%2lu", num);
|
||||
random_yielder(4);
|
||||
failif((semtest_current != num));
|
||||
}
|
||||
kprintf("\n");
|
||||
kprintf_n("\n");
|
||||
|
||||
V(donesem);
|
||||
}
|
||||
|
||||
int
|
||||
semtest(int nargs, char **args)
|
||||
{
|
||||
int i, result;
|
||||
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
inititems();
|
||||
kprintf("Starting semaphore test...\n");
|
||||
kprintf("If this hangs, it's broken: ");
|
||||
int i, result;
|
||||
|
||||
kprintf_n("Starting sem1...\n");
|
||||
for (i=0; i<CREATELOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
testsem = sem_create("testsem", 2);
|
||||
if (testsem == NULL) {
|
||||
panic("sem1: sem_create failed\n");
|
||||
}
|
||||
donesem = sem_create("donesem", 0);
|
||||
if (donesem == NULL) {
|
||||
panic("sem1: sem_create failed\n");
|
||||
}
|
||||
if (i != CREATELOOPS - 1) {
|
||||
sem_destroy(testsem);
|
||||
sem_destroy(donesem);
|
||||
}
|
||||
}
|
||||
spinlock_init(&status_lock);
|
||||
test_status = TEST161_SUCCESS;
|
||||
|
||||
kprintf_n("If this hangs, it's broken: ");
|
||||
P(testsem);
|
||||
P(testsem);
|
||||
kprintf("ok\n");
|
||||
kprintf_n("OK\n");
|
||||
kprintf_t(".");
|
||||
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
kprintf_t(".");
|
||||
result = thread_fork("semtest", NULL, semtestthread, NULL, i);
|
||||
if (result) {
|
||||
panic("semtest: thread_fork failed: %s\n",
|
||||
panic("sem1: thread_fork failed: %s\n",
|
||||
strerror(result));
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
kprintf_t(".");
|
||||
V(testsem);
|
||||
P(donesem);
|
||||
}
|
||||
|
||||
/* so we can run it again */
|
||||
V(testsem);
|
||||
V(testsem);
|
||||
sem_destroy(testsem);
|
||||
sem_destroy(donesem);
|
||||
testsem = donesem = NULL;
|
||||
|
||||
kprintf_t("\n");
|
||||
success(test_status, SECRET, "sem1");
|
||||
|
||||
kprintf("Semaphore test done.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
fail(unsigned long num, const char *msg)
|
||||
{
|
||||
kprintf("thread %lu: Mismatch on %s\n", num, msg);
|
||||
kprintf("Test failed\n");
|
||||
|
||||
lock_release(testlock);
|
||||
|
||||
V(donesem);
|
||||
thread_exit();
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
locktestthread(void *junk, unsigned long num)
|
||||
{
|
||||
int i;
|
||||
(void)junk;
|
||||
|
||||
int i;
|
||||
|
||||
for (i=0; i<NLOCKLOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
lock_acquire(testlock);
|
||||
random_yielder(4);
|
||||
|
||||
testval1 = num;
|
||||
testval2 = num*num;
|
||||
testval3 = num%3;
|
||||
|
||||
if (testval2 != testval1*testval1) {
|
||||
fail(num, "testval2/testval1");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (testval2%3 != (testval3*testval3)%3) {
|
||||
fail(num, "testval2/testval3");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (testval3 != testval1%3) {
|
||||
fail(num, "testval3/testval1");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (testval1 != num) {
|
||||
fail(num, "testval1/num");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (testval2 != num*num) {
|
||||
fail(num, "testval2/num");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (testval3 != num%3) {
|
||||
fail(num, "testval3/num");
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
if (!(lock_do_i_hold(testlock))) {
|
||||
goto fail;
|
||||
}
|
||||
random_yielder(4);
|
||||
|
||||
lock_release(testlock);
|
||||
}
|
||||
|
||||
/* Check for solutions that don't track ownership properly */
|
||||
|
||||
for (i=0; i<NLOCKLOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
if (lock_do_i_hold(testlock)) {
|
||||
goto fail2;
|
||||
}
|
||||
}
|
||||
|
||||
V(donesem);
|
||||
return;
|
||||
|
||||
fail:
|
||||
lock_release(testlock);
|
||||
fail2:
|
||||
failif(true);
|
||||
V(donesem);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
locktest(int nargs, char **args)
|
||||
{
|
||||
int i, result;
|
||||
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
inititems();
|
||||
kprintf("Starting lock test...\n");
|
||||
int i, result;
|
||||
|
||||
kprintf_n("Starting lt1...\n");
|
||||
for (i=0; i<CREATELOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("lt1: lock_create failed\n");
|
||||
}
|
||||
donesem = sem_create("donesem", 0);
|
||||
if (donesem == NULL) {
|
||||
panic("lt1: sem_create failed\n");
|
||||
}
|
||||
if (i != CREATELOOPS - 1) {
|
||||
lock_destroy(testlock);
|
||||
sem_destroy(donesem);
|
||||
}
|
||||
}
|
||||
spinlock_init(&status_lock);
|
||||
test_status = TEST161_SUCCESS;
|
||||
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
result = thread_fork("synchtest", NULL, locktestthread,
|
||||
NULL, i);
|
||||
kprintf_t(".");
|
||||
result = thread_fork("synchtest", NULL, locktestthread, NULL, i);
|
||||
if (result) {
|
||||
panic("locktest: thread_fork failed: %s\n",
|
||||
strerror(result));
|
||||
panic("lt1: thread_fork failed: %s\n", strerror(result));
|
||||
}
|
||||
}
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
kprintf_t(".");
|
||||
P(donesem);
|
||||
}
|
||||
|
||||
kprintf("Lock test done.\n");
|
||||
lock_destroy(testlock);
|
||||
sem_destroy(donesem);
|
||||
testlock = NULL;
|
||||
donesem = NULL;
|
||||
|
||||
kprintf_t("\n");
|
||||
success(test_status, SECRET, "lt1");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
locktest2(int nargs, char **args) {
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
kprintf_n("Starting lt2...\n");
|
||||
kprintf_n("(This test panics on success!)\n");
|
||||
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("lt2: lock_create failed\n");
|
||||
}
|
||||
|
||||
secprintf(SECRET, "Should panic...", "lt2");
|
||||
lock_release(testlock);
|
||||
|
||||
/* Should not get here on success. */
|
||||
|
||||
success(TEST161_FAIL, SECRET, "lt2");
|
||||
|
||||
lock_destroy(testlock);
|
||||
testlock = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
locktest3(int nargs, char **args) {
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
kprintf_n("Starting lt3...\n");
|
||||
kprintf_n("(This test panics on success!)\n");
|
||||
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("lt3: lock_create failed\n");
|
||||
}
|
||||
|
||||
secprintf(SECRET, "Should panic...", "lt3");
|
||||
lock_acquire(testlock);
|
||||
lock_destroy(testlock);
|
||||
|
||||
/* Should not get here on success. */
|
||||
|
||||
success(TEST161_FAIL, SECRET, "lt3");
|
||||
|
||||
testlock = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -224,35 +345,38 @@ static
|
||||
void
|
||||
cvtestthread(void *junk, unsigned long num)
|
||||
{
|
||||
(void)junk;
|
||||
|
||||
int i;
|
||||
volatile int j;
|
||||
struct timespec ts1, ts2;
|
||||
|
||||
(void)junk;
|
||||
|
||||
for (i=0; i<NCVLOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
lock_acquire(testlock);
|
||||
while (testval1 != num) {
|
||||
testval2 = 0;
|
||||
random_yielder(4);
|
||||
gettime(&ts1);
|
||||
cv_wait(testcv, testlock);
|
||||
gettime(&ts2);
|
||||
random_yielder(4);
|
||||
|
||||
/* ts2 -= ts1 */
|
||||
timespec_sub(&ts2, &ts1, &ts2);
|
||||
|
||||
/* Require at least 2000 cpu cycles (we're 25mhz) */
|
||||
if (ts2.tv_sec == 0 && ts2.tv_nsec < 40*2000) {
|
||||
kprintf("cv_wait took only %u ns\n",
|
||||
ts2.tv_nsec);
|
||||
kprintf("That's too fast... you must be "
|
||||
"busy-looping\n");
|
||||
kprintf_n("cv_wait took only %u ns\n", ts2.tv_nsec);
|
||||
kprintf_n("That's too fast... you must be busy-looping\n");
|
||||
failif(true);
|
||||
V(donesem);
|
||||
thread_exit();
|
||||
}
|
||||
|
||||
testval2 = 0xFFFFFFFF;
|
||||
}
|
||||
kprintf("Thread %lu\n", num);
|
||||
testval1 = (testval1 + NTHREADS - 1)%NTHREADS;
|
||||
testval2 = num;
|
||||
|
||||
/*
|
||||
* loop a little while to make sure we can measure the
|
||||
@@ -260,7 +384,13 @@ cvtestthread(void *junk, unsigned long num)
|
||||
*/
|
||||
for (j=0; j<3000; j++);
|
||||
|
||||
random_yielder(4);
|
||||
cv_broadcast(testcv, testlock);
|
||||
random_yielder(4);
|
||||
failif((testval1 != testval2));
|
||||
|
||||
kprintf_n("Thread %lu\n", testval2);
|
||||
testval1 = (testval1 + NTHREADS - 1) % NTHREADS;
|
||||
lock_release(testlock);
|
||||
}
|
||||
V(donesem);
|
||||
@@ -269,30 +399,57 @@ cvtestthread(void *junk, unsigned long num)
|
||||
int
|
||||
cvtest(int nargs, char **args)
|
||||
{
|
||||
|
||||
int i, result;
|
||||
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
inititems();
|
||||
kprintf("Starting CV test...\n");
|
||||
kprintf("Threads should print out in reverse order.\n");
|
||||
int i, result;
|
||||
|
||||
kprintf_n("Starting cvt1...\n");
|
||||
for (i=0; i<CREATELOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("cvt1: lock_create failed\n");
|
||||
}
|
||||
testcv = cv_create("testcv");
|
||||
if (testcv == NULL) {
|
||||
panic("cvt1: cv_create failed\n");
|
||||
}
|
||||
donesem = sem_create("donesem", 0);
|
||||
if (donesem == NULL) {
|
||||
panic("cvt1: sem_create failed\n");
|
||||
}
|
||||
if (i != CREATELOOPS - 1) {
|
||||
lock_destroy(testlock);
|
||||
cv_destroy(testcv);
|
||||
sem_destroy(donesem);
|
||||
}
|
||||
}
|
||||
spinlock_init(&status_lock);
|
||||
test_status = TEST161_SUCCESS;
|
||||
|
||||
testval1 = NTHREADS-1;
|
||||
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
result = thread_fork("synchtest", NULL, cvtestthread, NULL, i);
|
||||
kprintf_t(".");
|
||||
result = thread_fork("cvt1", NULL, cvtestthread, NULL, (long unsigned) i);
|
||||
if (result) {
|
||||
panic("cvtest: thread_fork failed: %s\n",
|
||||
strerror(result));
|
||||
panic("cvt1: thread_fork failed: %s\n", strerror(result));
|
||||
}
|
||||
}
|
||||
for (i=0; i<NTHREADS; i++) {
|
||||
kprintf_t(".");
|
||||
P(donesem);
|
||||
}
|
||||
|
||||
kprintf("CV test done\n");
|
||||
lock_destroy(testlock);
|
||||
cv_destroy(testcv);
|
||||
sem_destroy(donesem);
|
||||
testlock = NULL;
|
||||
testcv = NULL;
|
||||
donesem = NULL;
|
||||
|
||||
kprintf_t("\n");
|
||||
success(test_status, SECRET, "cvt1");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -318,19 +475,28 @@ static
|
||||
void
|
||||
sleepthread(void *junk1, unsigned long junk2)
|
||||
{
|
||||
unsigned i, j;
|
||||
|
||||
(void)junk1;
|
||||
(void)junk2;
|
||||
|
||||
unsigned i, j;
|
||||
|
||||
random_yielder(4);
|
||||
|
||||
for (j=0; j<NLOOPS; j++) {
|
||||
kprintf_t(".");
|
||||
for (i=0; i<NCVS; i++) {
|
||||
lock_acquire(testlocks[i]);
|
||||
random_yielder(4);
|
||||
V(gatesem);
|
||||
random_yielder(4);
|
||||
spinlock_acquire(&status_lock);
|
||||
testval4++;
|
||||
spinlock_release(&status_lock);
|
||||
cv_wait(testcvs[i], testlocks[i]);
|
||||
random_yielder(4);
|
||||
lock_release(testlocks[i]);
|
||||
}
|
||||
kprintf("sleepthread: %u\n", j);
|
||||
kprintf_n("sleepthread: %u\n", j);
|
||||
}
|
||||
V(exitsem);
|
||||
}
|
||||
@@ -339,19 +505,28 @@ static
|
||||
void
|
||||
wakethread(void *junk1, unsigned long junk2)
|
||||
{
|
||||
unsigned i, j;
|
||||
|
||||
(void)junk1;
|
||||
(void)junk2;
|
||||
|
||||
unsigned i, j;
|
||||
|
||||
random_yielder(4);
|
||||
|
||||
for (j=0; j<NLOOPS; j++) {
|
||||
kprintf_t(".");
|
||||
for (i=0; i<NCVS; i++) {
|
||||
random_yielder(4);
|
||||
P(gatesem);
|
||||
random_yielder(4);
|
||||
lock_acquire(testlocks[i]);
|
||||
random_yielder(4);
|
||||
testval4--;
|
||||
failif((testval4 != 0));
|
||||
cv_signal(testcvs[i], testlocks[i]);
|
||||
random_yielder(4);
|
||||
lock_release(testlocks[i]);
|
||||
}
|
||||
kprintf("wakethread: %u\n", j);
|
||||
kprintf_n("wakethread: %u\n", j);
|
||||
}
|
||||
V(exitsem);
|
||||
}
|
||||
@@ -359,30 +534,44 @@ wakethread(void *junk1, unsigned long junk2)
|
||||
int
|
||||
cvtest2(int nargs, char **args)
|
||||
{
|
||||
unsigned i;
|
||||
int result;
|
||||
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
unsigned i;
|
||||
int result;
|
||||
|
||||
kprintf_n("Starting cvt2...\n");
|
||||
for (i=0; i<CREATELOOPS; i++) {
|
||||
kprintf_t(".");
|
||||
gatesem = sem_create("gatesem", 0);
|
||||
if (gatesem == NULL) {
|
||||
panic("cvt2: sem_create failed\n");
|
||||
}
|
||||
exitsem = sem_create("exitsem", 0);
|
||||
if (exitsem == NULL) {
|
||||
panic("cvt2: sem_create failed\n");
|
||||
}
|
||||
if (i != CREATELOOPS - 1) {
|
||||
sem_destroy(gatesem);
|
||||
sem_destroy(exitsem);
|
||||
}
|
||||
}
|
||||
for (i=0; i<NCVS; i++) {
|
||||
kprintf_t(".");
|
||||
testlocks[i] = lock_create("cvtest2 lock");
|
||||
testcvs[i] = cv_create("cvtest2 cv");
|
||||
}
|
||||
gatesem = sem_create("gatesem", 0);
|
||||
exitsem = sem_create("exitsem", 0);
|
||||
spinlock_init(&status_lock);
|
||||
test_status = TEST161_SUCCESS;
|
||||
|
||||
kprintf("cvtest2...\n");
|
||||
|
||||
result = thread_fork("cvtest2", NULL, sleepthread, NULL, 0);
|
||||
result = thread_fork("cvt2", NULL, sleepthread, NULL, 0);
|
||||
if (result) {
|
||||
panic("cvtest2: thread_fork failed\n");
|
||||
panic("cvt2: thread_fork failed\n");
|
||||
}
|
||||
result = thread_fork("cvtest2", NULL, wakethread, NULL, 0);
|
||||
result = thread_fork("cvt2", NULL, wakethread, NULL, 0);
|
||||
if (result) {
|
||||
panic("cvtest2: thread_fork failed\n");
|
||||
panic("cvt2: thread_fork failed\n");
|
||||
}
|
||||
|
||||
P(exitsem);
|
||||
P(exitsem);
|
||||
|
||||
@@ -390,12 +579,194 @@ cvtest2(int nargs, char **args)
|
||||
sem_destroy(gatesem);
|
||||
exitsem = gatesem = NULL;
|
||||
for (i=0; i<NCVS; i++) {
|
||||
kprintf_t(".");
|
||||
lock_destroy(testlocks[i]);
|
||||
cv_destroy(testcvs[i]);
|
||||
testlocks[i] = NULL;
|
||||
testcvs[i] = NULL;
|
||||
}
|
||||
|
||||
kprintf("cvtest2 done\n");
|
||||
kprintf_t("\n");
|
||||
success(test_status, SECRET, "cvt2");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
cvtest3(int nargs, char **args) {
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
kprintf_n("Starting cvt3...\n");
|
||||
kprintf_n("(This test panics on success!)\n");
|
||||
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("cvt3: lock_create failed\n");
|
||||
}
|
||||
testcv = cv_create("testcv");
|
||||
if (testcv == NULL) {
|
||||
panic("cvt3: cv_create failed\n");
|
||||
}
|
||||
|
||||
secprintf(SECRET, "Should panic...", "cvt3");
|
||||
cv_wait(testcv, testlock);
|
||||
|
||||
/* Should not get here on success. */
|
||||
|
||||
success(TEST161_FAIL, SECRET, "cvt3");
|
||||
|
||||
lock_destroy(testlock);
|
||||
cv_destroy(testcv);
|
||||
testcv = NULL;
|
||||
testlock = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
cvtest4(int nargs, char **args) {
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
kprintf_n("Starting cvt4...\n");
|
||||
kprintf_n("(This test panics on success!)\n");
|
||||
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("cvt4: lock_create failed\n");
|
||||
}
|
||||
testcv = cv_create("testcv");
|
||||
if (testcv == NULL) {
|
||||
panic("cvt4: cv_create failed\n");
|
||||
}
|
||||
|
||||
secprintf(SECRET, "Should panic...", "cvt4");
|
||||
cv_broadcast(testcv, testlock);
|
||||
|
||||
/* Should not get here on success. */
|
||||
|
||||
success(TEST161_FAIL, SECRET, "cvt4");
|
||||
|
||||
lock_destroy(testlock);
|
||||
cv_destroy(testcv);
|
||||
testcv = NULL;
|
||||
testlock = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
sleeperthread(void *junk1, unsigned long junk2) {
|
||||
(void)junk1;
|
||||
(void)junk2;
|
||||
|
||||
random_yielder(4);
|
||||
lock_acquire(testlock);
|
||||
random_yielder(4);
|
||||
failif((testval1 != 0));
|
||||
testval1 = 1;
|
||||
cv_signal(testcv, testlock);
|
||||
|
||||
random_yielder(4);
|
||||
cv_wait(testcv, testlock);
|
||||
failif((testval1 != 3));
|
||||
testval1 = 4;
|
||||
random_yielder(4);
|
||||
lock_release(testlock);
|
||||
random_yielder(4);
|
||||
|
||||
V(exitsem);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
wakerthread(void *junk1, unsigned long junk2) {
|
||||
(void)junk1;
|
||||
(void)junk2;
|
||||
|
||||
random_yielder(4);
|
||||
lock_acquire(testlock2);
|
||||
failif((testval1 != 2));
|
||||
testval1 = 3;
|
||||
|
||||
random_yielder(4);
|
||||
cv_signal(testcv, testlock2);
|
||||
random_yielder(4);
|
||||
lock_release(testlock2);
|
||||
random_yielder(4);
|
||||
|
||||
V(exitsem);
|
||||
}
|
||||
|
||||
int
|
||||
cvtest5(int nargs, char **args) {
|
||||
(void)nargs;
|
||||
(void)args;
|
||||
|
||||
int result;
|
||||
|
||||
kprintf_n("Starting cvt5...\n");
|
||||
|
||||
testlock = lock_create("testlock");
|
||||
if (testlock == NULL) {
|
||||
panic("cvt5: lock_create failed\n");
|
||||
}
|
||||
testlock2 = lock_create("testlock2");
|
||||
if (testlock == NULL) {
|
||||
panic("cvt5: lock_create failed\n");
|
||||
}
|
||||
testcv = cv_create("testcv");
|
||||
if (testcv == NULL) {
|
||||
panic("cvt5: cv_create failed\n");
|
||||
}
|
||||
exitsem = sem_create("exitsem", 0);
|
||||
if (exitsem == NULL) {
|
||||
panic("cvt5: sem_create failed\n");
|
||||
}
|
||||
spinlock_init(&status_lock);
|
||||
test_status = TEST161_SUCCESS;
|
||||
testval1 = 0;
|
||||
|
||||
lock_acquire(testlock);
|
||||
lock_acquire(testlock2);
|
||||
|
||||
result = thread_fork("cvt5", NULL, sleeperthread, NULL, 0);
|
||||
if (result) {
|
||||
panic("cvt5: thread_fork failed\n");
|
||||
}
|
||||
result = thread_fork("cvt5", NULL, wakerthread, NULL, 0);
|
||||
if (result) {
|
||||
panic("cvt5: thread_fork failed\n");
|
||||
}
|
||||
|
||||
random_yielder(4);
|
||||
cv_wait(testcv, testlock);
|
||||
failif((testval1 != 1));
|
||||
testval1 = 2;
|
||||
random_yielder(4);
|
||||
lock_release(testlock);
|
||||
random_yielder(4);
|
||||
lock_release(testlock2);
|
||||
|
||||
P(exitsem);
|
||||
P(exitsem);
|
||||
failif((testval1 != 4));
|
||||
|
||||
sem_destroy(exitsem);
|
||||
cv_destroy(testcv);
|
||||
lock_destroy(testlock2);
|
||||
lock_destroy(testlock);
|
||||
|
||||
success(test_status, SECRET, "cvt5");
|
||||
|
||||
exitsem = NULL;
|
||||
testcv = NULL;
|
||||
testlock2 = NULL;
|
||||
testlock = NULL;
|
||||
testsem2 = NULL;
|
||||
testsem = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user