| 6 | #define BASE_TASK_COMMON_OPERATIONS_CONTROLLER_H_ |
| 7 |
| 8 | #include <atomic> |
| 9 | #include <cstdint> |
| 10 |
| 11 | #include "base/synchronization/waitable_event.h" |
| 12 |
| 13 | namespace base { |
| 14 | namespace internal { |
| 15 |
| 16 | // A lock-free thread-safe controller to manage critical multi-threaded |
| 17 | // operations without locks. |
| 18 | // |
| 19 | // The controller is used to determine if operations are allowed, and to keep |
| 20 | // track of how many are currently active. Users will call TryBeginOperation() |
| 21 | // before starting such operations. If the call succeeds the user can run the |
| 22 | // operation and the controller will keep track of it until the user signals |
| 23 | // that the operation is completed. No operations are allowed before |
| 24 | // StartAcceptingOperations() is called, or after |
| 25 | // ShutdownAndWaitForZeroOperations() is called. |
| 26 | // |
| 27 | // There is no explicit way of telling the controller when an operation is |
| 28 | // completed, instead for convenience TryBeginOperation() will return a RAII |
| 29 | // like object that will do so on destruction. |
| 30 | // |
| 31 | // For example: |
| 32 | // |
| 33 | // OperationsController controller_; |
| 34 | // |
| 35 | // void SetUp() { |
| 36 | // controller_.StartAcceptingOperations(); |
| 37 | // } |
| 38 | // |
| 39 | // void TearDown() { |
| 40 | // controller_.ShutdownAndWaitForZeroOperations(); |
| 41 | // } |
| 42 | // |
| 43 | // void MaybeRunOperation() { |
| 44 | // auto operation_token = controller_.TryBeginOperation(); |
| 45 | // if (operation_token) { |
| 46 | // Process(); |
| 47 | // } |
| 48 | // } |
| 49 | // |
| 50 | // This class is thread-safe. |
| 51 | // But note that StartAcceptingOperations can never be called after |
| 52 | // ShutdownAndWaitForZeroOperations. |
| 53 | class BASE_EXPORT OperationsController { |
| 54 | public: |
| 55 | // The owner of an OperationToken which evaluates to true can safely perform |
| 56 | // an operation while being certain it happens-after |
| 57 | // StartAcceptingOperations() and happens-before |
| 58 | // ShutdownAndWaitForZeroOperations(). Releasing this OperationToken |
| 59 | // relinquishes this right. |
| 60 | // |
| 61 | // This class is thread-safe |
| 62 | class OperationToken { |
| 63 | public: |
| 64 | ~OperationToken() { |
| 65 | if (outer_) |
| 66 | outer_->DecrementBy(1); |
| 67 | } |
| 68 | OperationToken(const OperationToken&) = delete; |
| 69 | OperationToken(OperationToken&& other) { |
| 70 | this->outer_ = other.outer_; |
| 71 | other.outer_ = nullptr; |
| 72 | } |
| 73 |
| 74 | operator bool() const { return !!outer_; } |
| 75 |
| 76 | private: |
| 77 | friend class OperationsController; |
| 78 | explicit OperationToken(OperationsController* outer) : outer_(outer) {} |
| 79 | OperationsController* outer_; |
| 80 | }; |
| 81 |
| 82 | OperationsController(); |
| 83 |
| 84 | // Users must call ShutdownAndWaitForZeroOperations() before destroying an |
| 85 | // instance of this class if StartAcceptingOperations() was called. |
| 86 | ~OperationsController(); |
| 87 |
| 88 | OperationsController(const OperationsController&) = delete; |
| 89 | OperationsController& operator=(const OperationsController&) = delete; |
| 90 |
| 91 | // Starts to accept operations (before this point TryBeginOperation() returns |
| 92 | // an invalid token). Returns true if an attempt to perform an operation was |
| 93 | // made and denied before StartAcceptingOperations() was called. Can be called |
| 94 | // at most once, never after ShutdownAndWaitForZeroOperations(). |
| 95 | bool StartAcceptingOperations(); |
| 96 |
| 97 | // Returns a RAII like object that implicitly converts to true if operations |
| 98 | // are allowed i.e. if this call happens-after StartAcceptingOperations() and |
| 99 | // happens-before Shutdown(), otherwise the object will convert to false. On |
| 100 | // successful return, this OperationsController will keep track of the |
| 101 | // operation until the returned object goes out of scope. |
| 102 | OperationToken TryBeginOperation(); |
| 103 |
| 104 | // Prevents further calls to TryBeginOperation() from succeeding and waits for |
| 105 | // all the ongoing operations to complete. |
| 106 | // |
| 107 | // Attention: Can only be called once. |
| 108 | void ShutdownAndWaitForZeroOperations(); |
| 109 |
| 110 | private: |
| 111 | // Atomic representation of the state of this class. We use the upper 2 bits |
| 112 | // to keep track of flag like values and the remainder bits are used as a |
| 113 | // counter. The 2 flags are used to represent 3 different states: |
| 114 | // |
| 115 | // State | AcceptOperations Bit | ShuttingDown Bit |
| 116 | // -------------------------------------------------------------- |
| 117 | // kRejectingOperations | 0 | 0 |
| 118 | // kAcceptingOperations | 1 | 0 |
| 119 | // kShuttingDown | * | 1 |
| 120 | // |
| 121 | // The counter keeps track of the rejected operations when we are in |
| 122 | // the kRejectingOperations state, the number of inflight operations |
| 123 | // otherwise. If the count reaches zero and we are in the shutting down state |
| 124 | // |shutdown_complete_| will be signaled. |
| 125 | static constexpr uint32_t kShuttingDownBitMask = uint32_t{1} << 31; |
| 126 | static constexpr uint32_t kAcceptingOperationsBitMask = uint32_t{1} << 30; |
| 127 | static constexpr uint32_t kFlagsBitMask = |
| 128 | (kShuttingDownBitMask | kAcceptingOperationsBitMask); |
| 129 | static constexpr uint32_t kCountBitMask = ~kFlagsBitMask; |
| 130 | enum class State { |
| 131 | kRejectingOperations, |
| 132 | kAcceptingOperations, |
| 133 | kShuttingDown, |
| 134 | }; |
| 135 |
| 136 | // Helper methods for the bit fiddling. Pass a |state_and_count_| value to |
| 137 | // extract state or count out of it. |
| 138 | static uint32_t ExtractCount(uint32_t value) { return value & kCountBitMask; } |
| 139 | static State ExtractState(uint32_t value); |
| 140 |
| 141 | // Decrements the counter by |n| and signals |shutdown_complete_| if needed. |
| 142 | void DecrementBy(uint32_t n); |
| 143 |
| 144 | std::atomic<uint32_t> state_and_count_{0}; |
| 145 | WaitableEvent shutdown_complete_; |
| 146 | }; |
| 147 |
| 148 | } // namespace internal |
| 149 | } // namespace base |
| 150 |
| 151 | #endif // BASE_TASK_COMMON_OPERATIONS_CONTROLLER_H_ |
| 152 |