Skip to content

Commit 6d12ce9

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 d708df8 commit 6d12ce9

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
@@ -665,6 +665,7 @@ var SyscallsLibrary = {
665665
const pthread_ptr = PThread.currentProxiedOperationCallerThread;
666666
deactivateSelectCallbacks(pthread_ptr); // deactivate all old callbacks
667667
var makeNotifyCallback = (fd) => null;
668+
var cleanupFuncs = [];
668669
if (timeoutInMillis != 0) {
669670
var info = getActiveSelectCallbacks(pthread_ptr);
670671
{{{ makeSetValue('waitPtr', 0, 'info.buf', '*') }}};
@@ -675,10 +676,14 @@ var SyscallsLibrary = {
675676
return; // This callback is no longer active.
676677
}
677678
deactivateSelectCallbacks(pthread_ptr); // Only the first event is notified.
679+
cleanupFuncs.forEach(cb => cb());
678680
Atomics.store(HEAP32, info.buf >> 2 + 1, flags);
679681
Atomics.store(HEAP32, info.buf >> 2, fd);
680682
Atomics.notify(HEAP32, info.buf >> 2);
681683
}
684+
cb.registerCleanupFunc = (f) => {
685+
if (f != null) cleanupFuncs.push(f);
686+
}
682687
activateSelectCallback(pthread_ptr, cb);
683688
return cb;
684689
}
@@ -716,6 +721,7 @@ var SyscallsLibrary = {
716721
fdSet.commit(fd, flags);
717722
// No wait will happen in the caller. Deactivate all callbacks.
718723
deactivateSelectCallbacks(pthread_ptr);
724+
cleanupFuncs.forEach(f => f());
719725
}
720726
#else
721727
fdSet.commit(fd, flags);

0 commit comments

Comments
 (0)