/src/mozilla-central/xpcom/threads/nsMemoryPressure.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 |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsMemoryPressure.h" |
8 | | #include "mozilla/Assertions.h" |
9 | | #include "mozilla/Atomics.h" |
10 | | |
11 | | #include "nsThreadUtils.h" |
12 | | |
13 | | using namespace mozilla; |
14 | | |
15 | | static Atomic<int32_t, Relaxed> sMemoryPressurePending; |
16 | | static_assert(MemPressure_None == 0, |
17 | | "Bad static initialization with the default constructor."); |
18 | | |
19 | | MemoryPressureState |
20 | | NS_GetPendingMemoryPressure() |
21 | 0 | { |
22 | 0 | int32_t value = sMemoryPressurePending.exchange(MemPressure_None); |
23 | 0 | return MemoryPressureState(value); |
24 | 0 | } |
25 | | |
26 | | void |
27 | | NS_DispatchEventualMemoryPressure(MemoryPressureState aState) |
28 | | { |
29 | | /* |
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. |
33 | | */ |
34 | | switch (aState) { |
35 | | case MemPressure_None: |
36 | | sMemoryPressurePending = MemPressure_None; |
37 | | break; |
38 | | case MemPressure_New: |
39 | | sMemoryPressurePending = MemPressure_New; |
40 | | break; |
41 | | case MemPressure_Ongoing: |
42 | | case MemPressure_Stopping: |
43 | | sMemoryPressurePending.compareExchange(MemPressure_None, |
44 | | aState); |
45 | | break; |
46 | | } |
47 | | } |
48 | | |
49 | | nsresult |
50 | | NS_DispatchMemoryPressure(MemoryPressureState aState) |
51 | 0 | { |
52 | 0 | NS_DispatchEventualMemoryPressure(aState); |
53 | 0 | nsCOMPtr<nsIRunnable> event = new Runnable("NS_DispatchEventualMemoryPressure"); |
54 | 0 | return NS_DispatchToMainThread(event); |
55 | 0 | } |