2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #include "core/event.hpp"
24 #include "util/algorithm.hpp"
25 #include "pipe/p_screen.h"
27 using namespace clover
;
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
) {
38 _cl_event::~_cl_event() {
42 _cl_event::trigger() {
46 while (!_chain
.empty()) {
47 _chain
.back()->trigger();
54 _cl_event::abort(cl_int status
) {
58 while (!_chain
.empty()) {
59 _chain
.back()->abort(status
);
65 _cl_event::signalled() const {
70 _cl_event::chain(clover::event
*ev
) {
75 ev
->deps
.push_back(this);
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
);
89 hard_event::~hard_event() {
90 pipe_screen
*screen
= queue()->dev
.pipe
;
91 screen
->fence_reference(screen
, &_fence
, NULL
);
95 hard_event::status() const {
96 pipe_screen
*screen
= queue()->dev
.pipe
;
104 else if (!screen
->fence_signalled(screen
, _fence
))
112 hard_event::queue() const {
117 hard_event::command() const {
122 hard_event::wait() const {
123 pipe_screen
*screen
= queue()->dev
.pipe
;
125 if (status() == CL_QUEUED
)
129 !screen
->fence_finish(screen
, _fence
, PIPE_TIMEOUT_INFINITE
))
130 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST
);
133 const lazy
<cl_ulong
> &
134 hard_event::time_queued() const {
138 const lazy
<cl_ulong
> &
139 hard_event::time_submit() const {
143 const lazy
<cl_ulong
> &
144 hard_event::time_start() const {
148 const lazy
<cl_ulong
> &
149 hard_event::time_end() const {
154 hard_event::fence(pipe_fence_handle
*fence
) {
155 pipe_screen
*screen
= queue()->dev
.pipe
;
156 screen
->fence_reference(screen
, &_fence
, fence
);
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
);
165 hev
._time_submit
= timestamp::current(q
);
166 hev
._time_start
= timestamp::query(q
);
170 hev
._time_end
= timestamp::query(q
);
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
) {
187 soft_event::status() const {
191 else if (!signalled() ||
192 any_of([](const ref_ptr
<event
> &ev
) {
193 return ev
->status() != CL_COMPLETE
;
202 soft_event::queue() const {
207 soft_event::command() const {
208 return CL_COMMAND_USER
;
212 soft_event::wait() const {
216 if (status() != CL_COMPLETE
)
217 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST
);