clover: Replace a bunch of double underscores with single underscores.
[mesa.git] / src / gallium / state_trackers / clover / core / event.cpp
1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #include "core/event.hpp"
24 #include "pipe/p_screen.h"
25
26 using namespace clover;
27
28 _cl_event::_cl_event(clover::context &ctx,
29 std::vector<clover::event *> deps,
30 action action_ok, action action_fail) :
31 ctx(ctx), _status(0), wait_count(1),
32 action_ok(action_ok), action_fail(action_fail) {
33 for (auto ev : deps)
34 ev->chain(this);
35 }
36
37 _cl_event::~_cl_event() {
38 }
39
40 void
41 _cl_event::trigger() {
42 if (!--wait_count) {
43 action_ok(*this);
44
45 while (!_chain.empty()) {
46 _chain.back()->trigger();
47 _chain.pop_back();
48 }
49 }
50 }
51
52 void
53 _cl_event::abort(cl_int status) {
54 _status = status;
55 action_fail(*this);
56
57 while (!_chain.empty()) {
58 _chain.back()->abort(status);
59 _chain.pop_back();
60 }
61 }
62
63 bool
64 _cl_event::signalled() const {
65 return !wait_count;
66 }
67
68 void
69 _cl_event::chain(clover::event *ev) {
70 if (wait_count) {
71 ev->wait_count++;
72 _chain.push_back(ev);
73 }
74 ev->deps.push_back(this);
75 }
76
77 hard_event::hard_event(clover::command_queue &q, cl_command_type command,
78 std::vector<clover::event *> deps, action action) :
79 _cl_event(q.ctx, deps, profile(q, action), [](event &ev){}),
80 _queue(q), _command(command), _fence(NULL) {
81 if (q.profiling_enabled())
82 _time_queued = timestamp::current(q);
83
84 q.sequence(this);
85 trigger();
86 }
87
88 hard_event::~hard_event() {
89 pipe_screen *screen = queue()->dev.pipe;
90 screen->fence_reference(screen, &_fence, NULL);
91 }
92
93 cl_int
94 hard_event::status() const {
95 pipe_screen *screen = queue()->dev.pipe;
96
97 if (_status < 0)
98 return _status;
99
100 else if (!_fence)
101 return CL_QUEUED;
102
103 else if (!screen->fence_signalled(screen, _fence))
104 return CL_SUBMITTED;
105
106 else
107 return CL_COMPLETE;
108 }
109
110 cl_command_queue
111 hard_event::queue() const {
112 return &_queue;
113 }
114
115 cl_command_type
116 hard_event::command() const {
117 return _command;
118 }
119
120 void
121 hard_event::wait() const {
122 pipe_screen *screen = queue()->dev.pipe;
123
124 if (status() == CL_QUEUED)
125 queue()->flush();
126
127 if (!_fence ||
128 !screen->fence_finish(screen, _fence, PIPE_TIMEOUT_INFINITE))
129 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
130 }
131
132 const lazy<cl_ulong> &
133 hard_event::time_queued() const {
134 return _time_queued;
135 }
136
137 const lazy<cl_ulong> &
138 hard_event::time_submit() const {
139 return _time_submit;
140 }
141
142 const lazy<cl_ulong> &
143 hard_event::time_start() const {
144 return _time_start;
145 }
146
147 const lazy<cl_ulong> &
148 hard_event::time_end() const {
149 return _time_end;
150 }
151
152 void
153 hard_event::fence(pipe_fence_handle *fence) {
154 pipe_screen *screen = queue()->dev.pipe;
155 screen->fence_reference(screen, &_fence, fence);
156 }
157
158 event::action
159 hard_event::profile(command_queue &q, const action &action) const {
160 if (q.profiling_enabled()) {
161 return [&q, action] (event &ev) {
162 auto &hev = static_cast<hard_event &>(ev);
163
164 hev._time_submit = timestamp::current(q);
165 hev._time_start = timestamp::query(q);
166
167 action(ev);
168
169 hev._time_end = timestamp::query(q);
170 };
171
172 } else {
173 return action;
174 }
175 }
176
177 soft_event::soft_event(clover::context &ctx,
178 std::vector<clover::event *> deps,
179 bool _trigger, action action) :
180 _cl_event(ctx, deps, action, action) {
181 if (_trigger)
182 trigger();
183 }
184
185 cl_int
186 soft_event::status() const {
187 if (_status < 0)
188 return _status;
189
190 else if (!signalled() ||
191 any_of([](const ref_ptr<event> &ev) {
192 return ev->status() != CL_COMPLETE;
193 }, deps.begin(), deps.end()))
194 return CL_SUBMITTED;
195
196 else
197 return CL_COMPLETE;
198 }
199
200 cl_command_queue
201 soft_event::queue() const {
202 return NULL;
203 }
204
205 cl_command_type
206 soft_event::command() const {
207 return CL_COMMAND_USER;
208 }
209
210 void
211 soft_event::wait() const {
212 for (auto ev : deps)
213 ev->wait();
214
215 if (status() != CL_COMPLETE)
216 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
217 }