harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paulex Yang <paulex.y...@gmail.com>
Subject [Fwd: [classlib][NIO|VMI]Interruptible channel implementation - how to interact with Thread?]
Date Wed, 14 Jun 2006 07:36:27 GMT

I'm trying to complete the interruptible channel feature of NIO module, 
but there is a question.

The spec for InterruptibleChannel says:

"A channel that implements this interface is asynchronously closeable: 
If a thread is blocked in an I/O operation on an interruptible channel 
then another thread may invoke the channel's close method. This will 
cause the blocked thread to receive an AsynchronousCloseException.

A channel that implements this interface is also interruptible: If a 
thread is blocked in an I/O operation on an interruptible channel then 
another thread may invoke the blocked thread's interrupt method. *This 
will cause the channel to be closed, the blocked thread to receive a 
ClosedByInterruptException, and the blocked thread's interrupt status to 
be set. *
"

 From the spec, the latter one (Close by interrupt) probably needs some 
interaction with Thread, and the Thread's interrupt() method is defined as:

"If this thread is blocked in an invocation of the wait(), ..., then its 
interrupt status will be cleared and it will receive an 
InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible 
channel then the channel will be closed, the thread's interrupt status 
will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt 
status will be set and it will return immediately from the selection 
operation, possibly with a non-zero value, just as if the selector's 
wakeup method were invoked.
"

Seems Thread's implementation must be aware of what operation it is 
blocking on. So I propose the following solution:

1. Add this method to kernel class Thread (in VMI): private void 
setInterruptAction(Runnable action);

2. Thread's default value of action is null. Thread's interrupt() method 
will check the action field at first, if it is not null, execute the 
action at first, and then perform the normal interruption.

3. If some interruptible blocking I/O operation is going to happen, then 
invoke this method to current thread like below (probably by reflection):

Thread.currentThread().setInterruptAction(new Runnable(){
    public void run(){
       interrupted = true;
       AbstractInterruptibleChannel.this.close();
    }
});

4. It is the AbstractInterruptibleChannel's responsibility to reset 
Thread's interrupt action to null after the blocking I/O operation.

5. The interruption of select operation can be implemented in same way, 
all difference is another Runnable inner class, which invokes selector's 
wake-up.

comments? ideas?

-- 
Paulex Yang
China Software Development Lab
IBM



---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Mime
View raw message