barrier.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef Y_ABSL_SYNCHRONIZATION_BARRIER_H_
  19. #define Y_ABSL_SYNCHRONIZATION_BARRIER_H_
  20. #include "y_absl/base/thread_annotations.h"
  21. #include "y_absl/synchronization/mutex.h"
  22. namespace y_absl {
  23. Y_ABSL_NAMESPACE_BEGIN
  24. // Barrier
  25. //
  26. // This class creates a barrier which blocks threads until a prespecified
  27. // threshold of threads (`num_threads`) utilizes the barrier. A thread utilizes
  28. // the `Barrier` by calling `Block()` on the barrier, which will block that
  29. // thread; no call to `Block()` will return until `num_threads` threads have
  30. // called it.
  31. //
  32. // Exactly one call to `Block()` will return `true`, which is then responsible
  33. // for destroying the barrier; because stack allocation will cause the barrier
  34. // to be deleted when it is out of scope, barriers should not be stack
  35. // allocated.
  36. //
  37. // Example:
  38. //
  39. // // Main thread creates a `Barrier`:
  40. // barrier = new Barrier(num_threads);
  41. //
  42. // // Each participating thread could then call:
  43. // if (barrier->Block()) delete barrier; // Exactly one call to `Block()`
  44. // // returns `true`; that call
  45. // // deletes the barrier.
  46. class Barrier {
  47. public:
  48. // `num_threads` is the number of threads that will participate in the barrier
  49. explicit Barrier(int num_threads)
  50. : num_to_block_(num_threads), num_to_exit_(num_threads) {}
  51. Barrier(const Barrier&) = delete;
  52. Barrier& operator=(const Barrier&) = delete;
  53. // Barrier::Block()
  54. //
  55. // Blocks the current thread, and returns only when the `num_threads`
  56. // threshold of threads utilizing this barrier has been reached. `Block()`
  57. // returns `true` for precisely one caller, which may then destroy the
  58. // barrier.
  59. //
  60. // Memory ordering: For any threads X and Y, any action taken by X
  61. // before X calls `Block()` will be visible to Y after Y returns from
  62. // `Block()`.
  63. bool Block();
  64. private:
  65. Mutex lock_;
  66. int num_to_block_ Y_ABSL_GUARDED_BY(lock_);
  67. int num_to_exit_ Y_ABSL_GUARDED_BY(lock_);
  68. };
  69. Y_ABSL_NAMESPACE_END
  70. } // namespace y_absl
  71. #endif // Y_ABSL_SYNCHRONIZATION_BARRIER_H_