source: tests/concurrent/mutexstmt/locks.cfa @ 7b2c8c3c

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 7b2c8c3c was f57f6ea0, checked in by caparsons <caparson@…>, 2 years ago

added tests for scoped locks

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include <mutex_stmt.hfa>
2#include <locks.hfa>
3
4const unsigned int num_times = 10000;
5
6single_acquisition_lock m1, m2, m3, m4, m5;
7
8thread T_Mutex {};
9bool insideFlag = false;
10int count = 0;
11
12void main( T_Mutex & this ) {
13        for (unsigned int i = 0; i < num_times; i++) {
14                mutex ( m1 ) count++;
15                mutex ( m1 ) {
16                        assert(!insideFlag);
17                        insideFlag = true;
18                        assert(insideFlag);
19                        insideFlag = false;
20                }
21        }
22}
23
24thread T_Multi {};
25
26void main( T_Multi & this ) {
27        for (unsigned int i = 0; i < num_times; i++) {
28                mutex ( m1 ) {
29                        assert(!insideFlag);
30                        insideFlag = true;
31                        assert(insideFlag);
32                        insideFlag = false;
33                }
34                mutex ( m1, m2, m3, m4, m5 ) {
35                        assert(!insideFlag);
36                        insideFlag = true;
37                        assert(insideFlag);
38                        insideFlag = false;
39                }
40                mutex ( m3, m1 ) {
41                        assert(!insideFlag);
42                        insideFlag = true;
43                        assert(insideFlag);
44                        insideFlag = false;
45                }
46                mutex ( m1, m2, m4 ) {
47                        assert(!insideFlag);
48                        insideFlag = true;
49                        assert(insideFlag);
50                        insideFlag = false;
51                }
52                mutex ( m1, m3, m4, m5 ) {
53                        assert(!insideFlag);
54                        insideFlag = true;
55                        assert(insideFlag);
56                        insideFlag = false;
57                }
58        }
59}
60
61thread T_Mutex_Scoped {};
62
63void main( T_Mutex_Scoped & this ) {
64        for (unsigned int i = 0; i < num_times; i++) {
65                {
66                        scoped_lock(single_acquisition_lock) s{m1};
67                        count++;
68                }
69                {
70                        scoped_lock(single_acquisition_lock) s{m1};
71                        assert(!insideFlag);
72                        insideFlag = true;
73                        assert(insideFlag);
74                        insideFlag = false;
75                }
76        }
77}
78
79thread T_Multi_Scoped {};
80
81void main( T_Multi_Scoped & this ) {
82        for (unsigned int i = 0; i < num_times; i++) {
83                {
84                        scoped_lock(single_acquisition_lock) s{m1};
85                        assert(!insideFlag);
86                        insideFlag = true;
87                        assert(insideFlag);
88                        insideFlag = false;
89                }
90                {
91                        scoped_lock(single_acquisition_lock) s1{m1};
92                        scoped_lock(single_acquisition_lock) s2{m2};
93                        scoped_lock(single_acquisition_lock) s3{m3};
94                        scoped_lock(single_acquisition_lock) s4{m4};
95                        scoped_lock(single_acquisition_lock) s5{m5};
96                        assert(!insideFlag);
97                        insideFlag = true;
98                        assert(insideFlag);
99                        insideFlag = false;
100                }
101                {
102                        scoped_lock(single_acquisition_lock) s1{m1};
103                        scoped_lock(single_acquisition_lock) s3{m3};
104                        assert(!insideFlag);
105                        insideFlag = true;
106                        assert(insideFlag);
107                        insideFlag = false;
108                }
109                {
110                        scoped_lock(single_acquisition_lock) s1{m1};
111                        scoped_lock(single_acquisition_lock) s2{m2};
112                        scoped_lock(single_acquisition_lock) s4{m4};
113                        assert(!insideFlag);
114                        insideFlag = true;
115                        assert(insideFlag);
116                        insideFlag = false;
117                }
118                {
119                        scoped_lock(single_acquisition_lock) s1{m1};
120                        scoped_lock(single_acquisition_lock) s3{m3};
121                        scoped_lock(single_acquisition_lock) s4{m4};
122                        scoped_lock(single_acquisition_lock) s5{m5};
123                        assert(!insideFlag);
124                        insideFlag = true;
125                        assert(insideFlag);
126                        insideFlag = false;
127                }
128        }
129}
130
131int num_tasks = 10;
132int main() {
133        processor p[10];
134
135        printf("Start Test: single lock mutual exclusion\n");
136        {
137                T_Mutex t[10];
138        }
139        assert(count == num_tasks * num_times);
140        printf("End Test: single lock mutual exclusion\n");
141        printf("Start Test: multi lock deadlock/mutual exclusion\n");
142        {
143                T_Multi t[10];
144        }
145        printf("End Test: multi lock deadlock/mutual exclusion\n");
146       
147        count = 0;
148        printf("Start Test: single scoped lock mutual exclusion\n");
149        {
150                T_Mutex_Scoped t[10];
151        }
152        assert(count == num_tasks * num_times);
153        printf("End Test: single scoped lock mutual exclusion\n");
154        printf("Start Test: multi scoped lock deadlock/mutual exclusion\n");
155        {
156                T_Multi_Scoped t[10];
157        }
158        printf("End Test: multi scoped lock deadlock/mutual exclusion\n");     
159}
Note: See TracBrowser for help on using the repository browser.