| [636d45f5] | 1 | #include <stdio.h>
 | 
|---|
| [ab388c5] | 2 | #include <locks.hfa>
 | 
|---|
 | 3 | #include <alarm.hfa>
 | 
|---|
| [636d45f5] | 4 | #include <stdlib.hfa>
 | 
|---|
 | 5 | #include <thread.hfa>
 | 
|---|
| [ab388c5] | 6 | #include <kernel.hfa>
 | 
|---|
| [636d45f5] | 7 | 
 | 
|---|
| [b984d54] | 8 | multiple_acquisition_lock m, n;
 | 
|---|
 | 9 | condition_variable( multiple_acquisition_lock ) c_m, c_n;
 | 
|---|
| [636d45f5] | 10 | 
 | 
|---|
| [9bb9545b] | 11 | const unsigned int NoOfTimes = 20;
 | 
|---|
| [636d45f5] | 12 | 
 | 
|---|
| [b984d54] | 13 | void block() { // used for barrier like behaviour
 | 
|---|
 | 14 |         lock(n);
 | 
|---|
 | 15 |         if (empty( c_n )) {
 | 
|---|
 | 16 |                 wait( c_n, n );
 | 
|---|
| [636d45f5] | 17 |         } else {
 | 
|---|
| [b984d54] | 18 |                 notify_one( c_n );
 | 
|---|
| [636d45f5] | 19 |         }
 | 
|---|
| [b984d54] | 20 |         unlock(n);
 | 
|---|
| [636d45f5] | 21 | }
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | thread T1 {};
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | void main( T1 & this ) {
 | 
|---|
 | 26 |         lock(m);
 | 
|---|
 | 27 |         wait( c_m, m, 1`s );
 | 
|---|
| [ebd1899] | 28 |         // printf("Thread: %p timedout\n", active_thread()); // removed since can't expect non deterministic output
 | 
|---|
| [636d45f5] | 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 ) {
 | 
|---|
| [ab388c5] | 35 |             if ( wait( c_m, m, 1000000`ns ) ) {
 | 
|---|
| [ebd1899] | 36 |                         // printf("Thread: %p signalled\n", active_thread()); // removed since can't expect non deterministic output
 | 
|---|
| [636d45f5] | 37 |             } else {
 | 
|---|
| [ebd1899] | 38 |                         // printf("Thread: %p timedout\n", active_thread()); // removed since can't expect non deterministic output
 | 
|---|
| [636d45f5] | 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 | 
 | 
|---|
| [7404cdc] | 53 |         sleep( 100000`ns );
 | 
|---|
| [636d45f5] | 54 |         notify_one(c_m);
 | 
|---|
 | 55 |         block();
 | 
|---|
 | 56 | 
 | 
|---|
| [7404cdc] | 57 |         sleep( 500000`ns );
 | 
|---|
| [636d45f5] | 58 |         notify_one(c_m);
 | 
|---|
 | 59 |         block();
 | 
|---|
 | 60 | 
 | 
|---|
| [7404cdc] | 61 |         sleep( 900000`ns );
 | 
|---|
| [636d45f5] | 62 |         notify_one(c_m);
 | 
|---|
 | 63 |         block();
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |         for ( unsigned int i = 0; i < NoOfTimes; i += 1 ) {
 | 
|---|
| [7404cdc] | 66 |             sleep( 999700`ns );
 | 
|---|
| [636d45f5] | 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");
 | 
|---|
| [ab388c5] | 75 |         wait( c_m, 1`ns );                                                                                                              // bool wait( condition_variable(L) & this, Duration duration );
 | 
|---|
| [636d45f5] | 76 |         wait( c_m, 10, 1`ns );                                                                                                  // bool wait( condition_variable(L) & this, uintptr_t info, Duration duration );
 | 
|---|
 | 77 |         lock(m); wait( c_m, m, 1`ns ); unlock(m);                                                               // bool wait( condition_variable(L) & this, L & l, Duration duration );
 | 
|---|
 | 78 |         lock(m); wait( c_m, m, 10, 1`ns ); unlock(m);                                                   // bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
 | 
|---|
 | 79 |         printf("Done Test 1\n");
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |         printf("Start Test 2: testing timeout vs signalling with varying timeout durations\n");
 | 
|---|
 | 82 |         {
 | 
|---|
 | 83 |                 T1 t1;
 | 
|---|
 | 84 |                 T2 t2;
 | 
|---|
 | 85 |         }
 | 
|---|
 | 86 |         printf("Done Test 2\n");
 | 
|---|
 | 87 | }
 | 
|---|