| [357ab79] | 1 | #include <stdio.h>
 | 
|---|
| [50be8af5] | 2 | #include <locks.hfa>
 | 
|---|
| [357ab79] | 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 | 
 | 
|---|
| [10b5970] | 20 | void main( Wait_Signal_1 & ) {
 | 
|---|
| [357ab79] | 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 | 
 | 
|---|
| [10b5970] | 34 | void main( Wait_3_Signal_3 & ) {
 | 
|---|
| [357ab79] | 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 | 
 | 
|---|
| [10b5970] | 50 | void main( Rec_Lock_Wait_Signal_1 & ) {
 | 
|---|
| [357ab79] | 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 | 
 | 
|---|
| [10b5970] | 68 | void main( Wait_Time_Signal_1 & ) {
 | 
|---|
| [357ab79] | 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);
 | 
|---|
| [10b5970] | 76 |                         (void) woken;
 | 
|---|
| [357ab79] | 77 |                 }else{
 | 
|---|
 | 78 |                         notify_one(c);
 | 
|---|
 | 79 |                 }
 | 
|---|
 | 80 |                 unlock(l);
 | 
|---|
 | 81 |         }
 | 
|---|
 | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | int main() {
 | 
|---|
 | 85 |         processor p[1];
 | 
|---|
 | 86 |         printf("Start Test 1: lock and condition variable single wait/notify\n");
 | 
|---|
 | 87 |         {
 | 
|---|
 | 88 |                 Wait_Signal_1 t1[2];
 | 
|---|
 | 89 |         }
 | 
|---|
 | 90 |         printf("Done Test 1\n");
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |         printf("Start Test 2: lock and condition variable 3 wait/notify all\n");
 | 
|---|
 | 93 |         {
 | 
|---|
 | 94 |                 Wait_3_Signal_3 t1[4];
 | 
|---|
 | 95 |         }
 | 
|---|
 | 96 |         printf("Done Test 2\n");
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |         printf("Start Test 3: lock and condition variable multiple acquire and wait/notify\n");
 | 
|---|
 | 99 |         {
 | 
|---|
 | 100 |                 Rec_Lock_Wait_Signal_1 t1[2];
 | 
|---|
 | 101 |         }
 | 
|---|
 | 102 |         printf("Done Test 3\n");
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |         printf("Start Test 4: lock and condition variable single timed wait/notify\n");
 | 
|---|
 | 105 |         {
 | 
|---|
 | 106 |                 Wait_Time_Signal_1 t1[2];
 | 
|---|
 | 107 |         }
 | 
|---|
 | 108 |         printf("Done Test 4\n");
 | 
|---|
 | 109 | }
 | 
|---|