gallium: add PIPE_CAP_RESOURCE_FROM_USER_MEMORY_COMPUTE_ONLY
[mesa.git] / src / gallium / frontends / 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 event::event(clover::context &ctx, const ref_vector<event> &deps,
29 action action_ok, action action_fail) :
30 context(ctx), _wait_count(1), _status(0),
31 action_ok(action_ok), action_fail(action_fail) {
32 for (auto &ev : deps)
33 ev.chain(*this);
34 }
35
36 event::~event() {
37 }
38
39 std::vector<intrusive_ref<event>>
40 event::trigger_self() {
41 std::lock_guard<std::mutex> lock(mutex);
42 std::vector<intrusive_ref<event>> evs;
43
44 if (_wait_count && !--_wait_count)
45 std::swap(_chain, evs);
46
47 cv.notify_all();
48 return evs;
49 }
50
51 void
52 event::trigger() try {
53 if (wait_count() == 1)
54 action_ok(*this);
55
56 for (event &ev : trigger_self())
57 ev.trigger();
58 } catch (error &e) {
59 abort(e.get());
60 }
61
62 std::vector<intrusive_ref<event>>
63 event::abort_self(cl_int status) {
64 std::lock_guard<std::mutex> lock(mutex);
65 std::vector<intrusive_ref<event>> evs;
66
67 _status = status;
68 _wait_count = 0;
69 std::swap(_chain, evs);
70
71 cv.notify_all();
72 return evs;
73 }
74
75 void
76 event::abort(cl_int status) {
77 action_fail(*this);
78
79 for (event &ev : abort_self(status))
80 ev.abort(status);
81 }
82
83 unsigned
84 event::wait_count() const {
85 std::lock_guard<std::mutex> lock(mutex);
86 return _wait_count;
87 }
88
89 bool
90 event::signalled() const {
91 return !wait_count();
92 }
93
94 cl_int
95 event::status() const {
96 std::lock_guard<std::mutex> lock(mutex);
97 return _status;
98 }
99
100 void
101 event::chain(event &ev) {
102 std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
103 std::unique_lock<std::mutex> lock_ev(ev.mutex, std::defer_lock);
104 std::lock(lock, lock_ev);
105
106 if (_wait_count) {
107 ev._wait_count++;
108 _chain.push_back(ev);
109 }
110 ev.deps.push_back(*this);
111 }
112
113 void
114 event::wait_signalled() const {
115 std::unique_lock<std::mutex> lock(mutex);
116 cv.wait(lock, [=]{ return !_wait_count; });
117 }
118
119 void
120 event::wait() const {
121 for (event &ev : deps)
122 ev.wait();
123
124 wait_signalled();
125 }
126
127 hard_event::hard_event(command_queue &q, cl_command_type command,
128 const ref_vector<event> &deps, action action) :
129 event(q.context(), deps, profile(q, action), [](event &ev){}),
130 _queue(q), _command(command), _fence(NULL) {
131 if (q.profiling_enabled())
132 _time_queued = timestamp::current(q);
133
134 q.sequence(*this);
135 trigger();
136 }
137
138 hard_event::~hard_event() {
139 pipe_screen *screen = queue()->device().pipe;
140 screen->fence_reference(screen, &_fence, NULL);
141 }
142
143 cl_int
144 hard_event::status() const {
145 pipe_screen *screen = queue()->device().pipe;
146
147 if (event::status() < 0)
148 return event::status();
149
150 else if (!_fence)
151 return CL_QUEUED;
152
153 else if (!screen->fence_finish(screen, NULL, _fence, 0))
154 return CL_SUBMITTED;
155
156 else
157 return CL_COMPLETE;
158 }
159
160 command_queue *
161 hard_event::queue() const {
162 return &_queue();
163 }
164
165 cl_command_type
166 hard_event::command() const {
167 return _command;
168 }
169
170 void
171 hard_event::wait() const {
172 pipe_screen *screen = queue()->device().pipe;
173
174 event::wait();
175
176 if (status() == CL_QUEUED)
177 queue()->flush();
178
179 if (!_fence ||
180 !screen->fence_finish(screen, NULL, _fence, PIPE_TIMEOUT_INFINITE))
181 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
182 }
183
184 const lazy<cl_ulong> &
185 hard_event::time_queued() const {
186 return _time_queued;
187 }
188
189 const lazy<cl_ulong> &
190 hard_event::time_submit() const {
191 return _time_submit;
192 }
193
194 const lazy<cl_ulong> &
195 hard_event::time_start() const {
196 return _time_start;
197 }
198
199 const lazy<cl_ulong> &
200 hard_event::time_end() const {
201 return _time_end;
202 }
203
204 void
205 hard_event::fence(pipe_fence_handle *fence) {
206 pipe_screen *screen = queue()->device().pipe;
207 screen->fence_reference(screen, &_fence, fence);
208 }
209
210 event::action
211 hard_event::profile(command_queue &q, const action &action) const {
212 if (q.profiling_enabled()) {
213 return [&q, action] (event &ev) {
214 auto &hev = static_cast<hard_event &>(ev);
215
216 hev._time_submit = timestamp::current(q);
217 hev._time_start = timestamp::query(q);
218
219 action(ev);
220
221 hev._time_end = timestamp::query(q);
222 };
223
224 } else {
225 return action;
226 }
227 }
228
229 soft_event::soft_event(clover::context &ctx, const ref_vector<event> &deps,
230 bool _trigger, action action) :
231 event(ctx, deps, action, action) {
232 if (_trigger)
233 trigger();
234 }
235
236 cl_int
237 soft_event::status() const {
238 if (event::status() < 0)
239 return event::status();
240
241 else if (!signalled() ||
242 any_of([](const event &ev) {
243 return ev.status() != CL_COMPLETE;
244 }, deps))
245 return CL_SUBMITTED;
246
247 else
248 return CL_COMPLETE;
249 }
250
251 command_queue *
252 soft_event::queue() const {
253 return NULL;
254 }
255
256 cl_command_type
257 soft_event::command() const {
258 return CL_COMMAND_USER;
259 }
260
261 void
262 soft_event::wait() const {
263 event::wait();
264
265 if (status() != CL_COMPLETE)
266 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
267 }