OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "base/memory/memory_coordinator_proxy.h" | 5 #include "base/memory/memory_coordinator_proxy.h" |
6 | 6 |
7 #include "base/atomicops.h" | |
8 | |
7 namespace base { | 9 namespace base { |
8 | 10 |
11 namespace { | |
12 | |
13 subtle::AtomicWord g_memory_coordinator = 0; | |
dcheng
2017/02/20 03:07:21
Is it necessary to use atomics? The base::Callback
bashi
2017/02/20 03:16:59
Probably it isn't necessary to use atomics. I just
| |
14 | |
15 inline MemoryCoordinator* GetMemoryCoordinator() { | |
16 subtle::AtomicWord coordinator = | |
17 base::subtle::Acquire_Load(&g_memory_coordinator); | |
18 return reinterpret_cast<MemoryCoordinator*>(coordinator); | |
19 } | |
20 | |
21 } // namespace | |
22 | |
9 MemoryCoordinatorProxy::MemoryCoordinatorProxy() { | 23 MemoryCoordinatorProxy::MemoryCoordinatorProxy() { |
10 } | 24 } |
11 | 25 |
12 MemoryCoordinatorProxy::~MemoryCoordinatorProxy() { | 26 MemoryCoordinatorProxy::~MemoryCoordinatorProxy() { |
13 } | 27 } |
14 | 28 |
29 // static | |
15 MemoryCoordinatorProxy* MemoryCoordinatorProxy::GetInstance() { | 30 MemoryCoordinatorProxy* MemoryCoordinatorProxy::GetInstance() { |
16 return Singleton<base::MemoryCoordinatorProxy>::get(); | 31 return Singleton<base::MemoryCoordinatorProxy>::get(); |
17 } | 32 } |
18 | 33 |
34 // static | |
35 void MemoryCoordinatorProxy::SetMemoryCoordinator( | |
36 MemoryCoordinator* coordinator) { | |
37 base::subtle::Release_Store( | |
38 &g_memory_coordinator, reinterpret_cast<subtle::AtomicWord>(coordinator)); | |
39 } | |
40 | |
19 MemoryState MemoryCoordinatorProxy::GetCurrentMemoryState() const { | 41 MemoryState MemoryCoordinatorProxy::GetCurrentMemoryState() const { |
20 if (!getter_callback_) | 42 auto* coordinator = GetMemoryCoordinator(); |
43 if (!coordinator) | |
21 return MemoryState::NORMAL; | 44 return MemoryState::NORMAL; |
22 return getter_callback_.Run(); | 45 return coordinator->GetCurrentMemoryState(); |
23 } | 46 } |
24 | 47 |
25 void MemoryCoordinatorProxy::SetCurrentMemoryStateForTesting( | 48 void MemoryCoordinatorProxy::SetCurrentMemoryStateForTesting( |
26 MemoryState memory_state) { | 49 MemoryState memory_state) { |
27 DCHECK(setter_callback_); | 50 auto* coordinator = GetMemoryCoordinator(); |
28 setter_callback_.Run(memory_state); | 51 DCHECK(coordinator); |
29 } | 52 coordinator->SetCurrentMemoryStateForTesting(memory_state); |
30 | |
31 void MemoryCoordinatorProxy::SetGetCurrentMemoryStateCallback( | |
32 GetCurrentMemoryStateCallback callback) { | |
33 getter_callback_ = callback; | |
34 } | |
35 | |
36 void MemoryCoordinatorProxy::SetSetCurrentMemoryStateForTestingCallback( | |
37 SetCurrentMemoryStateCallback callback) { | |
38 setter_callback_ = callback; | |
39 } | 53 } |
40 | 54 |
41 } // namespace base | 55 } // namespace base |
OLD | NEW |