/work/obj-fuzz/dist/include/ThreadSafeRefcountingWithMainThreadDestruction.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef THREADSAFEREFCOUNTINGWITHMAINTHREADDESTRUCTION_H_ |
8 | | #define THREADSAFEREFCOUNTINGWITHMAINTHREADDESTRUCTION_H_ |
9 | | |
10 | | #include "base/message_loop.h" |
11 | | #include "MainThreadUtils.h" |
12 | | #include "nsThreadUtils.h" |
13 | | |
14 | | namespace mozilla { |
15 | | namespace layers { |
16 | | |
17 | | inline MessageLoop* GetMainLoopAssertingMainThread() |
18 | 0 | { |
19 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
20 | 0 | return MessageLoop::current(); |
21 | 0 | } |
22 | | |
23 | | inline MessageLoop* GetMainLoop() |
24 | 0 | { |
25 | 0 | static MessageLoop* sMainLoop = GetMainLoopAssertingMainThread(); |
26 | 0 | return sMainLoop; |
27 | 0 | } |
28 | | |
29 | | struct HelperForMainThreadDestruction |
30 | | { |
31 | | HelperForMainThreadDestruction() |
32 | 0 | { |
33 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
34 | 0 | GetMainLoop(); |
35 | 0 | } |
36 | | |
37 | | ~HelperForMainThreadDestruction() |
38 | 0 | { |
39 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
40 | 0 | } |
41 | | }; |
42 | | |
43 | | template<typename T> |
44 | | struct DeleteOnMainThreadTask : public Runnable |
45 | | { |
46 | | T* mToDelete; |
47 | | explicit DeleteOnMainThreadTask(T* aToDelete) |
48 | | : Runnable("layers::DeleteOnMainThreadTask") |
49 | | , mToDelete(aToDelete) |
50 | 0 | { |
51 | 0 | } Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::layers::CompositorThreadHolder>::DeleteOnMainThreadTask(mozilla::layers::CompositorThreadHolder*) Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::gfx::VRListenerThreadHolder>::DeleteOnMainThreadTask(mozilla::gfx::VRListenerThreadHolder*) Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::wr::RenderThread>::DeleteOnMainThreadTask(mozilla::wr::RenderThread*) Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::SourceListener>::DeleteOnMainThreadTask(mozilla::SourceListener*) |
52 | 0 | NS_IMETHOD Run() override { |
53 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
54 | 0 | mToDelete->DeleteToBeCalledOnMainThread(); |
55 | 0 | return NS_OK; |
56 | 0 | } Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::wr::RenderThread>::Run() Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::layers::CompositorThreadHolder>::Run() Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::gfx::VRListenerThreadHolder>::Run() Unexecuted instantiation: mozilla::layers::DeleteOnMainThreadTask<mozilla::SourceListener>::Run() |
57 | | }; |
58 | | |
59 | | } // namespace layers |
60 | | } // namespace mozilla |
61 | | |
62 | | #define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION_AND_RECORDING(_class, _recording) \ |
63 | | public: \ |
64 | 0 | NS_METHOD_(MozExternalRefCountType) AddRef(void) { \ |
65 | 0 | MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \ |
66 | 0 | MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \ |
67 | 0 | nsrefcnt count = ++mRefCnt; \ |
68 | 0 | NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \ |
69 | 0 | return (nsrefcnt) count; \ |
70 | 0 | } \ Unexecuted instantiation: mozilla::wr::RenderThread::AddRef() Unexecuted instantiation: mozilla::layers::CompositorThreadHolder::AddRef() Unexecuted instantiation: mozilla::gfx::VRListenerThreadHolder::AddRef() Unexecuted instantiation: mozilla::SourceListener::AddRef() |
71 | 0 | void DeleteToBeCalledOnMainThread() { \ |
72 | 0 | MOZ_ASSERT(NS_IsMainThread()); \ |
73 | 0 | NS_LOG_RELEASE(this, 0, #_class); \ |
74 | 0 | delete this; \ |
75 | 0 | } \ Unexecuted instantiation: mozilla::wr::RenderThread::DeleteToBeCalledOnMainThread() Unexecuted instantiation: mozilla::layers::CompositorThreadHolder::DeleteToBeCalledOnMainThread() Unexecuted instantiation: mozilla::gfx::VRListenerThreadHolder::DeleteToBeCalledOnMainThread() Unexecuted instantiation: mozilla::SourceListener::DeleteToBeCalledOnMainThread() |
76 | 0 | NS_METHOD_(MozExternalRefCountType) Release(void) { \ |
77 | 0 | MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \ |
78 | 0 | nsrefcnt count = --mRefCnt; \ |
79 | 0 | if (count == 0) { \ |
80 | 0 | if (NS_IsMainThread()) { \ |
81 | 0 | DeleteToBeCalledOnMainThread(); \ |
82 | 0 | } else { \ |
83 | 0 | NS_DispatchToMainThread( \ |
84 | 0 | new mozilla::layers::DeleteOnMainThreadTask<_class>(this)); \ |
85 | 0 | } \ |
86 | 0 | } else { \ |
87 | 0 | NS_LOG_RELEASE(this, count, #_class); \ |
88 | 0 | } \ |
89 | 0 | return count; \ |
90 | 0 | } \ Unexecuted instantiation: mozilla::wr::RenderThread::Release() Unexecuted instantiation: mozilla::layers::CompositorThreadHolder::Release() Unexecuted instantiation: mozilla::gfx::VRListenerThreadHolder::Release() Unexecuted instantiation: mozilla::SourceListener::Release() |
91 | | protected: \ |
92 | | ::mozilla::ThreadSafeAutoRefCntWithRecording<_recording> mRefCnt; \ |
93 | | private: \ |
94 | | ::mozilla::layers::HelperForMainThreadDestruction mHelperForMainThreadDestruction; \ |
95 | | public: |
96 | | |
97 | | #define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION(_class) \ |
98 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION_AND_RECORDING \ |
99 | | (_class, recordreplay::Behavior::DontPreserve) |
100 | | |
101 | | #endif |