[freenet-cvs] r17919 - in trunk/freenet/src/freenet: node support support/io

nextgens at freenetproject.org nextgens at freenetproject.org
Fri Feb 15 07:33:49 UTC 2008


Author: nextgens
Date: 2008-02-15 07:33:49 +0000 (Fri, 15 Feb 2008)
New Revision: 17919

Modified:
   trunk/freenet/src/freenet/node/NodeStarter.java
   trunk/freenet/src/freenet/node/NodeStats.java
   trunk/freenet/src/freenet/support/Executor.java
   trunk/freenet/src/freenet/support/PooledExecutor.java
   trunk/freenet/src/freenet/support/io/NativeThread.java
Log:
Basic implementation of NativeThread in the Executor

Modified: trunk/freenet/src/freenet/node/NodeStarter.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeStarter.java	2008-02-15 06:22:08 UTC (rev 17918)
+++ trunk/freenet/src/freenet/node/NodeStarter.java	2008-02-15 07:33:49 UTC (rev 17919)
@@ -76,6 +76,8 @@
     		return new Integer(-1);
     	}
     	
+	getExtBuild();
+	
     	File configFilename;
     	if(args.length == 0) {
     		System.out.println("Using default config filename freenet.ini");
@@ -111,8 +113,6 @@
 
     	executor.start();
     	
-    	getExtBuild();
-    	
     	// Setup RNG
     	RandomSource random = new Yarrow();
     	

Modified: trunk/freenet/src/freenet/node/NodeStats.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeStats.java	2008-02-15 06:22:08 UTC (rev 17918)
+++ trunk/freenet/src/freenet/node/NodeStats.java	2008-02-15 07:33:49 UTC (rev 17919)
@@ -773,7 +773,12 @@
 	}
 
 	public int getActiveThreadCount() {
-		return rootThreadGroup.activeCount() - node.executor.waitingThreads();
+		int waitingThreads = 0;
+		int[] toCount = node.executor.waitingThreads();
+		for(int i=0; i<toCount.length; i++)
+			waitingThreads += toCount[i];
+		
+		return rootThreadGroup.activeCount() - waitingThreads;
 	}
 
 	public int getThreadLimit() {

Modified: trunk/freenet/src/freenet/support/Executor.java
===================================================================
--- trunk/freenet/src/freenet/support/Executor.java	2008-02-15 06:22:08 UTC (rev 17918)
+++ trunk/freenet/src/freenet/support/Executor.java	2008-02-15 07:33:49 UTC (rev 17919)
@@ -12,5 +12,5 @@
 	public void execute(Runnable job, String jobName);
 
 	/** Count the number of threads waiting for work */
-	public int waitingThreads();
+	public int[] waitingThreads();
 }

Modified: trunk/freenet/src/freenet/support/PooledExecutor.java
===================================================================
--- trunk/freenet/src/freenet/support/PooledExecutor.java	2008-02-15 06:22:08 UTC (rev 17918)
+++ trunk/freenet/src/freenet/support/PooledExecutor.java	2008-02-15 07:33:49 UTC (rev 17919)
@@ -3,6 +3,7 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package freenet.support;
 
+import freenet.support.io.NativeThread;
 import java.util.ArrayList;
 
 /**
@@ -12,9 +13,9 @@
  */
 public class PooledExecutor implements Executor {
 
-	private final ArrayList runningThreads /* <MyThread> */ = new ArrayList();
-	private final ArrayList waitingThreads /* <MyThread> */ = new ArrayList();
-	long threadCounter = 0;
+	private final ArrayList[] runningThreads /* <MyThread> */ = new ArrayList[NativeThread.JAVA_PRIO_RANGE];
+	private final ArrayList[] waitingThreads /* <MyThread> */ = new ArrayList[NativeThread.JAVA_PRIO_RANGE];
+	long[] threadCounter;
 	private long jobCount;
 	private long jobMisses;
 	private static boolean logMINOR;
@@ -27,17 +28,21 @@
 	}
 	
 	public void execute(Runnable job, String jobName) {
+		execute(job, jobName, Thread.NORM_PRIORITY);
+	}
+	
+	public void execute(Runnable job, String jobName, int prio) {
 		while(true) {
 			MyThread t;
 			boolean mustStart = false;
 			boolean miss = false;
 			synchronized(this) {
 				jobCount++;
-				if(!waitingThreads.isEmpty()) {
-					t = (MyThread) waitingThreads.remove(waitingThreads.size()-1);
+				if(!waitingThreads[prio].isEmpty()) {
+					t = (MyThread) waitingThreads[prio].remove(waitingThreads[prio].size()-1);
 				} else {
 					// Will be coalesced by thread count listings if we use "@" or "for"
-					t = new MyThread("Pooled thread awaiting work @"+(threadCounter++), threadCounter);
+					t = new MyThread("Pooled thread awaiting work @"+(threadCounter[prio]++), threadCounter[prio], prio);
 					t.setDaemon(true);
 					mustStart = true;
 					miss = true;
@@ -57,7 +62,7 @@
 			if(mustStart) {
 				t.start();
 				synchronized(this) {
-					runningThreads.add(t);
+					runningThreads[prio].add(t);
 					if(miss)
 						jobMisses++;
 					if(logMINOR)
@@ -68,25 +73,29 @@
 		}
 	}
 
-	public synchronized int waitingThreads() {
-		return waitingThreads.size();
+	public synchronized int[] waitingThreads() {
+		int[] result = new int[waitingThreads.length];
+		for(int i=0; i<result.length; i++)
+			result[i] = waitingThreads[i].size();
+		return result;
 	}
 	
-	class MyThread extends Thread {
+	class MyThread extends NativeThread {
 		
 		final String defaultName;
 		boolean alive = true;
 		Runnable nextJob;
 		final long threadNo;
 		
-		public MyThread(String defaultName, long threadCounter) {
-			super(defaultName);
+		public MyThread(String defaultName, long threadCounter, int prio) {
+			super(defaultName, prio);
 			this.defaultName = defaultName;
 			threadNo = threadCounter;
 		}
 
 		public void run() {
 			long ranJobs = 0;
+			int nativePriority = getNativePriority();
 			while(true) {
 				Runnable job;
 				
@@ -97,7 +106,7 @@
 				
 				if(job == null) {
 					synchronized(PooledExecutor.this) {
-						waitingThreads.add(this);
+						waitingThreads[nativePriority].add(this);
 					}
 					synchronized(this) {
 						if(nextJob == null) {
@@ -116,9 +125,9 @@
 						}
 					}
 					synchronized(PooledExecutor.this) {
-						waitingThreads.remove(this);
+						waitingThreads[nativePriority].remove(this);
 						if(!alive) {
-							runningThreads.remove(this);
+							runningThreads[nativePriority].remove(this);
 							if(logMINOR)
 								Logger.minor(this, "Exiting having executed "+ranJobs+" jobs : "+this);
 							return;

Modified: trunk/freenet/src/freenet/support/io/NativeThread.java
===================================================================
--- trunk/freenet/src/freenet/support/io/NativeThread.java	2008-02-15 06:22:08 UTC (rev 17918)
+++ trunk/freenet/src/freenet/support/io/NativeThread.java	2008-02-15 07:33:49 UTC (rev 17919)
@@ -14,9 +14,9 @@
  */
 public class NativeThread extends Thread {
 	private static boolean _loadNative;
-	private static final int JAVA_PRIO_RANGE = MAX_PRIORITY - MIN_PRIORITY;
+	public static final int JAVA_PRIO_RANGE = MAX_PRIORITY - MIN_PRIORITY;
 	private static final int NATIVE_PRIORITY_BASE;
-	private static final int NATIVE_PRIORITY_RANGE;
+	public static final int NATIVE_PRIORITY_RANGE;
 	private int currentPriority = Thread.MAX_PRIORITY;
 
 	public static final boolean HAS_THREE_NICE_LEVELS;
@@ -86,6 +86,11 @@
 		}
 	}
 	
+	public NativeThread(String name, int priority) {
+		super(name);
+		this.currentPriority = priority;
+	}
+	
 	public NativeThread(Runnable r, String name, int priority) {
 		super(r, name);
 		this.currentPriority = priority;
@@ -139,4 +144,8 @@
 				+currentPriority+':'+NATIVE_PRIORITY_BASE+") SHOUDLN'T HAPPEN, please report!");
 		return setLinuxPriority(linuxPriority);
 	}
+	
+	public int getNativePriority() {
+		return currentPriority;
+	}
 }




More information about the cvs mailing list