1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright 2012 Francisco Jerez
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The above copyright notice and this permission notice shall be included in
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// all copies or substantial portions of the Software.
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SOFTWARE.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef __CORE_EVENT_HPP__
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define __CORE_EVENT_HPP__
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <functional>
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/base.hpp"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/queue.hpp"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace clover {
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   typedef struct _cl_event event;
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org///
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// Class that represents a task that might be executed asynchronously
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// at some point in the future.
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org///
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// An event consists of a list of dependencies, a boolean signalled()
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// flag, and an associated task.  An event is considered signalled as
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// soon as all its dependencies (if any) are signalled as well, and
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// the trigger() method is called; at that point the associated task
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// will be started through the specified \a action_ok.  If the
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// abort() method is called instead, the specified \a action_fail is
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// executed and the associated task will never be started.  Dependent
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// events will be aborted recursively.
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org///
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// The execution status of the associated task can be queried using
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// the status() method, and it can be waited for completion using the
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/// wait() method.
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org///
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct _cl_event : public clover::ref_counter {
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   typedef std::function<void (clover::event &)> action;
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _cl_event(clover::context &ctx, std::vector<clover::event *> deps,
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             action action_ok, action action_fail);
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ~_cl_event();
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void trigger();
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void abort(cl_int status);
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool signalled() const;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual cl_int status() const = 0;
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual cl_command_queue queue() const = 0;
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual cl_command_type command() const = 0;
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual void wait() const = 0;
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   clover::context &ctx;
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprotected:
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void chain(clover::event *ev);
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   cl_int __status;
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   std::vector<clover::ref_ptr<clover::event>> deps;
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprivate:
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned wait_count;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   action action_ok;
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   action action_fail;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   std::vector<clover::ref_ptr<clover::event>> __chain;
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace clover {
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// Class that represents a task executed by a command queue.
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// Similar to a normal clover::event.  In addition it's associated
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// with a given command queue \a q and a given OpenCL \a command.
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// hard_event instances created for the same queue are implicitly
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// ordered with respect to each other, and they are implicitly
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// triggered on construction.
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// A hard_event is considered complete when the associated
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// hardware task finishes execution.
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   class hard_event : public event {
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   public:
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      hard_event(clover::command_queue &q, cl_command_type command,
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                 std::vector<clover::event *> deps,
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                 action action = [](event &){});
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ~hard_event();
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_int status() const;
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_command_queue queue() const;
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_command_type command() const;
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual void wait() const;
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      friend class ::_cl_command_queue;
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   private:
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual void fence(pipe_fence_handle *fence);
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      clover::command_queue &__queue;
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      cl_command_type __command;
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_fence_handle *__fence;
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// Class that represents a software event.
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// A soft_event is not associated with any specific hardware task
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// or command queue.  It's considered complete as soon as all its
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// dependencies finish execution.
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ///
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   class soft_event : public event {
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   public:
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      soft_event(clover::context &ctx, std::vector<clover::event *> deps,
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                 bool trigger, action action = [](event &){});
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_int status() const;
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_command_queue queue() const;
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual cl_command_type command() const;
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      virtual void wait() const;
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
139