// -*- 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 : Wed Nov 13 12:37:04 2024 // Update Count : 9 // #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 }; static inline void ?{}( barrier & b, unsigned int group ) { b.group = b.arrivals = group; // arrivals count backward } // 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 Gth thread before unblocking the other threads. static inline unsigned int 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 }