harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r418307 - in /incubator/harmony/enhanced/classlib/trunk/modules: luni-kernel/src/main/java/java/lang/ nio/src/main/java/java/nio/channels/spi/
Date Fri, 30 Jun 2006 15:13:09 GMT
Author: tellison
Date: Fri Jun 30 08:13:08 2006
New Revision: 418307

URL: http://svn.apache.org/viewvc?rev=418307&view=rev
Log:
Apply patch HARMONY-635 ([classlib][nio]Interruptible channel is not fully implemented)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni-kernel/src/main/java/java/lang/Thread.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni-kernel/src/main/java/java/lang/Thread.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni-kernel/src/main/java/java/lang/Thread.java?rev=418307&r1=418306&r2=418307&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni-kernel/src/main/java/java/lang/Thread.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni-kernel/src/main/java/java/lang/Thread.java
Fri Jun 30 08:13:08 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -79,6 +79,8 @@
 
 	Object slot3;
 
+	private Runnable action = null;
+
 	/**
 	 * Constructs a new Thread with no runnable object and a newly generated
 	 * name. The new Thread will belong to the same ThreadGroup as the Thread
@@ -370,7 +372,7 @@
 	}
 
 	/**
-	 * Posts an interrupt request to the receiver
+	 * Posts an interrupt request to the receiver. 
 	 * 
 	 * @exception SecurityException
 	 *                if <code>group.checkAccess()</code> fails with a
@@ -381,7 +383,23 @@
 	 * @see Thread#isInterrupted
 	 */
 	public void interrupt() {
+		if(action != null){
+			action.run();
+		}
 		return;
+	}
+	
+	/**
+	 * Set the action to be executed when interruption, which is probably be used 
+     * to implement the interruptible channel. The action is null by default. And 
+     * if this method is invoked by passing in a non-null value, this action's run() 
+     * method will be invoked in <code>interrupt()</code>. 
+     * 
+	 * @param action
+     *      the action to be executed when interruption 
+	 */
+	private void setInterruptAction(Runnable action){
+		this.action  = action;
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java?rev=418307&r1=418306&r2=418307&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
Fri Jun 30 08:13:08 2006
@@ -16,10 +16,13 @@
 package java.nio.channels.spi;
 
 import java.io.IOException;
+import java.lang.reflect.Method;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.Channel;
 import java.nio.channels.ClosedByInterruptException;
 import java.nio.channels.InterruptibleChannel;
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * This class roots the implementation of interruptable channels.
@@ -35,7 +38,30 @@
 public abstract class AbstractInterruptibleChannel implements Channel,
 		InterruptibleChannel {
 
-    private volatile boolean closed = false;
+	static Method setInterruptAction = null;
+
+	static {
+		try {
+			setInterruptAction = AccessController
+					.doPrivileged(new PrivilegedExceptionAction<Method>() {
+						public Method run() throws Exception {
+							return Thread.class.getDeclaredMethod(
+									"setInterruptAction",
+									new Class[] { Runnable.class });
+
+						}
+					});
+			setInterruptAction.setAccessible(true);
+		} catch (Exception e) {
+			// FIXME: be accomodate before VM actually provides
+			// setInterruptAction method
+			// throw new Error(e);
+		}
+	}
+
+	private volatile boolean closed = false;
+
+	public volatile boolean interrupted = false;
 
 	/**
 	 * Default constructor.
@@ -80,10 +106,27 @@
 	 * Once the operation is completed the application should invoke a
 	 * corresponding <code>end(boolean)</code>.
 	 */
-    protected final void begin() {
-        // FIXME: not implemented yet, need kernel class Thread's support
-        // idea is to indicate current thread that a blocking I/O begins
-    }
+	protected final void begin() {
+		// FIXME: be accomodate before VM actually provides
+		// setInterruptAction method
+		if (setInterruptAction != null) {
+			try {
+				setInterruptAction.invoke(Thread.currentThread(),
+						new Object[] { new Runnable() {
+							public void run() {
+								try {
+									interrupted = true;
+									AbstractInterruptibleChannel.this.close();
+								} catch (IOException e) {
+									// ignore
+								}
+							}
+						} });
+			} catch (Exception e) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
 
 	/**
 	 * End an IO operation that was previously started with <code>begin()</code>.
@@ -97,16 +140,25 @@
 	 * @throws java.nio.channels.ClosedByInterruptException
 	 *             the thread conducting the IO operation was interrupted.
 	 */
-    protected final void end(boolean success) throws AsynchronousCloseException {
-        if(Thread.currentThread().isInterrupted()){
-            throw new ClosedByInterruptException();
-        }
-        if (!success && closed){
-            throw new AsynchronousCloseException();
-        }
-        // FIXME: not fully implemented yet, need kernel class Thread's support
-        // idea is to indicate current thread that a blocking I/O ends        
-    }
+	protected final void end(boolean success) throws AsynchronousCloseException {
+		// FIXME: be accomodate before VM actually provides
+		// setInterruptAction method
+		if (setInterruptAction != null) {
+			try {
+				setInterruptAction.invoke(Thread.currentThread(),
+						new Object[] { null });
+			} catch (Exception e) {
+				throw new RuntimeException(e);
+			}
+			if (interrupted) {
+				interrupted = false;
+				throw new ClosedByInterruptException();
+			}
+		}
+		if (!success && closed) {
+			throw new AsynchronousCloseException();
+		}
+	}
 
 	/**
 	 * Implements the close channel behavior.

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java?rev=418307&r1=418306&r2=418307&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java
Fri Jun 30 08:13:08 2006
@@ -121,11 +121,33 @@
     }
 
     protected final void begin() {
-        // TODO wait for AbstractInterruptibleChannel
+        // FIXME: be accomodate before VM actually provides
+        // setInterruptAction method
+        if (AbstractInterruptibleChannel.setInterruptAction != null) {
+            try {
+                AbstractInterruptibleChannel.setInterruptAction.invoke(Thread
+                        .currentThread(), new Object[] { new Runnable() {
+                    public void run() {
+                        AbstractSelector.this.wakeup();
+                    }
+                } });
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
     }
 
     protected final void end() {
-        // TODO wait for AbstractInterruptibleChannel
+        // FIXME: be accomodate before VM actually provides
+        // setInterruptAction method
+        if (AbstractInterruptibleChannel.setInterruptAction != null) {
+            try {
+                AbstractInterruptibleChannel.setInterruptAction.invoke(Thread
+                        .currentThread(), new Object[] { null });
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
     }
 
     /*



Mime
View raw message