Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "gpu/ipc/common/activity_flags.h" | |
| 6 | |
| 7 #include "base/memory/singleton.h" | |
| 8 | |
| 9 namespace gpu { | |
| 10 | |
| 11 ActivityFlagsBase::ActivityFlagsBase() = default; | |
| 12 | |
| 13 ActivityFlagsBase::~ActivityFlagsBase() = default; | |
| 14 | |
| 15 void ActivityFlagsBase::Initialize(mojo::ScopedSharedBufferHandle handle) { | |
| 16 handle_ = std::move(handle); | |
| 17 mapping_ = handle_->Map(sizeof(Flag)); | |
| 18 } | |
| 19 | |
| 20 volatile base::subtle::Atomic32* ActivityFlagsBase::AsAtomic() { | |
| 21 return reinterpret_cast<volatile base::subtle::Atomic32*>(mapping_.get()); | |
| 22 } | |
| 23 | |
| 24 GpuProcessActivityFlags* GpuProcessActivityFlags::GetInstance() { | |
| 25 return base::Singleton<GpuProcessActivityFlags>::get(); | |
| 26 } | |
| 27 | |
| 28 void GpuProcessActivityFlags::InitializeInstance( | |
| 29 mojo::ScopedSharedBufferHandle handle) { | |
| 30 // In cases where we are running without a GpuProcessHost, we may not | |
| 31 // have a valid handle. In this case, just return. | |
| 32 if (!handle.is_valid()) | |
| 33 return; | |
| 34 | |
| 35 GetInstance()->Initialize(std::move(handle)); | |
| 36 } | |
| 37 | |
| 38 void GpuProcessActivityFlags::SetFlag(Flag flag) { | |
| 39 // In cases where we are running without a GpuProcessHost, we may not | |
| 40 // initialize the GpuProcessActivityFlags. In this case, just return. | |
| 41 if (!is_initialized()) | |
| 42 return; | |
| 43 | |
| 44 base::subtle::Atomic32 old_value = base::subtle::NoBarrier_Load(AsAtomic()); | |
| 45 base::subtle::Atomic32 new_value = old_value | flag; | |
| 46 | |
| 47 // These flags are *only* written by a single process, and should only be | |
| 48 // written from the main GPU thread. We should never double-set them. | |
| 49 DCHECK_NE(old_value, new_value); | |
| 50 base::subtle::Acquire_Store(AsAtomic(), new_value); | |
| 51 } | |
| 52 | |
| 53 void GpuProcessActivityFlags::UnsetFlag(Flag flag) { | |
| 54 // In cases where we are running without a GpuProcessHost, we may not | |
| 55 // initialize the GpuProcessActivityFlags. In this case, just return. | |
| 56 if (!is_initialized()) | |
| 57 return; | |
| 58 | |
| 59 // These flags are *only* written by a single process, and should only be | |
| 60 // written from the main GPU thread. No need to check for failure. | |
| 61 base::subtle::Atomic32 old_value = base::subtle::NoBarrier_Load(AsAtomic()); | |
| 62 base::subtle::Atomic32 new_value = old_value ^ flag; | |
| 63 | |
| 64 // These flags are *only* written by a single process, and should only be | |
| 65 // written from the main GPU thread. We should never double-unset them. | |
| 66 DCHECK_NE(old_value, new_value); | |
|
vmiura
2017/03/13 20:18:36
This doesn't really check anything since x ^ flag
ericrk
2017/03/13 20:30:45
Yup, good point.
| |
| 67 base::subtle::Release_Store(AsAtomic(), new_value); | |
| 68 } | |
| 69 | |
| 70 GpuProcessHostActivityFlags::GpuProcessHostActivityFlags() { | |
| 71 Initialize(mojo::SharedBufferHandle::Create(sizeof(Flag))); | |
| 72 } | |
| 73 | |
| 74 bool GpuProcessHostActivityFlags::IsFlagSet(Flag flag) { | |
| 75 DCHECK(is_initialized()); | |
| 76 return !!(base::subtle::Acquire_Load(AsAtomic()) & flag); | |
| 77 } | |
| 78 | |
| 79 } // namespace gpu | |
| OLD | NEW |