Skip to content

Commit

Permalink
Merge pull request #234 from msfm/3.7_XNIO-376_XNIO-377
Browse files Browse the repository at this point in the history
3.7: XNIO-376 and XNIO-377
  • Loading branch information
dmlloyd committed Aug 12, 2020
2 parents 47a7996 + 94a2970 commit 3a6cde7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/src/main/java/org/xnio/Xnio.java
Expand Up @@ -494,6 +494,12 @@ public FileSystemWatcher createFileSystemWatcher(final String name, final Option
return new PollingFileSystemWatcher(name, pollInterval, daemonThread);
}

/**
* Implement tasks that will be executed on thread exits if a task worker
* thread is initialized through {@code XnioWorker$WorkerThreadFactory}.
*/
protected void handleThreadExit() {}

//==================================================
//
// General methods
Expand Down
11 changes: 10 additions & 1 deletion api/src/main/java/org/xnio/XnioWorker.java
Expand Up @@ -1273,7 +1273,16 @@ class WorkerThreadFactory implements ThreadFactory {
public Thread newThread(final Runnable r) {
return doPrivileged(new PrivilegedAction<Thread>() {
public Thread run() {
final Thread taskThread = new Thread(threadGroup, r, name + " task-" + getNextSeq(), stackSize);
final Thread taskThread = new Thread(threadGroup, new Runnable() {
@Override
public void run() {
try {
r.run();
} finally {
xnio.handleThreadExit();
}
}
}, name + " task-" + getNextSeq(), stackSize);
// Mark the thread as daemon if the Options.THREAD_DAEMON has been set
if (markThreadAsDaemon) {
taskThread.setDaemon(true);
Expand Down
7 changes: 7 additions & 0 deletions nio-impl/src/main/java/org/xnio/nio/NioXnio.java
Expand Up @@ -248,6 +248,13 @@ public FileSystemWatcher createFileSystemWatcher(String name, OptionMap options)
return super.createFileSystemWatcher(name, options);
}

@Override
protected void handleThreadExit() {
log.tracef("Invoke selectorThreadLocal.remove() on Thread [%s] exits", Thread.currentThread().getName());
selectorThreadLocal.remove();
super.handleThreadExit();
}

private final ThreadLocal<FinalizableSelectorHolder> selectorThreadLocal = new ThreadLocal<FinalizableSelectorHolder>() {
public void remove() {
// if no selector was created, none will be closed
Expand Down

0 comments on commit 3a6cde7

Please sign in to comment.