summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
authorAapo Alasuutari <aapo.alasuutari@gmail.com>2022-06-28 12:23:36 +0300
committerGitHub <noreply@github.com>2022-06-28 14:53:36 +0530
commit00f4521b205bf25c79f0fa7c9a6840941342bda4 (patch)
treeb988db360143384ae216022846b518ceb262c80a /ext/ffi/00_ffi.js
parente1c90963fbbf6571ae1b66971b83159681928ec3 (diff)
feat(ext/ffi): Thread safe callbacks (#14942)
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js18
1 files changed, 18 insertions, 0 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js
index 190873307..f308ecad9 100644
--- a/ext/ffi/00_ffi.js
+++ b/ext/ffi/00_ffi.js
@@ -201,6 +201,7 @@
}
class UnsafeCallback {
+ #refcount;
#rid;
definition;
callback;
@@ -217,13 +218,30 @@
definition,
callback,
);
+ this.#refcount = 0;
this.#rid = rid;
this.pointer = pointer;
this.definition = definition;
this.callback = callback;
}
+ ref() {
+ if (this.#refcount++ === 0) {
+ core.opSync("op_ffi_unsafe_callback_ref", true);
+ }
+ }
+
+ unref() {
+ if (--this.#refcount === 0) {
+ core.opSync("op_ffi_unsafe_callback_ref", false);
+ }
+ }
+
close() {
+ if (this.#refcount) {
+ this.#refcount = 0;
+ core.opSync("op_ffi_unsafe_callback_ref", false);
+ }
core.close(this.#rid);
}
}