|
| 1 | +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See /LICENSE for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +#ifndef CARBON_COMMON_LATCH_H_ |
| 6 | +#define CARBON_COMMON_LATCH_H_ |
| 7 | + |
| 8 | +#include <atomic> |
| 9 | + |
| 10 | +#include "llvm/ADT/FunctionExtras.h" |
| 11 | + |
| 12 | +namespace Carbon { |
| 13 | + |
| 14 | +// A synchronization primitive similar to `std::latch` to coordinate starting |
| 15 | +// some action once all of a set of other actions complete. |
| 16 | +// |
| 17 | +// Users initialize the latch (with `Init`), and receive a handle RAII object. |
| 18 | +// This handle can be copied, and the latch is satisfied when the last copy of |
| 19 | +// the handle returned by `Init` is destroyed. |
| 20 | +// |
| 21 | +// The latch synchronizes between every destruction of a handle and the |
| 22 | +// destruction of the last handle, allowing code that runs after the latch is |
| 23 | +// satisfied to access everything written by any thread that destroyed a handle. |
| 24 | +// For more details of the synchronization mechanics, see the comments on `Inc` |
| 25 | +// and `Dec` that implement this logic. |
| 26 | +// |
| 27 | +// This type also supports holding a closure to run when satisfied to simplify |
| 28 | +// patterns where that body of code is easier to express at the start of work |
| 29 | +// being synchronized instead of as each work item completes. |
| 30 | +// |
| 31 | +// The initialization API is separate from the constructor both for convenience |
| 32 | +// and to enable it to provide the initial handle. This makes it easy to build |
| 33 | +// constructively correct code where each work unit holds a handle until |
| 34 | +// finished, including the initializer of the latch, often using by-value |
| 35 | +// captures in a lambda that does the work. |
| 36 | +class Latch { |
| 37 | + public: |
| 38 | + class Handle; |
| 39 | + |
| 40 | + Latch() = default; |
| 41 | + Latch(const Latch&) = delete; |
| 42 | + Latch(Latch&&) = delete; |
| 43 | + |
| 44 | + // Initialize a latch and get the initial handle to it. |
| 45 | + // |
| 46 | + // When the last copy of the returned handle is destroyed, the latch will be |
| 47 | + // satisfied. |
| 48 | + // |
| 49 | + // A closure may be provided which will be called when that last handle is |
| 50 | + // destroyed. Note that the closure will run on whatever thread executes the |
| 51 | + // last handle destruction. Typically, the closure here should _schedule_ the |
| 52 | + // next step of work on some thread pool rather than performing it directly. |
| 53 | + // |
| 54 | + // Once this method is called, it cannot be called again until all handles are |
| 55 | + // destroyed and the latch is satisfied. It can then be called again to get a |
| 56 | + // fresh handle (and provide a new closure if desired). |
| 57 | + auto Init(llvm::unique_function<auto()->void> on_zero = [] {}) -> Handle; |
| 58 | + |
| 59 | + private: |
| 60 | + // Increments the latch's counter. |
| 61 | + // |
| 62 | + // This is thread-safe, and may be called concurrently on multiple threads, |
| 63 | + // and may be called concurrently with `Dec`. However, the caller _must_ call |
| 64 | + // `Inc` and then `Dec`, and provide some happens-before relationship between |
| 65 | + // the `Inc` and `Dec`. Typically, this is done with either same-thread |
| 66 | + // happens-before, or because some other synchronization event such as |
| 67 | + // starting a thread or popping a task from a thread pool provides the |
| 68 | + // inter-thread happens-before relationship. |
| 69 | + auto Inc() -> void; |
| 70 | + |
| 71 | + // Decrements the latch's counter, and returns true when it reaches zero. |
| 72 | + // |
| 73 | + // This is thread-safe, and may be called concurrently with other calls to |
| 74 | + // `Dec` or `Inc`. |
| 75 | + // |
| 76 | + // It also ensures that all threads which call `Dec` and receive `false` |
| 77 | + // synchronize-with the thread that calls `Dec` and receives `true`. As a |
| 78 | + // consequence everything that happens-before the call to `Dec` has an |
| 79 | + // inter-thread happens-before for any code when `Dec` returns `true`. |
| 80 | + // |
| 81 | + // Note that there is no guarantee of inter-thread happens-before to |
| 82 | + // operations after a `Dec` call that returns `false`. |
| 83 | + auto Dec() -> bool; |
| 84 | + |
| 85 | + std::atomic<int> count_; |
| 86 | + llvm::unique_function<auto()->void> on_zero_; |
| 87 | +}; |
| 88 | + |
| 89 | +// A copyable RAII handle around a `Latch`. |
| 90 | +// |
| 91 | +// When the last copy of a handle returned by `Latch::Init` is destroyed, the |
| 92 | +// latch is considered satisfied. Copying is supported by incrementing the |
| 93 | +// count of the latch. That increment can always be performed because it starts |
| 94 | +// from a live handle and so the count cannot have reached zero. |
| 95 | +// |
| 96 | +// For more details, see the `Latch` class. |
| 97 | +class Latch::Handle { |
| 98 | + public: |
| 99 | + Handle(const Handle& arg) : latch_(arg.latch_) { |
| 100 | + if (latch_) { |
| 101 | + arg.latch_->Inc(); |
| 102 | + } |
| 103 | + } |
| 104 | + Handle(Handle&& arg) noexcept : latch_(std::exchange(arg.latch_, nullptr)) {} |
| 105 | + |
| 106 | + ~Handle() { |
| 107 | + if (latch_) { |
| 108 | + latch_->Dec(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Drops a handle explicitly, rather than waiting for it to fall out of scope. |
| 113 | + // |
| 114 | + // This also allows observing whether the underlying latch is satisfied. |
| 115 | + // Calls to this function synchronize with all other drops or destructions of |
| 116 | + // latch handles when it returns true, and only the last will return true. |
| 117 | + auto Drop() && -> bool { |
| 118 | + bool last = latch_->Dec(); |
| 119 | + latch_ = nullptr; |
| 120 | + return last; |
| 121 | + } |
| 122 | + |
| 123 | + private: |
| 124 | + friend Latch; |
| 125 | + |
| 126 | + // Private constructor used by `Latch::Init` to create the initial handle for |
| 127 | + // a latch. |
| 128 | + explicit Handle(Latch* latch) : latch_(latch) { latch_->Inc(); } |
| 129 | + |
| 130 | + Latch* latch_ = nullptr; |
| 131 | +}; |
| 132 | + |
| 133 | +} // namespace Carbon |
| 134 | + |
| 135 | +#endif // CARBON_COMMON_LATCH_H_ |
0 commit comments