Nearing done with ASST1 changes.
This commit is contained in:
parent
71f0074f44
commit
27b85a44ab
@ -45,6 +45,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#undef SECRET_TESTING
|
#undef SECRET_TESTING
|
||||||
#define SECRET "TEST"
|
#define SECRET "SECRET"
|
||||||
|
|
||||||
#endif /* _SECRET_H_ */
|
#endif /* _SECRET_H_ */
|
||||||
|
@ -25,38 +25,12 @@
|
|||||||
static uint32_t startcount;
|
static uint32_t startcount;
|
||||||
static struct lock *testlock;
|
static struct lock *testlock;
|
||||||
static struct cv *startcv;
|
static struct cv *startcv;
|
||||||
static struct cv *endcv;
|
static struct semaphore *startsem;
|
||||||
static struct semaphore *endsem;
|
static struct semaphore *endsem;
|
||||||
|
|
||||||
struct spinlock status_lock;
|
struct spinlock status_lock;
|
||||||
static bool test_status = FAIL;
|
static bool test_status = FAIL;
|
||||||
|
const char *test_message;
|
||||||
static
|
|
||||||
void
|
|
||||||
inititems(uint32_t count)
|
|
||||||
{
|
|
||||||
startcount = count;
|
|
||||||
|
|
||||||
if (testlock==NULL) {
|
|
||||||
testlock = lock_create("testlock");
|
|
||||||
if (testlock == NULL) {
|
|
||||||
panic("synchprobs: lock_create failed\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (startcv==NULL) {
|
|
||||||
startcv = cv_create("startcv");
|
|
||||||
if (startcv == NULL) {
|
|
||||||
panic("synchprobs: cv_create failed\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (endsem==NULL) {
|
|
||||||
endsem = sem_create("endsem", 0);
|
|
||||||
if (endsem == NULL) {
|
|
||||||
panic("synchprobs: sem_create failed\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
spinlock_init(&status_lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper function to initialize the thread pool.
|
* Helper function to initialize the thread pool.
|
||||||
@ -66,6 +40,7 @@ void
|
|||||||
initialize_thread(volatile void* threads[], uint32_t index) {
|
initialize_thread(volatile void* threads[], uint32_t index) {
|
||||||
if (threads[index] != NULL) {
|
if (threads[index] != NULL) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: incorrect thread type";
|
||||||
}
|
}
|
||||||
threads[index] = curthread->t_stack;
|
threads[index] = curthread->t_stack;
|
||||||
}
|
}
|
||||||
@ -78,6 +53,7 @@ void
|
|||||||
check_thread(volatile void* threads[], uint32_t index) {
|
check_thread(volatile void* threads[], uint32_t index) {
|
||||||
if (threads[index] != curthread->t_stack) {
|
if (threads[index] != curthread->t_stack) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: incorrect thread type";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,6 +89,7 @@ void
|
|||||||
check_role(uint32_t index, int role) {
|
check_role(uint32_t index, int role) {
|
||||||
if (whale_roles[index] != role) {
|
if (whale_roles[index] != role) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: incorrect role";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,10 +102,8 @@ male_wrapper(void * unused1, unsigned long index) {
|
|||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
initialize_thread(whale_threads, (uint32_t)index);
|
initialize_thread(whale_threads, (uint32_t)index);
|
||||||
whale_roles[index] = MALE;
|
whale_roles[index] = MALE;
|
||||||
startcount--;
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
male((uint32_t)index);
|
male((uint32_t)index);
|
||||||
V(endsem);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -139,12 +114,12 @@ male_start(uint32_t index) {
|
|||||||
check_thread(whale_threads, index);
|
check_thread(whale_threads, index);
|
||||||
check_role(index, MALE);
|
check_role(index, MALE);
|
||||||
male_start_count++;
|
male_start_count++;
|
||||||
cv_signal(startcv, testlock);
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s starting\n", curthread->t_name);
|
kprintf_n("%s starting\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
|
V(startsem);
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
male_end(uint32_t index) {
|
male_end(uint32_t index) {
|
||||||
@ -152,13 +127,13 @@ male_end(uint32_t index) {
|
|||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
check_thread(whale_threads, index);
|
check_thread(whale_threads, index);
|
||||||
check_role(index, MALE);
|
check_role(index, MALE);
|
||||||
cv_signal(endcv, testlock);
|
|
||||||
male_end_count++;
|
male_end_count++;
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s ending\n", curthread->t_name);
|
kprintf_n("%s ending\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
|
V(endsem);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
@ -170,10 +145,8 @@ female_wrapper(void * unused1, unsigned long index) {
|
|||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
initialize_thread(whale_threads, (uint32_t)index);
|
initialize_thread(whale_threads, (uint32_t)index);
|
||||||
whale_roles[index] = FEMALE;
|
whale_roles[index] = FEMALE;
|
||||||
startcount--;
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
female((uint32_t)index);
|
female((uint32_t)index);
|
||||||
V(endsem);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -184,12 +157,12 @@ female_start(uint32_t index) {
|
|||||||
check_thread(whale_threads, index);
|
check_thread(whale_threads, index);
|
||||||
check_role(index, FEMALE);
|
check_role(index, FEMALE);
|
||||||
female_start_count++;
|
female_start_count++;
|
||||||
cv_signal(startcv, testlock);
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s starting\n", curthread->t_name);
|
kprintf_n("%s starting\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
|
V(startsem);
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
female_end(uint32_t index) {
|
female_end(uint32_t index) {
|
||||||
@ -197,13 +170,13 @@ female_end(uint32_t index) {
|
|||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
check_thread(whale_threads, index);
|
check_thread(whale_threads, index);
|
||||||
check_role(index, FEMALE);
|
check_role(index, FEMALE);
|
||||||
cv_signal(endcv, testlock);
|
|
||||||
female_end_count++;
|
female_end_count++;
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s ending\n", curthread->t_name);
|
kprintf_n("%s ending\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
|
V(endsem);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
@ -215,27 +188,25 @@ matchmaker_wrapper(void * unused1, unsigned long index) {
|
|||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
initialize_thread(whale_threads, (uint32_t)index);
|
initialize_thread(whale_threads, (uint32_t)index);
|
||||||
whale_roles[index] = MATCHMAKER;
|
whale_roles[index] = MATCHMAKER;
|
||||||
startcount--;
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
matchmaker((uint32_t)index);
|
matchmaker((uint32_t)index);
|
||||||
V(endsem);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
matchmaker_start(uint32_t index) {
|
matchmaker_start(uint32_t index) {
|
||||||
(void)index;
|
(void)index;
|
||||||
|
P(matcher_sem);
|
||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
check_thread(whale_threads, index);
|
check_thread(whale_threads, index);
|
||||||
check_role(index, MATCHMAKER);
|
check_role(index, MATCHMAKER);
|
||||||
matchmaker_start_count++;
|
matchmaker_start_count++;
|
||||||
cv_signal(startcv, testlock);
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s starting\n", curthread->t_name);
|
kprintf_n("%s starting\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
P(matcher_sem);
|
V(startsem);
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
matchmaker_end(uint32_t index) {
|
matchmaker_end(uint32_t index) {
|
||||||
@ -251,12 +222,12 @@ matchmaker_end(uint32_t index) {
|
|||||||
|
|
||||||
match_count++;
|
match_count++;
|
||||||
matchmaker_end_count++;
|
matchmaker_end_count++;
|
||||||
cv_signal(endcv, testlock);
|
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
random_yielder(PROBLEMS_MAX_YIELDER);
|
random_yielder(PROBLEMS_MAX_YIELDER);
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
kprintf_n("%s ending\n", curthread->t_name);
|
kprintf_n("%s ending\n", curthread->t_name);
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
|
V(endsem);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
@ -265,6 +236,7 @@ check_zero(int count, const char *name) {
|
|||||||
(void)name;
|
(void)name;
|
||||||
if (count != 0) {
|
if (count != 0) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: not all threads completed";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +247,8 @@ whalemating(int nargs, char **args) {
|
|||||||
|
|
||||||
int i, j, err = 0;
|
int i, j, err = 0;
|
||||||
char name[32];
|
char name[32];
|
||||||
|
bool loop_status;
|
||||||
|
int total_count = 0;
|
||||||
|
|
||||||
male_start_count = 0 ;
|
male_start_count = 0 ;
|
||||||
male_end_count = 0 ;
|
male_end_count = 0 ;
|
||||||
@ -284,18 +258,13 @@ whalemating(int nargs, char **args) {
|
|||||||
matchmaker_end_count = 0;
|
matchmaker_end_count = 0;
|
||||||
match_count = 0;
|
match_count = 0;
|
||||||
|
|
||||||
startcount = 3 * NMATING;
|
|
||||||
testlock = lock_create("testlock");
|
testlock = lock_create("testlock");
|
||||||
if (testlock == NULL) {
|
if (testlock == NULL) {
|
||||||
panic("sp1: lock_create failed\n");
|
panic("sp1: lock_create failed\n");
|
||||||
}
|
}
|
||||||
startcv = cv_create("startcv");
|
startsem = sem_create("startsem", 0);
|
||||||
if (startcv == NULL) {
|
if (startsem == NULL) {
|
||||||
panic("sp1: cv_create failed\n");
|
panic("sp1: sem_create failed\n");
|
||||||
}
|
|
||||||
endcv = cv_create("endcv");
|
|
||||||
if (endcv == NULL) {
|
|
||||||
panic("sp1: cv_create failed\n");
|
|
||||||
}
|
}
|
||||||
endsem = sem_create("endsem", 0);
|
endsem = sem_create("endsem", 0);
|
||||||
if (endsem == NULL) {
|
if (endsem == NULL) {
|
||||||
@ -306,7 +275,8 @@ whalemating(int nargs, char **args) {
|
|||||||
panic("sp1: sem_create failed\n");
|
panic("sp1: sem_create failed\n");
|
||||||
}
|
}
|
||||||
spinlock_init(&status_lock);
|
spinlock_init(&status_lock);
|
||||||
test_status = FAIL;
|
test_status = SUCCESS;
|
||||||
|
test_message = "";
|
||||||
|
|
||||||
whalemating_init();
|
whalemating_init();
|
||||||
|
|
||||||
@ -326,6 +296,7 @@ whalemating(int nargs, char **args) {
|
|||||||
err = thread_fork(name, NULL, female_wrapper, NULL, index);
|
err = thread_fork(name, NULL, female_wrapper, NULL, index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
total_count += 1;
|
||||||
if (err) {
|
if (err) {
|
||||||
panic("whalemating: thread_fork failed: (%s)\n", strerror(err));
|
panic("whalemating: thread_fork failed: (%s)\n", strerror(err));
|
||||||
}
|
}
|
||||||
@ -335,14 +306,12 @@ whalemating(int nargs, char **args) {
|
|||||||
/* Wait for males and females to start. */
|
/* Wait for males and females to start. */
|
||||||
for (i = 0; i < NMATING * 2; i++) {
|
for (i = 0; i < NMATING * 2; i++) {
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
lock_acquire(testlock);
|
P(startsem);
|
||||||
cv_wait(startcv, testlock);
|
|
||||||
lock_release(testlock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure nothing is happening... */
|
/* Make sure nothing is happening... */
|
||||||
bool loop_status = SUCCESS;
|
loop_status = SUCCESS;
|
||||||
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; ) {
|
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; i++) {
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
@ -354,6 +323,7 @@ whalemating(int nargs, char **args) {
|
|||||||
}
|
}
|
||||||
if (loop_status == FAIL) {
|
if (loop_status == FAIL) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: uncoordinated matchmaking is occurring";
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,6 +337,7 @@ whalemating(int nargs, char **args) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
panic("whalemating: thread_fork failed: (%s)\n", strerror(err));
|
panic("whalemating: thread_fork failed: (%s)\n", strerror(err));
|
||||||
}
|
}
|
||||||
|
total_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -380,26 +351,26 @@ whalemating(int nargs, char **args) {
|
|||||||
}
|
}
|
||||||
for (i = 0; i < 3 * pivot; i++) {
|
for (i = 0; i < 3 * pivot; i++) {
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
lock_acquire(testlock);
|
P(endsem);
|
||||||
cv_wait(endcv, testlock);
|
total_count--;
|
||||||
lock_release(testlock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure nothing else is happening... */
|
/* Make sure nothing else is happening... */
|
||||||
bool loop_status = SUCCESS;
|
loop_status = SUCCESS;
|
||||||
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; ) {
|
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; i++) {
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
random_spinner(PROBLEMS_MAX_SPINNER);
|
random_spinner(PROBLEMS_MAX_SPINNER);
|
||||||
lock_acquire(testlock);
|
lock_acquire(testlock);
|
||||||
if ((male_start_count != NMATING) || (female_start_count != NMATING) ||
|
if ((male_start_count != NMATING) || (female_start_count != NMATING) ||
|
||||||
(male_end_count != pivot) || (female_end_count != pivot) ||
|
(matchmaker_start_count != pivot) || (male_end_count != pivot) ||
|
||||||
(matchmaker_start_count != pivot) || (matchmaker_end_count != pivot)) {
|
(female_end_count != pivot) || (matchmaker_end_count != pivot)) {
|
||||||
loop_status = FAIL;
|
loop_status = FAIL;
|
||||||
}
|
}
|
||||||
lock_release(testlock);
|
lock_release(testlock);
|
||||||
}
|
}
|
||||||
if (loop_status == FAIL) {
|
if (loop_status == FAIL) {
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: uncoordinating matchmaking is occurring";
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,12 +379,14 @@ whalemating(int nargs, char **args) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
for (i = pivot; i < NMATING; i++) {
|
for (i = pivot; i < NMATING; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
V(matcher_sem);
|
V(matcher_sem);
|
||||||
}
|
}
|
||||||
for (i = 0; i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
for (j = 0; j < NMATING; j++) {
|
for (j = pivot; j < NMATING; j++) {
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
P(endsem);
|
P(endsem);
|
||||||
|
total_count--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,22 +408,31 @@ whalemating(int nargs, char **args) {
|
|||||||
if (male == 0 || female == 0) {
|
if (male == 0 || female == 0) {
|
||||||
spinlock_acquire(&status_lock);
|
spinlock_acquire(&status_lock);
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: not all males were matched";
|
||||||
spinlock_release(&status_lock);
|
spinlock_release(&status_lock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
spinlock_acquire(&status_lock);
|
spinlock_acquire(&status_lock);
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
|
test_message = "failed: not all males were matched";
|
||||||
spinlock_release(&status_lock);
|
spinlock_release(&status_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
for (i = 0; i < total_count; i++) {
|
||||||
|
P(endsem);
|
||||||
|
}
|
||||||
|
|
||||||
lock_destroy(testlock);
|
lock_destroy(testlock);
|
||||||
cv_destroy(startcv);
|
sem_destroy(startsem);
|
||||||
cv_destroy(endcv);
|
|
||||||
sem_destroy(endsem);
|
sem_destroy(endsem);
|
||||||
sem_destroy(matcher_sem);
|
sem_destroy(matcher_sem);
|
||||||
|
|
||||||
|
kprintf_t("\n");
|
||||||
|
if (test_status != SUCCESS) {
|
||||||
|
ksecprintf(SECRET, test_message, "sp1");
|
||||||
|
}
|
||||||
success(test_status, SECRET, "sp1");
|
success(test_status, SECRET, "sp1");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -493,7 +475,7 @@ check_intersection() {
|
|||||||
int n = 0;
|
int n = 0;
|
||||||
for (int i = 0; i < NUM_QUADRANTS; i++) {
|
for (int i = 0; i < NUM_QUADRANTS; i++) {
|
||||||
if (quadrant_array[i] > 1) {
|
if (quadrant_array[i] > 1) {
|
||||||
// panic("stoplight: more than 1 car in same quadrant %d\n", i);
|
test_message = "failed: collision";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
n += quadrant_array[i];
|
n += quadrant_array[i];
|
||||||
@ -588,11 +570,13 @@ inQuadrant(int quadrant, uint32_t index) {
|
|||||||
switch (car_turn_times[index]) {
|
switch (car_turn_times[index]) {
|
||||||
case 0:
|
case 0:
|
||||||
if (pre_quadrant != UNKNOWN_CAR) {
|
if (pre_quadrant != UNKNOWN_CAR) {
|
||||||
|
test_message = "failed: invalid turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
if (pre_quadrant != target_quadrant) {
|
if (pre_quadrant != target_quadrant) {
|
||||||
|
test_message = "failed: invalid turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
||||||
@ -600,6 +584,7 @@ inQuadrant(int quadrant, uint32_t index) {
|
|||||||
case 2:
|
case 2:
|
||||||
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
||||||
if (pre_quadrant != target_quadrant) {
|
if (pre_quadrant != target_quadrant) {
|
||||||
|
test_message = "failed: invalid turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
target_quadrant = (target_quadrant + NUM_QUADRANTS - 1) % NUM_QUADRANTS;
|
||||||
@ -614,7 +599,7 @@ inQuadrant(int quadrant, uint32_t index) {
|
|||||||
car_turn_times[index]++;
|
car_turn_times[index]++;
|
||||||
|
|
||||||
if (quadrant_array[quadrant] > 0) {
|
if (quadrant_array[quadrant] > 0) {
|
||||||
// panic("%s inQuadrant %d which already has another car\n", curthread->t_name, quadrant);
|
test_message = "failed: collision";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
quadrant_array[quadrant]++;
|
quadrant_array[quadrant]++;
|
||||||
@ -635,16 +620,19 @@ leaveIntersection(uint32_t index) {
|
|||||||
switch (car_turns[index]) {
|
switch (car_turns[index]) {
|
||||||
case GO_STRAIGHT:
|
case GO_STRAIGHT:
|
||||||
if (car_turn_times[index] != 2) {
|
if (car_turn_times[index] != 2) {
|
||||||
|
test_message = "failed: incorrect turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TURN_LEFT:
|
case TURN_LEFT:
|
||||||
if (car_turn_times[index] != 3) {
|
if (car_turn_times[index] != 3) {
|
||||||
|
test_message = "failed: incorrect turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TURN_RIGHT:
|
case TURN_RIGHT:
|
||||||
if (car_turn_times[index] != 1) {
|
if (car_turn_times[index] != 1) {
|
||||||
|
test_message = "failed: incorrect turn";
|
||||||
test_status = FAIL;
|
test_status = FAIL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -658,22 +646,16 @@ leaveIntersection(uint32_t index) {
|
|||||||
kprintf_n("%s left the intersection\n", curthread->t_name);
|
kprintf_n("%s left the intersection\n", curthread->t_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: should we delete this?
|
|
||||||
struct semaphore * stoplightMenuSemaphore;
|
|
||||||
|
|
||||||
int stoplight(int nargs, char **args) {
|
int stoplight(int nargs, char **args) {
|
||||||
(void) nargs;
|
(void) nargs;
|
||||||
(void) args;
|
(void) args;
|
||||||
int i, direction, turn, err = 0;
|
int i, direction, turn, err = 0;
|
||||||
char name[32];
|
char name[32];
|
||||||
|
int required_quadrant = 0;
|
||||||
inititems(NCARS);
|
int passed = 0;
|
||||||
stoplight_init();
|
|
||||||
test_status = SUCCESS;
|
|
||||||
|
|
||||||
max_car_count = 0;
|
max_car_count = 0;
|
||||||
all_quadrant = 0;
|
all_quadrant = 0;
|
||||||
int required_quadrant = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < NUM_QUADRANTS; i++) {
|
for (i = 0; i < NUM_QUADRANTS; i++) {
|
||||||
quadrant_array[i] = 0;
|
quadrant_array[i] = 0;
|
||||||
@ -685,7 +667,26 @@ int stoplight(int nargs, char **args) {
|
|||||||
car_directions[i] = -1;
|
car_directions[i] = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
startcount = NCARS;
|
||||||
|
testlock = lock_create("testlock");
|
||||||
|
if (testlock == NULL) {
|
||||||
|
panic("sp2: lock_create failed\n");
|
||||||
|
}
|
||||||
|
startcv = cv_create("startcv");
|
||||||
|
if (startcv == NULL) {
|
||||||
|
panic("sp2: cv_create failed\n");
|
||||||
|
}
|
||||||
|
endsem = sem_create("endsem", 0);
|
||||||
|
if (endsem == NULL) {
|
||||||
|
panic("sp2: sem_create failed\n");
|
||||||
|
}
|
||||||
|
spinlock_init(&status_lock);
|
||||||
|
test_status = SUCCESS;
|
||||||
|
|
||||||
|
stoplight_init();
|
||||||
|
|
||||||
for (i = 0; i < NCARS; i++) {
|
for (i = 0; i < NCARS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
|
|
||||||
direction = random() % 4;
|
direction = random() % 4;
|
||||||
turn = random() % 3;
|
turn = random() % 3;
|
||||||
@ -712,39 +713,36 @@ int stoplight(int nargs, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < NCARS; i++) {
|
for (i = 0; i < NCARS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
P(endsem);
|
P(endsem);
|
||||||
}
|
}
|
||||||
|
|
||||||
stoplight_cleanup();
|
stoplight_cleanup();
|
||||||
|
|
||||||
if (test_status == FAIL) {
|
|
||||||
success(test_status, SECRET, "sp2");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check all cars pass the intersection
|
|
||||||
int passed = 0;
|
|
||||||
for (i = 0; i < NCARS; i++) {
|
for (i = 0; i < NCARS; i++) {
|
||||||
passed += car_locations[i] == PASSED_CAR ? 1 : 0;
|
passed += car_locations[i] == PASSED_CAR ? 1 : 0;
|
||||||
}
|
}
|
||||||
if (passed != NCARS) {
|
if (test_status == SUCCESS) {
|
||||||
// panic("stoplight: not all cars pass the intersection, total: %d, passed: %d\n", NCARS, passed);
|
if (passed != NCARS) {
|
||||||
success(FAIL, SECRET, "sp2");
|
test_message = "failed: not enough cars";
|
||||||
return 0;
|
test_status = FAIL;
|
||||||
|
} else if (all_quadrant != required_quadrant) {
|
||||||
|
test_message = "failed: didn't do the right turns";
|
||||||
|
test_status = FAIL;
|
||||||
|
} else if (max_car_count <= 1) {
|
||||||
|
test_message = "failed: no concurrency achieved";
|
||||||
|
test_status = FAIL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check hit quadrant times is same as required, for example, student can force all cars turn right
|
lock_destroy(testlock);
|
||||||
if (all_quadrant != required_quadrant) {
|
cv_destroy(startcv);
|
||||||
// panic("stoplight: you may make wrong turn for some cars\n");
|
sem_destroy(endsem);
|
||||||
success(FAIL, SECRET, "sp2");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (max_car_count <= 1) {
|
kprintf_t("\n");
|
||||||
test_status = FAIL;
|
if (test_status != SUCCESS) {
|
||||||
// panic("stoplight: only one car in the intersection at same time, did you use big lock?\n");
|
ksecprintf(SECRET, test_message, "sp2");
|
||||||
}
|
}
|
||||||
|
|
||||||
success(test_status, SECRET, "sp2");
|
success(test_status, SECRET, "sp2");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -107,6 +107,7 @@ semtest(int nargs, char **args)
|
|||||||
|
|
||||||
kprintf_n("Starting sem1...\n");
|
kprintf_n("Starting sem1...\n");
|
||||||
for (i=0; i<CREATELOOPS; i++) {
|
for (i=0; i<CREATELOOPS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
testsem = sem_create("testsem", 2);
|
testsem = sem_create("testsem", 2);
|
||||||
if (testsem == NULL) {
|
if (testsem == NULL) {
|
||||||
panic("sem1: sem_create failed\n");
|
panic("sem1: sem_create failed\n");
|
||||||
@ -121,16 +122,16 @@ semtest(int nargs, char **args)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
spinlock_init(&status_lock);
|
spinlock_init(&status_lock);
|
||||||
test_status = FAIL;
|
test_status = SUCCESS;
|
||||||
|
|
||||||
kprintf_n("If this hangs, it's broken: ");
|
kprintf_n("If this hangs, it's broken: ");
|
||||||
P(testsem);
|
P(testsem);
|
||||||
P(testsem);
|
P(testsem);
|
||||||
kprintf_n("OK\n");
|
kprintf_n("OK\n");
|
||||||
kprintf_t(".");
|
kprintf_t(".");
|
||||||
test_status = SUCCESS;
|
|
||||||
|
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
result = thread_fork("semtest", NULL, semtestthread, NULL, i);
|
result = thread_fork("semtest", NULL, semtestthread, NULL, i);
|
||||||
if (result) {
|
if (result) {
|
||||||
panic("sem1: thread_fork failed: %s\n",
|
panic("sem1: thread_fork failed: %s\n",
|
||||||
@ -138,6 +139,7 @@ semtest(int nargs, char **args)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
V(testsem);
|
V(testsem);
|
||||||
P(donesem);
|
P(donesem);
|
||||||
}
|
}
|
||||||
@ -240,6 +242,7 @@ locktest(int nargs, char **args)
|
|||||||
|
|
||||||
kprintf_n("Starting lt1...\n");
|
kprintf_n("Starting lt1...\n");
|
||||||
for (i=0; i<CREATELOOPS; i++) {
|
for (i=0; i<CREATELOOPS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
testlock = lock_create("testlock");
|
testlock = lock_create("testlock");
|
||||||
if (testlock == NULL) {
|
if (testlock == NULL) {
|
||||||
panic("lt1: lock_create failed\n");
|
panic("lt1: lock_create failed\n");
|
||||||
@ -257,12 +260,14 @@ locktest(int nargs, char **args)
|
|||||||
test_status = SUCCESS;
|
test_status = SUCCESS;
|
||||||
|
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
result = thread_fork("synchtest", NULL, locktestthread, NULL, i);
|
result = thread_fork("synchtest", NULL, locktestthread, NULL, i);
|
||||||
if (result) {
|
if (result) {
|
||||||
panic("lt1: thread_fork failed: %s\n", strerror(result));
|
panic("lt1: thread_fork failed: %s\n", strerror(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
P(donesem);
|
P(donesem);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,6 +418,7 @@ cvtest(int nargs, char **args)
|
|||||||
|
|
||||||
kprintf_n("Starting cvt1...\n");
|
kprintf_n("Starting cvt1...\n");
|
||||||
for (i=0; i<CREATELOOPS; i++) {
|
for (i=0; i<CREATELOOPS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
testlock = lock_create("testlock");
|
testlock = lock_create("testlock");
|
||||||
if (testlock == NULL) {
|
if (testlock == NULL) {
|
||||||
panic("lockt1: lock_create failed\n");
|
panic("lockt1: lock_create failed\n");
|
||||||
@ -436,12 +442,14 @@ cvtest(int nargs, char **args)
|
|||||||
|
|
||||||
testval1 = NTHREADS-1;
|
testval1 = NTHREADS-1;
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
result = thread_fork("cvt1", NULL, cvtestthread, NULL, (long unsigned) i);
|
result = thread_fork("cvt1", NULL, cvtestthread, NULL, (long unsigned) i);
|
||||||
if (result) {
|
if (result) {
|
||||||
panic("cvt1: thread_fork failed: %s\n", strerror(result));
|
panic("cvt1: thread_fork failed: %s\n", strerror(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i=0; i<NTHREADS; i++) {
|
for (i=0; i<NTHREADS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
P(donesem);
|
P(donesem);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -550,6 +558,7 @@ cvtest2(int nargs, char **args)
|
|||||||
|
|
||||||
kprintf_n("Starting cvt2...\n");
|
kprintf_n("Starting cvt2...\n");
|
||||||
for (i=0; i<CREATELOOPS; i++) {
|
for (i=0; i<CREATELOOPS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
gatesem = sem_create("gatesem", 0);
|
gatesem = sem_create("gatesem", 0);
|
||||||
if (gatesem == NULL) {
|
if (gatesem == NULL) {
|
||||||
panic("cvt2: sem_create failed\n");
|
panic("cvt2: sem_create failed\n");
|
||||||
@ -564,6 +573,7 @@ cvtest2(int nargs, char **args)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i=0; i<NCVS; i++) {
|
for (i=0; i<NCVS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
testlocks[i] = lock_create("cvtest2 lock");
|
testlocks[i] = lock_create("cvtest2 lock");
|
||||||
testcvs[i] = cv_create("cvtest2 cv");
|
testcvs[i] = cv_create("cvtest2 cv");
|
||||||
}
|
}
|
||||||
@ -585,6 +595,7 @@ cvtest2(int nargs, char **args)
|
|||||||
sem_destroy(gatesem);
|
sem_destroy(gatesem);
|
||||||
exitsem = gatesem = NULL;
|
exitsem = gatesem = NULL;
|
||||||
for (i=0; i<NCVS; i++) {
|
for (i=0; i<NCVS; i++) {
|
||||||
|
kprintf_t(".");
|
||||||
lock_destroy(testlocks[i]);
|
lock_destroy(testlocks[i]);
|
||||||
cv_destroy(testcvs[i]);
|
cv_destroy(testcvs[i]);
|
||||||
testlocks[i] = NULL;
|
testlocks[i] = NULL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user