Repository navigation
Expand file tree
/
Copy pathkthread.patch
More file actions
1247 lines (1227 loc) · 29.5 KB
/
Copy pathkthread.patch
File metadata and controls
1247 lines (1227 loc) · 29.5 KB
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/Makefile b/Makefile
index 39a99d7..929f6d4 100644
--- a/Makefile
+++ b/Makefile
@@ -132,6 +132,10 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_threads\
+ $U/_test\
+ $U/_mtest\
+
fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
diff --git a/kernel/defs.h b/kernel/defs.h
index a3c962b..a00a929 100644
--- a/kernel/defs.h
+++ b/kernel/defs.h
@@ -106,6 +106,11 @@ void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
+int create_thread(void (*fcn)(void*) , void* arg , void* stack) ;
+int join_thread(int id) ;
+void exit_thread() ;
+void new_sleep(uint64) ;
+void new_wakeup(int) ;
// swtch.S
void swtch(struct context*, struct context*);
@@ -173,7 +178,10 @@ uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64);
int copyinstr(pagetable_t, char *, uint64, uint64);
-
+int uvmmirror(pagetable_t ,pagetable_t,uint64,uint64) ;
+void freewalk(pagetable_t) ;
+uint64 uvmallocmirror(pagetable_t ,pagetable_t, uint64 , uint64) ;
+uint64 uvmdeallocmirror(pagetable_t , uint64 , uint64) ;
// plic.c
void plicinit(void);
void plicinithart(void);
diff --git a/kernel/proc.c b/kernel/proc.c
index 959b778..771d685 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -9,10 +9,11 @@
struct cpu cpus[NCPU];
struct proc proc[NPROC];
+struct spinlock memlocks[NPROC] ;
struct proc *initproc;
-int nextpid = 1;
+int nextpid = 1 ;
struct spinlock pid_lock;
extern void forkret(void);
@@ -50,9 +51,11 @@ procinit(void)
struct proc *p;
initlock(&pid_lock, "nextpid");
- initlock(&wait_lock, "wait_lock");
+ initlock(&wait_lock, "wait_lock") ;
+ int i = 0 ;
for(p = proc; p < &proc[NPROC]; p++) {
initlock(&p->lock, "proc");
+ initlock(&memlocks[i++], "mem") ;
p->state = UNUSED;
p->kstack = KSTACK((int) (p - proc));
}
@@ -102,6 +105,7 @@ allocpid()
return pid;
}
+
// Look in the process table for an UNUSED proc.
// If found, initialize state required to run in the kernel,
// and return with p->lock held.
@@ -110,20 +114,25 @@ static struct proc*
allocproc(void)
{
struct proc *p;
-
+ int i = 0 ;
for(p = proc; p < &proc[NPROC]; p++) {
acquire(&p->lock);
if(p->state == UNUSED) {
+ p->memlock = &memlocks[i] ;
+ //printf("%d\n",i) ;
goto found;
} else {
release(&p->lock);
}
+ i++ ;
}
return 0;
found:
p->pid = allocpid();
+ p->mem_id = p->pid ;
p->state = USED;
+ p->is_thread = 0 ;
// Allocate a trapframe page.
if((p->trapframe = (struct trapframe *)kalloc()) == 0){
@@ -155,11 +164,23 @@ found:
static void
freeproc(struct proc *p)
{
+ acquire(p->memlock) ;
if(p->trapframe)
kfree((void*)p->trapframe);
- p->trapframe = 0;
- if(p->pagetable)
- proc_freepagetable(p->pagetable, p->sz);
+ p->trapframe = 0 ;
+
+ //modifying the proc_freepagetable()
+ if(p->pagetable) {
+ uvmunmap(p->pagetable, TRAMPOLINE, 1, 0) ;
+ uvmunmap(p->pagetable, TRAPFRAME, 1, 0) ;
+ if(p->sz > 0) {
+ // if p->is_thread = 1 then it will not freeing the frames/pages of memory!
+ //because we didnot allocate(call kalloc()) in memory for thread creation
+ uvmunmap(p->pagetable, 0, PGROUNDUP(p->sz)/PGSIZE,!(p->is_thread));
+ }
+ freewalk(p->pagetable);
+ }
+ release(p->memlock) ;
p->pagetable = 0;
p->sz = 0;
p->pid = 0;
@@ -169,6 +190,9 @@ freeproc(struct proc *p)
p->killed = 0;
p->xstate = 0;
p->state = UNUSED;
+ p->is_thread = 0 ;
+ p->mem_id = 0 ;
+ p->memlock = 0 ;
}
// Create a user page table for a given process, with no user memory,
@@ -262,15 +286,40 @@ growproc(int n)
uint64 sz;
struct proc *p = myproc();
+ acquire(p->memlock) ;
sz = p->sz;
+
+ //for normal process / child process
if(n > 0){
if((sz = uvmalloc(p->pagetable, sz, sz + n, PTE_W)) == 0) {
+ release(p->memlock) ;
return -1;
}
} else if(n < 0){
sz = uvmdealloc(p->pagetable, sz, sz + n);
}
- p->sz = sz;
+ p->sz = sz ;
+ //release(p->memlock) ;
+
+
+ //newly added for to increase/decrease size in physical memory of all threads pointing to same memory location
+ //acquire(p->memlock) ;
+ for(struct proc* pr = proc ; pr < &proc[NPROC] ; pr++) {
+ acquire(&pr->lock) ;
+ if(p->pid != pr->pid && p->mem_id == pr->mem_id) {
+ sz = pr->sz ;
+ if(n > 0) {
+ //printf("HGSFDG\n\n") ;
+ sz = uvmallocmirror(p->pagetable, pr->pagetable ,sz, sz+n) ;
+ pr->sz = !sz ? pr->sz : sz ;
+ }
+ else if(n < 0) {
+ pr->sz = uvmdeallocmirror(p->pagetable, sz, sz + n);
+ }
+ }
+ release(&pr->lock) ;
+ }
+ release(p->memlock) ;
return 0;
}
@@ -287,7 +336,6 @@ fork(void)
if((np = allocproc()) == 0){
return -1;
}
-
// Copy user memory from parent to child.
if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
freeproc(np);
@@ -311,6 +359,7 @@ fork(void)
safestrcpy(np->name, p->name, sizeof(p->name));
pid = np->pid;
+ np->is_thread = 0 ;
release(&np->lock);
@@ -681,3 +730,138 @@ procdump(void)
printf("\n");
}
}
+
+int
+create_thread(void(*fcn)(void*) , void* arg , void* stack) {
+ int i, pid;
+ struct proc *np;
+ struct proc *p = myproc();
+
+ // Allocate process.
+ if((np = allocproc()) == 0){
+ return -1;
+ }
+
+ //just use the same adress space, same content
+ //donot allocate memory
+ if(uvmmirror(p->pagetable, np->pagetable, (uint64) 0, p->sz) < 0){
+ freeproc(np);
+ release(&np->lock);
+ return -1;
+ }
+ np->memlock = p->memlock ;
+ acquire(np->memlock) ;
+ np->sz = p->sz ;
+ release(np->memlock) ;
+ np->is_thread = 1 ;
+ np->mem_id = p->mem_id ;
+
+ // copy saved user registers.
+ *(np->trapframe) = *(p->trapframe);
+
+ //taking the arguments in a0 resgister
+ np->trapframe->a0 = (uint64)arg ;
+ np->trapframe->epc = (uint64) fcn ;
+ np->trapframe->sp = (1 << 12) + (uint64) stack ;
+ np->trapframe->sp-= np->trapframe->sp % 16 ; // same from exec.c
+ np->trapframe->ra = 0xffffffff ;
+
+ // increment reference counts on open file descriptors.
+ for(i = 0; i < NOFILE; i++)
+ if(p->ofile[i])
+ np->ofile[i] = filedup(p->ofile[i]);
+ np->cwd = idup(p->cwd);
+
+ safestrcpy(np->name, p->name, sizeof(p->name));
+
+ pid = np->pid;
+
+ release(&np->lock);
+
+ acquire(&wait_lock);
+ np->parent = p;
+ release(&wait_lock);
+
+ acquire(&np->lock);
+ np->state = RUNNABLE;
+ release(&np->lock);
+
+ return pid;
+}
+
+int
+join_thread(int id) {
+ struct proc *pp;
+ int havekids, pid;
+ struct proc *p = myproc();
+
+ acquire(&wait_lock);
+
+ for(;;){
+ // Scan through table looking for exited children.
+ havekids = 0;
+ for(pp = proc; pp < &proc[NPROC]; pp++){
+ if(pp->parent == p && pp->pid == id && pp->mem_id == p->mem_id){
+ // make sure the child isn't still in exit() or swtch().
+ acquire(&pp->lock);
+
+ havekids = 1;
+ if(pp->state == ZOMBIE){
+ // Found one.
+ pid = pp->pid ;
+ freeproc(pp);
+ release(&pp->lock);
+ release(&wait_lock);
+ return pid ;
+ }
+ release(&pp->lock);
+ }
+ }
+
+ // No point waiting if we don't have any children.
+ if(!havekids || killed(p)){
+ release(&wait_lock);
+ return -1;
+ }
+
+ // Wait for a child to exit.
+ sleep(p, &wait_lock); //DOC: wait-sleep
+ }
+}
+
+void
+exit_thread(void) {
+ exit(0) ;
+}
+
+void new_sleep(uint64 locked) {
+
+ struct proc* p = myproc() ;
+ acquire(&p->lock) ;
+ uint64 va0, pa0;
+ va0 = PGROUNDDOWN(locked);
+ pa0 = walkaddr(myproc()->pagetable, va0);
+ // if(pa0 == 0)
+ // return -1;
+ __sync_synchronize();
+ __sync_lock_release((uint8*)(pa0 + (locked - va0)));
+
+ p->state = SLEEPING ;
+ sched() ;
+
+ release(&p->lock) ;
+}
+
+void new_wakeup(int id) {
+ struct proc *p;
+
+ for(p = proc; p < &proc[NPROC]; p++) {
+ if(p != myproc()){
+ acquire(&p->lock);
+ if(p->state == SLEEPING && p->pid == id) {
+ p->state = RUNNABLE;
+ }
+ release(&p->lock);
+ }
+ }
+}
diff --git a/kernel/proc.h b/kernel/proc.h
index d021857..7342f14 100644
--- a/kernel/proc.h
+++ b/kernel/proc.h
@@ -104,4 +104,7 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ struct spinlock* memlock ;
+ int mem_id ;
+ int is_thread ;
};
diff --git a/kernel/syscall.c b/kernel/syscall.c
index ed65409..f17c9f0 100644
--- a/kernel/syscall.c
+++ b/kernel/syscall.c
@@ -101,6 +101,12 @@ extern uint64 sys_unlink(void);
extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
+extern uint64 sys_create_thread(void);
+extern uint64 sys_exit_thread(void);
+extern uint64 sys_join_thread(void);
+extern uint64 sys_yield(void) ;
+extern uint64 sys_release_sleep(void) ;
+extern uint64 sys_wakeup(void) ;
// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
@@ -126,6 +132,12 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
+[SYS_create_thread] sys_create_thread,
+[SYS_exit_thread] sys_exit_thread,
+[SYS_join_thread] sys_join_thread,
+[SYS_yield] sys_yield ,
+[SYS_release_sleep] sys_release_sleep ,
+[SYS_wakeup] sys_wakeup ,
};
void
diff --git a/kernel/syscall.h b/kernel/syscall.h
index bc5f356..c3a38c9 100644
--- a/kernel/syscall.h
+++ b/kernel/syscall.h
@@ -20,3 +20,10 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
+#define SYS_create_thread 22
+#define SYS_exit_thread 23
+#define SYS_join_thread 24
+#define SYS_yield 25
+#define SYS_release_sleep 26
+#define SYS_wakeup 27
+
diff --git a/kernel/sysproc.c b/kernel/sysproc.c
index 1de184e..9190a74 100644
--- a/kernel/sysproc.c
+++ b/kernel/sysproc.c
@@ -89,3 +89,53 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}
+
+uint64
+sys_create_thread(void) {
+ uint64 fcn ;
+ uint64 arg ;
+ uint64 stack ;
+ argaddr(0, &fcn) ;
+ argaddr(1 , &arg) ;
+ argaddr(2, &stack) ;
+
+ return create_thread((void*) fcn, (void*) arg, (void*) stack) ;
+}
+
+uint64
+sys_exit_thread(void) {
+ exit_thread() ;
+ return 0 ;
+}
+
+uint64
+sys_join_thread(void) {
+ int id ;
+ argint(0,&id) ;
+ return join_thread(id) ;
+}
+
+uint64
+sys_yield(void) {
+ int x ;
+ argint(0,&x) ;
+ yield() ;
+ return 0 ;
+}
+
+uint64
+sys_release_sleep(void) {
+ uint64 addr ;
+ argaddr(0, &addr) ;
+ new_sleep(addr) ;
+
+ return 0 ;
+}
+
+uint64
+sys_wakeup(void) {
+ int id ;
+ argint(0, &id) ;
+ new_wakeup(id) ;
+ return 0 ;
+}
\ No newline at end of file
diff --git a/kernel/vm.c b/kernel/vm.c
index 9f69783..21bc49e 100644
--- a/kernel/vm.c
+++ b/kernel/vm.c
@@ -290,9 +290,9 @@ freewalk(pagetable_t pagetable)
// then free page-table pages.
void
uvmfree(pagetable_t pagetable, uint64 sz)
-{
+{
if(sz > 0)
- uvmunmap(pagetable, 0, PGROUNDUP(sz)/PGSIZE, 1);
+ uvmunmap(pagetable, 0, PGROUNDUP(sz)/PGSIZE,1);
freewalk(pagetable);
}
@@ -332,6 +332,82 @@ uvmcopy(pagetable_t old, pagetable_t new, uint64 sz)
return -1;
}
+int
+uvmmirror(pagetable_t old, pagetable_t new, uint64 oldsz , uint64 newsz)
+{
+ pte_t *pte;
+ uint64 pa, i;
+ uint flags;
+
+ //newsz need to be aligned. if oldsz isd 3.5 KB and and newsz is 3.8 KB..round up
+ //of oldsz makes it 4096. But that size is allocated already using kalloc() before, kalloc() allocates 4KB for each page
+ //but whenever we have newsz = 4.6KB then we need iterate the loop to 4 KB + 4KB block.Because kalloc always allocate
+ //4KB for each allocation!
+ oldsz = PGROUNDUP(oldsz) ; //if oldsz is 0 the PG-ROUND-UP makes it 0.(0+4095) & (~4095) = 0
+ for(i = oldsz ; i < newsz; i += PGSIZE){
+ if((pte = walk(old, i, 0)) == 0)
+ panic("uvmmirror: pte should exist");
+ if((*pte & PTE_V) == 0)
+ panic("uvmmirror: page not present");
+ pa = PTE2PA(*pte);
+ flags = PTE_FLAGS(*pte);
+ if(mappages(new, i, PGSIZE, (uint64)pa, flags) != 0){
+ goto err;
+ }
+ }
+ return newsz ;
+
+ err:
+ uvmunmap(new, 0, i / PGSIZE, 0);
+ return -1;
+}
+
+uint64
+uvmallocmirror(pagetable_t parentptable,pagetable_t threadptable, uint64 oldsz, uint64 newsz)
+{
+ pte_t *pte;
+ uint64 pa, i;
+ uint flags;
+
+
+ if(newsz < oldsz)
+ return oldsz;
+
+ oldsz = PGROUNDUP(oldsz);
+ for(i = oldsz; i < newsz; i += PGSIZE){
+ if((pte = walk(parentptable, i, 0)) == 0)
+ panic("uvmmallocmirror: pte should exist");
+ if((*pte & PTE_V) == 0)
+ panic("uvmmallocmirror: page not present");
+ pa = PTE2PA(*pte);
+ flags = PTE_FLAGS(*pte);
+ if(mappages(threadptable, i, PGSIZE, (uint64)pa, flags) != 0){
+ uvmdeallocmirror(threadptable,i,oldsz);
+ return 0;
+ }
+ }
+ return newsz;
+
+}
+
+uint64
+uvmdeallocmirror(pagetable_t pagetable, uint64 oldsz, uint64 newsz)
+{
+ if(newsz >= oldsz)
+ return oldsz;
+
+ if(PGROUNDUP(newsz) < PGROUNDUP(oldsz)){
+ int npages = (PGROUNDUP(oldsz) - PGROUNDUP(newsz)) / PGSIZE;
+ uvmunmap(pagetable, PGROUNDUP(newsz), npages, 0);
+ }
+
+ return newsz;
+}
+
+
+
+
+
// mark a PTE invalid for user access.
// used by exec for the user stack guard page.
void
diff --git a/user/cv.h b/user/cv.h
new file mode 100644
index 0000000..2738e69
--- /dev/null
+++ b/user/cv.h
@@ -0,0 +1,84 @@
+#pragma once
+
+#include "kernel/types.h"
+#include "user/lock.h"
+#include "user/queue.h"
+#include "user/user.h"
+#define NTHREAD 64
+
+#include "kernel/types.h"
+struct tqueue{
+ int arr[64];
+ int front ;
+ int rear ;
+ int size ;
+} ;
+void init_tqueue(struct tqueue* q) {
+ q->front = q->size = q->rear = 0 ;
+}
+
+void tpush(struct tqueue* q , int x)
+{
+ q->arr[q->rear] = x;
+ q->rear = (q->rear+1)%NTHREAD;
+ q->size++;
+}
+int tfront(struct tqueue* q) {
+ if(q->size == 0) return -1;
+ return q->arr[q->front];
+}
+void tpop(struct tqueue *q)
+{
+ if(q->size != 0)
+ {
+ q->front = (q->front+1)%NTHREAD;
+ q->size--;
+ }
+}
+struct cond_var{
+ struct thread_mutex qlock ;
+ struct tqueue waiting_queue ;
+};
+
+void init_cond_var(struct cond_var* cv,struct thread_mutex* mutex) {
+ thread_mutex_init(&cv->qlock) ;
+ thread_mutex_init(mutex) ;
+ init_tqueue(&cv->waiting_queue) ;
+}
+
+void await(struct cond_var* cv ,struct thread_mutex* mutex) {
+ thread_mutex_lock(&cv->qlock) ;
+ tpush(&cv->waiting_queue,getpid()) ;
+ thread_mutex_unlock(&cv->qlock) ;
+
+ release_sleep(&mutex->locked) ;
+
+ thread_mutex_lock(mutex) ;
+}
+
+void notify(struct cond_var* cv) {
+ int tid ;
+
+ thread_mutex_lock(&cv->qlock); /* protect the queue */
+ tid = tfront(&cv->waiting_queue) ;
+ tpop(&cv->waiting_queue) ;
+ thread_mutex_unlock(&cv->qlock) ;
+
+ if (tid > 0)
+ wakeup(tid) ;
+
+ return;
+}
+
+void notifyall(struct cond_var* cv) {
+ int tid;
+
+ thread_mutex_lock(&cv->qlock); /* protect the queue */
+ while (cv->waiting_queue.size != 0) /* queue is not empty */
+ {
+ tid = tfront(&cv->waiting_queue); /* wake one */
+ tpop(&cv->waiting_queue) ;
+ wakeup(tid); /* Make it runnable */
+ }
+ thread_mutex_unlock(&cv->qlock) ; /* done with the queue */
+ }
\ No newline at end of file
diff --git a/user/lock.h b/user/lock.h
new file mode 100644
index 0000000..86d7a8f
--- /dev/null
+++ b/user/lock.h
@@ -0,0 +1,132 @@
+// Mutual exclusion spin locks.
+#pragma once
+
+#include "kernel/types.h"
+#include "kernel/riscv.h"
+#include "user/user.h"
+
+struct thread_spinlock {
+ uint8 locked; // Is the lock held?
+ int pid ;
+};
+
+int holding(struct thread_spinlock *lk) ;
+
+void
+thread_spin_init(struct thread_spinlock *lk)
+{
+ lk->locked = 0;
+ lk->pid = 0 ;
+}
+
+// Acquire the lock.
+// Loops (spins) until the lock is acquired.
+void
+thread_spin_lock(struct thread_spinlock *lk)
+{
+ // if(!holding(lk))
+ // return ;
+
+ // On RISC-V, sync_lock_test_and_set turns into an atomic swap:
+ // a5 = 1
+ // s1 = &lk->locked
+ // amoswap.w.aq a5, a5, (s1)
+ while(__sync_lock_test_and_set(&lk->locked, 1) != 0) ;
+
+ // Tell the C compiler and the processor to not move loads or stores
+ // past this point, to ensure that the critical section's memory
+ // references happen strictly after the lock is acquired.
+ // On RISC-V, this emits a fence instruction.
+ __sync_synchronize();
+
+ // Record info about lock acquisition for holding() and debugging.
+ lk->pid = getpid() ;
+}
+
+// Release the lock.
+void
+thread_spin_unlock(struct thread_spinlock *lk)
+{
+// if(!holding(lk))
+// return ;
+
+ lk->pid = 0 ;
+
+ // Tell the C compiler and the CPU to not move loads or stores
+ // past this point, to ensure that all the stores in the critical
+ // section are visible to other CPUs before the lock is released,
+ // and that loads in the critical section occur strictly before
+ // the lock is released.
+ // On RISC-V, this emits a fence instruction.
+ __sync_synchronize();
+
+ // Release the lock, equivalent to lk->locked = 0.
+ // This code doesn't use a C assignment, since the C standard
+ // implies that an assignment might be implemented with
+ // multiple store instructions.
+ // On RISC-V, sync_lock_release turns into an atomic swap:
+ // s1 = &lk->locked
+ // amoswap.w zero, zero, (s1)
+ __sync_lock_release(&lk->locked);
+}
+
+// Check whether this cpu is holding the lock.
+// Interrupts must be off.
+int
+holding(struct thread_spinlock *lk)
+{
+ return lk->locked && lk->pid == getpid() ;
+}
+
+struct thread_mutex{
+ uint8 locked ;
+ int pid ;
+} ;
+
+int holding2(struct thread_mutex *lk) {
+ return lk->locked && lk->pid == getpid() ;
+}
+
+void
+thread_mutex_init(struct thread_mutex *lk)
+{
+ lk->locked = 0;
+ lk->pid = 0;
+}
+
+void thread_mutex_lock(struct thread_mutex *lk) {
+ while(__sync_lock_test_and_set(&lk->locked, 1) != 0) {
+ //sleep(1) ;
+ yield(0) ;
+ }
+
+ // Tell the C compiler and the processor to not move loads or stores
+ // past this point, to ensure that the critical section's memory
+ // references happen strictly after the lock is acquired.
+ // On RISC-V, this emits a fence instruction.
+ __sync_synchronize();
+
+ // Record info about lock acquisition for holding() and debugging.
+ //lk->pid = getpid() ;
+}
+
+void thread_mutex_unlock(struct thread_mutex *lk) {
+ lk->pid = 0 ;
+
+ // Tell the C compiler and the CPU to not move loads or stores
+ // past this point, to ensure that all the stores in the critical
+ // section are visible to other CPUs before the lock is released,
+ // and that loads in the critical section occur strictly before
+ // the lock is released.
+ // On RISC-V, this emits a fence instruction.
+ __sync_synchronize();
+
+ // Release the lock, equivalent to lk->locked = 0.
+ // This code doesn't use a C assignment, since the C standard
+ // implies that an assignment might be implemented with
+ // multiple store instructions.
+ // On RISC-V, sync_lock_release turns into an atomic swap:
+ // s1 = &lk->locked
+ // amoswap.w zero, zero, (s1)
+ __sync_lock_release(&lk->locked);
+}
diff --git a/user/mtest.c b/user/mtest.c
new file mode 100644
index 0000000..390e334
--- /dev/null
+++ b/user/mtest.c
@@ -0,0 +1,142 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+#include "user/lock.h"
+
+struct balance {
+ char name[32];
+ int amount;
+};
+
+struct thread_mutex mx;
+
+volatile int total_balance = 0;
+
+volatile unsigned int delay(unsigned int d) {
+ unsigned int i;
+ for (i = 0; i < d; i++) {
+ __asm volatile("nop" :::);
+ }
+
+ return i;
+}
+
+volatile int _tid = 0;
+int *x[2];
+volatile int locked = 0;
+int n = 10000;
+
+int *smem;
+
+void do_work(void *arg) {
+ int i;
+ int old;
+ int tid;
+ tid = _tid++;
+ // printf("&tid: %p\n", &tid);
+
+ thread_mutex_lock(&mx);
+ printf("thread %d started\n", tid);
+ thread_mutex_unlock(&mx);
+
+ struct balance *b = (struct balance *)arg;
+ // printf( "Starting do_work: s:%s\n", b->name);
+ x[tid] = malloc(sizeof(int) * n);
+
+ thread_mutex_lock(&mx);
+ printf("thread %d malloced %p\n", tid, x[tid]);
+ thread_mutex_unlock(&mx);
+
+ for (int i = 0; i < n; i++) {
+ x[tid][i] = i;
+ }
+
+ for (i = 0; i < b->amount; i++) {
+ // lock and mlock will be implemented by you.
+ // thread_spin_lock(&lock);
+ // thread_mutex_lock(&mlock);
+ old = total_balance;
+ delay(100000);
+ // if(old != total_balance) printf("we will miss an update. old: %d
+ // total_balance: %d\n", old, total_balance);
+ total_balance = old + 1;
+ // thread_spin_unlock(&lock);
+ // thread_mutex_lock(&mlock);
+ }
+
+ for (int i = 0; i < n; i++) {
+ thread_mutex_lock(&mx);
+ smem[i]++;
+ thread_mutex_unlock(&mx);
+ }
+
+ // printf( "Done s:%x\n", b->name);
+ exit_thread();
+ return;
+}
+
+void empty_task() {
+ int tid;
+ tid = _tid++;
+ // printf("&tid: %p\n", &tid);
+ thread_mutex_lock(&mx);
+ printf("thread %d started\n", tid);
+ thread_mutex_unlock(&mx);
+
+ // printf( "Starting do_work: s:%s\n", b->name);
+
+ for (int i = 0; i < n; i++) {
+ thread_mutex_lock(&mx);
+ smem[i]++;
+ thread_mutex_unlock(&mx);
+ }
+
+ int *x = (int*)malloc(sizeof(int)*n);
+ int i = 0;
+ while(i<100){
+ x[i]++;
+ i = (i + 1) % n;
+ }
+ // printf( "Done s:%x\n", b->name);
+ free(x) ;
+ exit_thread();
+ return;
+}
+
+int main(int argc, char *argv[]) {
+
+ struct balance b1 = {"b1", 3200};
+ struct balance b2 = {"b2", 2800};
+ thread_mutex_init(&mx);
+
+ int thread1, thread2, r1, r2;
+
+ smem = malloc(sizeof(int) * n);
+
+ thread1 = create_thread(do_work, (void *)&b1,malloc(4096));
+ thread2 = create_thread(do_work, (void *)&b2,malloc(4096));
+ int cc = create_thread(empty_task, 0,malloc(4096));
+
+ r1 = join_thread(thread1);
+ r2 = join_thread(thread2);
+ join_thread(cc) ;
+
+ printf("Threads finished: (%d):%d, (%d):%d, shared balance:%d\n", thread1, r1,
+ thread2, r2, total_balance);
+ printf("Thread 1 malloced: %p\n", x[0]);
+ printf("Thread 2 malloced: %p\n", x[1]);
+ for (int i = 0; i < n; i++) {
+ if (x[0][i] != i || x[1][i] != i) {
+ printf("Doesn't match\n");
+ printf("%d vs %d for %d\n", x[0][i], x[0][i], i);
+ return 0;
+ }
+ if (smem[i] != 3) {
+ printf("is't 3\n");
+ return 0;
+ }
+ }
+ free(smem) ;
+ printf("Match\n");
+ exit(0);
+}
\ No newline at end of file
diff --git a/user/queue.h b/user/queue.h
new file mode 100644
index 0000000..fff948c
--- /dev/null
+++ b/user/queue.h
@@ -0,0 +1,29 @@
+#include "kernel/types.h"
+struct queue{
+ int arr[16];
+ int front ;
+ int rear ;
+ int size ;
+} ;
+void init_queue(struct queue* q) {
+ q->front = q->size = q->rear = 0 ;
+}
+
+void push(struct queue* q , int x)
+{
+ q->arr[q->rear] = x;
+ q->rear = (q->rear+1)%16;
+ q->size++;
+}
+int front(struct queue* q) {
+ if(q->size == 0) return -1;
+ return q->arr[q->front];
+}
+void pop(struct queue *q)
+{
+ if(q->size != 0)
+ {
+ q->front = (q->front+1)%16;
+ q->size--;
+ }
+}
\ No newline at end of file
diff --git a/user/semaphore.h b/user/semaphore.h
new file mode 100644
index 0000000..955abae
--- /dev/null
+++ b/user/semaphore.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "kernel/types.h"
+#include "user/lock.h"
+#include "user/cv.h"
+
+struct sem_t {
+ int count;
+ struct thread_mutex m;
+ struct cond_var cv;
+};
+
+int sem_init(struct sem_t* s, int value) {
+ s->count = value;
+ init_cond_var(&s->cv,&s->m);
+ return 0;
+}
+
+
+void sem_wait(struct sem_t* s) {
+ thread_mutex_lock(&s->m);
+ while (s->count <= 0) {
+ await(&s->cv, &s->m); /*unlock mutex, wait, relock mutex */