Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: ui/gfx/shared_event.cc

Issue 2383753002: gpu: Add GpuFence framework.
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/atomicops.h"
6 #include "base/macros.h"
7 #include "base/process/memory.h"
8 #include "base/time/time.h"
9 #include "ui/gfx/gfx_export.h"
10 #include "ui/gfx/shared_event.h"
11
12 #if defined(OS_LINUX) || defined(OS_ANDROID)
13 #include <linux/futex.h>
14 #include <stdint.h>
15 #include <sys/syscall.h>
16 #else
17 #include "base/threading/platform_thread.h"
18 #endif
19
20 namespace gfx {
21 namespace {
22
23 enum { NOT_SIGNALED = 0, WAITING, SIGNALED };
24
25 constexpr size_t kSharedMemorySize = sizeof(int32_t);
ericrk 2017/03/10 03:04:39 I'm curious about the size efficiency here. From m
reveman 2017/03/13 12:33:05 Correct. Each fence will end up using one page of
26
27 } // namespace
28
29 SharedEvent::SharedEvent() {
30 bool rv = shared_memory_.CreateAndMapAnonymous(kSharedMemorySize);
31 CHECK(rv);
32 }
33
34 SharedEvent::SharedEvent(const SharedEventHandle& handle)
35 : shared_memory_(handle, false) {
36 bool rv = shared_memory_.Map(kSharedMemorySize);
37 CHECK(rv);
38 }
39
40 // static
41 SharedEventHandle SharedEvent::CreateForProcess(base::ProcessHandle process) {
42 base::SharedMemory shared_memory;
43 if (!shared_memory.CreateAnonymous(kSharedMemorySize))
44 return SharedEventHandle();
45 base::SharedMemoryHandle handle;
46 shared_memory.GiveToProcess(process, &handle);
47 return handle;
48 }
49
50 // static
51 SharedEventHandle SharedEvent::DuplicateHandle(
52 const SharedEventHandle& handle) {
53 return base::SharedMemory::DuplicateHandle(handle);
54 }
55
56 void SharedEvent::Reset() {
57 // Change state to un-signaled iff event is currently in signaled state.
58 base::subtle::NoBarrier_CompareAndSwap(uaddr(), SIGNALED, NOT_SIGNALED);
59 }
60
61 void SharedEvent::Signal() {
62 // Change state to signaled and return early unless waiters exist that need
63 // to be woken up.
64 if (base::subtle::Release_CompareAndSwap(uaddr(), NOT_SIGNALED, SIGNALED) !=
65 WAITING)
66 return;
67
68 // Unconditionally change state to signaled before waking up waiters.
69 base::subtle::Release_Store(uaddr(), SIGNALED);
70
71 #if defined(OS_LINUX) || defined(OS_ANDROID)
72 // Perform non-blocking futex operation that will wake up all waiters.
73 int rv = syscall(SYS_futex, uaddr(), FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
74 PLOG_IF(ERROR, rv < 0) << "futex";
75 #endif
76 }
77
78 bool SharedEvent::IsSignaled() {
79 return base::subtle::Acquire_Load(uaddr()) == SIGNALED;
80 }
81
82 bool SharedEvent::Wait(const base::TimeDelta& max_time) {
83 #if defined(OS_LINUX) || defined(OS_ANDROID)
84 struct timespec timeout = max_time.ToTimeSpec();
85 #endif
86
87 // Wait for signaled state by repeatadly attempting to enter waiting state
88 // until it fails because current state is signaled.
89 while (base::subtle::Acquire_CompareAndSwap(uaddr(), NOT_SIGNALED, WAITING) !=
90 SIGNALED) {
91 #if defined(OS_LINUX) || defined(OS_ANDROID)
92 // Perform futex operation that will block until woken up or |timeout| has
93 // expired. Note: If the futex value does not match waiting state, then the
94 // call fails immediately with the error EAGAIN.
95 if (syscall(SYS_futex, uaddr(), FUTEX_WAIT, WAITING, &timeout, NULL, 0)) {
96 PLOG_IF(ERROR, errno != EAGAIN && errno != ETIMEDOUT) << "futex";
97 return false;
98 }
99 // Protect against spurious wake-ups by checking state again. Reset
100 // timeout to prevent this function from blocking significantly longer
101 // than |max_time|.
102 timeout = {0};
103 #else
104 base::PlatformThread::YieldCurrentThread();
105 return false;
106 #endif
107 }
108
109 return true;
110 }
111
112 SharedEventHandle SharedEvent::GetHandle() const {
113 return shared_memory_.handle();
114 }
115
116 void SharedEvent::Close() {
117 shared_memory_.Close();
118 }
119
120 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698