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