forked from Legacy25/Kernel-Panic-OS161
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_my.patch
1304 lines (1287 loc) · 34.1 KB
/
submit_my.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/.gitignore b/.gitignore
index ccea0c4..6520458 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,9 +1,6 @@
-.depend
-.settings
-.project
-.cproject
-/kern/compile/
-defs.mk
build
-/user/testbin/randcall/calls.c
-*.swp
+.depend
+/defs.mk
+kern/compile/
+user/testbin/randcall/calls.c
+typescript
diff --git a/confinstall.sh b/confinstall.sh
new file mode 100755
index 0000000..1934176
--- /dev/null
+++ b/confinstall.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+default=ASST1
+
+if [ $# -eq 0 ]
+then
+ asst=$default
+ echo $asst
+else
+ asst=$1
+fi
+
+cd /home/trinity/src/kern/conf
+./config $asst
+cd ../compile/$asst
+bmake depend
+bmake
+bmake install
\ No newline at end of file
diff --git a/kern/include/synch.h b/kern/include/synch.h
index ac3714b..5d3281d 100644
--- a/kern/include/synch.h
+++ b/kern/include/synch.h
@@ -1,156 +1,172 @@
-/*
- * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 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.
- */
-
-#ifndef _SYNCH_H_
-#define _SYNCH_H_
-
-/*
- * Header file for synchronization primitives.
- */
-
-
-#include <spinlock.h>
-
-/*
- * Dijkstra-style semaphore.
- *
- * The name field is for easier debugging. A copy of the name is made
- * internally.
- */
-struct semaphore {
- char *sem_name;
- struct wchan *sem_wchan;
- struct spinlock sem_lock;
- volatile int sem_count;
-};
-
-struct semaphore *sem_create(const char *name, int initial_count);
-void sem_destroy(struct semaphore *);
-
-/*
- * Operations (both atomic):
- * P (proberen): decrement count. If the count is 0, block until
- * the count is 1 again before decrementing.
- * V (verhogen): increment count.
- */
-void P(struct semaphore *);
-void V(struct semaphore *);
-
-
-/*
- * Simple lock for mutual exclusion.
- *
- * When the lock is created, no thread should be holding it. Likewise,
- * when the lock is destroyed, no thread should be holding it.
- *
- * The name field is for easier debugging. A copy of the name is
- * (should be) made internally.
- */
-struct lock {
- char *lk_name;
- // add what you need here
- // (don't forget to mark things volatile as needed)
-};
-
-struct lock *lock_create(const char *name);
-void lock_acquire(struct lock *);
-
-/*
- * Operations:
- * lock_acquire - Get the lock. Only one thread can hold the lock at the
- * same time.
- * lock_release - Free the lock. Only the thread holding the lock may do
- * this.
- * lock_do_i_hold - Return true if the current thread holds the lock;
- * false otherwise.
- *
- * These operations must be atomic. You get to write them.
- */
-void lock_release(struct lock *);
-bool lock_do_i_hold(struct lock *);
-void lock_destroy(struct lock *);
-
-
-/*
- * Condition variable.
- *
- * Note that the "variable" is a bit of a misnomer: a CV is normally used
- * to wait until a variable meets a particular condition, but there's no
- * actual variable, as such, in the CV.
- *
- * These CVs are expected to support Mesa semantics, that is, no
- * guarantees are made about scheduling.
- *
- * The name field is for easier debugging. A copy of the name is
- * (should be) made internally.
- */
-
-struct cv {
- char *cv_name;
- // add what you need here
- // (don't forget to mark things volatile as needed)
-};
-
-struct cv *cv_create(const char *name);
-void cv_destroy(struct cv *);
-
-/*
- * Operations:
- * cv_wait - Release the supplied lock, go to sleep, and, after
- * waking up again, re-acquire the lock.
- * cv_signal - Wake up one thread that's sleeping on this CV.
- * cv_broadcast - Wake up all threads sleeping on this CV.
- *
- * For all three operations, the current thread must hold the lock passed
- * in. Note that under normal circumstances the same lock should be used
- * on all operations with any particular CV.
- *
- * These operations must be atomic. You get to write them.
- */
-void cv_wait(struct cv *cv, struct lock *lock);
-void cv_signal(struct cv *cv, struct lock *lock);
-void cv_broadcast(struct cv *cv, struct lock *lock);
-
-/*
- * 13 Feb 2012 : GWA : Reader-writer locks.
- */
-
-struct rwlock {
- char *rwlock_name;
-};
-
-struct rwlock * rwlock_create(const char *);
-void rwlock_destroy(struct rwlock *);
-
-void rwlock_acquire_read(struct rwlock *);
-void rwlock_release_read(struct rwlock *);
-void rwlock_acquire_write(struct rwlock *);
-void rwlock_release_write(struct rwlock *);
-
-#endif /* _SYNCH_H_ */
+/*
+ * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 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.
+ */
+
+#ifndef _SYNCH_H_
+#define _SYNCH_H_
+
+/*
+ * Header file for synchronization primitives.
+ */
+
+#include <types.h>
+#include <spinlock.h>
+
+/*
+ * Dijkstra-style semaphore.
+ *
+ * The name field is for easier debugging. A copy of the name is made
+ * internally.
+ */
+struct semaphore {
+ char *sem_name;
+ struct wchan *sem_wchan;
+ struct spinlock sem_lock;
+ volatile int sem_count;
+};
+
+struct semaphore *sem_create(const char *name, int initial_count);
+void sem_destroy(struct semaphore *);
+
+/*
+ * Operations (both atomic):
+ * P (proberen): decrement count. If the count is 0, block until
+ * the count is 1 again before decrementing.
+ * V (verhogen): increment count.
+ */
+void P(struct semaphore *);
+void V(struct semaphore *);
+
+
+/*
+ * Simple lock for mutual exclusion.
+ *
+ * When the lock is created, no thread should be holding it. Likewise,
+ * when the lock is destroyed, no thread should be holding it.
+ *
+ * The name field is for easier debugging. A copy of the name is
+ * (should be) made internally.
+ */
+struct lock {
+ char *lk_name;
+ struct thread *lk_holder;
+ struct wchan *lk_wchan;
+ struct spinlock lk_lock;
+};
+
+struct lock *lock_create(const char *name);
+void lock_acquire(struct lock *);
+
+/*
+ * Operations:
+ * lock_acquire - Get the lock. Only one thread can hold the lock at the
+ * same time.
+ * lock_release - Free the lock. Only the thread holding the lock may do
+ * this.
+ * lock_do_i_hold - Return true if the current thread holds the lock;
+ * false otherwise.
+ *
+ * These operations must be atomic. You get to write them.
+ */
+void lock_release(struct lock *);
+bool lock_do_i_hold(struct lock *);
+void lock_destroy(struct lock *);
+
+
+/*
+ * Condition variable.
+ *
+ * Note that the "variable" is a bit of a misnomer: a CV is normally used
+ * to wait until a variable meets a particular condition, but there's no
+ * actual variable, as such, in the CV.
+ *
+ * These CVs are expected to support Mesa semantics, that is, no
+ * guarantees are made about scheduling.
+ *
+ * The name field is for easier debugging. A copy of the name is
+ * (should be) made internally.
+ */
+
+struct cv {
+ char *cv_name;
+ struct wchan *cv_wchan;
+};
+
+struct cv *cv_create(const char *name);
+void cv_destroy(struct cv *);
+
+/*
+ * Operations:
+ * cv_wait - Release the supplied lock, go to sleep, and, after
+ * waking up again, re-acquire the lock.
+ * cv_signal - Wake up one thread that's sleeping on this CV.
+ * cv_broadcast - Wake up all threads sleeping on this CV.
+ *
+ * For all three operations, the current thread must hold the lock passed
+ * in. Note that under normal circumstances the same lock should be used
+ * on all operations with any particular CV.
+ *
+ * These operations must be atomic. You get to write them.
+ */
+void cv_wait(struct cv *cv, struct lock *lock);
+void cv_signal(struct cv *cv, struct lock *lock);
+void cv_broadcast(struct cv *cv, struct lock *lock);
+
+/*
+ * 13 Feb 2012 : GWA : Reader-writer locks.
+ */
+
+#define RW_MODE_READ 0
+#define RW_MODE_WRITES_WAITING 1
+#define RW_MODE_WRITE 2
+#define RW_MIN_READ_THREADS 4
+
+struct rwlock {
+ char *rwlock_name;
+
+ struct wchan *rwlock_rd_wchan;
+ struct wchan *rwlock_wr_wchan;
+
+ volatile int rwlock_rdlk_count;
+ volatile int rwlock_wrlk_count;
+
+ struct spinlock rwlock_lock;
+
+ uint8_t rwlock_mode;
+ uint8_t rwlock_rd_threads_serviced;
+};
+
+struct rwlock * rwlock_create(const char *);
+void rwlock_destroy(struct rwlock *);
+
+void rwlock_acquire_read(struct rwlock *);
+void rwlock_release_read(struct rwlock *);
+void rwlock_acquire_write(struct rwlock *);
+void rwlock_release_write(struct rwlock *);
+
+#endif /* _SYNCH_H_ */
diff --git a/kern/include/test.h b/kern/include/test.h
index 240d583..8a195ac 100644
--- a/kern/include/test.h
+++ b/kern/include/test.h
@@ -90,6 +90,7 @@ int semtest(int, char **);
int locktest(int, char **);
int cvtest(int, char **);
int cvtest2(int, char **);
+int rwlocktest(int, char **);
/* filesystem tests */
int fstest(int, char **);
diff --git a/kern/startup/main.c b/kern/startup/main.c
index be4c4b8..4c4f3c8 100644
--- a/kern/startup/main.c
+++ b/kern/startup/main.c
@@ -1,3 +1,5 @@
+/* Comment added by arindamn@buffalo.edu */
+
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
@@ -100,7 +102,7 @@ boot(void)
kprintf("%s", harvard_copyright);
kprintf("\n");
- kprintf("Put-your-group-name-here's system version %s (%s #%d)\n",
+ kprintf("Kernel Panic's system version %s (%s #%d)\n",
GROUP_VERSION, buildconfig, buildversion);
kprintf("\n");
diff --git a/kern/startup/menu.c b/kern/startup/menu.c
index 6c71551..28a045f 100644
--- a/kern/startup/menu.c
+++ b/kern/startup/menu.c
@@ -449,6 +449,7 @@ static const char *testmenu[] = {
"[sy1] Semaphore test ",
"[sy2] Lock test (1) ",
"[sy3] CV test (1) ",
+ "[sy4] RW Lock test ",
"[sy5] CV test 2 (1) ",
"[sp1] Whalematching Driver (1) ",
"[sp2] Stoplight Driver (1) ",
@@ -546,6 +547,7 @@ static struct {
/* synchronization assignment tests */
{ "sy2", locktest },
{ "sy3", cvtest },
+ { "sy4", rwlocktest },
{ "sy5", cvtest2 },
#if OPT_SYNCHPROBS
diff --git a/kern/synchprobs/problems.c b/kern/synchprobs/problems.c
index 81d2f0e..d1154f5 100644
--- a/kern/synchprobs/problems.c
+++ b/kern/synchprobs/problems.c
@@ -47,7 +47,14 @@
// functions will allow you to do local initialization. They are called at
// the top of the corresponding driver code.
+struct semaphore *whl_male;
+struct semaphore *whl_female;
+struct semaphore *whl_matchmkr;
+
void whalemating_init() {
+ whl_male = sem_create("male_whale", 0);
+ whl_female = sem_create("female_whale", 0);
+ whl_matchmkr = sem_create("matchmaker_whale", 0);
return;
}
@@ -55,6 +62,9 @@ void whalemating_init() {
// care if your problems leak memory, but if you do, use this to clean up.
void whalemating_cleanup() {
+ sem_destroy(whl_male);
+ sem_destroy(whl_female);
+ sem_destroy(whl_matchmkr);
return;
}
@@ -66,6 +76,7 @@ male(void *p, unsigned long which)
male_start();
// Implement this function
+ P(whl_male);
male_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
@@ -82,6 +93,7 @@ female(void *p, unsigned long which)
female_start();
// Implement this function
+ P(whl_female);
female_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
@@ -98,6 +110,10 @@ matchmaker(void *p, unsigned long which)
matchmaker_start();
// Implement this function
+ V(whl_male);
+ V(whl_female);
+ V(whl_matchmkr);
+ P(whl_matchmkr);
matchmaker_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
diff --git a/kern/test/synchtest.c b/kern/test/synchtest.c
index d072469..d5ddef1 100644
--- a/kern/test/synchtest.c
+++ b/kern/test/synchtest.c
@@ -50,6 +50,7 @@ static struct semaphore *testsem;
static struct lock *testlock;
static struct cv *testcv;
static struct semaphore *donesem;
+static struct rwlock *rw;
static
void
@@ -79,6 +80,12 @@ inititems(void)
panic("synchtest: sem_create failed\n");
}
}
+ if (rw==NULL) {
+ rw = rwlock_create("rwlock");
+ if (rw == NULL) {
+ panic("synchtest: rwlock_create failed\n");
+ }
+ }
}
static
@@ -357,3 +364,68 @@ cvtest2(int nargs, char **args)
return 0;
}
+
+
+
+volatile int rwtestval = 7;
+
+static
+void
+rwtestthread(void *junk, unsigned long num)
+{
+ (void)junk;
+
+ bool writer = false;
+
+ if((num+1) % 4 == 0)
+ writer = true;
+
+ if(writer) {
+ rwlock_acquire_write(rw);
+ rwtestval--;
+ kprintf("Writer thread %lu decrementing value to %d\n", num, rwtestval);
+ thread_yield();
+ rwlock_release_write(rw);
+ }
+ else {
+ rwlock_acquire_read(rw);
+ kprintf("Reader thread %lu reads value %d\n", num, rwtestval);
+ rwlock_release_read(rw);
+ }
+
+ V(donesem);
+}
+
+int
+rwlocktest(int nargs, char **args)
+{
+ int i, result;
+
+ (void)nargs;
+ (void)args;
+
+ inititems();
+
+ kprintf("Starting RW Lock test...\n");
+
+ for (i=0; i<NTHREADS; i++) {
+ if(i%4 == 0)
+ result = thread_fork("writer", rwtestthread, NULL, i, NULL);
+ else
+ result = thread_fork("reader", rwtestthread, NULL, i, NULL);
+
+ if (result) {
+ panic("rwtest: thread_fork failed: %s\n",
+ strerror(result));
+ }
+ }
+
+ for (i=0; i<NTHREADS; i++) {
+ P(donesem);
+ }
+
+ kprintf("RW test done\n");
+
+ return 0;
+
+}
diff --git a/kern/thread/synch.c b/kern/thread/synch.c
index 9a7468c..e3f0682 100644
--- a/kern/thread/synch.c
+++ b/kern/thread/synch.c
@@ -1,267 +1,454 @@
-/*
- * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 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.
- */
-
-/*
- * Synchronization primitives.
- * The specifications of the functions are in synch.h.
- */
-
-#include <types.h>
-#include <lib.h>
-#include <spinlock.h>
-#include <wchan.h>
-#include <thread.h>
-#include <current.h>
-#include <synch.h>
-
-////////////////////////////////////////////////////////////
-//
-// Semaphore.
-
-struct semaphore *
-sem_create(const char *name, int initial_count)
-{
- struct semaphore *sem;
-
- KASSERT(initial_count >= 0);
-
- sem = kmalloc(sizeof(struct semaphore));
- if (sem == NULL) {
- return NULL;
- }
-
- sem->sem_name = kstrdup(name);
- if (sem->sem_name == NULL) {
- kfree(sem);
- return NULL;
- }
-
- sem->sem_wchan = wchan_create(sem->sem_name);
- if (sem->sem_wchan == NULL) {
- kfree(sem->sem_name);
- kfree(sem);
- return NULL;
- }
-
- spinlock_init(&sem->sem_lock);
- sem->sem_count = initial_count;
-
- return sem;
-}
-
-void
-sem_destroy(struct semaphore *sem)
-{
- KASSERT(sem != NULL);
-
- /* wchan_cleanup will assert if anyone's waiting on it */
- spinlock_cleanup(&sem->sem_lock);
- wchan_destroy(sem->sem_wchan);
- kfree(sem->sem_name);
- kfree(sem);
-}
-
-void
-P(struct semaphore *sem)
-{
- KASSERT(sem != NULL);
-
- /*
- * May not block in an interrupt handler.
- *
- * For robustness, always check, even if we can actually
- * complete the P without blocking.
- */
- KASSERT(curthread->t_in_interrupt == false);
-
- spinlock_acquire(&sem->sem_lock);
- while (sem->sem_count == 0) {
- /*
- * Bridge to the wchan lock, so if someone else comes
- * along in V right this instant the wakeup can't go
- * through on the wchan until we've finished going to
- * sleep. Note that wchan_sleep unlocks the wchan.
- *
- * Note that we don't maintain strict FIFO ordering of
- * threads going through the semaphore; that is, we
- * might "get" it on the first try even if other
- * threads are waiting. Apparently according to some
- * textbooks semaphores must for some reason have
- * strict ordering. Too bad. :-)
- *
- * Exercise: how would you implement strict FIFO
- * ordering?
- */
- wchan_lock(sem->sem_wchan);
- spinlock_release(&sem->sem_lock);
- wchan_sleep(sem->sem_wchan);
-
- spinlock_acquire(&sem->sem_lock);
- }
- KASSERT(sem->sem_count > 0);
- sem->sem_count--;
- spinlock_release(&sem->sem_lock);
-}
-
-void
-V(struct semaphore *sem)
-{
- KASSERT(sem != NULL);
-
- spinlock_acquire(&sem->sem_lock);
-
- sem->sem_count++;
- KASSERT(sem->sem_count > 0);
- wchan_wakeone(sem->sem_wchan);
-
- spinlock_release(&sem->sem_lock);
-}
-
-////////////////////////////////////////////////////////////
-//
-// Lock.
-
-struct lock *
-lock_create(const char *name)
-{
- struct lock *lock;
-
- lock = kmalloc(sizeof(struct lock));
- if (lock == NULL) {
- return NULL;
- }
-
- lock->lk_name = kstrdup(name);
- if (lock->lk_name == NULL) {
- kfree(lock);
- return NULL;
- }
-
- // add stuff here as needed
-
- return lock;
-}
-
-void
-lock_destroy(struct lock *lock)
-{
- KASSERT(lock != NULL);
-
- // add stuff here as needed
-
- kfree(lock->lk_name);
- kfree(lock);
-}
-
-void
-lock_acquire(struct lock *lock)
-{
- // Write this
-
- (void)lock; // suppress warning until code gets written
-}
-
-void
-lock_release(struct lock *lock)
-{
- // Write this
-
- (void)lock; // suppress warning until code gets written
-}
-
-bool
-lock_do_i_hold(struct lock *lock)
-{
- // Write this
-
- (void)lock; // suppress warning until code gets written
-
- return true; // dummy until code gets written
-}
-
-////////////////////////////////////////////////////////////
-//
-// CV
-
-
-struct cv *
-cv_create(const char *name)
-{
- struct cv *cv;
-
- cv = kmalloc(sizeof(struct cv));
- if (cv == NULL) {
- return NULL;
- }
-
- cv->cv_name = kstrdup(name);
- if (cv->cv_name==NULL) {
- kfree(cv);
- return NULL;
- }
-
- // add stuff here as needed
-
- return cv;
-}
-
-void
-cv_destroy(struct cv *cv)
-{
- KASSERT(cv != NULL);
-
- // add stuff here as needed
-
- kfree(cv->cv_name);
- kfree(cv);
-}
-
-void
-cv_wait(struct cv *cv, struct lock *lock)
-{
- // Write this
- (void)cv; // suppress warning until code gets written
- (void)lock; // suppress warning until code gets written
-}
-
-void
-cv_signal(struct cv *cv, struct lock *lock)
-{
- // Write this
- (void)cv; // suppress warning until code gets written
- (void)lock; // suppress warning until code gets written
-}
-
-void
-cv_broadcast(struct cv *cv, struct lock *lock)
-{
- // Write this
- (void)cv; // suppress warning until code gets written
- (void)lock; // suppress warning until code gets written
-}
+/*
+ * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 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.
+ */
+
+/*
+ * Synchronization primitives.
+ * The specifications of the functions are in synch.h.
+ */
+
+#include <types.h>
+#include <lib.h>
+#include <spinlock.h>
+#include <wchan.h>
+#include <thread.h>
+#include <current.h>
+#include <synch.h>
+
+////////////////////////////////////////////////////////////
+//
+// Semaphore.
+
+struct semaphore *
+sem_create(const char *name, int initial_count)
+{
+ struct semaphore *sem;
+
+ KASSERT(initial_count >= 0);
+
+ sem = kmalloc(sizeof(struct semaphore));
+ if (sem == NULL) {
+ return NULL;
+ }
+
+ sem->sem_name = kstrdup(name);
+ if (sem->sem_name == NULL) {
+ kfree(sem);
+ return NULL;
+ }
+
+ sem->sem_wchan = wchan_create(sem->sem_name);
+ if (sem->sem_wchan == NULL) {
+ kfree(sem->sem_name);
+ kfree(sem);
+ return NULL;
+ }
+
+ spinlock_init(&sem->sem_lock);
+ sem->sem_count = initial_count;
+
+ return sem;
+}
+
+void
+sem_destroy(struct semaphore *sem)
+{
+ KASSERT(sem != NULL);
+
+ /* wchan_cleanup will assert if anyone's waiting on it */
+ spinlock_cleanup(&sem->sem_lock);
+ wchan_destroy(sem->sem_wchan);
+ kfree(sem->sem_name);
+ kfree(sem);
+}
+
+void
+P(struct semaphore *sem)
+{
+ KASSERT(sem != NULL);
+
+ /*
+ * May not block in an interrupt handler.
+ *
+ * For robustness, always check, even if we can actually
+ * complete the P without blocking.
+ */
+ KASSERT(curthread->t_in_interrupt == false);
+
+ spinlock_acquire(&sem->sem_lock);
+ while (sem->sem_count == 0) {
+ /*
+ * Bridge to the wchan lock, so if someone else comes
+ * along in V right this instant the wakeup can't go
+ * through on the wchan until we've finished going to
+ * sleep. Note that wchan_sleep unlocks the wchan.
+ *
+ * Note that we don't maintain strict FIFO ordering of
+ * threads going through the semaphore; that is, we
+ * might "get" it on the first try even if other
+ * threads are waiting. Apparently according to some
+ * textbooks semaphores must for some reason have
+ * strict ordering. Too bad. :-)
+ *
+ * Exercise: how would you implement strict FIFO
+ * ordering?
+ */
+ wchan_lock(sem->sem_wchan);
+ spinlock_release(&sem->sem_lock);
+ wchan_sleep(sem->sem_wchan);
+
+ spinlock_acquire(&sem->sem_lock);
+ }
+ KASSERT(sem->sem_count > 0);
+ sem->sem_count--;
+ spinlock_release(&sem->sem_lock);
+}
+
+void
+V(struct semaphore *sem)
+{
+ KASSERT(sem != NULL);
+
+ spinlock_acquire(&sem->sem_lock);
+
+ sem->sem_count++;
+ KASSERT(sem->sem_count > 0);
+ wchan_wakeone(sem->sem_wchan);
+
+ spinlock_release(&sem->sem_lock);
+}
+
+////////////////////////////////////////////////////////////
+//
+// Lock.
+
+struct lock *