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 "pipe/p_screen.h"
26 using namespace clover
;
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
) {
37 _cl_event::~_cl_event() {
41 _cl_event::trigger() {
45 while (!__chain
.empty()) {
46 __chain
.back()->trigger();
53 _cl_event::abort(cl_int status
) {
57 while (!__chain
.empty()) {
58 __chain
.back()->abort(status
);
64 _cl_event::signalled() const {
69 _cl_event::chain(clover::event
*ev
) {
72 __chain
.push_back(ev
);
73 ev
->deps
.push_back(this);
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
, action
, [](event
&ev
){}),
80 __queue(q
), __command(command
), __fence(NULL
) {
85 hard_event::~hard_event() {
86 pipe_screen
*screen
= queue()->dev
.pipe
;
87 screen
->fence_reference(screen
, &__fence
, NULL
);
91 hard_event::status() const {
92 pipe_screen
*screen
= queue()->dev
.pipe
;
100 else if (!screen
->fence_signalled(screen
, __fence
))
108 hard_event::queue() const {
113 hard_event::command() const {
118 hard_event::wait() const {
119 pipe_screen
*screen
= queue()->dev
.pipe
;
121 if (status() == CL_QUEUED
)
125 !screen
->fence_finish(screen
, __fence
, PIPE_TIMEOUT_INFINITE
))
126 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST
);
130 hard_event::fence(pipe_fence_handle
*fence
) {
131 pipe_screen
*screen
= queue()->dev
.pipe
;
132 screen
->fence_reference(screen
, &__fence
, fence
);
135 soft_event::soft_event(clover::context
&ctx
,
136 std::vector
<clover::event
*> deps
,
137 bool __trigger
, action action
) :
138 _cl_event(ctx
, deps
, action
, action
) {
144 soft_event::status() const {
148 else if (!signalled() ||
149 any_of([](const ref_ptr
<event
> &ev
) {
150 return ev
->status() != CL_COMPLETE
;
151 }, deps
.begin(), deps
.end()))
159 soft_event::queue() const {
164 soft_event::command() const {
165 return CL_COMMAND_USER
;
169 soft_event::wait() const {
173 if (status() != CL_COMPLETE
)
174 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST
);