xref: /aosp_15_r20/external/cronet/base/profiler/suspendable_thread_delegate.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2019 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <memory>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/profiler/register_context.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/profiler/thread_delegate.h"
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker namespace base {
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker // Platform-specific thread and stack manipulation delegate, for use by the
17*6777b538SAndroid Build Coastguard Worker // platform-independent stack copying/walking implementation in
18*6777b538SAndroid Build Coastguard Worker // StackSamplerImpl for suspension-based stack copying.
19*6777b538SAndroid Build Coastguard Worker //
20*6777b538SAndroid Build Coastguard Worker // IMPORTANT NOTE: Most methods in this interface are invoked while the target
21*6777b538SAndroid Build Coastguard Worker // thread is suspended so must not do any allocation from the heap, including
22*6777b538SAndroid Build Coastguard Worker // indirectly via use of DCHECK/CHECK or other logging statements. Otherwise the
23*6777b538SAndroid Build Coastguard Worker // implementation can deadlock on heap locks acquired by the target thread
24*6777b538SAndroid Build Coastguard Worker // before it was suspended. These functions are commented with "NO HEAP
25*6777b538SAndroid Build Coastguard Worker // ALLOCATIONS".
26*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT SuspendableThreadDelegate : public ThreadDelegate {
27*6777b538SAndroid Build Coastguard Worker  public:
28*6777b538SAndroid Build Coastguard Worker   // Implementations of this interface should suspend the thread for the
29*6777b538SAndroid Build Coastguard Worker   // object's lifetime. NO HEAP ALLOCATIONS between the time the thread is
30*6777b538SAndroid Build Coastguard Worker   // suspended and resumed.
31*6777b538SAndroid Build Coastguard Worker   class BASE_EXPORT ScopedSuspendThread {
32*6777b538SAndroid Build Coastguard Worker    public:
33*6777b538SAndroid Build Coastguard Worker     ScopedSuspendThread() = default;
34*6777b538SAndroid Build Coastguard Worker     virtual ~ScopedSuspendThread() = default;
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker     ScopedSuspendThread(const ScopedSuspendThread&) = delete;
37*6777b538SAndroid Build Coastguard Worker     ScopedSuspendThread& operator=(const ScopedSuspendThread&) = delete;
38*6777b538SAndroid Build Coastguard Worker 
39*6777b538SAndroid Build Coastguard Worker     virtual bool WasSuccessful() const = 0;
40*6777b538SAndroid Build Coastguard Worker   };
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker   SuspendableThreadDelegate() = default;
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker   // Creates an object that holds the thread suspended for its lifetime.
45*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<ScopedSuspendThread> CreateScopedSuspendThread() = 0;
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker   // Gets the register context for the thread.
48*6777b538SAndroid Build Coastguard Worker   // NO HEAP ALLOCATIONS.
49*6777b538SAndroid Build Coastguard Worker   virtual bool GetThreadContext(RegisterContext* thread_context) = 0;
50*6777b538SAndroid Build Coastguard Worker 
51*6777b538SAndroid Build Coastguard Worker   // Returns true if the thread's stack can be copied, where the bottom address
52*6777b538SAndroid Build Coastguard Worker   // of the thread is at |stack_pointer|.
53*6777b538SAndroid Build Coastguard Worker   // NO HEAP ALLOCATIONS.
54*6777b538SAndroid Build Coastguard Worker   virtual bool CanCopyStack(uintptr_t stack_pointer) = 0;
55*6777b538SAndroid Build Coastguard Worker };
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker }  // namespace base
58*6777b538SAndroid Build Coastguard Worker 
59*6777b538SAndroid Build Coastguard Worker #endif  // BASE_PROFILER_SUSPENDABLE_THREAD_DELEGATE_H_
60