[a3af522] | 1 | // -*- Mode: C -*-
|
---|
| 2 | //
|
---|
[93b8cf4] | 3 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
|
---|
[a3af522] | 4 | //
|
---|
[93b8cf4] | 5 | // The contents of this file are covered under the licence agreement in the
|
---|
| 6 | // file "LICENCE" distributed with Cforall.
|
---|
| 7 | //
|
---|
[a3af522] | 8 | // barrier.hfa -- simple barrier implemented using a monitor
|
---|
| 9 | //
|
---|
| 10 | // Author : Peter A. Buhr
|
---|
| 11 | // Created On : Sun Nov 10 08:07:35 2024
|
---|
| 12 | // Last Modified By : Peter A. Buhr
|
---|
[95707a3] | 13 | // Last Modified On : Wed Nov 13 12:37:04 2024
|
---|
| 14 | // Update Count : 9
|
---|
[a3af522] | 15 | //
|
---|
[93b8cf4] | 16 |
|
---|
| 17 | #pragma once
|
---|
| 18 |
|
---|
| 19 | #include <monitor.hfa>
|
---|
| 20 |
|
---|
[a3af522] | 21 | // Plan 9 inheritance does not work with monitors. Two monitor locks are created.
|
---|
[31ef267] | 22 |
|
---|
[a3af522] | 23 | monitor barrier {
|
---|
| 24 | unsigned int group, arrivals; // group size, arrival counter
|
---|
| 25 | condition c; // wait for group to form
|
---|
[93b8cf4] | 26 | };
|
---|
| 27 |
|
---|
[95707a3] | 28 | static inline void ?{}( barrier & b, unsigned int group ) {
|
---|
| 29 | b.group = b.arrivals = group; // arrivals count backward
|
---|
[93b8cf4] | 30 | }
|
---|
| 31 |
|
---|
[95707a3] | 32 | // Returns a value indicating the reverse order the threads arrived, i.e. last thread returns 0 (and does not block)
|
---|
| 33 | // last is an optional hook that is called by the Gth thread before unblocking the other threads.
|
---|
| 34 | static inline unsigned int block( barrier & mutex b, fptr_t last = (fptr_t)0 ) with( b ) {
|
---|
[a3af522] | 35 | arrivals -= 1; // prefix decrement so last is 0 not 1
|
---|
| 36 | unsigned arrived = b.arrivals; // note arrival order
|
---|
| 37 | if ( arrivals != 0 ) { // wait for group to form
|
---|
| 38 | wait( b.c );
|
---|
| 39 | } else { // group formed
|
---|
| 40 | if ( last ) last(); // safe to call
|
---|
| 41 | signal_all( c ); // unblock group
|
---|
| 42 | arrivals = group; // reset
|
---|
| 43 | } // if
|
---|
| 44 | return arrived; // return arrival order
|
---|
[b2f3880] | 45 | }
|
---|