xpcom/threads/nsMemoryPressure.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
     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"
    11 #include "nsThreadUtils.h"
    13 using namespace mozilla;
    15 static Atomic<int32_t, Relaxed> sMemoryPressurePending;
    16 static_assert(MemPressure_None == 0,
    17               "Bad static initialization with the default constructor.");
    19 MemoryPressureState
    20 NS_GetPendingMemoryPressure()
    21 {
    22   int32_t value = sMemoryPressurePending.exchange(MemPressure_None);
    23   return MemoryPressureState(value);
    24 }
    26 void
    27 NS_DispatchEventualMemoryPressure(MemoryPressureState state)
    28 {
    29   /*
    30    * A new memory pressure event erases an ongoing memory pressure, but an
    31    * existing "new" memory pressure event takes precedence over a new "ongoing"
    32    * memory pressure event.
    33    */
    34   switch (state) {
    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       sMemoryPressurePending.compareExchange(MemPressure_None, MemPressure_Ongoing);
    43       break;
    44   }
    45 }
    47 nsresult
    48 NS_DispatchMemoryPressure(MemoryPressureState state)
    49 {
    50   NS_DispatchEventualMemoryPressure(state);
    51   nsCOMPtr<nsIRunnable> event = new nsRunnable;
    52   return NS_DispatchToMainThread(event);
    53 }

mercurial