michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsMemoryPressure.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Atomics.h" michael@0: michael@0: #include "nsThreadUtils.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static Atomic sMemoryPressurePending; michael@0: static_assert(MemPressure_None == 0, michael@0: "Bad static initialization with the default constructor."); michael@0: michael@0: MemoryPressureState michael@0: NS_GetPendingMemoryPressure() michael@0: { michael@0: int32_t value = sMemoryPressurePending.exchange(MemPressure_None); michael@0: return MemoryPressureState(value); michael@0: } michael@0: michael@0: void michael@0: NS_DispatchEventualMemoryPressure(MemoryPressureState state) michael@0: { michael@0: /* michael@0: * A new memory pressure event erases an ongoing memory pressure, but an michael@0: * existing "new" memory pressure event takes precedence over a new "ongoing" michael@0: * memory pressure event. michael@0: */ michael@0: switch (state) { michael@0: case MemPressure_None: michael@0: sMemoryPressurePending = MemPressure_None; michael@0: break; michael@0: case MemPressure_New: michael@0: sMemoryPressurePending = MemPressure_New; michael@0: break; michael@0: case MemPressure_Ongoing: michael@0: sMemoryPressurePending.compareExchange(MemPressure_None, MemPressure_Ongoing); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: NS_DispatchMemoryPressure(MemoryPressureState state) michael@0: { michael@0: NS_DispatchEventualMemoryPressure(state); michael@0: nsCOMPtr event = new nsRunnable; michael@0: return NS_DispatchToMainThread(event); michael@0: }