Skip to content

Commit 97bc642

Browse files
committed
Improve program callbacks
1 parent af675e7 commit 97bc642

File tree

13 files changed

+113
-136
lines changed

13 files changed

+113
-136
lines changed

index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ declare namespace cl {
501501
export type TClSampler = TClObject & { __brand: "cl_sampler" };
502502
export type TClQueue = TClObject & { __brand: "cl_command_queue" };
503503
export type TClEvent = TClObject & { __brand: "cl_event" };
504-
export type TClMapped = TClObject & { __brand: "cl_mapped_ptr" };
505504

506505
export type TClEventOrVoid = TClEvent | undefined;
507506
export type TClHostData = ArrayBuffer | ArrayBufferView | Buffer;
@@ -635,12 +634,13 @@ declare namespace cl {
635634
) => TClImageFormat[];
636635

637636
/**
637+
* For cl.MEM_HOST_PTR returns `ArrayBuffer` for cl.MEM_USE_HOST_PTR buffers, and `null` for others.
638638
* @see [clGetMemObjectInfo](https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clGetMemObjectInfo.html)
639639
*/
640640
const getMemObjectInfo: (
641641
mem: TClMem,
642642
param_name: number,
643-
) => (number | TClMem | TClContext | Object);
643+
) => (number | TClMem | TClContext | ArrayBuffer | null);
644644

645645
/**
646646
* @see [clGetImageInfo](https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clGetImageInfo.html)
@@ -720,7 +720,7 @@ declare namespace cl {
720720
*/
721721
const releaseProgram: (program: TClProgram) => TClStatus;
722722

723-
export type TBuildProgramCb = (program: TClProgram, user_data: Object) => undefined;
723+
export type TBuildProgramCb = (program: TClProgram, userData: unknown) => void;
724724

725725
/**
726726
* @see [clBuildProgram](https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clBuildProgram.html)
@@ -730,7 +730,7 @@ declare namespace cl {
730730
devices?: TClDevice[] | null,
731731
options?: string | null,
732732
cb?: TBuildProgramCb | null,
733-
user_data?: Object,
733+
userData?: unknown,
734734
) => TClStatus;
735735

736736
/**
@@ -743,7 +743,7 @@ declare namespace cl {
743743
headers?: TClProgram[] | null,
744744
names?: string[] | null,
745745
cb?: TBuildProgramCb | null,
746-
user_data?: Object,
746+
userData?: unknown,
747747
) => TClStatus;
748748

749749
/**
@@ -755,7 +755,7 @@ declare namespace cl {
755755
options?: string | null,
756756
programs?: TClProgram[],
757757
cb?: TBuildProgramCb | null,
758-
user_data?: Object,
758+
userData?: unknown,
759759
) => TClProgram;
760760

761761
/**

src/cpp/memobj.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,29 @@ JS_METHOD(getMemObjectInfo) { NAPI_ENV;
246246
RET_NUM(val);
247247
}
248248
case CL_MEM_HOST_PTR: {
249-
void* val;
249+
size_t memSize;
250+
CHECK_ERR(clGetMemObjectInfo(
251+
mem,
252+
CL_MEM_SIZE,
253+
sizeof(size_t),
254+
&memSize,
255+
nullptr
256+
));
257+
if (!memSize) {
258+
RET_NULL;
259+
}
260+
void* memPtr;
250261
CHECK_ERR(clGetMemObjectInfo(
251262
mem,
252263
param_name,
253264
sizeof(void*),
254-
&val,
265+
&memPtr,
255266
nullptr
256267
));
257-
RET_WRAPPER(val);
268+
if (!memPtr) {
269+
RET_NULL;
270+
}
271+
RET_VALUE(Napi::ArrayBuffer::New(env, memPtr, memSize));
258272
}
259273
case CL_MEM_CONTEXT: {
260274
cl_context val;

src/cpp/program.cpp

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,40 @@ JS_METHOD(releaseProgram) { NAPI_ENV;
116116
RET_NUM(CL_SUCCESS);
117117
}
118118

119-
class ProgramWorker : public Napi::AsyncWorker {
119+
class NotifyHelper {
120120
public:
121-
ProgramWorker(Napi::Function callback, Napi::Object userData, Napi::Object wrapper):
122-
Napi::AsyncWorker(callback, "CL::ProgramWorker") {
123-
_refProgram.Reset(wrapper, 1);
124-
_refData.Reset(userData, 1);
121+
NotifyHelper(Napi::Function callback, Napi::Value userData) {
122+
_ref.Reset(Napi::Object::New(callback.Env()), 1);
123+
_ref.Set("cb", callback);
124+
_ref.Set("data", userData);
125125
}
126126

127-
~ProgramWorker() {}
128-
129-
// Executed inside the worker-thread.
130-
void Execute() {}
127+
~NotifyHelper() {
128+
Napi::Env env = _ref.Env();
129+
_ref.Set("cb", env.Undefined());
130+
_ref.Set("data", env.Undefined());
131+
_ref.Reset();
132+
}
131133

132-
// Executed when the async work is complete
133-
// this function will be run inside the main event loop
134-
void OnOK () {
135-
Napi::Env env = Env();
136-
NAPI_HS;
137-
Callback().Call({
138-
_refProgram.Value(), // program
139-
_refData.Value() // userdata
140-
});
134+
static void CL_CALLBACK callNotify(cl_program prog, void *ptr) {
135+
NotifyHelper *notifier = reinterpret_cast<NotifyHelper*>(ptr);
136+
notifier->notify(prog);
137+
delete notifier;
141138
}
142139

143140
private:
144-
Napi::ObjectReference _refProgram;
145-
Napi::ObjectReference _refData;
141+
Napi::ObjectReference _ref;
142+
143+
void notify(cl_program prog) {
144+
Napi::Env env = _ref.Env(); NAPI_HS;
145+
146+
_ref.Get("cb").As<Napi::Function>().Call({
147+
Wrapper::fromRaw(env, prog),
148+
_ref.Get("data")
149+
});
150+
}
146151
};
147152

148-
void CL_CALLBACK notifyPCB (cl_program prog, void *user_data) {
149-
ProgramWorker* asyncCB = reinterpret_cast<ProgramWorker*>(user_data);
150-
asyncCB->Queue();
151-
}
152-
153153
JS_METHOD(buildProgram) { NAPI_ENV;
154154
REQ_CL_ARG(0, p, cl_program);
155155

@@ -172,19 +172,13 @@ JS_METHOD(buildProgram) { NAPI_ENV;
172172
// callback + userdata
173173
if (!IS_ARG_EMPTY(3)) {
174174
REQ_FUN_ARG(3, callback);
175-
ProgramWorker* cb = new ProgramWorker(
176-
callback,
177-
info[4].As<Napi::Object>(),
178-
info[0].As<Napi::Object>()
179-
);
180-
181175
err = clBuildProgram(
182176
p,
183177
(cl_uint) cl_devices.size(),
184178
&cl_devices.front(),
185179
options.length() > 0 ? options.c_str() : nullptr,
186-
notifyPCB,
187-
cb
180+
NotifyHelper::callNotify,
181+
new NotifyHelper(callback, info[4])
188182
);
189183
} else {
190184
err = clBuildProgram(
@@ -250,11 +244,6 @@ JS_METHOD(compileProgram) { NAPI_ENV;
250244

251245
if (!IS_ARG_EMPTY(5)) {
252246
REQ_FUN_ARG(5, callback);
253-
ProgramWorker* cb = new ProgramWorker(
254-
callback,
255-
info[6].As<Napi::Object>(),
256-
info[0].As<Napi::Object>()
257-
);
258247
err = clCompileProgram(
259248
p,
260249
(cl_uint) cl_devices.size(),
@@ -263,8 +252,8 @@ JS_METHOD(compileProgram) { NAPI_ENV;
263252
(cl_uint) program_headers.size(),
264253
&program_headers.front(),
265254
&names.front(),
266-
notifyPCB,
267-
cb
255+
NotifyHelper::callNotify,
256+
new NotifyHelper(callback, info[6])
268257
);
269258
} else {
270259
err = clCompileProgram(
@@ -311,27 +300,21 @@ JS_METHOD(linkProgram) { NAPI_ENV;
311300
}
312301
}
313302

314-
// OSX ISSUE: ret always equals to CL_SUCCESS
315303
cl_int ret = CL_SUCCESS;
316304

317305
cl_program prg;
318306

319307
if (!IS_ARG_EMPTY(4)) {
320308
REQ_FUN_ARG(4, callback);
321-
ProgramWorker* cb = new ProgramWorker(
322-
callback,
323-
info[5].As<Napi::Object>(),
324-
info[0].As<Napi::Object>()
325-
);
326309
prg = clLinkProgram(
327310
ctx,
328311
(cl_uint) cl_devices.size(),
329312
&cl_devices.front(),
330313
options.length() > 0 ? options.c_str() : nullptr,
331314
(cl_uint) cl_programs.size(),
332315
&cl_programs.front(),
333-
notifyPCB,
334-
cb,
316+
NotifyHelper::callNotify,
317+
new NotifyHelper(callback, info[5]),
335318
&ret
336319
);
337320
} else {

src/cpp/types.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ TypeInfo typeInfo[] = {
5353
reinterpret_cast<cl_func>(clReleaseEvent),
5454
reinterpret_cast<cl_func>(clRetainEvent)
5555
},
56-
{ "cl_mapped_ptr", noop, noop }
5756
};
5857

5958

@@ -63,6 +62,7 @@ IMPLEMENT_ES5_CLASS(Wrapper);
6362
void Wrapper::init(Napi::Env env, Napi::Object exports) {
6463
Napi::Function ctor = wrap(env);
6564
JS_ASSIGN_METHOD(toString);
65+
JS_ASSIGN_GETTER(_);
6666
exports.Set("Wrapper", ctor);
6767
}
6868

@@ -94,9 +94,6 @@ Napi::Object Wrapper::fromRaw(Napi::Env env, cl_command_queue raw) {
9494
Napi::Object Wrapper::fromRaw(Napi::Env env, cl_event raw) {
9595
return _ctorEs5.New({ JS_EXT(raw), JS_NUM(9) });
9696
}
97-
Napi::Object Wrapper::fromRaw(Napi::Env env, cl_mapped_ptr raw) {
98-
return _ctorEs5.New({ JS_EXT(raw), JS_NUM(10) });
99-
}
10097

10198

10299
Wrapper::Wrapper(const Napi::CallbackInfo& info) { NAPI_ENV;
@@ -116,15 +113,13 @@ Wrapper::Wrapper(const Napi::CallbackInfo& info) { NAPI_ENV;
116113
int32_t infoIdx = info[1].ToNumber().Int32Value();
117114

118115
_data = extParam.Data();
119-
_released = 1;
120116
_acquire = typeInfo[infoIdx].acquire;
121117
_release = typeInfo[infoIdx].release;
122118
_typeName = typeInfo[infoIdx].typeName;
123119
}
124120

125121

126122
Wrapper::~Wrapper() {
127-
release();
128123
}
129124

130125

@@ -134,6 +129,9 @@ JS_IMPLEMENT_METHOD(Wrapper, toString) { NAPI_ENV;
134129
RET_STR(out.str());
135130
}
136131

132+
JS_IMPLEMENT_GETTER(Wrapper, _) { NAPI_ENV;
133+
RET_X64(reinterpret_cast<uint64_t>(_data));
134+
}
137135

138136
void Wrapper::throwArrayEx(Napi::Env env, int i, const char* msg) {
139137
std::stringstream out;
@@ -143,16 +141,11 @@ void Wrapper::throwArrayEx(Napi::Env env, int i, const char* msg) {
143141

144142

145143
cl_int Wrapper::acquire() {
146-
_released++;
147144
return _acquire(_data);
148145
}
149146

150147

151148
cl_int Wrapper::release() {
152-
if (_released < 1) {
153-
return CL_SUCCESS;
154-
}
155-
_released--;
156149
return _release(_data);
157150
}
158151

src/cpp/types.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace opencl {
1111

12-
typedef void *cl_mapped_ptr;
1312
typedef int (*cl_func)(void*);
1413

1514

@@ -28,12 +27,12 @@ DECLARE_ES5_CLASS(Wrapper, Wrapper);
2827
static Napi::Object fromRaw(Napi::Env env, cl_sampler raw);
2928
static Napi::Object fromRaw(Napi::Env env, cl_command_queue raw);
3029
static Napi::Object fromRaw(Napi::Env env, cl_event raw);
31-
static Napi::Object fromRaw(Napi::Env env, cl_mapped_ptr raw);
3230

3331
explicit Wrapper(const Napi::CallbackInfo& info);
3432
~Wrapper();
3533

3634
JS_DECLARE_METHOD(Wrapper, toString);
35+
JS_DECLARE_GETTER(Wrapper, _);
3736

3837
cl_int acquire();
3938
cl_int release();

test/device.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ describe('Device', () => {
1616
});
1717

1818
const testBoolean = (name: keyof typeof cl) => {
19-
it(name + ' returns a boolean', (t, done) => {
19+
it(name + ' returns a boolean', (_t, done) => {
2020
const val = cl.getDeviceInfo(device, cl[name] as number);
2121
U.assertType(val, 'boolean');
2222
done();
2323
});
2424
};
2525
const testInteger = (name: keyof typeof cl) => {
26-
it(name + ' returns an integer', (t, done) => {
26+
it(name + ' returns an integer', (_t, done) => {
2727
const val = cl.getDeviceInfo(device, cl[name] as number);
2828
U.assertType(val, 'number');
2929
done();
3030
});
3131
};
3232
const testString = (name: keyof typeof cl) => {
33-
it(name + ' returns a string', (t, done) => {
33+
it(name + ' returns a string', (_t, done) => {
3434
const val = cl.getDeviceInfo(device, cl[name] as number);
3535
U.assertType(val, 'string');
3636
done();
@@ -43,7 +43,7 @@ describe('Device', () => {
4343
});
4444
};
4545
const testArray = (name: keyof typeof cl) => {
46-
it(name + ' returns an array', (t, done) => {
46+
it(name + ' returns an array', (_t, done) => {
4747
const val = cl.getDeviceInfo(device, cl[name] as number);
4848
U.assertType(val, 'array');
4949
done();

test/event.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('Event', () => {
106106
});
107107

108108
describe('#setEventCallback', () => {
109-
it('calls cb', (t, done) => {
109+
it('calls cb', (_t, done) => {
110110
const uEvent = cl.createUserEvent(context);
111111
cl.setEventCallback(
112112
uEvent,

test/memobj-info.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as U from './utils.ts';
77
describe('MemObj', () => {
88
const { context } = cl.quickStart();
99

10-
const buffer = cl.createBuffer(context, cl.MEM_WRITE_ONLY, 8, null);
10+
const buffer = cl.createBuffer(context, cl.MEM_WRITE_ONLY, 8);
1111
const format: cl.TClImageFormat = {
1212
'channel_order': cl.RGBA,
1313
'channel_data_type': cl.UNSIGNED_INT8,
@@ -105,7 +105,7 @@ describe('MemObj', () => {
105105
});
106106

107107
it('returns cl.MEM_HOST_PTR', () => {
108-
const buffer = cl.createBuffer(context, cl.MEM_ALLOC_HOST_PTR, 8, null);
108+
const buffer = cl.createBuffer(context, cl.MEM_USE_HOST_PTR, 32, Buffer.alloc(32));
109109
const ret = cl.getMemObjectInfo(buffer, cl.MEM_HOST_PTR);
110110
U.assertType(ret, 'object');
111111
});

0 commit comments

Comments
 (0)