Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsMemoryPressure.h" |
michael@0 | 8 | #include "mozilla/Assertions.h" |
michael@0 | 9 | #include "mozilla/Atomics.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsThreadUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | using namespace mozilla; |
michael@0 | 14 | |
michael@0 | 15 | static Atomic<int32_t, Relaxed> sMemoryPressurePending; |
michael@0 | 16 | static_assert(MemPressure_None == 0, |
michael@0 | 17 | "Bad static initialization with the default constructor."); |
michael@0 | 18 | |
michael@0 | 19 | MemoryPressureState |
michael@0 | 20 | NS_GetPendingMemoryPressure() |
michael@0 | 21 | { |
michael@0 | 22 | int32_t value = sMemoryPressurePending.exchange(MemPressure_None); |
michael@0 | 23 | return MemoryPressureState(value); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | void |
michael@0 | 27 | NS_DispatchEventualMemoryPressure(MemoryPressureState state) |
michael@0 | 28 | { |
michael@0 | 29 | /* |
michael@0 | 30 | * A new memory pressure event erases an ongoing memory pressure, but an |
michael@0 | 31 | * existing "new" memory pressure event takes precedence over a new "ongoing" |
michael@0 | 32 | * memory pressure event. |
michael@0 | 33 | */ |
michael@0 | 34 | switch (state) { |
michael@0 | 35 | case MemPressure_None: |
michael@0 | 36 | sMemoryPressurePending = MemPressure_None; |
michael@0 | 37 | break; |
michael@0 | 38 | case MemPressure_New: |
michael@0 | 39 | sMemoryPressurePending = MemPressure_New; |
michael@0 | 40 | break; |
michael@0 | 41 | case MemPressure_Ongoing: |
michael@0 | 42 | sMemoryPressurePending.compareExchange(MemPressure_None, MemPressure_Ongoing); |
michael@0 | 43 | break; |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | nsresult |
michael@0 | 48 | NS_DispatchMemoryPressure(MemoryPressureState state) |
michael@0 | 49 | { |
michael@0 | 50 | NS_DispatchEventualMemoryPressure(state); |
michael@0 | 51 | nsCOMPtr<nsIRunnable> event = new nsRunnable; |
michael@0 | 52 | return NS_DispatchToMainThread(event); |
michael@0 | 53 | } |