summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/clover/api/event.cpp
blob: d6c37f6aef23d49a9cfeb39d8e2d59cb017fbdbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// Copyright 2012 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#include "api/util.hpp"
#include "core/event.hpp"

using namespace clover;

PUBLIC cl_event
clCreateUserEvent(cl_context ctx, cl_int *errcode_ret) try {
   if (!ctx)
      throw error(CL_INVALID_CONTEXT);

   ret_error(errcode_ret, CL_SUCCESS);
   return new soft_event(*ctx, {}, false);

} catch(error &e) {
   ret_error(errcode_ret, e);
   return NULL;
}

PUBLIC cl_int
clSetUserEventStatus(cl_event ev, cl_int status) {
   if (!dynamic_cast<soft_event *>(ev))
      return CL_INVALID_EVENT;

   if (status > 0)
      return CL_INVALID_VALUE;

   if (ev->status() <= 0)
      return CL_INVALID_OPERATION;

   if (status)
      ev->abort(status);
   else
      ev->trigger();

   return CL_SUCCESS;
}

PUBLIC cl_int
clWaitForEvents(cl_uint num_evs, const cl_event *evs) try {
   if (!num_evs || !evs)
      throw error(CL_INVALID_VALUE);

   std::for_each(evs, evs + num_evs, [&](const cl_event ev) {
         if (!ev)
            throw error(CL_INVALID_EVENT);

         if (&ev->ctx != &evs[0]->ctx)
            throw error(CL_INVALID_CONTEXT);

         if (ev->status() < 0)
            throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
      });

   // Create a temporary soft event that depends on all the events in
   // the wait list
   ref_ptr<soft_event> sev = transfer(
      new soft_event(evs[0]->ctx, { evs, evs + num_evs }, true));

   // ...and wait on it.
   sev->wait();

   return CL_SUCCESS;

} catch(error &e) {
   return e.get();
}

PUBLIC cl_int
clGetEventInfo(cl_event ev, cl_event_info param,
               size_t size, void *buf, size_t *size_ret) {
   if (!ev)
      return CL_INVALID_EVENT;

   switch (param) {
   case CL_EVENT_COMMAND_QUEUE:
      return scalar_property<cl_command_queue>(buf, size, size_ret, ev->queue());

   case CL_EVENT_CONTEXT:
      return scalar_property<cl_context>(buf, size, size_ret, &ev->ctx);

   case CL_EVENT_COMMAND_TYPE:
      return scalar_property<cl_command_type>(buf, size, size_ret, ev->command());

   case CL_EVENT_COMMAND_EXECUTION_STATUS:
      return scalar_property<cl_int>(buf, size, size_ret, ev->status());

   case CL_EVENT_REFERENCE_COUNT:
      return scalar_property<cl_uint>(buf, size, size_ret, ev->ref_count());

   default:
      return CL_INVALID_VALUE;
   }
}

PUBLIC cl_int
clSetEventCallback(cl_event ev, cl_int type,
                   void (CL_CALLBACK *pfn_event_notify)(cl_event, cl_int,
                                                        void *),
                   void *user_data) try {
   if (!ev)
      throw error(CL_INVALID_EVENT);

   if (!pfn_event_notify || type != CL_COMPLETE)
      throw error(CL_INVALID_VALUE);

   // Create a temporary soft event that depends on ev, with
   // pfn_event_notify as completion action.
   ref_ptr<soft_event> sev = transfer(
      new soft_event(ev->ctx, { ev }, true,
                     [=](event &) {
                        ev->wait();
                        pfn_event_notify(ev, ev->status(), user_data);
                     }));

   return CL_SUCCESS;

} catch(error &e) {
   return e.get();
}

PUBLIC cl_int
clRetainEvent(cl_event ev) {
   if (!ev)
      return CL_INVALID_EVENT;

   ev->retain();
   return CL_SUCCESS;
}

PUBLIC cl_int
clReleaseEvent(cl_event ev) {
   if (!ev)
      return CL_INVALID_EVENT;

   if (ev->release())
      delete ev;

   return CL_SUCCESS;
}

PUBLIC cl_int
clEnqueueMarker(cl_command_queue q, cl_event *ev) try {
   if (!q)
      throw error(CL_INVALID_COMMAND_QUEUE);

   if (!ev)
      throw error(CL_INVALID_VALUE);

   *ev = new hard_event(*q, CL_COMMAND_MARKER, {});

   return CL_SUCCESS;

} catch(error &e) {
   return e.get();
}

PUBLIC cl_int
clEnqueueBarrier(cl_command_queue q) {
   if (!q)
      return CL_INVALID_COMMAND_QUEUE;

   // No need to do anything, q preserves data ordering strictly.
   return CL_SUCCESS;
}

PUBLIC cl_int
clEnqueueWaitForEvents(cl_command_queue q, cl_uint num_evs,
                       const cl_event *evs) try {
   if (!q)
      throw error(CL_INVALID_COMMAND_QUEUE);

   if (!num_evs || !evs)
      throw error(CL_INVALID_VALUE);

   std::for_each(evs, evs + num_evs, [&](const cl_event ev) {
         if (!ev)
            throw error(CL_INVALID_EVENT);

         if (&ev->ctx != &q->ctx)
            throw error(CL_INVALID_CONTEXT);
      });

   // Create a hard event that depends on the events in the wait list:
   // subsequent commands in the same queue will be implicitly
   // serialized with respect to it -- hard events always are.
   ref_ptr<hard_event> hev = transfer(
      new hard_event(*q, 0, { evs, evs + num_evs }));

   return CL_SUCCESS;

} catch(error &e) {
   return e.get();
}

PUBLIC cl_int
clGetEventProfilingInfo(cl_event ev, cl_profiling_info param,
                        size_t size, void *buf, size_t *size_ret) {
   return CL_PROFILING_INFO_NOT_AVAILABLE;
}

PUBLIC cl_int
clFinish(cl_command_queue q) try {
   if (!q)
      throw error(CL_INVALID_COMMAND_QUEUE);

   // Create a temporary hard event -- it implicitly depends on all
   // the previously queued hard events.
   ref_ptr<hard_event> hev = transfer(new hard_event(*q, 0, { }));

   // And wait on it.
   hev->wait();

   return CL_SUCCESS;

} catch(error &e) {
   return e.get();
}