// -*- Mode: C -*- // // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // barrier.hfa -- simple barrier implemented using a monitor // // Author : Peter A. Buhr // Created On : Sun Nov 10 08:07:35 2024 // Last Modified By : Peter A. Buhr // Last Modified On : Sun Nov 10 08:11:55 2024 // Update Count : 3 // #pragma once #include // Plan 9 inheritance does not work with monitors. Two monitor locks are created. monitor barrier { unsigned int group, arrivals; // group size, arrival counter condition c; // wait for group to form }; // Constructor void ?{}( barrier & b, unsigned group ) { b.group = b.arrivals = group; // count backwards } // Returns a value indicating the reverse order the threads arrived i.e. last thread returns 0 (and does not block) // last is an optional hook that is called by the last thread before unblocking the others. static inline unsigned block( barrier & mutex b, fptr_t last = (fptr_t)0 ) with( b ) { arrivals -= 1; // prefix decrement so last is 0 not 1 unsigned arrived = b.arrivals; // note arrival order if ( arrivals != 0 ) { // wait for group to form wait( b.c ); } else { // group formed if ( last ) last(); // safe to call signal_all( c ); // unblock group arrivals = group; // reset } // if return arrived; // return arrival order }