Ignore:
Timestamp:
Apr 1, 2022, 12:40:35 PM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
1417f6b
Parents:
93b8cf4
Message:

Added comments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/barrier.hfa

    r93b8cf4 r31ef267  
    1818#include <monitor.hfa>
    1919
     20// Simple barrier based on a monitor
    2021monitor barrier {
    21         int max;
    22         int count;
     22        // Number of threads blocking needed to unblock the barrier
     23        // Unsigned should be enough, I don't expect use cases with 2^32 thread barriers.
     24        unsigned width;
    2325
     26        // Current count (counting backwards)
     27        unsigned count;
     28
     29        // Barrier uses internal scheduling
    2430        condition c;
    2531};
    2632
    27 void ?{}( barrier & this, int max ) {
    28         this.max = max;
    29         this.count = max;
     33// Constructor
     34void ?{}( barrier & this, unsigned width ) {
     35        this.width = width;
     36        this.count = width; // Count backwards so initialize at width
    3037}
    3138
    32 static inline int block(barrier & mutex this ) {
    33         this.count -= 1;
    34         int arrival = this.count;
    35         if(arrival != 0) {
     39// block until the number of threads needed have blocked
     40// returns an value indicating the reverse order the threads arrived in
     41// i.e. last thread will return 0 (and not block)
     42//      second last thread returns 1
     43//      etc.
     44static inline unsigned block(barrier & mutex this ) {
     45        this.count -= 1; // prefix decrement so we the last is 0 and not 1
     46        unsigned arrival = this.count; // Note arrival order
     47        if(arrival == 0) {
     48                // If arrived last unblock everyone and reset
     49                signal_all(this.c);
     50                this.count = this.width;
     51        } else {
     52                // Otherwise block
    3653                wait(this.c);
    37         } else {
    38                 signal_all(this.c);
    39                 this.count = this.max;
    4054        }
    41         return arrival;
     55        return arrival; // return arrival order
    4256}
Note: See TracChangeset for help on using the changeset viewer.