source: tests/concurrency/unified_locking/timeout_lock.cfa @ f6fd22a4

Last change on this file since f6fd22a4 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#include <stdio.h>
2#include <locks.hfa>
3#include <alarm.hfa>
4#include <stdlib.hfa>
5#include <thread.hfa>
6#include <kernel.hfa>
7
8multiple_acquisition_lock m, n;
9condition_variable( multiple_acquisition_lock ) c_m, c_n;
10
11const unsigned int NoOfTimes = 20;
12
13void block() { // used for barrier like behaviour
14        lock(n);
15        if (empty( c_n )) {
16                wait( c_n, n );
17        } else {
18                notify_one( c_n );
19        }
20        unlock(n);
21}
22
23thread T1 {};
24
25void 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, 1000000`ns ) ) {
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
46thread T2 {};
47
48void main( T2 & this ) {
49        block();
50
51        // Test calls which occur increasingly close to timeout value.
52
53        sleep( 100000`ns );
54        notify_one(c_m);
55        block();
56
57        sleep( 500000`ns );
58        notify_one(c_m);
59        block();
60
61        sleep( 900000`ns );
62        notify_one(c_m);
63        block();
64
65        for ( unsigned int i = 0; i < NoOfTimes; i += 1 ) {
66            sleep( 999700`ns );
67                notify_one(c_m);
68            block();
69        } // for
70}
71
72int 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        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}
Note: See TracBrowser for help on using the repository browser.