// Copyright 2022 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CC_BASE_PROTECTED_SEQUENCE_SYNCHRONIZER_H_ #define CC_BASE_PROTECTED_SEQUENCE_SYNCHRONIZER_H_ #include <memory> #include <utility> namespace cc { // ProtectedSequenceSynchronizer can be used to enforce thread safety of data // that are owned and produced by an "owner" thread; and then passed by // reference to another thread to use for a limited duration (a "protected // sequence"). A protected sequence must be initiated on the owning thread, and // it must be concluded on the non-owning thread. See the code comment above // InProtectedSequence() for more on this requirement. class ProtectedSequenceSynchronizer { … }; // ProtectedSequenceForbidden values cannot be accessed for read or write by any // non-owner thread. There are no restrictions on access by the owner thread. template <typename T> class ProtectedSequenceForbidden { … }; // ProtectedSequenceReadable values are... // - readable by the owner thread at any time without blocking // - writable by the owner thread, but not during a protected sequence // - readable by a non-owner thread during a protected sequence // - never writable by a non-owner thread template <typename T> class ProtectedSequenceReadable { … }; // ProtectedSequenceWritable values are... // - readable by the owner thread, but not during a protected sequence // - writable by the owner thread, but not during a protected sequence // - readable by a non-owner thread during a protected sequence // - writable by a non-owner thread during a protected sequence // // Note that it is not safe to use ProtectedSequenceWritable values concurrently // on two or more non-owner threads. template <typename T> class ProtectedSequenceWritable { … }; // Type specializations for various containers. ProtectedSequenceForbidden<std::unique_ptr<T>>; ProtectedSequenceReadable<std::unique_ptr<T>>; ProtectedSequenceWritable<std::unique_ptr<T>>; } // namespace cc #endif // CC_BASE_PROTECTED_SEQUENCE_SYNCHRONIZER_H_