[10dafa4] | 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 |
---|
[ee4b77b] | 12 | // Last Modified On : Sat Apr 9 15:22:03 2022 |
---|
| 13 | // Update Count : 76 |
---|
[10dafa4] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
| 17 | #include <thread.hfa> |
---|
[237df76] | 18 | #include <mutex_stmt.hfa> |
---|
[10dafa4] | 19 | |
---|
| 20 | thread T {}; |
---|
| 21 | void main( T & ) { |
---|
| 22 | // output from parallel threads should not be scrambled |
---|
| 23 | |
---|
[237df76] | 24 | for ( 100 ) { // expression protection |
---|
[c52f033] | 25 | mutex( sout ) sout | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; |
---|
[10dafa4] | 26 | } |
---|
[237df76] | 27 | mutex( sout ) { // statement protection |
---|
[10dafa4] | 28 | for ( 100 ) { |
---|
| 29 | sout | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | { // duplicate protection demonstrating recursive lock |
---|
[237df76] | 33 | ofstream & h1( ofstream & os ) { // helper |
---|
| 34 | mutex( os ) return os | 1 | 2 | 3 | 4; // unnecessary mutex |
---|
| 35 | } |
---|
| 36 | ofstream & h2( ofstream & os ) { // helper |
---|
| 37 | mutex( os ) return os | 6 | 7 | 8 | 9; // unnecessary mutex |
---|
| 38 | } |
---|
| 39 | mutex( sout ) { // unnecessary mutex |
---|
| 40 | for ( 100 ) { |
---|
| 41 | mutex( sout ) { |
---|
| 42 | sout | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; |
---|
| 43 | sout | h1 | 5 | h2; // refactored code |
---|
| 44 | } |
---|
| 45 | } |
---|
[10dafa4] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // above output used as input to parallel threads |
---|
| 50 | |
---|
| 51 | int a, b, c, d, e, f, g, h, i; |
---|
[237df76] | 52 | for ( 100 ) { // expression protection |
---|
[c52f033] | 53 | mutex( sin ) sin | a | b | c | d | e | f | g | h | i; |
---|
[10dafa4] | 54 | } |
---|
[237df76] | 55 | mutex( sin ) { // statement protection |
---|
[10dafa4] | 56 | for ( 100 ) { |
---|
| 57 | sin | a | b | c | d | e | f | g | h | i; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | { // duplicate protection demonstrating recursive lock |
---|
[237df76] | 61 | ifstream & h1( ifstream & is ) { // helper |
---|
| 62 | mutex( is ) return is | a | b | c | d; // unnecessary mutex |
---|
| 63 | } |
---|
| 64 | ifstream & h2( ifstream & is ) { // helper |
---|
| 65 | mutex( is ) return is | f | g | h | i; // unnecessary mutex |
---|
| 66 | } |
---|
| 67 | mutex( sin ) { // unnecessary mutex |
---|
| 68 | for ( 5 ) { |
---|
| 69 | mutex( sin ) { |
---|
| 70 | sin | a | b | c | d | e | f | g | h | i; |
---|
| 71 | sin | h1 | e | h2; // refactored code |
---|
| 72 | } |
---|
| 73 | } |
---|
[10dafa4] | 74 | } |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | int main() { |
---|
| 78 | processor p; |
---|
| 79 | T t[5]; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // Local Variables: // |
---|
| 83 | // tab-width: 4 // |
---|
| 84 | // compile-command: "cfa io-acquire.cfa" // |
---|
| 85 | // End: // |
---|