summaryrefslogtreecommitdiffstats
path: root/xpcom/threads/nsMemoryPressure.cpp
blob: dbd3a92f792912810ae1312e1f67d6fe264c70e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsMemoryPressure.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Services.h"

#include "nsThreadUtils.h"
#include "nsIObserverService.h"

using namespace mozilla;

const char* const kTopicMemoryPressure = "memory-pressure";
const char* const kTopicMemoryPressureStop = "memory-pressure-stop";
const char16_t* const kSubTopicLowMemoryNew = u"low-memory";
const char16_t* const kSubTopicLowMemoryOngoing = u"low-memory-ongoing";

// This is accessed from any thread through NS_NotifyOfEventualMemoryPressure
static Atomic<MemoryPressureState, Relaxed> sMemoryPressurePending(
    MemoryPressureState::NoPressure);

void NS_NotifyOfEventualMemoryPressure(MemoryPressureState aState) {
  MOZ_ASSERT(aState != MemoryPressureState::None);

  /*
   * A new memory pressure event erases an ongoing (or stop of) memory pressure,
   * but an existing "new" memory pressure event takes precedence over a new
   * "ongoing" or "stop" memory pressure event.
   */
  switch (aState) {
    case MemoryPressureState::None:
    case MemoryPressureState::LowMemory:
      sMemoryPressurePending = aState;
      break;
    case MemoryPressureState::NoPressure:
      sMemoryPressurePending.compareExchange(MemoryPressureState::None, aState);
      break;
  }
}

nsresult NS_NotifyOfMemoryPressure(MemoryPressureState aState) {
  NS_NotifyOfEventualMemoryPressure(aState);
  nsCOMPtr<nsIRunnable> event =
      new Runnable("NS_DispatchEventualMemoryPressure");
  return NS_DispatchToMainThread(event);
}

void NS_DispatchMemoryPressure() {
  MOZ_ASSERT(NS_IsMainThread());
  static MemoryPressureState sMemoryPressureStatus =
      MemoryPressureState::NoPressure;

  MemoryPressureState mpPending =
      sMemoryPressurePending.exchange(MemoryPressureState::None);
  if (mpPending == MemoryPressureState::None) {
    return;
  }

  nsCOMPtr<nsIObserverService> os = services::GetObserverService();
  if (!os) {
    NS_WARNING("Can't get observer service!");
    return;
  }

  switch (mpPending) {
    case MemoryPressureState::None:
      MOZ_ASSERT_UNREACHABLE("Already handled this case above.");
      break;
    case MemoryPressureState::LowMemory:
      switch (sMemoryPressureStatus) {
        case MemoryPressureState::None:
          MOZ_ASSERT_UNREACHABLE("The internal status should never be None.");
          break;
        case MemoryPressureState::NoPressure:
          sMemoryPressureStatus = MemoryPressureState::LowMemory;
          os->NotifyObservers(nullptr, kTopicMemoryPressure,
                              kSubTopicLowMemoryNew);
          break;
        case MemoryPressureState::LowMemory:
          os->NotifyObservers(nullptr, kTopicMemoryPressure,
                              kSubTopicLowMemoryOngoing);
          break;
      }
      break;
    case MemoryPressureState::NoPressure:
      switch (sMemoryPressureStatus) {
        case MemoryPressureState::None:
          MOZ_ASSERT_UNREACHABLE("The internal status should never be None.");
          break;
        case MemoryPressureState::NoPressure:
          // Already no pressure.  Do nothing.
          break;
        case MemoryPressureState::LowMemory:
          sMemoryPressureStatus = MemoryPressureState::NoPressure;
          os->NotifyObservers(nullptr, kTopicMemoryPressureStop, nullptr);
          break;
      }
      break;
  }
}