1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SEQUENCE_CHECKER_IMPL_H_
6 #define BASE_SEQUENCE_CHECKER_IMPL_H_
10 #include "base/base_export.h"
11 #include "base/synchronization/lock.h"
12 #include "base/thread_annotations.h"
19 // Real implementation of SequenceChecker for use in debug mode or for temporary
20 // use in release mode (e.g. to CHECK on a threading issue seen only in the
23 // Note: You should almost always use the SequenceChecker class to get the right
24 // version for your build configuration.
25 // Note: This is marked with "context" capability in order to support
26 // thread_annotations.h.
27 class THREAD_ANNOTATION_ATTRIBUTE__(capability("context"))
28 BASE_EXPORT SequenceCheckerImpl {
30 static void EnableStackLogging();
32 SequenceCheckerImpl();
34 // Allow move construct/assign. This must be called on |other|'s associated
35 // sequence and assignment can only be made into a SequenceCheckerImpl which
36 // is detached or already associated with the current sequence. This isn't
37 // thread-safe (|this| and |other| shouldn't be in use while this move is
38 // performed). If the assignment was legal, the resulting SequenceCheckerImpl
39 // will be bound to the current sequence and |other| will be detached.
40 SequenceCheckerImpl(SequenceCheckerImpl&& other);
41 SequenceCheckerImpl& operator=(SequenceCheckerImpl&& other);
42 SequenceCheckerImpl(const SequenceCheckerImpl&) = delete;
43 SequenceCheckerImpl& operator=(const SequenceCheckerImpl&) = delete;
44 ~SequenceCheckerImpl();
46 // Returns true if called in sequence with previous calls to this method and
48 // On returning false, if logging is enabled with EnableStackLogging() and
49 // `out_bound_at` is not null, this method allocates a StackTrace and returns
50 // it in the out-parameter, storing inside it the stack from where the failing
51 // SequenceChecker was bound to its sequence. Otherwise, out_bound_at is left
53 bool CalledOnValidSequence(std::unique_ptr<debug::StackTrace>* out_bound_at =
54 nullptr) const WARN_UNUSED_RESULT;
56 // Unbinds the checker from the currently associated sequence. The checker
57 // will be re-bound on the next call to CalledOnValidSequence().
58 void DetachFromSequence();
63 // Calls straight to ThreadLocalStorage::HasBeenDestroyed(). Exposed purely
64 // for 'friend' to work.
65 static bool HasThreadLocalStorageBeenDestroyed();
68 mutable std::unique_ptr<Core> core_ GUARDED_BY(lock_);
73 #endif // BASE_SEQUENCE_CHECKER_IMPL_H_