Refactor test code.

This commit is contained in:
Geoffrey Challen 2015-12-31 19:26:33 -05:00
parent 3b2267123d
commit 26612b6142
6 changed files with 191 additions and 114 deletions

33
kern/conf/BASE Normal file
View File

@ -0,0 +1,33 @@
# Kernel config file using dumbvm.
# This should be used until you have your own VM system.
include conf/conf.kern # get definitions of available options
debug # Compile with debug info.
#
# Device drivers for hardware.
#
device lamebus0 # System/161 main bus
device emu* at lamebus* # Emulator passthrough filesystem
device ltrace* at lamebus* # trace161 trace control device
device ltimer* at lamebus* # Timer device
device lrandom* at lamebus* # Random device
device lhd* at lamebus* # Disk device
device lser* at lamebus* # Serial port
#device lscreen* at lamebus* # Text screen (not supported yet)
#device lnet* at lamebus* # Network interface (not supported yet)
device beep0 at ltimer* # Abstract beep handler device
device con0 at lser* # Abstract console on serial port
#device con0 at lscreen* # Abstract console on screen (not supported)
device rtclock0 at ltimer* # Abstract realtime clock
device random0 at lrandom* # Abstract randomness device
#options net # Network stack (not supported)
options semfs # Semaphores for userland
options sfs # Always use the file system
#options netfs # You might write this as a project.
options dumbvm # Chewing gum and baling wire.
#options synchprobs # Uncomment to enable ASST1 synchronization problems

View File

@ -11,17 +11,17 @@
void whalemating_init(void); void whalemating_init(void);
void whalemating_cleanup(void); void whalemating_cleanup(void);
void male(void *, unsigned long); void male(void);
void female(void *, unsigned long); void female(void);
void matchmaker(void *, unsigned long); void matchmaker(void);
/* /*
* stoplight.c. * stoplight.c.
*/ */
void gostraight(void *, unsigned long); void gostraight(unsigned long);
void turnleft(void *, unsigned long); void turnleft(unsigned long);
void turnright(void *, unsigned long); void turnright(unsigned long);
void stoplight_init(void); void stoplight_init(void);
void stoplight_cleanup(void); void stoplight_cleanup(void);

View File

@ -85,52 +85,29 @@ void stoplight_cleanup() {
} }
void void
gostraight(void *p, unsigned long direction) turnright(unsigned long direction)
{ {
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
(void)direction; (void)direction;
/* /*
* Implement this function. * Implement this function.
*/ */
/*
* This code allows the test to return to the menu cleanly.
*/
V(stoplightMenuSemaphore);
return; return;
} }
void void
turnleft(void *p, unsigned long direction) gostraight(unsigned long direction)
{ {
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
(void)direction; (void)direction;
/* /*
* Implement this function. * Implement this function.
*/ */
/*
* This code allows the test to return to the menu cleanly.
*/
V(stoplightMenuSemaphore);
return; return;
} }
void void
turnright(void *p, unsigned long direction) turnleft(unsigned long direction)
{ {
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
(void)direction; (void)direction;
/* /*
* Implement this function. * Implement this function.
*/ */
/*
* This code allows the test to return to the menu cleanly.
*/
V(stoplightMenuSemaphore);
return; return;
} }

View File

@ -59,58 +59,31 @@ whalemating_cleanup() {
} }
void void
male(void *p, unsigned long which) male()
{ {
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
(void)which;
male_start();
/* /*
* Implement this function. * Implement this function by calling male_start and male_end when
* appropriate.
*/ */
male_end();
/*
* This code allows the test to return to the menu cleanly.
*/
V(whalematingMenuSemaphore);
return; return;
} }
void void
female(void *p, unsigned long which) female()
{ {
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
(void)which;
female_start();
/* /*
* Implement this function. * Implement this function by calling female_start and female_end when
* appropriate.
*/ */
female_end();
/*
* This code allows the test to return to the menu cleanly.
*/
V(whalematingMenuSemaphore);
return; return;
} }
void void
matchmaker(void *p, unsigned long which) matchmaker()
{ {
struct semaphore * whalematingMenuSemaphore = (struct semaphore *)p;
(void)which;
matchmaker_start();
/* /*
* Implement this function. * Implement this function by calling matchmaker_start and matchmaker_end
* when appropriate.
*/ */
matchmaker_end();
/*
* This code allows the test to return to the menu cleanly.
*/
V(whalematingMenuSemaphore);
return; return;
} }

View File

@ -16,75 +16,127 @@
#define PROBLEMS_MAX_YIELDER 16 #define PROBLEMS_MAX_YIELDER 16
#define PROBLEMS_MAX_SPINNER 8192 #define PROBLEMS_MAX_SPINNER 8192
/*
* Shared initialization routines
*/
static struct semaphore *testsem;
static
void
inititems(void)
{
if (testsem==NULL) {
testsem = sem_create("testsem", 0);
if (testsem == NULL) {
panic("synchprobs: sem_create failed\n");
}
}
}
/* /*
* Driver code for the whalemating problem. * Driver code for the whalemating problem.
*/ */
inline void male_start(void) { static
void
male_wrapper(void * unused1, unsigned long unused2) {
(void)unused1;
(void)unused2;
random_yielder(4);
P(testsem);
male();
V(testsem);
return;
}
void
male_start(void) {
random_yielder(PROBLEMS_MAX_YIELDER); random_yielder(PROBLEMS_MAX_YIELDER);
random_spinner(PROBLEMS_MAX_SPINNER); random_spinner(PROBLEMS_MAX_SPINNER);
tkprintf("%s starting\n", curthread->t_name); tkprintf("%s starting\n", curthread->t_name);
} }
void
inline void male_end(void) { male_end(void) {
tkprintf("%s ending\n", curthread->t_name); tkprintf("%s ending\n", curthread->t_name);
} }
inline void female_start(void) { static
void
female_wrapper(void * unused1, unsigned long unused2) {
(void)unused1;
(void)unused2;
random_yielder(4);
P(testsem);
female();
V(testsem);
return;
}
void
female_start(void) {
random_spinner(PROBLEMS_MAX_SPINNER); random_spinner(PROBLEMS_MAX_SPINNER);
random_yielder(PROBLEMS_MAX_YIELDER); random_yielder(PROBLEMS_MAX_YIELDER);
tkprintf("%s starting\n", curthread->t_name); tkprintf("%s starting\n", curthread->t_name);
} }
void
inline void female_end(void) { female_end(void) {
tkprintf("%s ending\n", curthread->t_name); tkprintf("%s ending\n", curthread->t_name);
} }
inline void matchmaker_start(void) { static
void
matchmaker_wrapper(void * unused1, unsigned long unused2) {
(void)unused1;
(void)unused2;
random_yielder(4);
P(testsem);
matchmaker();
V(testsem);
return;
}
void
matchmaker_start(void) {
random_yielder(PROBLEMS_MAX_YIELDER); random_yielder(PROBLEMS_MAX_YIELDER);
random_spinner(PROBLEMS_MAX_SPINNER); random_spinner(PROBLEMS_MAX_SPINNER);
tkprintf("%s starting\n", curthread->t_name); tkprintf("%s starting\n", curthread->t_name);
} }
void
inline void matchmaker_end(void) { matchmaker_end(void) {
tkprintf("%s ending\n", curthread->t_name); tkprintf("%s ending\n", curthread->t_name);
} }
#define NMATING 10 #define NMATING 10
struct semaphore * whalematingMenuSemaphore; int
whalemating(int nargs, char **args) {
int whalemating(int nargs, char **args) {
(void) nargs; (void) nargs;
(void) args; (void) args;
int i, j, err = 0; int i, j, err = 0;
char name[32]; char name[32];
whalematingMenuSemaphore = sem_create("Whalemating Driver Semaphore", 0); inititems();
if (whalematingMenuSemaphore == NULL ) {
panic("whalemating: sem_create failed.\n");
}
whalemating_init(); whalemating_init();
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
for (j = 0; j < NMATING; j++) { for (j = 0; j < NMATING; j++) {
random_yielder(PROBLEMS_MAX_YIELDER);
switch (i) { switch (i) {
case 0: case 0:
snprintf(name, sizeof(name), "Male Whale Thread %d", (i * 3) + j); snprintf(name, sizeof(name), "Male Whale Thread %d", (i * 3) + j);
err = thread_fork(name, NULL, male, whalematingMenuSemaphore, j); err = thread_fork(name, NULL, male_wrapper, NULL, 0);
break; break;
case 1: case 1:
snprintf(name, sizeof(name), "Female Whale Thread %d", (i * 3) + j); snprintf(name, sizeof(name), "Female Whale Thread %d", (i * 3) + j);
err = thread_fork(name, NULL, female, whalematingMenuSemaphore, j); err = thread_fork(name, NULL, female_wrapper, NULL, 0);
break; break;
case 2: case 2:
snprintf(name, sizeof(name), "Matchmaker Whale Thread %d", (i * 3) + j); snprintf(name, sizeof(name), "Matchmaker Whale Thread %d", (i * 3) + j);
err = thread_fork(name, NULL, matchmaker, whalematingMenuSemaphore, j); err = thread_fork(name, NULL, matchmaker_wrapper, NULL, 0);
break; break;
} }
if (err) { if (err) {
@ -95,11 +147,16 @@ int whalemating(int nargs, char **args) {
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
for (j = 0; j < NMATING; j++) { for (j = 0; j < NMATING; j++) {
P(whalematingMenuSemaphore); V(testsem);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < NMATING; j++) {
P(testsem);
} }
} }
sem_destroy(whalematingMenuSemaphore);
whalemating_cleanup(); whalemating_cleanup();
return 0; return 0;
@ -109,13 +166,55 @@ int whalemating(int nargs, char **args) {
* Driver code for the stoplight problem. * Driver code for the stoplight problem.
*/ */
inline void inQuadrant(int quadrant) { static
void
turnright_wrapper(void *unused, unsigned long direction)
{
(void)unused;
random_yielder(4);
P(testsem);
turnright(direction);
V(testsem);
return;
}
static
void
gostraight_wrapper(void *unused, unsigned long direction)
{
(void)unused;
random_yielder(4);
P(testsem);
gostraight(direction);
V(testsem);
return;
}
static
void
turnleft_wrapper(void *unused, unsigned long direction)
{
(void)unused;
random_yielder(4);
P(testsem);
turnleft(direction);
V(testsem);
return;
}
void
inQuadrant(int quadrant) {
random_spinner(PROBLEMS_MAX_SPINNER); random_spinner(PROBLEMS_MAX_SPINNER);
random_yielder(PROBLEMS_MAX_YIELDER); random_yielder(PROBLEMS_MAX_YIELDER);
tkprintf("%s in quadrant %d\n", curthread->t_name, quadrant); tkprintf("%s in quadrant %d\n", curthread->t_name, quadrant);
} }
inline void leaveIntersection() { void
leaveIntersection() {
tkprintf("%s left the intersection\n", curthread->t_name); tkprintf("%s left the intersection\n", curthread->t_name);
} }
@ -129,11 +228,7 @@ int stoplight(int nargs, char **args) {
int i, direction, turn, err = 0; int i, direction, turn, err = 0;
char name[32]; char name[32];
stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0); inititems();
if (stoplightMenuSemaphore == NULL ) {
panic("stoplight: sem_create failed.\n");
}
stoplight_init(); stoplight_init();
for (i = 0; i < NCARS; i++) { for (i = 0; i < NCARS; i++) {
@ -144,17 +239,14 @@ int stoplight(int nargs, char **args) {
snprintf(name, sizeof(name), "Car Thread %d", i); snprintf(name, sizeof(name), "Car Thread %d", i);
switch (turn) { switch (turn) {
random_yielder(PROBLEMS_MAX_YIELDER);
case 0: case 0:
err = thread_fork(name, NULL, gostraight, stoplightMenuSemaphore, direction); err = thread_fork(name, NULL, gostraight_wrapper, NULL, direction);
break; break;
case 1: case 1:
err = thread_fork(name, NULL, turnleft, stoplightMenuSemaphore, direction); err = thread_fork(name, NULL, turnleft_wrapper, NULL, direction);
break; break;
case 2: case 2:
err = thread_fork(name, NULL, turnright, stoplightMenuSemaphore, direction); err = thread_fork(name, NULL, turnright_wrapper, NULL, direction);
break; break;
} }
if (err) { if (err) {
@ -163,10 +255,13 @@ int stoplight(int nargs, char **args) {
} }
for (i = 0; i < NCARS; i++) { for (i = 0; i < NCARS; i++) {
P(stoplightMenuSemaphore); V(testsem);
}
for (i = 0; i < NCARS; i++) {
P(testsem);
} }
sem_destroy(stoplightMenuSemaphore);
stoplight_cleanup(); stoplight_cleanup();
return 0; return 0;

View File

@ -196,10 +196,7 @@ locktestthread(void *junk, unsigned long num)
int i; int i;
(void)junk; (void)junk;
random_yielder(4);
for (i=0; i<NLOCKLOOPS; i++) { for (i=0; i<NLOCKLOOPS; i++) {
random_yielder(4);
lock_acquire(testlock); lock_acquire(testlock);
random_yielder(4); random_yielder(4);
@ -285,9 +282,11 @@ cvtestthread(void *junk, unsigned long num)
lock_acquire(testlock); lock_acquire(testlock);
while (testval1 != num) { while (testval1 != num) {
testval2 = 0; testval2 = 0;
random_yielder(4);
gettime(&ts1); gettime(&ts1);
cv_wait(testcv, testlock); cv_wait(testcv, testlock);
gettime(&ts2); gettime(&ts2);
random_yielder(4);
/* ts2 -= ts1 */ /* ts2 -= ts1 */
timespec_sub(&ts2, &ts1, &ts2); timespec_sub(&ts2, &ts1, &ts2);