/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- catmouse_sync_init
- catmouse_sync_cleanup
- cat_before_eating
- cat_after_eating
- mouse_before_eating
- mouse_after_eating
1 #include <types.h>
2 #include <lib.h>
3 #include <synchprobs.h>
4 #include <synch.h>
5
6 /*
7 * This simple default synchronization mechanism allows only creature at a time to
8 * eat. The globalCatMouseSem is used as a a lock. We use a semaphore
9 * rather than a lock so that this code will work even before locks are implemented.
10 */
11
12 /*
13 * Replace this default synchronization mechanism with your own (better) mechanism
14 * needed for your solution. Your mechanism may use any of the available synchronzation
15 * primitives, e.g., semaphores, locks, condition variables. You are also free to
16 * declare other global variables if your solution requires them.
17 */
18
19 /*
20 * replace this with declarations of any synchronization and other variables you need here
21 */
22 static struct semaphore *globalCatMouseSem;
23
24
25 /*
26 * The CatMouse simulation will call this function once before any cat or
27 * mouse tries to each.
28 *
29 * You can use it to initialize synchronization and other variables.
30 *
31 * parameters: the number of bowls
32 */
33 void
34 catmouse_sync_init(int bowls)
35 {
36 /* replace this default implementation with your own implementation of catmouse_sync_init */
37
38 (void)bowls; /* keep the compiler from complaining about unused parameters */
39 globalCatMouseSem = sem_create("globalCatMouseSem",1);
40 if (globalCatMouseSem == NULL) {
41 panic("could not create global CatMouse synchronization semaphore");
42 }
43 return;
44 }
45
46 /*
47 * The CatMouse simulation will call this function once after all cat
48 * and mouse simulations are finished.
49 *
50 * You can use it to clean up any synchronization and other variables.
51 *
52 * parameters: the number of bowls
53 */
54 void
55 catmouse_sync_cleanup(int bowls)
56 {
57 /* replace this default implementation with your own implementation of catmouse_sync_cleanup */
58 (void)bowls; /* keep the compiler from complaining about unused parameters */
59 KASSERT(globalCatMouseSem != NULL);
60 sem_destroy(globalCatMouseSem);
61 }
62
63
64 /*
65 * The CatMouse simulation will call this function each time a cat wants
66 * to eat, before it eats.
67 * This function should cause the calling thread (a cat simulation thread)
68 * to block until it is OK for a cat to eat at the specified bowl.
69 *
70 * parameter: the number of the bowl at which the cat is trying to eat
71 * legal bowl numbers are 1..NumBowls
72 *
73 * return value: none
74 */
75
76 void
77 cat_before_eating(unsigned int bowl)
78 {
79 /* replace this default implementation with your own implementation of cat_before_eating */
80 (void)bowl; /* keep the compiler from complaining about an unused parameter */
81 KASSERT(globalCatMouseSem != NULL);
82 P(globalCatMouseSem);
83 }
84
85 /*
86 * The CatMouse simulation will call this function each time a cat finishes
87 * eating.
88 *
89 * You can use this function to wake up other creatures that may have been
90 * waiting to eat until this cat finished.
91 *
92 * parameter: the number of the bowl at which the cat is finishing eating.
93 * legal bowl numbers are 1..NumBowls
94 *
95 * return value: none
96 */
97
98 void
99 cat_after_eating(unsigned int bowl)
100 {
101 /* replace this default implementation with your own implementation of cat_after_eating */
102 (void)bowl; /* keep the compiler from complaining about an unused parameter */
103 KASSERT(globalCatMouseSem != NULL);
104 V(globalCatMouseSem);
105 }
106
107 /*
108 * The CatMouse simulation will call this function each time a mouse wants
109 * to eat, before it eats.
110 * This function should cause the calling thread (a mouse simulation thread)
111 * to block until it is OK for a mouse to eat at the specified bowl.
112 *
113 * parameter: the number of the bowl at which the mouse is trying to eat
114 * legal bowl numbers are 1..NumBowls
115 *
116 * return value: none
117 */
118
119 void
120 mouse_before_eating(unsigned int bowl)
121 {
122 /* replace this default implementation with your own implementation of mouse_before_eating */
123 (void)bowl; /* keep the compiler from complaining about an unused parameter */
124 KASSERT(globalCatMouseSem != NULL);
125 P(globalCatMouseSem);
126 }
127
128 /*
129 * The CatMouse simulation will call this function each time a mouse finishes
130 * eating.
131 *
132 * You can use this function to wake up other creatures that may have been
133 * waiting to eat until this mouse finished.
134 *
135 * parameter: the number of the bowl at which the mouse is finishing eating.
136 * legal bowl numbers are 1..NumBowls
137 *
138 * return value: none
139 */
140
141 void
142 mouse_after_eating(unsigned int bowl)
143 {
144 /* replace this default implementation with your own implementation of mouse_after_eating */
145 (void)bowl; /* keep the compiler from complaining about an unused parameter */
146 KASSERT(globalCatMouseSem != NULL);
147 V(globalCatMouseSem);
148 }