Skip to content

Commit

Permalink
CombinerSecurityManager - Replace ExecutorService using platform thre…
Browse files Browse the repository at this point in the history
…ads with virtual threads. #48
  • Loading branch information
pfirmstone committed Jan 22, 2025
1 parent ee07ebd commit e102e4b
Showing 1 changed file with 2 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import au.zeus.jdk.concurrent.Ref;
import au.zeus.jdk.concurrent.Referrer;
import java.lang.ScopedValue.CallableOp;
import java.util.concurrent.Executors;

/**
* CombinerSecurityManager, is intended to be a highly scalable
Expand Down Expand Up @@ -170,37 +171,10 @@ private CombinerSecurityManager(boolean check){
checked = RC.concurrentMap(refmap, Ref.TIME, Ref.STRONG, 20000L, 20000L);
g = new SecurityPermission("getPolicy");
action = new Action();
// Make this a tunable property.
double blocking_coefficient = 0.6; // 0 CPU intensive to 0.9 IO intensive
int numberOfCores = Runtime.getRuntime().availableProcessors();
int poolSizeLimit = (int) (numberOfCores / ( 1 - blocking_coefficient));
// The intent here is to parallelise security checks as well as weed
// out blocking SocketPermission's to execute them in parallel to
// reduce the wait on network IO.
// Once the pool size has reached it's maximum, the tasks are handed
// back to the calling thread to execute, this strategy also eliminates
// the possiblity of deadlock caused by circular dependencies between
// permission checks.
executor =
new ThreadPoolExecutor(numberOfCores, poolSizeLimit, 20L,
TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
new ThreadFactory(){

public Thread newThread(Runnable r) {
Integer count = TRUSTED_RECURSIVE_CALL.isBound() ? TRUSTED_RECURSIVE_CALL.get() : null;
if (count == null) count = Integer.valueOf(0);
return ScopedValue.where(TRUSTED_RECURSIVE_CALL, count + 1).call(
new CallableOp<Thread, SecurityException>(){
@Override
public Thread call() throws SecurityException {
Thread t = new Thread(r, "CombinerSecurityManager_thread");
t.setDaemon(true);
return t;
}
});
}
},
new ThreadPoolExecutor.CallerRunsPolicy());
executor = Executors.newVirtualThreadPerTaskExecutor();
permCompare = RC.comparator(new PermissionComparator());
// This is to avoid unnecessarily refreshing the policy.
// Permission createAccPerm = new SecurityPermission("createAccessControlContext");
Expand Down

0 comments on commit e102e4b

Please sign in to comment.