OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MemoryPressure provides static APIs for handling memory pressure on | 5 // MemoryPressure provides static APIs for handling memory pressure on |
6 // platforms that have such signals, such as Android and ChromeOS. | 6 // platforms that have such signals, such as Android and ChromeOS. |
7 // The app will try to discard buffers that aren't deemed essential (individual | 7 // The app will try to discard buffers that aren't deemed essential (individual |
8 // modules will implement their own policy). | 8 // modules will implement their own policy). |
9 | 9 |
10 #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ | 10 #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |
11 #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ | 11 #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |
12 | 12 |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/lazy_instance.h" | |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/observer_list_threadsafe.h" | |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 | 20 |
19 // To start listening, create a new instance, passing a callback to a | 21 // To start listening, create a new instance, passing a callback to a |
20 // function that takes a MemoryPressureLevel parameter. To stop listening, | 22 // function that takes a MemoryPressureLevel parameter. To stop listening, |
21 // simply delete the listener object. The implementation guarantees | 23 // simply delete the listener object. The implementation guarantees |
22 // that the callback will always be called on the thread that created | 24 // that the callback will always be called on the thread that created |
23 // the listener. | 25 // the listener. |
24 // Note that even on the same thread, the callback is not guaranteed to be | 26 // Note that even on the same thread, the callback is not guaranteed to be |
25 // called synchronously within the system memory pressure broadcast. | 27 // called synchronously within the system memory pressure broadcast. |
(...skipping 18 matching lines...) Expand all Loading... | |
44 // delete my_listener; | 46 // delete my_listener; |
45 // | 47 // |
46 class BASE_EXPORT MemoryPressureListener { | 48 class BASE_EXPORT MemoryPressureListener { |
47 public: | 49 public: |
48 // A Java counterpart will be generated for this enum. | 50 // A Java counterpart will be generated for this enum. |
49 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base | 51 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base |
50 enum MemoryPressureLevel { | 52 enum MemoryPressureLevel { |
51 // No problems, there is enough memory to use. This event is not sent via | 53 // No problems, there is enough memory to use. This event is not sent via |
52 // callback, but the enum is used in other places to find out the current | 54 // callback, but the enum is used in other places to find out the current |
53 // state of the system. | 55 // state of the system. |
54 MEMORY_PRESSURE_LEVEL_NONE = -1, | 56 MEMORY_PRESSURE_LEVEL_NONE, |
55 | 57 |
56 // Modules are advised to free buffers that are cheap to re-allocate and not | 58 // Modules are advised to free buffers that are cheap to re-allocate and not |
57 // immediately needed. | 59 // immediately needed. |
58 MEMORY_PRESSURE_LEVEL_MODERATE = 0, | 60 MEMORY_PRESSURE_LEVEL_MODERATE, |
59 | 61 |
60 // At this level, modules are advised to free all possible memory. The | 62 // At this level, modules are advised to free all possible memory. The |
61 // alternative is to be killed by the system, which means all memory will | 63 // alternative is to be killed by the system, which means all memory will |
62 // have to be re-created, plus the cost of a cold start. | 64 // have to be re-created, plus the cost of a cold start. |
63 MEMORY_PRESSURE_LEVEL_CRITICAL = 2, | 65 MEMORY_PRESSURE_LEVEL_CRITICAL, |
64 }; | 66 }; |
65 | 67 |
66 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; | 68 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; |
69 typedef base::Callback<void(MemoryPressureLevel)> SyncMemoryPressureCallback; | |
67 | 70 |
68 explicit MemoryPressureListener( | 71 explicit MemoryPressureListener( |
69 const MemoryPressureCallback& memory_pressure_callback); | 72 const MemoryPressureCallback& memory_pressure_callback); |
73 explicit MemoryPressureListener( | |
kinuko
2016/04/11 09:30:40
nit: no need of explicit here
hong.zheng
2016/04/15 06:05:57
Done.
| |
74 const MemoryPressureCallback& memory_pressure_callback, | |
75 const SyncMemoryPressureCallback& sync_memory_pressure_callback); | |
76 | |
70 ~MemoryPressureListener(); | 77 ~MemoryPressureListener(); |
71 | 78 |
72 // Intended for use by the platform specific implementation. | 79 // Intended for use by the platform specific implementation. |
73 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); | 80 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
74 | 81 |
75 // These methods should not be used anywhere else but in memory measurement | 82 // These methods should not be used anywhere else but in memory measurement |
76 // code, where they are intended to maintain stable conditions across | 83 // code, where they are intended to maintain stable conditions across |
77 // measurements. | 84 // measurements. |
78 static bool AreNotificationsSuppressed(); | 85 static bool AreNotificationsSuppressed(); |
79 static void SetNotificationsSuppressed(bool suppressed); | 86 static void SetNotificationsSuppressed(bool suppressed); |
80 static void SimulatePressureNotification( | 87 static void SimulatePressureNotification( |
81 MemoryPressureLevel memory_pressure_level); | 88 MemoryPressureLevel memory_pressure_level); |
82 | 89 |
83 private: | 90 private: |
91 // ObserverListThreadSafe is RefCountedThreadSafe, this traits is needed | |
92 // to ensure the LazyInstance will hold a reference to it. | |
93 struct LeakyLazyObserverListTraits : | |
94 base::internal::LeakyLazyInstanceTraits< | |
95 ObserverListThreadSafe<MemoryPressureListener> > { | |
96 static ObserverListThreadSafe<MemoryPressureListener>* | |
97 New(void* instance) { | |
98 ObserverListThreadSafe<MemoryPressureListener>* ret = | |
99 base::internal::LeakyLazyInstanceTraits< | |
100 ObserverListThreadSafe<MemoryPressureListener>>::New(instance); | |
101 // Leaky. | |
102 ret->AddRef(); | |
103 return ret; | |
104 } | |
105 }; | |
106 | |
107 MemoryPressureListener(); | |
108 | |
84 void Notify(MemoryPressureLevel memory_pressure_level); | 109 void Notify(MemoryPressureLevel memory_pressure_level); |
110 void SyncNotify(MemoryPressureLevel memory_pressure_level); | |
85 | 111 |
86 static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); | 112 static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
87 | 113 |
88 MemoryPressureCallback callback_; | 114 MemoryPressureCallback callback_; |
115 SyncMemoryPressureCallback sync_memory_pressure_callback_; | |
116 | |
117 LazyInstance<ObserverListThreadSafe<MemoryPressureListener>, | |
118 LeakyLazyObserverListTraits> observers_; | |
119 LazyInstance<ObserverList<MemoryPressureListener>> sync_observers_; | |
120 base::LazyInstance<base::Lock>::Leaky sync_observers_lock_; | |
121 | |
122 static MemoryPressureListener* g_listener; | |
89 | 123 |
90 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener); | 124 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener); |
91 }; | 125 }; |
92 | 126 |
93 } // namespace base | 127 } // namespace base |
94 | 128 |
95 #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ | 129 #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |
OLD | NEW |