/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#ifndef _SYNCH_H_30#define _SYNCH_H_3132/*33* Header file for synchronization primitives.34*/353637#include <spinlock.h>3839/*40* Dijkstra-style semaphore.41*42* The name field is for easier debugging. A copy of the name is made43* internally.44*/45struct semaphore {46char *sem_name;47struct wchan *sem_wchan;48struct spinlock sem_lock;49volatile int sem_count;50};5152struct semaphore *sem_create(const char *name, int initial_count);53void sem_destroy(struct semaphore *);5455/*56* Operations (both atomic):57* P (proberen): decrement count. If the count is 0, block until58* the count is 1 again before decrementing.59* V (verhogen): increment count.60*/61void P(struct semaphore *);62void V(struct semaphore *);636465/*66* Simple lock for mutual exclusion.67*68* When the lock is created, no thread should be holding it. Likewise,69* when the lock is destroyed, no thread should be holding it.70*71* The name field is for easier debugging. A copy of the name is72* (should be) made internally.73*/74struct lock {75char *lk_name;7677// BEGIN SOLUTION78struct wchan *lk_wchan;79struct spinlock lk_lock;80volatile struct thread *lk_holder;81// END SOLUTION82};8384struct lock *lock_create(const char *name);85void lock_acquire(struct lock *);8687/*88* Operations:89* lock_acquire - Get the lock. Only one thread can hold the lock at the90* same time.91* lock_release - Free the lock. Only the thread holding the lock may do92* this.93* lock_do_i_hold - Return true if the current thread holds the lock;94* false otherwise.95*96* These operations must be atomic. You get to write them.97*/98void lock_release(struct lock *);99bool lock_do_i_hold(struct lock *);100void lock_destroy(struct lock *);101102103/*104* Condition variable.105*106* Note that the "variable" is a bit of a misnomer: a CV is normally used107* to wait until a variable meets a particular condition, but there's no108* actual variable, as such, in the CV.109*110* These CVs are expected to support Mesa semantics, that is, no111* guarantees are made about scheduling.112*113* The name field is for easier debugging. A copy of the name is114* (should be) made internally.115*/116117struct cv {118char *cv_name;119120// BEGIN SOLUTION121struct wchan *cv_wchan;122// END SOLUTION123};124125struct cv *cv_create(const char *name);126void cv_destroy(struct cv *);127128/*129* Operations:130* cv_wait - Release the supplied lock, go to sleep, and, after131* waking up again, re-acquire the lock.132* cv_signal - Wake up one thread that's sleeping on this CV.133* cv_broadcast - Wake up all threads sleeping on this CV.134*135* For all three operations, the current thread must hold the lock passed136* in. Note that under normal circumstances the same lock should be used137* on all operations with any particular CV.138*139* These operations must be atomic. You get to write them.140*/141void cv_wait(struct cv *cv, struct lock *lock);142void cv_signal(struct cv *cv, struct lock *lock);143void cv_broadcast(struct cv *cv, struct lock *lock);144145/*146* 13 Feb 2012 : GWA : Reader-writer locks.147*/148149struct rwlock {150char *rwlock_name;151};152153struct rwlock * rwlock_create(const char *);154void rwlock_destroy(struct rwlock *);155156void rwlock_acquire_read(struct rwlock *);157void rwlock_release_read(struct rwlock *);158void rwlock_acquire_write(struct rwlock *);159void rwlock_release_write(struct rwlock *);160161#endif /* _SYNCH_H_ */162163164