|
1 | 1 | #include <uv.h> |
2 | 2 |
|
3 | | -#include "types.hpp" |
| 3 | +#include "wrapper.hpp" |
4 | 4 | #include "common.hpp" |
| 5 | +#include "notify-helper.hpp" |
5 | 6 |
|
6 | 7 |
|
7 | 8 | namespace opencl { |
@@ -132,91 +133,17 @@ JS_METHOD(getEventProfilingInfo) { NAPI_ENV; |
132 | 133 | THROW_ERR(CL_INVALID_VALUE); |
133 | 134 | } |
134 | 135 |
|
135 | | - |
136 | | -class EventWorker : public Napi::AsyncWorker { |
137 | | -public: |
138 | | - EventWorker(Napi::Function callback, Napi::Object userData, Napi::Object wrapper): |
139 | | - Napi::AsyncWorker(callback, "CL::EventWorker") { |
140 | | - _refEvent.Reset(wrapper, 1); |
141 | | - _refData.Reset(userData, 1); |
142 | | - this->async = new uv_async_t(); |
143 | | - this->async->data = reinterpret_cast<void*>(this); |
144 | | - uv_async_init( |
145 | | - uv_default_loop(), |
146 | | - this->async, |
147 | | - (uv_async_cb) |
148 | | - dispatched_async_uv_callback |
149 | | - ); |
150 | | - } |
151 | | - |
152 | | - ~EventWorker() { |
153 | | - uv_close(reinterpret_cast<uv_handle_t*>(this->async), &delete_async_handle); |
154 | | - } |
155 | | - |
156 | | - uv_async_t *async; |
157 | | - |
158 | | - void setStatus(int status) { |
159 | | - _status = status; |
160 | | - } |
161 | | - |
162 | | - // Executed inside the worker-thread. |
163 | | - void Execute() { |
164 | | - } |
165 | | - |
166 | | - // Executed when the async work is complete |
167 | | - // this function will be run inside the main event loop |
168 | | - void OnOK () { |
169 | | - Napi::Env env = Env(); |
170 | | - NAPI_HS; |
171 | | - Callback().Call({ |
172 | | - _refData.Value(), // userdata |
173 | | - JS_NUM(_status), // error status |
174 | | - _refEvent.Value() // event |
175 | | - }); |
176 | | - } |
177 | | - |
178 | | -protected: |
179 | | - static void delete_async_handle(uv_handle_t *handle); |
180 | | - // The callback invoked by the call to uv_async_send() in notifyCB. |
181 | | - // Invoked on the main thread, so it's safe to call AsyncQueueWorker. |
182 | | - static void dispatched_async_uv_callback(uv_async_t*); |
183 | | - |
184 | | -private: |
185 | | - int _status = 0; |
186 | | - Napi::ObjectReference _refEvent; |
187 | | - Napi::ObjectReference _refData; |
188 | | -}; |
189 | | - |
190 | | -void EventWorker::delete_async_handle(uv_handle_t *handle) { |
191 | | - delete reinterpret_cast<uv_async_t*>(handle); |
192 | | -} |
193 | | - |
194 | | -void EventWorker::dispatched_async_uv_callback(uv_async_t *req) { |
195 | | - EventWorker* asyncCB = static_cast<EventWorker*>(req->data); |
196 | | - asyncCB->Queue(); |
197 | | -} |
198 | | - |
199 | | -// callback invoked off the main thread by clSetEventCallback |
200 | | -void CL_CALLBACK notifyCB (cl_event event, cl_int event_command_exec_status, void *user_data) { |
201 | | - EventWorker* asyncCB = reinterpret_cast<EventWorker*>(user_data); |
202 | | - asyncCB->setStatus(event_command_exec_status); |
203 | | - // send a message to the main thread to safely invoke the JS callback |
204 | | - uv_async_send(asyncCB->async); |
205 | | -} |
206 | | - |
207 | 136 | JS_METHOD(setEventCallback) { NAPI_ENV; |
208 | 137 | REQ_CL_ARG(0, ev, cl_event); |
209 | 138 | REQ_UINT32_ARG(1, callbackStatusType); |
210 | 139 | REQ_FUN_ARG(2, callback); |
211 | | - LET_OBJ_ARG(3, userData); |
212 | 140 |
|
213 | | - EventWorker* asyncCB = new EventWorker( |
214 | | - callback, |
215 | | - userData, |
216 | | - info[0].As<Napi::Object>() |
217 | | - ); |
218 | | - |
219 | | - CHECK_ERR(clSetEventCallback(ev, callbackStatusType, notifyCB, asyncCB)); |
| 141 | + CHECK_ERR(clSetEventCallback( |
| 142 | + ev, |
| 143 | + callbackStatusType, |
| 144 | + NotifyHelper<cl_event>::callNotifyStatus, |
| 145 | + new NotifyHelper<cl_event>(callback, info[3]) |
| 146 | + )); |
220 | 147 |
|
221 | 148 | RET_NUM(CL_SUCCESS); |
222 | 149 | } |
|
0 commit comments