55793031ad188ea982cd92fc92ab0110c863cbee
[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 event::event(clover::context &ctx, const ref_vector<event> &deps,
29 action action_ok, action action_fail) :
30 context(ctx), _status(0), wait_count(1),
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 void
40 event::trigger() {
41 if (!--wait_count) {
42 cv.notify_all();
43 action_ok(*this);
44
45 while (!_chain.empty()) {
46 _chain.back()().trigger();
47 _chain.pop_back();
48 }
49 }
50 }
51
52 void
53 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 event::signalled() const {
65 return !wait_count;
66 }
67
68 void
69 event::chain(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 void
78 event::wait() const {
79 for (event &ev : deps)
80 ev.wait();
81
82 std::unique_lock<std::mutex> lock(mutex);
83 cv.wait(lock, [=]{ return !wait_count; });
84 }
85
86 hard_event::hard_event(command_queue &q, cl_command_type command,
87 const ref_vector<event> &deps, action action) :
88 event(q.context(), deps, profile(q, action), [](event &ev){}),
89 _queue(q), _command(command), _fence(NULL) {
90 if (q.profiling_enabled())
91 _time_queued = timestamp::current(q);
92
93 q.sequence(*this);
94 trigger();
95 }
96
97 hard_event::~hard_event() {
98 pipe_screen *screen = queue()->device().pipe;
99 screen->fence_reference(screen, &_fence, NULL);
100 }
101
102 cl_int
103 hard_event::status() const {
104 pipe_screen *screen = queue()->device().pipe;
105
106 if (_status < 0)
107 return _status;
108
109 else if (!_fence)
110 return CL_QUEUED;
111
112 else if (!screen->fence_signalled(screen, _fence))
113 return CL_SUBMITTED;
114
115 else
116 return CL_COMPLETE;
117 }
118
119 command_queue *
120 hard_event::queue() const {
121 return &_queue();
122 }
123
124 cl_command_type
125 hard_event::command() const {
126 return _command;
127 }
128
129 void
130 hard_event::wait() const {
131 pipe_screen *screen = queue()->device().pipe;
132
133 event::wait();
134
135 if (status() == CL_QUEUED)
136 queue()->flush();
137
138 if (!_fence ||
139 !screen->fence_finish(screen, _fence, PIPE_TIMEOUT_INFINITE))
140 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
141 }
142
143 const lazy<cl_ulong> &
144 hard_event::time_queued() const {
145 return _time_queued;
146 }
147
148 const lazy<cl_ulong> &
149 hard_event::time_submit() const {
150 return _time_submit;
151 }
152
153 const lazy<cl_ulong> &
154 hard_event::time_start() const {
155 return _time_start;
156 }
157
158 const lazy<cl_ulong> &
159 hard_event::time_end() const {
160 return _time_end;
161 }
162
163 void
164 hard_event::fence(pipe_fence_handle *fence) {
165 pipe_screen *screen = queue()->device().pipe;
166 screen->fence_reference(screen, &_fence, fence);
167 }
168
169 event::action
170 hard_event::profile(command_queue &q, const action &action) const {
171 if (q.profiling_enabled()) {
172 return [&q, action] (event &ev) {
173 auto &hev = static_cast<hard_event &>(ev);
174
175 hev._time_submit = timestamp::current(q);
176 hev._time_start = timestamp::query(q);
177
178 action(ev);
179
180 hev._time_end = timestamp::query(q);
181 };
182
183 } else {
184 return action;
185 }
186 }
187
188 soft_event::soft_event(clover::context &ctx, const ref_vector<event> &deps,
189 bool _trigger, action action) :
190 event(ctx, deps, action, action) {
191 if (_trigger)
192 trigger();
193 }
194
195 cl_int
196 soft_event::status() const {
197 if (_status < 0)
198 return _status;
199
200 else if (!signalled() ||
201 any_of([](const event &ev) {
202 return ev.status() != CL_COMPLETE;
203 }, deps))
204 return CL_SUBMITTED;
205
206 else
207 return CL_COMPLETE;
208 }
209
210 command_queue *
211 soft_event::queue() const {
212 return NULL;
213 }
214
215 cl_command_type
216 soft_event::command() const {
217 return CL_COMMAND_USER;
218 }
219
220 void
221 soft_event::wait() const {
222 event::wait();
223
224 if (status() != CL_COMPLETE)
225 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
226 }