source: libcfa/src/concurrency/barrier.hfa @ 2295320

Last change on this file since 2295320 was a3af522, checked in by Peter A. Buhr <pabuhr@…>, 8 days ago

first attempt at updating barrier lock

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[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
13// Last Modified On : Sun Nov 10 08:11:55 2024
14// Update Count     : 3
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]23monitor barrier {
24        unsigned int group, arrivals;                                           // group size, arrival counter
25        condition c;                                                                            // wait for group to form
[93b8cf4]26};
27
[31ef267]28// Constructor
[a3af522]29void ?{}( barrier & b, unsigned group ) {
30        b.group = b.arrivals = group;                                           // count backwards
[93b8cf4]31}
32
[a3af522]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.
35static 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
[b2f3880]46}
Note: See TracBrowser for help on using the repository browser.