tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

barrier.h (2866B)


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