| 1 | #include <stdio.h> | 
|---|
| 2 | #include "locks.hfa" | 
|---|
| 3 | #include "alarm.hfa" | 
|---|
| 4 | #include <stdlib.hfa> | 
|---|
| 5 | #include <thread.hfa> | 
|---|
| 6 | #include "kernel.cfa" | 
|---|
| 7 |  | 
|---|
| 8 | multiple_acquisition_lock m; | 
|---|
| 9 | condition_variable( multiple_acquisition_lock ) c_m; | 
|---|
| 10 |  | 
|---|
| 11 | semaphore s; // used for barrier like behaviour | 
|---|
| 12 |  | 
|---|
| 13 | const unsigned int NoOfTimes = 20; | 
|---|
| 14 |  | 
|---|
| 15 | void block() { | 
|---|
| 16 | if (s.count == 0) { | 
|---|
| 17 | P(s); | 
|---|
| 18 | } else { | 
|---|
| 19 | V(s); | 
|---|
| 20 | } | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | thread T1 {}; | 
|---|
| 24 |  | 
|---|
| 25 | void main( T1 & this ) { | 
|---|
| 26 | lock(m); | 
|---|
| 27 | wait( c_m, m, 1`s ); | 
|---|
| 28 | // printf("Thread: %p timedout\n", active_thread()); // removed since can't expect non deterministic output | 
|---|
| 29 |  | 
|---|
| 30 | block(); | 
|---|
| 31 |  | 
|---|
| 32 | // Test calls which occur increasingly close to timeout value. | 
|---|
| 33 |  | 
|---|
| 34 | for ( unsigned int i = 0; i < NoOfTimes + 3; i += 1 ) { | 
|---|
| 35 | if ( wait( c_m, m, 1`s ) ) { | 
|---|
| 36 | // printf("Thread: %p signalled\n", active_thread()); // removed since can't expect non deterministic output | 
|---|
| 37 | } else { | 
|---|
| 38 | // printf("Thread: %p timedout\n", active_thread()); // removed since can't expect non deterministic output | 
|---|
| 39 | } // if | 
|---|
| 40 |  | 
|---|
| 41 | block(); | 
|---|
| 42 | } // for | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | thread T2 {}; | 
|---|
| 47 |  | 
|---|
| 48 | void main( T2 & this ) { | 
|---|
| 49 | block(); | 
|---|
| 50 |  | 
|---|
| 51 | // Test calls which occur increasingly close to timeout value. | 
|---|
| 52 |  | 
|---|
| 53 | sleep( 100000000`ns ); | 
|---|
| 54 | notify_one(c_m); | 
|---|
| 55 | block(); | 
|---|
| 56 |  | 
|---|
| 57 | sleep( 500000000`ns ); | 
|---|
| 58 | notify_one(c_m); | 
|---|
| 59 | block(); | 
|---|
| 60 |  | 
|---|
| 61 | sleep( 900000000`ns ); | 
|---|
| 62 | notify_one(c_m); | 
|---|
| 63 | block(); | 
|---|
| 64 |  | 
|---|
| 65 | for ( unsigned int i = 0; i < NoOfTimes; i += 1 ) { | 
|---|
| 66 | sleep( 999700000`ns ); | 
|---|
| 67 | notify_one(c_m); | 
|---|
| 68 | block(); | 
|---|
| 69 | } // for | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int main() { | 
|---|
| 73 | processor p[2]; | 
|---|
| 74 | printf("Start Test 1: surface testing condition variable timeout routines\n"); | 
|---|
| 75 | wait( c_m, 1`ns );                                                                                                              // bool wait( condition_variable(L) & this, Duration duration ); | 
|---|
| 76 | wait( c_m, 10, 1`ns );                                                                                                  // bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ); | 
|---|
| 77 | wait( c_m, __kernel_get_time() + 1`ns );                                                                // bool wait( condition_variable(L) & this, Time time ); | 
|---|
| 78 | wait( c_m, 10, __kernel_get_time() + 1`ns );                                                    // bool wait( condition_variable(L) & this, uintptr_t info, Time time ); | 
|---|
| 79 | lock(m); wait( c_m, m, 1`ns ); unlock(m);                                                               // bool wait( condition_variable(L) & this, L & l, Duration duration ); | 
|---|
| 80 | lock(m); wait( c_m, m, 10, 1`ns ); unlock(m);                                                   // bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); | 
|---|
| 81 | lock(m); wait( c_m, m, __kernel_get_time() + 1`ns ); unlock(m);                 // bool wait( condition_variable(L) & this, L & l, Time time ); | 
|---|
| 82 | lock(m); wait( c_m, m, 10, __kernel_get_time() + 1`ns ); unlock(m);             // bool wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ); | 
|---|
| 83 | printf("Done Test 1\n"); | 
|---|
| 84 |  | 
|---|
| 85 | printf("Start Test 2: testing timeout vs signalling with varying timeout durations\n"); | 
|---|
| 86 | s{ 0 }; | 
|---|
| 87 | { | 
|---|
| 88 | T1 t1; | 
|---|
| 89 | T2 t2; | 
|---|
| 90 | } | 
|---|
| 91 | printf("Done Test 2\n"); | 
|---|
| 92 | } | 
|---|