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