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 "nsMemoryPressure.h"
8 #include "mozilla/Assertions.h"
9 #include "mozilla/Atomics.h"
10 #include "mozilla/Services.h"
12 #include "nsThreadUtils.h"
13 #include "nsIObserverService.h"
15 using namespace mozilla
;
17 const char* const kTopicMemoryPressure
= "memory-pressure";
18 const char* const kTopicMemoryPressureStop
= "memory-pressure-stop";
19 const char16_t
* const kSubTopicLowMemoryNew
= u
"low-memory";
20 const char16_t
* const kSubTopicLowMemoryOngoing
= u
"low-memory-ongoing";
22 // This is accessed from any thread through NS_NotifyOfEventualMemoryPressure
23 static Atomic
<MemoryPressureState
, Relaxed
> sMemoryPressurePending(
24 MemoryPressureState::NoPressure
);
26 void NS_NotifyOfEventualMemoryPressure(MemoryPressureState aState
) {
27 MOZ_ASSERT(aState
!= MemoryPressureState::None
);
30 * A new memory pressure event erases an ongoing (or stop of) memory pressure,
31 * but an existing "new" memory pressure event takes precedence over a new
32 * "ongoing" or "stop" memory pressure event.
35 case MemoryPressureState::None
:
36 case MemoryPressureState::LowMemory
:
37 sMemoryPressurePending
= aState
;
39 case MemoryPressureState::NoPressure
:
40 sMemoryPressurePending
.compareExchange(MemoryPressureState::None
, aState
);
45 nsresult
NS_NotifyOfMemoryPressure(MemoryPressureState aState
) {
46 NS_NotifyOfEventualMemoryPressure(aState
);
47 nsCOMPtr
<nsIRunnable
> event
=
48 new Runnable("NS_DispatchEventualMemoryPressure");
49 return NS_DispatchToMainThread(event
);
52 void NS_DispatchMemoryPressure() {
53 MOZ_ASSERT(NS_IsMainThread());
54 static MemoryPressureState sMemoryPressureStatus
=
55 MemoryPressureState::NoPressure
;
57 MemoryPressureState mpPending
=
58 sMemoryPressurePending
.exchange(MemoryPressureState::None
);
59 if (mpPending
== MemoryPressureState::None
) {
63 nsCOMPtr
<nsIObserverService
> os
= services::GetObserverService();
65 NS_WARNING("Can't get observer service!");
70 case MemoryPressureState::None
:
71 MOZ_ASSERT_UNREACHABLE("Already handled this case above.");
73 case MemoryPressureState::LowMemory
:
74 switch (sMemoryPressureStatus
) {
75 case MemoryPressureState::None
:
76 MOZ_ASSERT_UNREACHABLE("The internal status should never be None.");
78 case MemoryPressureState::NoPressure
:
79 sMemoryPressureStatus
= MemoryPressureState::LowMemory
;
80 os
->NotifyObservers(nullptr, kTopicMemoryPressure
,
81 kSubTopicLowMemoryNew
);
83 case MemoryPressureState::LowMemory
:
84 os
->NotifyObservers(nullptr, kTopicMemoryPressure
,
85 kSubTopicLowMemoryOngoing
);
89 case MemoryPressureState::NoPressure
:
90 switch (sMemoryPressureStatus
) {
91 case MemoryPressureState::None
:
92 MOZ_ASSERT_UNREACHABLE("The internal status should never be None.");
94 case MemoryPressureState::NoPressure
:
95 // Already no pressure. Do nothing.
97 case MemoryPressureState::LowMemory
:
98 sMemoryPressureStatus
= MemoryPressureState::NoPressure
;
99 os
->NotifyObservers(nullptr, kTopicMemoryPressureStop
, nullptr);