| 6 | #include "base/logging.h" |
| 7 |
| 8 | namespace base { |
| 9 | namespace internal { |
| 10 |
| 11 | OperationsController::OperationsController() = default; |
| 12 |
| 13 | OperationsController::~OperationsController() { |
| 14 | #if DCHECK_IS_ON() |
| 15 | // An OperationsController may only be deleted when it was either not |
| 16 | // accepting operations or after it was shutdown and there are no in flight |
| 17 | // attempts to perform operations. |
| 18 | auto value = state_and_count_.load(); |
| 19 | DCHECK( |
| 20 | ExtractState(value) == State::kRejectingOperations || |
| 21 | (ExtractState(value) == State::kShuttingDown && ExtractCount(value) == 0)) |
| 22 | << value; |
| 23 | #endif |
| 24 | } |
| 25 |
| 26 | bool OperationsController::StartAcceptingOperations() { |
| 27 | // Release semantics are required to ensure that all memory accesses made on |
| 28 | // this thread happen-before any others done on a thread which is later |
| 29 | // allowed to perform an operation. |
| 30 | auto prev_value = state_and_count_.fetch_or(kAcceptingOperationsBitMask, |
| 31 | std::memory_order_release); |
| 32 |
| 33 | DCHECK_EQ(ExtractState(prev_value), State::kRejectingOperations); |
| 34 | // The count is the number of rejected operations, unwind them now. |
| 35 | auto num_rejected = ExtractCount(prev_value); |
| 36 | DecrementBy(num_rejected); |
| 37 | return num_rejected != 0; |
| 38 | } |
| 39 |
| 40 | OperationsController::OperationToken OperationsController::TryBeginOperation() { |
| 41 | // Acquire semantics are required to ensure that a thread which is allowed to |
| 42 | // perform an operation sees all the memory side-effects that happened-before |
| 43 | // StartAcceptingOperations(). They're also required so that no operations on |
| 44 | // this thread (e.g. the operation itself) can be reordered before this one. |
| 45 | auto prev_value = state_and_count_.fetch_add(1, std::memory_order_acquire); |
| 46 |
| 47 | switch (ExtractState(prev_value)) { |
| 48 | case State::kRejectingOperations: |
| 49 | return OperationToken(nullptr); |
| 50 | case State::kAcceptingOperations: |
| 51 | return OperationToken(this); |
| 52 | case State::kShuttingDown: |
| 53 | DecrementBy(1); |
| 54 | return OperationToken(nullptr); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | void OperationsController::ShutdownAndWaitForZeroOperations() { |
| 59 | // Acquire semantics are required to guarantee that all memory side-effects |
| 60 | // made by other threads that were allowed to perform operations are |
| 61 | // synchronized with this thread before it returns from this method. |
| 62 | auto prev_value = state_and_count_.fetch_or(kShuttingDownBitMask, |
| 63 | std::memory_order_acquire); |
| 64 |
| 65 | switch (ExtractState(prev_value)) { |
| 66 | case State::kRejectingOperations: |
| 67 | // The count is the number of rejected operations, unwind them now. |
| 68 | DecrementBy(ExtractCount(prev_value)); |
| 69 | break; |
| 70 | case State::kAcceptingOperations: |
| 71 | if (ExtractCount(prev_value) != 0) { |
| 72 | shutdown_complete_.Wait(); |
| 73 | } |
| 74 | break; |
| 75 | case State::kShuttingDown: |
| 76 | DCHECK(false) << "Multiple calls to ShutdownAndWaitForZeroOperations()"; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 |
| 81 | OperationsController::State OperationsController::ExtractState(uint32_t value) { |
| 82 | if (value & kShuttingDownBitMask) { |
| 83 | return State::kShuttingDown; |
| 84 | } else if (value & kAcceptingOperationsBitMask) { |
| 85 | return State::kAcceptingOperations; |
| 86 | } else { |
| 87 | return State::kRejectingOperations; |
| 88 | } |
| 89 | } |
| 90 |
| 91 | void OperationsController::DecrementBy(uint32_t n) { |
| 92 | // Release semantics are required to ensure that no operation on the current |
| 93 | // thread (e.g. the operation itself) can be reordered after this one. |
| 94 | auto prev_value = state_and_count_.fetch_sub(n, std::memory_order_release); |
| 95 | DCHECK_LE(n, ExtractCount(prev_value)) << "Decrement underflow"; |
| 96 |
| 97 | if (ExtractState(prev_value) == State::kShuttingDown && |
| 98 | ExtractCount(prev_value) == n) { |
| 99 | shutdown_complete_.Signal(); |
| 100 | } |
| 101 | } |
| 102 |
| 103 | } // namespace internal |
| 104 | } // namespace base |