1 | #include <stdio.h>
|
---|
2 | #include "locks.hfa"
|
---|
3 | #include <stdlib.hfa>
|
---|
4 | #include <thread.hfa>
|
---|
5 | #include <time.h>
|
---|
6 | #include <stdlib.hfa>
|
---|
7 |
|
---|
8 | const unsigned int num_times = 50;
|
---|
9 |
|
---|
10 | simple_owner_lock l;
|
---|
11 | pthread_cond_var( simple_owner_lock ) c;
|
---|
12 |
|
---|
13 | owner_lock l2;
|
---|
14 | condition_variable( owner_lock ) c2;
|
---|
15 |
|
---|
16 | volatile int counter = 0;
|
---|
17 |
|
---|
18 | thread Wait_Signal_1 {};
|
---|
19 |
|
---|
20 | void main( Wait_Signal_1 & this ) {
|
---|
21 | for (unsigned int i = 0; i < num_times; i++) {
|
---|
22 | lock(l);
|
---|
23 | if(empty(c) && i != num_times - 1) {
|
---|
24 | wait(c,l);
|
---|
25 | }else{
|
---|
26 | notify_one(c);
|
---|
27 | }
|
---|
28 | unlock(l);
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | thread Wait_3_Signal_3 {};
|
---|
33 |
|
---|
34 | void main( Wait_3_Signal_3 & this ) {
|
---|
35 | for (unsigned int i = 0; i < num_times; i++) {
|
---|
36 | lock(l);
|
---|
37 | counter++;
|
---|
38 | if(counter == 4 || i == num_times - 1) {
|
---|
39 | counter = 0;
|
---|
40 | notify_all(c);
|
---|
41 | }else{
|
---|
42 | wait(c,l);
|
---|
43 | }
|
---|
44 | unlock(l);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | thread Rec_Lock_Wait_Signal_1 {};
|
---|
49 |
|
---|
50 | void main( Rec_Lock_Wait_Signal_1 & this ) {
|
---|
51 | for (unsigned int i = 0; i < num_times; i++) {
|
---|
52 | lock(l);
|
---|
53 | lock(l);
|
---|
54 | lock(l);
|
---|
55 | if(empty(c) && i != num_times - 1) {
|
---|
56 | wait(c,l);
|
---|
57 | }else{
|
---|
58 | notify_one(c);
|
---|
59 | }
|
---|
60 | unlock(l);
|
---|
61 | unlock(l);
|
---|
62 | unlock(l);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | thread Wait_Time_Signal_1 {};
|
---|
67 |
|
---|
68 | void main( Wait_Time_Signal_1 & this ) {
|
---|
69 | for (unsigned int i = 0; i < num_times; i++) {
|
---|
70 | lock(l);
|
---|
71 | if(empty(c) || random(10) >= 9 ) {
|
---|
72 | timespec t;
|
---|
73 | clock_gettime(CLOCK_REALTIME, &t);
|
---|
74 | timespec waitTime{0,1};
|
---|
75 | bool woken = wait(c,l, t + waitTime);
|
---|
76 | }else{
|
---|
77 | notify_one(c);
|
---|
78 | }
|
---|
79 | unlock(l);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | int main() {
|
---|
84 | processor p[1];
|
---|
85 | printf("Start Test 1: lock and condition variable single wait/notify\n");
|
---|
86 | {
|
---|
87 | Wait_Signal_1 t1[2];
|
---|
88 | }
|
---|
89 | printf("Done Test 1\n");
|
---|
90 |
|
---|
91 | printf("Start Test 2: lock and condition variable 3 wait/notify all\n");
|
---|
92 | {
|
---|
93 | Wait_3_Signal_3 t1[4];
|
---|
94 | }
|
---|
95 | printf("Done Test 2\n");
|
---|
96 |
|
---|
97 | printf("Start Test 3: lock and condition variable multiple acquire and wait/notify\n");
|
---|
98 | {
|
---|
99 | Rec_Lock_Wait_Signal_1 t1[2];
|
---|
100 | }
|
---|
101 | printf("Done Test 3\n");
|
---|
102 |
|
---|
103 | printf("Start Test 4: lock and condition variable single timed wait/notify\n");
|
---|
104 | {
|
---|
105 | Wait_Time_Signal_1 t1[2];
|
---|
106 | }
|
---|
107 | printf("Done Test 4\n");
|
---|
108 | }
|
---|