Evan Stade | 25bd39a | 2024-08-15 01:54:38 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_WEB_APPLICATIONS_LOCKS_PARTITIONED_LOCK_H_ |
| 6 | #define CHROME_BROWSER_WEB_APPLICATIONS_LOCKS_PARTITIONED_LOCK_H_ |
| 7 | |
| 8 | #include <iosfwd> |
| 9 | |
| 10 | #include "base/functional/callback.h" |
| 11 | #include "chrome/browser/web_applications/locks/partitioned_lock_id.h" |
| 12 | |
| 13 | namespace web_app { |
| 14 | |
| 15 | // Represents a granted lock by the PartitionedLockManager. When this object is |
| 16 | // destroyed, the lock is released. Since default construction is supported, |
| 17 | // |is_locked()| can be used to inquire locked status. Also, |Release()| can |
| 18 | // be called to manually release the lock, which appropriately updates the |
| 19 | // |is_locked()| result. |
| 20 | class PartitionedLock { |
| 21 | public: |
| 22 | using LockReleasedCallback = |
| 23 | base::OnceCallback<void(PartitionedLockId lock_id)>; |
| 24 | |
| 25 | PartitionedLock(); |
| 26 | |
| 27 | PartitionedLock(const PartitionedLock&) = delete; |
| 28 | PartitionedLock& operator=(const PartitionedLock&) = delete; |
| 29 | |
| 30 | ~PartitionedLock(); |
| 31 | PartitionedLock(PartitionedLock&&) noexcept; |
| 32 | // |lock_released_callback| is called when the lock is released, either by |
| 33 | // destruction of this object or by the |Released()| call. It will be called |
| 34 | // synchronously on the sequence runner this lock is released on. |
| 35 | PartitionedLock(PartitionedLockId lock_id, |
| 36 | LockReleasedCallback lock_released_callback); |
| 37 | // The lock in |other| is not released, and |this| must not be holding a lock. |
| 38 | PartitionedLock& operator=(PartitionedLock&& other) noexcept; |
| 39 | |
| 40 | // Returns true if this object is holding a lock. |
| 41 | bool is_locked() const { return !lock_released_callback_.is_null(); } |
| 42 | |
| 43 | // Explicitly releases the granted lock. |
| 44 | // |
| 45 | // The lock is also released implicitly when this instance is destroyed. |
| 46 | // This method is idempotent, i.e. it's valid to call Release() on an |
| 47 | // instance that does not hold a granted lock. |
| 48 | void Release(); |
| 49 | |
| 50 | const PartitionedLockId& lock_id() const { return lock_id_; } |
| 51 | |
| 52 | private: |
| 53 | PartitionedLockId lock_id_; |
| 54 | |
| 55 | // Closure to run when the lock is released. The lock is held when this is |
| 56 | // non-null. |
| 57 | LockReleasedCallback lock_released_callback_; |
| 58 | }; |
| 59 | |
| 60 | // Logging support. |
| 61 | std::ostream& operator<<(std::ostream& out, const PartitionedLock& lock_id); |
| 62 | |
| 63 | // Equality doesn't take into account whether the lock 'is_locked()' or not, |
| 64 | // only the partition and the lock_id. |
| 65 | bool operator==(const PartitionedLock& x, const PartitionedLock& y); |
Evan Stade | 25bd39a | 2024-08-15 01:54:38 | [diff] [blame] | 66 | // Comparison operator to allow sorting for locking / unlocking order. |
| 67 | bool operator<(const PartitionedLock& x, const PartitionedLock& y); |
| 68 | |
| 69 | } // namespace web_app |
| 70 | |
| 71 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_LOCKS_PARTITIONED_LOCK_H_ |