00001 #include <types.h> 00002 #include <lib.h> 00003 #include <synchprobs.h> 00004 #include <synch.h> 00005 00006 /* 00007 * This simple default synchronization mechanism allows only creature at a time to 00008 * eat. The globalCatMouseSem is used as a a lock. We use a semaphore 00009 * rather than a lock so that this code will work even before locks are implemented. 00010 */ 00011 00012 /* 00013 * Replace this default synchronization mechanism with your own (better) mechanism 00014 * needed for your solution. Your mechanism may use any of the available synchronzation 00015 * primitives, e.g., semaphores, locks, condition variables. You are also free to 00016 * declare other global variables if your solution requires them. 00017 */ 00018 00019 /* 00020 * replace this with declarations of any synchronization and other variables you need here 00021 */ 00022 static struct semaphore *globalCatMouseSem; 00023 00024 00025 /* 00026 * The CatMouse simulation will call this function once before any cat or 00027 * mouse tries to each. 00028 * 00029 * You can use it to initialize synchronization and other variables. 00030 * 00031 * parameters: the number of bowls 00032 */ 00033 void 00034 catmouse_sync_init(int bowls) 00035 { 00036 /* replace this default implementation with your own implementation of catmouse_sync_init */ 00037 00038 (void)bowls; /* keep the compiler from complaining about unused parameters */ 00039 globalCatMouseSem = sem_create("globalCatMouseSem",1); 00040 if (globalCatMouseSem == NULL) { 00041 panic("could not create global CatMouse synchronization semaphore"); 00042 } 00043 return; 00044 } 00045 00046 /* 00047 * The CatMouse simulation will call this function once after all cat 00048 * and mouse simulations are finished. 00049 * 00050 * You can use it to clean up any synchronization and other variables. 00051 * 00052 * parameters: the number of bowls 00053 */ 00054 void 00055 catmouse_sync_cleanup(int bowls) 00056 { 00057 /* replace this default implementation with your own implementation of catmouse_sync_cleanup */ 00058 (void)bowls; /* keep the compiler from complaining about unused parameters */ 00059 KASSERT(globalCatMouseSem != NULL); 00060 sem_destroy(globalCatMouseSem); 00061 } 00062 00063 00064 /* 00065 * The CatMouse simulation will call this function each time a cat wants 00066 * to eat, before it eats. 00067 * This function should cause the calling thread (a cat simulation thread) 00068 * to block until it is OK for a cat to eat at the specified bowl. 00069 * 00070 * parameter: the number of the bowl at which the cat is trying to eat 00071 * legal bowl numbers are 1..NumBowls 00072 * 00073 * return value: none 00074 */ 00075 00076 void 00077 cat_before_eating(unsigned int bowl) 00078 { 00079 /* replace this default implementation with your own implementation of cat_before_eating */ 00080 (void)bowl; /* keep the compiler from complaining about an unused parameter */ 00081 KASSERT(globalCatMouseSem != NULL); 00082 P(globalCatMouseSem); 00083 } 00084 00085 /* 00086 * The CatMouse simulation will call this function each time a cat finishes 00087 * eating. 00088 * 00089 * You can use this function to wake up other creatures that may have been 00090 * waiting to eat until this cat finished. 00091 * 00092 * parameter: the number of the bowl at which the cat is finishing eating. 00093 * legal bowl numbers are 1..NumBowls 00094 * 00095 * return value: none 00096 */ 00097 00098 void 00099 cat_after_eating(unsigned int bowl) 00100 { 00101 /* replace this default implementation with your own implementation of cat_after_eating */ 00102 (void)bowl; /* keep the compiler from complaining about an unused parameter */ 00103 KASSERT(globalCatMouseSem != NULL); 00104 V(globalCatMouseSem); 00105 } 00106 00107 /* 00108 * The CatMouse simulation will call this function each time a mouse wants 00109 * to eat, before it eats. 00110 * This function should cause the calling thread (a mouse simulation thread) 00111 * to block until it is OK for a mouse to eat at the specified bowl. 00112 * 00113 * parameter: the number of the bowl at which the mouse is trying to eat 00114 * legal bowl numbers are 1..NumBowls 00115 * 00116 * return value: none 00117 */ 00118 00119 void 00120 mouse_before_eating(unsigned int bowl) 00121 { 00122 /* replace this default implementation with your own implementation of mouse_before_eating */ 00123 (void)bowl; /* keep the compiler from complaining about an unused parameter */ 00124 KASSERT(globalCatMouseSem != NULL); 00125 P(globalCatMouseSem); 00126 } 00127 00128 /* 00129 * The CatMouse simulation will call this function each time a mouse finishes 00130 * eating. 00131 * 00132 * You can use this function to wake up other creatures that may have been 00133 * waiting to eat until this mouse finished. 00134 * 00135 * parameter: the number of the bowl at which the mouse is finishing eating. 00136 * legal bowl numbers are 1..NumBowls 00137 * 00138 * return value: none 00139 */ 00140 00141 void 00142 mouse_after_eating(unsigned int bowl) 00143 { 00144 /* replace this default implementation with your own implementation of mouse_after_eating */ 00145 (void)bowl; /* keep the compiler from complaining about an unused parameter */ 00146 KASSERT(globalCatMouseSem != NULL); 00147 V(globalCatMouseSem); 00148 }