1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // io-acquire.cfa --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Mon Mar 1 18:40:09 2021
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Fri Jan 14 09:13:54 2022
|
---|
13 | // Update Count : 73
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <fstream.hfa>
|
---|
17 | #include <thread.hfa>
|
---|
18 | #include <mutex_stmt.hfa>
|
---|
19 |
|
---|
20 | Duration default_preemption() { return 0; }
|
---|
21 |
|
---|
22 | multiple_acquisition_lock soutLock, sinLock;
|
---|
23 |
|
---|
24 | long long i;
|
---|
25 | void doWork() {
|
---|
26 | for (int j = 0; j < 10000; j++) {
|
---|
27 | i += rand();
|
---|
28 | i -= rand();
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | thread T {};
|
---|
33 | void main( T & ) {
|
---|
34 | // output from parallel threads should not be scrambled
|
---|
35 |
|
---|
36 | for ( 100 ) { // expression protection
|
---|
37 | mutex(soutLock) doWork();
|
---|
38 | }
|
---|
39 | mutex( soutLock ) { // statement protection
|
---|
40 | for ( 100 ) {
|
---|
41 | doWork();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | { // duplicate protection demonstrating recursive lock
|
---|
45 | mutex( soutLock ) { // unnecessary mutex
|
---|
46 | for ( 100 ) {
|
---|
47 | mutex( soutLock ) {
|
---|
48 | doWork();
|
---|
49 | mutex( soutLock ) { doWork(); mutex( soutLock ) doWork(); } // refactored code
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // above output used as input to parallel threads
|
---|
56 |
|
---|
57 | for ( 100 ) { // expression protection
|
---|
58 | mutex(sinLock) doWork();
|
---|
59 | }
|
---|
60 | mutex( sinLock ) { // statement protection
|
---|
61 | for ( 100 ) {
|
---|
62 | doWork();
|
---|
63 | }
|
---|
64 | }
|
---|
65 | { // duplicate protection demonstrating recursive lock
|
---|
66 | mutex( sinLock ) { // unnecessary mutex
|
---|
67 | for ( 5 ) {
|
---|
68 | mutex( sinLock ) {
|
---|
69 | doWork();
|
---|
70 | mutex( sinLock ) { doWork(); mutex( sinLock ) doWork(); }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | int main() {
|
---|
77 | processor p;
|
---|
78 | {
|
---|
79 | T t[5];
|
---|
80 | }
|
---|
81 | sout | "done";
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Local Variables: //
|
---|
85 | // tab-width: 4 //
|
---|
86 | // compile-command: "cfa io-acquire.cfa" //
|
---|
87 | // End: //
|
---|