[56d711f] | 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
|
---|
[9ee3f54] | 12 | // Last Modified On : Fri Jan 14 09:13:54 2022
|
---|
| 13 | // Update Count : 73
|
---|
[56d711f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <fstream.hfa>
|
---|
| 17 | #include <thread.hfa>
|
---|
| 18 | #include <mutex_stmt.hfa>
|
---|
| 19 |
|
---|
[9ee3f54] | 20 | Duration default_preemption() { return 0; }
|
---|
| 21 |
|
---|
[56d711f] | 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 | int a, b, c, d, e, f, g, h, i;
|
---|
| 58 | for ( 100 ) { // expression protection
|
---|
| 59 | mutex(sinLock) doWork();
|
---|
| 60 | }
|
---|
| 61 | mutex( sinLock ) { // statement protection
|
---|
| 62 | for ( 100 ) {
|
---|
| 63 | doWork();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | { // duplicate protection demonstrating recursive lock
|
---|
| 67 | mutex( sinLock ) { // unnecessary mutex
|
---|
| 68 | for ( 5 ) {
|
---|
| 69 | mutex( sinLock ) {
|
---|
| 70 | doWork();
|
---|
| 71 | mutex( sinLock ) { doWork(); mutex( sinLock ) doWork(); }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | int main() {
|
---|
| 78 | processor p;
|
---|
| 79 | {
|
---|
| 80 | T t[5];
|
---|
| 81 | }
|
---|
| 82 | sout | "done";
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // Local Variables: //
|
---|
| 86 | // tab-width: 4 //
|
---|
| 87 | // compile-command: "cfa io-acquire.cfa" //
|
---|
| 88 | // End: //
|
---|