Skip to content

Commit 172650a

Browse files
committed
select: Allow registering cleanup method for the callback
This commit adds the registerCleanupFunc method for the callback passed to the stream implementation. It allows the stream implementation to optionally register a cleanup function that will be invoked when select is no longer interested in the stream events. This enables the stream implementation to perform additional cleanups such as discarding the reference to the event callback. Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
1 parent 9453cb9 commit 172650a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/lib/libsyscall.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ var SyscallsLibrary = {
702702
#if PROXY_TO_PTHREAD
703703
SYSCALLS.deactivateSelectCallbacks(fdIdx); // deactivate all old callbacks
704704
var makeNotifyCallback = (fd) => null;
705+
var cleanupFuncs = [];
705706
if (timeoutInMillis != 0) {
706707
Atomics.store(HEAP32, fdIdx, -1); // Initialize the shared region
707708
Atomics.store(HEAP32, flagsIdx, -1); // Initialize the shared region
@@ -711,10 +712,14 @@ var SyscallsLibrary = {
711712
return; // This callback is no longer active.
712713
}
713714
SYSCALLS.deactivateSelectCallbacks(fdIdx); // Only the first event is notified.
715+
cleanupFuncs.forEach(cb => cb());
714716
Atomics.store(HEAP32, flagsIdx, flags);
715717
Atomics.store(HEAP32, fdIdx, fd);
716718
Atomics.notify(HEAP32, fdIdx);
717719
}
720+
cb.registerCleanupFunc = (f) => {
721+
if (f != null) cleanupFuncs.push(f);
722+
}
718723
SYSCALLS.activateSelectCallback(fdIdx, cb);
719724
return cb;
720725
}
@@ -752,6 +757,7 @@ var SyscallsLibrary = {
752757
fdSet.commit(fd, flags);
753758
// No wait will happen in the caller. Deactivate all callbacks.
754759
SYSCALLS.deactivateSelectCallbacks(fdIdx);
760+
cleanupFuncs.forEach(f => f());
755761
}
756762
#else
757763
fdSet.commit(fd, flags);

0 commit comments

Comments
 (0)