os161/kern/synchprobs/stoplight.c
2015-12-31 14:28:15 -05:00

134 lines
3.7 KiB
C

/*
* Copyright (c) 2001, 2002, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Driver code is in kern/tests/synchprobs.c We will
* replace that file. This file is yours to modify as you see fit.
*
* You should implement your solution to the stoplight problem below. The
* quadrant and direction mappings for reference: (although the problem is,
* of course, stable under rotation)
*
* | 0 |
* -- --
* 0 1
* 3 1
* 3 2
* -- --
* | 2 |
*
* As way to think about it, assuming cars drive on the right: a car entering
* the intersection from direction X will enter intersection quadrant X
* first.
*
* You will probably want to write some helper functions to assist
* with the mappings. Modular arithmetic can help, e.g. a car passing
* straight through the intersection entering from direction X will leave to
* direction (X + 2) % 4 and pass through quadrants X and (X + 3) % 4.
* Boo-yah.
*
* Your solutions below should call the inQuadrant() and leaveIntersection()
* functions in synchprobs.c to record their progress.
*/
#include <types.h>
#include <lib.h>
#include <thread.h>
#include <test.h>
#include <synch.h>
#include <synchprobs.h>
/*
* Called by the driver during initialization.
*/
void
stoplight_init() {
return;
}
/*
* Called by the driver during teardown.
*/
void stoplight_cleanup() {
return;
}
void
gostraight(void *p, 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)
{
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)
{
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;
}