Bug 1845811 remove unused rv variable r=padenot
[gecko.git] / ipc / glue / IdleSchedulerChild.cpp
blobbd05f0958ad906190c01d590055259bbafca42d5
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/. */
7 #include "mozilla/ipc/IdleSchedulerChild.h"
8 #include "mozilla/ipc/IdleSchedulerParent.h"
9 #include "mozilla/ipc/PBackgroundChild.h"
10 #include "mozilla/Atomics.h"
11 #include "mozilla/IdlePeriodState.h"
12 #include "mozilla/Telemetry.h"
13 #include "BackgroundChild.h"
15 namespace mozilla::ipc {
17 static IdleSchedulerChild* sMainThreadIdleScheduler = nullptr;
18 static bool sIdleSchedulerDestroyed = false;
20 IdleSchedulerChild::~IdleSchedulerChild() {
21 if (sMainThreadIdleScheduler == this) {
22 sMainThreadIdleScheduler = nullptr;
23 sIdleSchedulerDestroyed = true;
25 MOZ_ASSERT(!mIdlePeriodState);
28 void IdleSchedulerChild::Init(IdlePeriodState* aIdlePeriodState) {
29 mIdlePeriodState = aIdlePeriodState;
31 RefPtr<IdleSchedulerChild> scheduler = this;
32 auto resolve =
33 [&](std::tuple<mozilla::Maybe<SharedMemoryHandle>, uint32_t>&& aResult) {
34 if (std::get<0>(aResult)) {
35 mActiveCounter.SetHandle(std::move(*std::get<0>(aResult)), false);
36 mActiveCounter.Map(sizeof(int32_t));
37 mChildId = std::get<1>(aResult);
38 if (mChildId && mIdlePeriodState && mIdlePeriodState->IsActive()) {
39 SetActive();
44 auto reject = [&](ResponseRejectReason) {};
45 SendInitForIdleUse(std::move(resolve), std::move(reject));
48 IPCResult IdleSchedulerChild::RecvIdleTime(uint64_t aId, TimeDuration aBudget) {
49 if (mIdlePeriodState) {
50 mIdlePeriodState->SetIdleToken(aId, aBudget);
52 return IPC_OK();
55 void IdleSchedulerChild::SetActive() {
56 if (mChildId && CanSend() && mActiveCounter.memory()) {
57 ++(static_cast<Atomic<int32_t>*>(
58 mActiveCounter.memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER]);
59 ++(static_cast<Atomic<int32_t>*>(mActiveCounter.memory())[mChildId]);
63 bool IdleSchedulerChild::SetPaused() {
64 if (mChildId && CanSend() && mActiveCounter.memory()) {
65 --(static_cast<Atomic<int32_t>*>(mActiveCounter.memory())[mChildId]);
66 // The following expression reduces the global activity count and checks if
67 // it drops below the cpu counter limit.
68 return (static_cast<Atomic<int32_t>*>(
69 mActiveCounter
70 .memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER])-- ==
71 static_cast<Atomic<int32_t>*>(
72 mActiveCounter.memory())[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER];
75 return false;
78 RefPtr<IdleSchedulerChild::MayGCPromise> IdleSchedulerChild::MayGCNow() {
79 if (mIsRequestingGC || mIsDoingGC) {
80 return MayGCPromise::CreateAndResolve(false, __func__);
82 TimeStamp wait_since = TimeStamp::Now();
84 mIsRequestingGC = true;
85 return SendRequestGC()->Then(
86 GetMainThreadSerialEventTarget(), __func__,
87 [self = RefPtr(this), wait_since](bool aIgnored) {
88 // Only one of these may be true at a time.
89 MOZ_ASSERT(!(self->mIsRequestingGC && self->mIsDoingGC));
91 // The parent process always says yes, sometimes after a delay.
92 if (self->mIsRequestingGC) {
93 Telemetry::AccumulateTimeDelta(Telemetry::GC_WAIT_FOR_IDLE_MS,
94 wait_since);
95 self->mIsRequestingGC = false;
96 self->mIsDoingGC = true;
97 return MayGCPromise::CreateAndResolve(true, __func__);
99 return MayGCPromise::CreateAndResolve(false, __func__);
101 [self = RefPtr(this)](ResponseRejectReason reason) {
102 self->mIsRequestingGC = false;
103 return MayGCPromise::CreateAndReject(reason, __func__);
107 void IdleSchedulerChild::StartedGC() {
108 // Only one of these may be true at a time.
109 MOZ_ASSERT(!(mIsRequestingGC && mIsDoingGC));
111 // If mRequestingGC was true then when the outstanding GC request returns
112 // it'll see that the GC has already started.
113 mIsRequestingGC = false;
115 if (!mIsDoingGC) {
116 if (CanSend()) {
117 SendStartedGC();
119 mIsDoingGC = true;
123 void IdleSchedulerChild::DoneGC() {
124 if (mIsDoingGC) {
125 if (CanSend()) {
126 SendDoneGC();
128 mIsDoingGC = false;
132 IdleSchedulerChild* IdleSchedulerChild::GetMainThreadIdleScheduler() {
133 MOZ_ASSERT(NS_IsMainThread());
135 if (sMainThreadIdleScheduler) {
136 return sMainThreadIdleScheduler;
139 if (sIdleSchedulerDestroyed) {
140 return nullptr;
143 ipc::PBackgroundChild* background =
144 ipc::BackgroundChild::GetOrCreateForCurrentThread();
145 if (background) {
146 sMainThreadIdleScheduler = new ipc::IdleSchedulerChild();
147 background->SendPIdleSchedulerConstructor(sMainThreadIdleScheduler);
149 return sMainThreadIdleScheduler;
152 } // namespace mozilla::ipc