Chromium Code Reviews| Index: ui/events/devices/input_device_manager.cc |
| diff --git a/ui/events/devices/input_device_manager.cc b/ui/events/devices/input_device_manager.cc |
| index b6ca664e25a4f42bf7f4dcd015cf7583e73560b8..ee51aa830c88893467310f270a9acac38926e537 100644 |
| --- a/ui/events/devices/input_device_manager.cc |
| +++ b/ui/events/devices/input_device_manager.cc |
| @@ -3,31 +3,41 @@ |
| // found in the LICENSE file. |
| #include "ui/events/devices/input_device_manager.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/threading/thread_local.h" |
| namespace ui { |
| +namespace { |
| -InputDeviceManager* InputDeviceManager::instance_ = nullptr; |
| +// InputDeviceManager singleton is thread-local so that different instances can |
| +// be used on different threads (eg. WindowServer thread vs. browser UI thread). |
| +base::LazyInstance<base::ThreadLocalPointer<InputDeviceManager>>::Leaky |
|
sky
2017/06/27 19:58:57
Is this actually necessary? What code in chrome/as
mfomitchev
2017/06/27 22:14:55
I believe so. This is the one on the chrome side,
sky
2017/06/28 00:22:02
Ok, I get it.
|
| + lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| // static |
| InputDeviceManager* InputDeviceManager::GetInstance() { |
| - DCHECK(instance_); |
| - return instance_; |
| + InputDeviceManager* instance = lazy_tls_ptr.Pointer()->Get(); |
| + DCHECK(instance) << "InputDeviceManager::SetInstance must be called before " |
| + "getting the instance of InputDeviceManager."; |
| + return instance; |
| } |
| // static |
| bool InputDeviceManager::HasInstance() { |
| - return instance_ != nullptr; |
| + return lazy_tls_ptr.Pointer()->Get() != nullptr; |
| } |
| // static |
| void InputDeviceManager::SetInstance(InputDeviceManager* instance) { |
| - DCHECK(!instance_); |
| - instance_ = instance; |
| + DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| + lazy_tls_ptr.Pointer()->Set(instance); |
| } |
| // static |
| void InputDeviceManager::ClearInstance() { |
| - instance_ = nullptr; |
| + lazy_tls_ptr.Pointer()->Set(nullptr); |
| } |
| } // namespace ui |