1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker // 3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker // 7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker // 9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker // limitations under the License. 14*9356374aSAndroid Build Coastguard Worker // 15*9356374aSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 16*9356374aSAndroid Build Coastguard Worker // barrier.h 17*9356374aSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 18*9356374aSAndroid Build Coastguard Worker 19*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_SYNCHRONIZATION_BARRIER_H_ 20*9356374aSAndroid Build Coastguard Worker #define ABSL_SYNCHRONIZATION_BARRIER_H_ 21*9356374aSAndroid Build Coastguard Worker 22*9356374aSAndroid Build Coastguard Worker #include "absl/base/thread_annotations.h" 23*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/mutex.h" 24*9356374aSAndroid Build Coastguard Worker 25*9356374aSAndroid Build Coastguard Worker namespace absl { 26*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN 27*9356374aSAndroid Build Coastguard Worker 28*9356374aSAndroid Build Coastguard Worker // Barrier 29*9356374aSAndroid Build Coastguard Worker // 30*9356374aSAndroid Build Coastguard Worker // This class creates a barrier which blocks threads until a prespecified 31*9356374aSAndroid Build Coastguard Worker // threshold of threads (`num_threads`) utilizes the barrier. A thread utilizes 32*9356374aSAndroid Build Coastguard Worker // the `Barrier` by calling `Block()` on the barrier, which will block that 33*9356374aSAndroid Build Coastguard Worker // thread; no call to `Block()` will return until `num_threads` threads have 34*9356374aSAndroid Build Coastguard Worker // called it. 35*9356374aSAndroid Build Coastguard Worker // 36*9356374aSAndroid Build Coastguard Worker // Exactly one call to `Block()` will return `true`, which is then responsible 37*9356374aSAndroid Build Coastguard Worker // for destroying the barrier; because stack allocation will cause the barrier 38*9356374aSAndroid Build Coastguard Worker // to be deleted when it is out of scope, barriers should not be stack 39*9356374aSAndroid Build Coastguard Worker // allocated. 40*9356374aSAndroid Build Coastguard Worker // 41*9356374aSAndroid Build Coastguard Worker // Example: 42*9356374aSAndroid Build Coastguard Worker // 43*9356374aSAndroid Build Coastguard Worker // // Main thread creates a `Barrier`: 44*9356374aSAndroid Build Coastguard Worker // barrier = new Barrier(num_threads); 45*9356374aSAndroid Build Coastguard Worker // 46*9356374aSAndroid Build Coastguard Worker // // Each participating thread could then call: 47*9356374aSAndroid Build Coastguard Worker // if (barrier->Block()) delete barrier; // Exactly one call to `Block()` 48*9356374aSAndroid Build Coastguard Worker // // returns `true`; that call 49*9356374aSAndroid Build Coastguard Worker // // deletes the barrier. 50*9356374aSAndroid Build Coastguard Worker class Barrier { 51*9356374aSAndroid Build Coastguard Worker public: 52*9356374aSAndroid Build Coastguard Worker // `num_threads` is the number of threads that will participate in the barrier Barrier(int num_threads)53*9356374aSAndroid Build Coastguard Worker explicit Barrier(int num_threads) 54*9356374aSAndroid Build Coastguard Worker : num_to_block_(num_threads), num_to_exit_(num_threads) {} 55*9356374aSAndroid Build Coastguard Worker 56*9356374aSAndroid Build Coastguard Worker Barrier(const Barrier&) = delete; 57*9356374aSAndroid Build Coastguard Worker Barrier& operator=(const Barrier&) = delete; 58*9356374aSAndroid Build Coastguard Worker 59*9356374aSAndroid Build Coastguard Worker // Barrier::Block() 60*9356374aSAndroid Build Coastguard Worker // 61*9356374aSAndroid Build Coastguard Worker // Blocks the current thread, and returns only when the `num_threads` 62*9356374aSAndroid Build Coastguard Worker // threshold of threads utilizing this barrier has been reached. `Block()` 63*9356374aSAndroid Build Coastguard Worker // returns `true` for precisely one caller, which may then destroy the 64*9356374aSAndroid Build Coastguard Worker // barrier. 65*9356374aSAndroid Build Coastguard Worker // 66*9356374aSAndroid Build Coastguard Worker // Memory ordering: For any threads X and Y, any action taken by X 67*9356374aSAndroid Build Coastguard Worker // before X calls `Block()` will be visible to Y after Y returns from 68*9356374aSAndroid Build Coastguard Worker // `Block()`. 69*9356374aSAndroid Build Coastguard Worker bool Block(); 70*9356374aSAndroid Build Coastguard Worker 71*9356374aSAndroid Build Coastguard Worker private: 72*9356374aSAndroid Build Coastguard Worker Mutex lock_; 73*9356374aSAndroid Build Coastguard Worker int num_to_block_ ABSL_GUARDED_BY(lock_); 74*9356374aSAndroid Build Coastguard Worker int num_to_exit_ ABSL_GUARDED_BY(lock_); 75*9356374aSAndroid Build Coastguard Worker }; 76*9356374aSAndroid Build Coastguard Worker 77*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 78*9356374aSAndroid Build Coastguard Worker } // namespace absl 79*9356374aSAndroid Build Coastguard Worker #endif // ABSL_SYNCHRONIZATION_BARRIER_H_ 80