Refactor test code.
This commit is contained in:
		
							
								
								
									
										33
									
								
								kern/conf/BASE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								kern/conf/BASE
									
									
									
									
									
										Normal 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 | ||||
| @@ -11,17 +11,17 @@ | ||||
|  | ||||
| void whalemating_init(void); | ||||
| void whalemating_cleanup(void); | ||||
| void male(void *, unsigned long); | ||||
| void female(void *, unsigned long); | ||||
| void matchmaker(void *, unsigned long); | ||||
| void male(void); | ||||
| void female(void); | ||||
| void matchmaker(void); | ||||
|   | ||||
| /* | ||||
|  * stoplight.c. | ||||
|  */ | ||||
|  | ||||
| void gostraight(void *, unsigned long); | ||||
| void turnleft(void *, unsigned long); | ||||
| void turnright(void *, unsigned long); | ||||
| void gostraight(unsigned long); | ||||
| void turnleft(unsigned long); | ||||
| void turnright(unsigned long); | ||||
| void stoplight_init(void); | ||||
| void stoplight_cleanup(void); | ||||
|  | ||||
|   | ||||
| @@ -85,52 +85,29 @@ void stoplight_cleanup() { | ||||
| } | ||||
|  | ||||
| void | ||||
| gostraight(void *p, unsigned long direction) | ||||
| turnright(unsigned long direction) | ||||
| { | ||||
| 	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p; | ||||
| 	(void)direction; | ||||
| 	 | ||||
| 	/* | ||||
| 	 * Implement this function. | ||||
| 	 */ | ||||
|  | ||||
| 	/* | ||||
| 	 * This code allows the test to return to the menu cleanly. | ||||
| 	 */ | ||||
| 	V(stoplightMenuSemaphore); | ||||
| 	return; | ||||
| } | ||||
|  | ||||
| void | ||||
| turnleft(void *p, unsigned long direction) | ||||
| gostraight(unsigned long direction) | ||||
| { | ||||
| 	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p; | ||||
| 	(void)direction; | ||||
| 	 | ||||
| 	/* | ||||
| 	 * Implement this function. | ||||
| 	 */ | ||||
|  | ||||
| 	/* | ||||
| 	 * This code allows the test to return to the menu cleanly. | ||||
| 	 */ | ||||
| 	V(stoplightMenuSemaphore); | ||||
| 	return; | ||||
| } | ||||
|  | ||||
| void | ||||
| turnright(void *p, unsigned long direction) | ||||
| turnleft(unsigned long direction) | ||||
| { | ||||
| 	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p; | ||||
| 	(void)direction; | ||||
| 	 | ||||
| 	/* | ||||
| 	 * Implement this function. | ||||
| 	 */ | ||||
|  | ||||
| 	/* | ||||
| 	 * This code allows the test to return to the menu cleanly. | ||||
| 	 */ | ||||
| 	V(stoplightMenuSemaphore); | ||||
| 	return; | ||||
| } | ||||
|   | ||||
| @@ -59,58 +59,31 @@ whalemating_cleanup() { | ||||
| } | ||||
|  | ||||
| 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; | ||||
| } | ||||
|  | ||||
| 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; | ||||
| } | ||||
|  | ||||
| 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; | ||||
| } | ||||
|   | ||||
| @@ -16,75 +16,127 @@ | ||||
| #define PROBLEMS_MAX_YIELDER 16 | ||||
| #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. | ||||
|  */ | ||||
|  | ||||
| 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_spinner(PROBLEMS_MAX_SPINNER); | ||||
| 	tkprintf("%s starting\n", curthread->t_name); | ||||
| } | ||||
|  | ||||
| inline void male_end(void) { | ||||
| void | ||||
| male_end(void) { | ||||
| 	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_yielder(PROBLEMS_MAX_YIELDER); | ||||
| 	tkprintf("%s starting\n", curthread->t_name); | ||||
| } | ||||
|  | ||||
| inline void female_end(void) { | ||||
| void | ||||
| female_end(void) { | ||||
| 	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_spinner(PROBLEMS_MAX_SPINNER); | ||||
| 	tkprintf("%s starting\n", curthread->t_name); | ||||
| } | ||||
|  | ||||
| inline void matchmaker_end(void) { | ||||
| void | ||||
| matchmaker_end(void) { | ||||
| 	tkprintf("%s ending\n", curthread->t_name); | ||||
| } | ||||
|  | ||||
| #define NMATING 10 | ||||
|  | ||||
| struct semaphore * whalematingMenuSemaphore; | ||||
|  | ||||
| int whalemating(int nargs, char **args) { | ||||
| int | ||||
| whalemating(int nargs, char **args) { | ||||
| 	(void) nargs; | ||||
| 	(void) args; | ||||
|  | ||||
| 	int i, j, err = 0; | ||||
| 	char name[32]; | ||||
|  | ||||
| 	whalematingMenuSemaphore = sem_create("Whalemating Driver Semaphore", 0); | ||||
| 	if (whalematingMenuSemaphore == NULL ) { | ||||
| 		panic("whalemating: sem_create failed.\n"); | ||||
| 	} | ||||
|  | ||||
| 	 | ||||
| 	inititems(); | ||||
| 	whalemating_init(); | ||||
|  | ||||
| 	for (i = 0; i < 3; i++) { | ||||
| 		for (j = 0; j < NMATING; j++) { | ||||
|  | ||||
| 			random_yielder(PROBLEMS_MAX_YIELDER); | ||||
|  | ||||
| 			switch (i) { | ||||
| 				case 0: | ||||
| 					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; | ||||
| 				case 1: | ||||
| 					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; | ||||
| 				case 2: | ||||
| 					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; | ||||
| 			} | ||||
| 			if (err) { | ||||
| @@ -92,14 +144,19 @@ int whalemating(int nargs, char **args) { | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	 | ||||
| 	for (i = 0; i < 3; i++) { | ||||
| 		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(); | ||||
|  | ||||
| 	return 0; | ||||
| @@ -109,13 +166,55 @@ int whalemating(int nargs, char **args) { | ||||
|  * 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_yielder(PROBLEMS_MAX_YIELDER); | ||||
| 	tkprintf("%s in quadrant %d\n", curthread->t_name, quadrant); | ||||
| } | ||||
|  | ||||
| inline void leaveIntersection() { | ||||
| void | ||||
| leaveIntersection() { | ||||
| 	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; | ||||
| 	char name[32]; | ||||
|  | ||||
| 	stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0); | ||||
| 	if (stoplightMenuSemaphore == NULL ) { | ||||
| 		panic("stoplight: sem_create failed.\n"); | ||||
| 	} | ||||
|  | ||||
| 	inititems(); | ||||
| 	stoplight_init(); | ||||
|  | ||||
| 	for (i = 0; i < NCARS; i++) { | ||||
| @@ -144,29 +239,29 @@ int stoplight(int nargs, char **args) { | ||||
| 		snprintf(name, sizeof(name), "Car Thread %d", i); | ||||
|  | ||||
| 		switch (turn) { | ||||
|  | ||||
| 			random_yielder(PROBLEMS_MAX_YIELDER); | ||||
|  | ||||
| 			case 0: | ||||
| 			err = thread_fork(name, NULL, gostraight, stoplightMenuSemaphore, direction); | ||||
| 			err = thread_fork(name, NULL, gostraight_wrapper, NULL, direction); | ||||
| 			break; | ||||
| 			case 1: | ||||
| 			err = thread_fork(name, NULL, turnleft, stoplightMenuSemaphore, direction); | ||||
| 			err = thread_fork(name, NULL, turnleft_wrapper, NULL, direction); | ||||
| 			break; | ||||
| 			case 2: | ||||
| 			err = thread_fork(name, NULL, turnright, stoplightMenuSemaphore, direction); | ||||
| 			err = thread_fork(name, NULL, turnright_wrapper, NULL, direction); | ||||
| 			break; | ||||
| 		} | ||||
| 		if (err) { | ||||
| 			panic("stoplight: thread_fork failed: (%s)\n", strerror(err)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	 | ||||
| 	for (i = 0; i < NCARS; i++) { | ||||
| 		P(stoplightMenuSemaphore); | ||||
| 		V(testsem); | ||||
| 	} | ||||
|  | ||||
| 	for (i = 0; i < NCARS; i++) { | ||||
| 		P(testsem); | ||||
| 	} | ||||
|  | ||||
| 	sem_destroy(stoplightMenuSemaphore); | ||||
| 	stoplight_cleanup(); | ||||
|  | ||||
| 	return 0; | ||||
|   | ||||
| @@ -196,10 +196,7 @@ locktestthread(void *junk, unsigned long num) | ||||
| 	int i; | ||||
| 	(void)junk; | ||||
|  | ||||
| 	random_yielder(4); | ||||
|  | ||||
| 	for (i=0; i<NLOCKLOOPS; i++) { | ||||
| 		random_yielder(4); | ||||
| 		lock_acquire(testlock); | ||||
| 		random_yielder(4); | ||||
|  | ||||
| @@ -285,9 +282,11 @@ cvtestthread(void *junk, unsigned long num) | ||||
| 		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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user