clover: Switch platform objects to the new model.
[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 "util/algorithm.hpp"
25 #include "pipe/p_screen.h"
26
27 using namespace clover;
28
29 _cl_event::_cl_event(clover::context &ctx,
30 std::vector<clover::event *> deps,
31 action action_ok, action action_fail) :
32 ctx(ctx), _status(0), wait_count(1),
33 action_ok(action_ok), action_fail(action_fail) {
34 for (auto ev : deps)
35 ev->chain(this);
36 }
37
38 _cl_event::~_cl_event() {
39 }
40
41 void
42 _cl_event::trigger() {
43 if (!--wait_count) {
44 action_ok(*this);
45
46 while (!_chain.empty()) {
47 _chain.back()->trigger();
48 _chain.pop_back();
49 }
50 }
51 }
52
53 void
54 _cl_event::abort(cl_int status) {
55 _status = status;
56 action_fail(*this);
57
58 while (!_chain.empty()) {
59 _chain.back()->abort(status);
60 _chain.pop_back();
61 }
62 }
63
64 bool
65 _cl_event::signalled() const {
66 return !wait_count;
67 }
68
69 void
70 _cl_event::chain(clover::event *ev) {
71 if (wait_count) {
72 ev->wait_count++;
73 _chain.push_back(ev);
74 }
75 ev->deps.push_back(this);
76 }
77
78 hard_event::hard_event(clover::command_queue &q, cl_command_type command,
79 std::vector<clover::event *> deps, action action) :
80 _cl_event(q.ctx, deps, profile(q, action), [](event &ev){}),
81 _queue(q), _command(command), _fence(NULL) {
82 if (q.profiling_enabled())
83 _time_queued = timestamp::current(q);
84
85 q.sequence(this);
86 trigger();
87 }
88
89 hard_event::~hard_event() {
90 pipe_screen *screen = queue()->dev.pipe;
91 screen->fence_reference(screen, &_fence, NULL);
92 }
93
94 cl_int
95 hard_event::status() const {
96 pipe_screen *screen = queue()->dev.pipe;
97
98 if (_status < 0)
99 return _status;
100
101 else if (!_fence)
102 return CL_QUEUED;
103
104 else if (!screen->fence_signalled(screen, _fence))
105 return CL_SUBMITTED;
106
107 else
108 return CL_COMPLETE;
109 }
110
111 cl_command_queue
112 hard_event::queue() const {
113 return &_queue;
114 }
115
116 cl_command_type
117 hard_event::command() const {
118 return _command;
119 }
120
121 void
122 hard_event::wait() const {
123 pipe_screen *screen = queue()->dev.pipe;
124
125 if (status() == CL_QUEUED)
126 queue()->flush();
127
128 if (!_fence ||
129 !screen->fence_finish(screen, _fence, PIPE_TIMEOUT_INFINITE))
130 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
131 }
132
133 const lazy<cl_ulong> &
134 hard_event::time_queued() const {
135 return _time_queued;
136 }
137
138 const lazy<cl_ulong> &
139 hard_event::time_submit() const {
140 return _time_submit;
141 }
142
143 const lazy<cl_ulong> &
144 hard_event::time_start() const {
145 return _time_start;
146 }
147
148 const lazy<cl_ulong> &
149 hard_event::time_end() const {
150 return _time_end;
151 }
152
153 void
154 hard_event::fence(pipe_fence_handle *fence) {
155 pipe_screen *screen = queue()->dev.pipe;
156 screen->fence_reference(screen, &_fence, fence);
157 }
158
159 event::action
160 hard_event::profile(command_queue &q, const action &action) const {
161 if (q.profiling_enabled()) {
162 return [&q, action] (event &ev) {
163 auto &hev = static_cast<hard_event &>(ev);
164
165 hev._time_submit = timestamp::current(q);
166 hev._time_start = timestamp::query(q);
167
168 action(ev);
169
170 hev._time_end = timestamp::query(q);
171 };
172
173 } else {
174 return action;
175 }
176 }
177
178 soft_event::soft_event(clover::context &ctx,
179 std::vector<clover::event *> deps,
180 bool _trigger, action action) :
181 _cl_event(ctx, deps, action, action) {
182 if (_trigger)
183 trigger();
184 }
185
186 cl_int
187 soft_event::status() const {
188 if (_status < 0)
189 return _status;
190
191 else if (!signalled() ||
192 any_of([](const ref_ptr<event> &ev) {
193 return ev->status() != CL_COMPLETE;
194 }, deps))
195 return CL_SUBMITTED;
196
197 else
198 return CL_COMPLETE;
199 }
200
201 cl_command_queue
202 soft_event::queue() const {
203 return NULL;
204 }
205
206 cl_command_type
207 soft_event::command() const {
208 return CL_COMMAND_USER;
209 }
210
211 void
212 soft_event::wait() const {
213 for (auto ev : deps)
214 ev->wait();
215
216 if (status() != CL_COMPLETE)
217 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
218 }