os161-1.99
 All Data Structures
synch.h
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #ifndef _SYNCH_H_
00031 #define _SYNCH_H_
00032 
00033 /*
00034  * Header file for synchronization primitives.
00035  */
00036 
00037 
00038 #include <spinlock.h>
00039 
00040 /*
00041  * Dijkstra-style semaphore.
00042  *
00043  * The name field is for easier debugging. A copy of the name is made
00044  * internally.
00045  */
00046 struct semaphore {
00047         char *sem_name;
00048         struct wchan *sem_wchan;
00049         struct spinlock sem_lock;
00050         volatile int sem_count;
00051 };
00052 
00053 struct semaphore *sem_create(const char *name, int initial_count);
00054 void sem_destroy(struct semaphore *);
00055 
00056 /*
00057  * Operations (both atomic):
00058  *     P (proberen): decrement count. If the count is 0, block until
00059  *                   the count is 1 again before decrementing.
00060  *     V (verhogen): increment count.
00061  */
00062 void P(struct semaphore *);
00063 void V(struct semaphore *);
00064 
00065 
00066 /*
00067  * Simple lock for mutual exclusion.
00068  *
00069  * When the lock is created, no thread should be holding it. Likewise,
00070  * when the lock is destroyed, no thread should be holding it.
00071  *
00072  * The name field is for easier debugging. A copy of the name is
00073  * (should be) made internally.
00074  */
00075 struct lock {
00076         char *lk_name;
00077         // add what you need here
00078         // (don't forget to mark things volatile as needed)
00079 };
00080 
00081 struct lock *lock_create(const char *name);
00082 void lock_acquire(struct lock *);
00083 
00084 /*
00085  * Operations:
00086  *    lock_acquire - Get the lock. Only one thread can hold the lock at the
00087  *                   same time.
00088  *    lock_release - Free the lock. Only the thread holding the lock may do
00089  *                   this.
00090  *    lock_do_i_hold - Return true if the current thread holds the lock; 
00091  *                   false otherwise.
00092  *
00093  * These operations must be atomic. You get to write them.
00094  */
00095 void lock_release(struct lock *);
00096 bool lock_do_i_hold(struct lock *);
00097 void lock_destroy(struct lock *);
00098 
00099 
00100 /*
00101  * Condition variable.
00102  *
00103  * Note that the "variable" is a bit of a misnomer: a CV is normally used
00104  * to wait until a variable meets a particular condition, but there's no
00105  * actual variable, as such, in the CV.
00106  *
00107  * These CVs are expected to support Mesa semantics, that is, no
00108  * guarantees are made about scheduling.
00109  *
00110  * The name field is for easier debugging. A copy of the name is
00111  * (should be) made internally.
00112  */
00113 
00114 struct cv {
00115         char *cv_name;
00116         // add what you need here
00117         // (don't forget to mark things volatile as needed)
00118 };
00119 
00120 struct cv *cv_create(const char *name);
00121 void cv_destroy(struct cv *);
00122 
00123 /*
00124  * Operations:
00125  *    cv_wait      - Release the supplied lock, go to sleep, and, after
00126  *                   waking up again, re-acquire the lock.
00127  *    cv_signal    - Wake up one thread that's sleeping on this CV.
00128  *    cv_broadcast - Wake up all threads sleeping on this CV.
00129  *
00130  * For all three operations, the current thread must hold the lock passed 
00131  * in. Note that under normal circumstances the same lock should be used
00132  * on all operations with any particular CV.
00133  *
00134  * These operations must be atomic. You get to write them.
00135  */
00136 void cv_wait(struct cv *cv, struct lock *lock);
00137 void cv_signal(struct cv *cv, struct lock *lock);
00138 void cv_broadcast(struct cv *cv, struct lock *lock);
00139 
00140 
00141 #endif /* _SYNCH_H_ */
 All Data Structures