clover: Replace a bunch of double underscores with single underscores.
[mesa.git] / src / gallium / state_trackers / clover / core / event.hpp
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 #ifndef _CORE_EVENT_HPP_
24 #define _CORE_EVENT_HPP_
25
26 #include <functional>
27
28 #include "core/base.hpp"
29 #include "core/queue.hpp"
30 #include "core/timestamp.hpp"
31 #include "util/lazy.hpp"
32
33 namespace clover {
34 typedef struct _cl_event event;
35 }
36
37 ///
38 /// Class that represents a task that might be executed asynchronously
39 /// at some point in the future.
40 ///
41 /// An event consists of a list of dependencies, a boolean signalled()
42 /// flag, and an associated task. An event is considered signalled as
43 /// soon as all its dependencies (if any) are signalled as well, and
44 /// the trigger() method is called; at that point the associated task
45 /// will be started through the specified \a action_ok. If the
46 /// abort() method is called instead, the specified \a action_fail is
47 /// executed and the associated task will never be started. Dependent
48 /// events will be aborted recursively.
49 ///
50 /// The execution status of the associated task can be queried using
51 /// the status() method, and it can be waited for completion using the
52 /// wait() method.
53 ///
54 struct _cl_event : public clover::ref_counter {
55 public:
56 typedef std::function<void (clover::event &)> action;
57
58 _cl_event(clover::context &ctx, std::vector<clover::event *> deps,
59 action action_ok, action action_fail);
60 virtual ~_cl_event();
61
62 void trigger();
63 void abort(cl_int status);
64 bool signalled() const;
65
66 virtual cl_int status() const = 0;
67 virtual cl_command_queue queue() const = 0;
68 virtual cl_command_type command() const = 0;
69 virtual void wait() const = 0;
70
71 clover::context &ctx;
72
73 protected:
74 void chain(clover::event *ev);
75
76 cl_int _status;
77 std::vector<clover::ref_ptr<clover::event>> deps;
78
79 private:
80 unsigned wait_count;
81 action action_ok;
82 action action_fail;
83 std::vector<clover::ref_ptr<clover::event>> _chain;
84 };
85
86 namespace clover {
87 ///
88 /// Class that represents a task executed by a command queue.
89 ///
90 /// Similar to a normal clover::event. In addition it's associated
91 /// with a given command queue \a q and a given OpenCL \a command.
92 /// hard_event instances created for the same queue are implicitly
93 /// ordered with respect to each other, and they are implicitly
94 /// triggered on construction.
95 ///
96 /// A hard_event is considered complete when the associated
97 /// hardware task finishes execution.
98 ///
99 class hard_event : public event {
100 public:
101 hard_event(clover::command_queue &q, cl_command_type command,
102 std::vector<clover::event *> deps,
103 action action = [](event &){});
104 ~hard_event();
105
106 virtual cl_int status() const;
107 virtual cl_command_queue queue() const;
108 virtual cl_command_type command() const;
109 virtual void wait() const;
110
111 const lazy<cl_ulong> &time_queued() const;
112 const lazy<cl_ulong> &time_submit() const;
113 const lazy<cl_ulong> &time_start() const;
114 const lazy<cl_ulong> &time_end() const;
115
116 friend class ::_cl_command_queue;
117
118 private:
119 virtual void fence(pipe_fence_handle *fence);
120 action profile(command_queue &q, const action &action) const;
121
122 clover::command_queue &_queue;
123 cl_command_type _command;
124 pipe_fence_handle *_fence;
125 lazy<cl_ulong> _time_queued, _time_submit, _time_start, _time_end;
126 };
127
128 ///
129 /// Class that represents a software event.
130 ///
131 /// A soft_event is not associated with any specific hardware task
132 /// or command queue. It's considered complete as soon as all its
133 /// dependencies finish execution.
134 ///
135 class soft_event : public event {
136 public:
137 soft_event(clover::context &ctx, std::vector<clover::event *> deps,
138 bool trigger, action action = [](event &){});
139
140 virtual cl_int status() const;
141 virtual cl_command_queue queue() const;
142 virtual cl_command_type command() const;
143 virtual void wait() const;
144 };
145 }
146
147 #endif