/src/mozilla-central/xpcom/threads/SystemGroup.cpp
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 file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "SystemGroup.h" |
8 | | |
9 | | #include "mozilla/AbstractThread.h" |
10 | | #include "mozilla/Move.h" |
11 | | #include "mozilla/StaticPtr.h" |
12 | | #include "mozilla/UniquePtr.h" |
13 | | #include "nsINamed.h" |
14 | | |
15 | | using namespace mozilla; |
16 | | |
17 | | class SystemGroupImpl final : public SchedulerGroup |
18 | | { |
19 | | public: |
20 | | SystemGroupImpl(); |
21 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SystemGroupImpl, override) |
22 | | |
23 | | static void InitStatic(); |
24 | | static void ShutdownStatic(); |
25 | | static SystemGroupImpl* Get(); |
26 | | |
27 | 102 | static bool Initialized() { return !!sSingleton; } |
28 | | |
29 | | private: |
30 | | ~SystemGroupImpl() = default; |
31 | | static StaticRefPtr<SystemGroupImpl> sSingleton; |
32 | | }; |
33 | | |
34 | | StaticRefPtr<SystemGroupImpl> SystemGroupImpl::sSingleton; |
35 | | |
36 | | SystemGroupImpl::SystemGroupImpl() |
37 | 3 | { |
38 | 3 | CreateEventTargets(/* aNeedValidation = */ true); |
39 | 3 | } |
40 | | |
41 | | /* static */ void |
42 | | SystemGroupImpl::InitStatic() |
43 | 3 | { |
44 | 3 | MOZ_ASSERT(!sSingleton); |
45 | 3 | MOZ_ASSERT(NS_IsMainThread()); |
46 | 3 | sSingleton = new SystemGroupImpl(); |
47 | 3 | } |
48 | | |
49 | | /* static */ void |
50 | | SystemGroupImpl::ShutdownStatic() |
51 | 0 | { |
52 | 0 | sSingleton->Shutdown(true); |
53 | 0 | sSingleton = nullptr; |
54 | 0 | } |
55 | | |
56 | | /* static */ SystemGroupImpl* |
57 | | SystemGroupImpl::Get() |
58 | 102 | { |
59 | 102 | MOZ_ASSERT(sSingleton); |
60 | 102 | return sSingleton.get(); |
61 | 102 | } |
62 | | |
63 | | void |
64 | | SystemGroup::InitStatic() |
65 | 3 | { |
66 | 3 | SystemGroupImpl::InitStatic(); |
67 | 3 | } |
68 | | |
69 | | void |
70 | | SystemGroup::Shutdown() |
71 | 0 | { |
72 | 0 | SystemGroupImpl::ShutdownStatic(); |
73 | 0 | } |
74 | | |
75 | | bool |
76 | | SystemGroup::Initialized() |
77 | 0 | { |
78 | 0 | return SystemGroupImpl::Initialized(); |
79 | 0 | } |
80 | | |
81 | | /* static */ nsresult |
82 | | SystemGroup::Dispatch(TaskCategory aCategory, |
83 | | already_AddRefed<nsIRunnable>&& aRunnable) |
84 | 18 | { |
85 | 18 | if (!SystemGroupImpl::Initialized()) { |
86 | 0 | return NS_DispatchToMainThread(std::move(aRunnable)); |
87 | 0 | } |
88 | 18 | return SystemGroupImpl::Get()->Dispatch(aCategory, std::move(aRunnable)); |
89 | 18 | } |
90 | | |
91 | | /* static */ nsISerialEventTarget* |
92 | | SystemGroup::EventTargetFor(TaskCategory aCategory) |
93 | 84 | { |
94 | 84 | if (!SystemGroupImpl::Initialized()) { |
95 | 0 | return GetMainThreadSerialEventTarget(); |
96 | 0 | } |
97 | 84 | return SystemGroupImpl::Get()->EventTargetFor(aCategory); |
98 | 84 | } |
99 | | |
100 | | /* static */ AbstractThread* |
101 | | SystemGroup::AbstractMainThreadFor(TaskCategory aCategory) |
102 | 0 | { |
103 | 0 | MOZ_ASSERT(SystemGroupImpl::Initialized()); |
104 | 0 | return SystemGroupImpl::Get()->AbstractMainThreadFor(aCategory); |
105 | 0 | } |