com.sun.jini.jeri.internal.runtime
Class SelectionManager

java.lang.Object
  extended by com.sun.jini.jeri.internal.runtime.SelectionManager

public final class SelectionManager
extends Object

A SelectionManager provides an event dispatching layer on top of the java.nio.Selector and java.nio.SelectableChannel abstractions; it manages one-shot registrations of interest in I/O readiness events and dispatching notifications of such events to registered callback objects. SelectionManager is designed to support multiple select/dispatch threads to allow for improved I/O concurrency on symmetric multiprocessor systems. If interest in a particular I/O event is (re-)registered while there is a blocking select operation in progress, that select operation must be woken up so that it can take the new interest into account; this wakeup is achieved by writing a byte to an internal pipe that is registered with the underlying selector. A current limitation of this API is that it does not allow an executing callback to yield control in such a way that it will be scheduled to execute again without another I/O readiness event occurring. Therefore, data that gets read during a callback must also get processed, unless such processing depends on more data that has not yet been read (like the remainder of a partial message).

This implementation uses the Logger named com.sun.jini.jeri.internal.runtime.SelectionManager to log information at the following levels:

Level Description
HANDLED I/O exception caught from select operation

Author:
Sun Microsystems, Inc.

Nested Class Summary
 class SelectionManager.Key
          A Key represents a given SelectableChannel's registration with this SelectionManager.
static interface SelectionManager.SelectionHandler
          SelectionHandler is the callback interface for an object that will process an I/O readiness event that has been detected by a SelectionManager.
private  class SelectionManager.SelectLoop
          SelectLoop provides the main loop for each I/O processing thread.
 
Field Summary
private static int concurrency
          number of concurrent I/O processing threads
private  Object lock
          lock guarding selectingThread, wakeupPending, renewQueue, readyQueue, renewMaskRef, and mutable state of all Key instances.
private static Logger logger
           
private  SelectionManager.Key readyQueue
          queue of keys that have I/O operations ready to be handled
private  Map registeredChannels
          set of registered channels, to detect duplicate registrations
private  int[] renewMaskRef
          holder used for pass-by-reference invocations
private  SelectionManager.Key renewQueue
          queue of keys that need to have interest masks updated
private  Thread selectingThread
          thread with exclusive right to perform a select operation, if any
private  Selector selector
          shared Selector used by this SelectionManager
private static Executor systemThreadPool
          pool of threads for executing tasks in system thread group
private  ByteBuffer wakeupBuffer
           
private  boolean wakeupPending
          true if a wakeup has been requested but not yet processed
private  SelectionKey wakeupPipeKey
           
private  Pipe.SinkChannel wakeupPipeSink
          internal pipe used to wake up a blocked select operation
private  Pipe.SourceChannel wakeupPipeSource
           
 
Constructor Summary
SelectionManager()
          Creates a new SelectionManager.
 
Method Summary
private  void addOrUpdateReadyQueue(SelectionManager.Key key, int newReadyMask)
           
private  void addOrUpdateRenewQueue(SelectionManager.Key key, int newRenewMask)
           
private  void drainWakeupPipe()
           
private  boolean isReadyQueueEmpty()
           
private  boolean isRenewQueueEmpty()
           
private  void processRenewQueue()
          In preparation for performing a select operation, process all new and renewed interest registrations so that current SelectionKey interest masks are up to date.
 SelectionManager.Key register(SelectableChannel channel, SelectionManager.SelectionHandler handler)
          Registers the given SelectableChannel with this SelectionManager.
private  SelectionManager.Key removeFromReadyQueue(int[] readyMaskOut)
           
private  SelectionManager.Key removeFromRenewQueue(int[] renewMaskOut)
           
private  SelectionManager.Key waitForReadyKey(int[] readyMaskOut)
          Waits until one of the registered channels is ready for one or more I/O operations.
private  void wakeupSelector()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

concurrency

private static final int concurrency
number of concurrent I/O processing threads

See Also:
Constant Field Values

logger

private static final Logger logger

systemThreadPool

private static final Executor systemThreadPool
pool of threads for executing tasks in system thread group


selector

private final Selector selector
shared Selector used by this SelectionManager


wakeupPipeSink

private final Pipe.SinkChannel wakeupPipeSink
internal pipe used to wake up a blocked select operation


wakeupPipeSource

private final Pipe.SourceChannel wakeupPipeSource

wakeupPipeKey

private final SelectionKey wakeupPipeKey

wakeupBuffer

private final ByteBuffer wakeupBuffer

registeredChannels

private final Map registeredChannels
set of registered channels, to detect duplicate registrations


lock

private final Object lock
lock guarding selectingThread, wakeupPending, renewQueue, readyQueue, renewMaskRef, and mutable state of all Key instances.


selectingThread

private Thread selectingThread
thread with exclusive right to perform a select operation, if any


wakeupPending

private boolean wakeupPending
true if a wakeup has been requested but not yet processed


renewQueue

private SelectionManager.Key renewQueue
queue of keys that need to have interest masks updated


readyQueue

private SelectionManager.Key readyQueue
queue of keys that have I/O operations ready to be handled


renewMaskRef

private final int[] renewMaskRef
holder used for pass-by-reference invocations

Constructor Detail

SelectionManager

public SelectionManager()
                 throws IOException
Creates a new SelectionManager. REMIND: Is this necessary, or should we just provide access to a singleton instance?

Throws:
IOException
Method Detail

register

public SelectionManager.Key register(SelectableChannel channel,
                                     SelectionManager.SelectionHandler handler)
Registers the given SelectableChannel with this SelectionManager. After registration, the returned Key's renewInterestMask method may be used to register one-shot interest in particular I/O events.


waitForReadyKey

private SelectionManager.Key waitForReadyKey(int[] readyMaskOut)
                                      throws InterruptedException
Waits until one of the registered channels is ready for one or more I/O operations. The Key for the ready channel is returned, and the first element of the supplied array is set to the mask of the channel's ready operations. If there is a ready channel available, then its key is returned. If another thread is already performing a select operation, then the current thread waits for that thread to complete and then begins again. Otherwise, the current thread assumes the responsibility of performing the next select operation.

Throws:
InterruptedException

wakeupSelector

private void wakeupSelector()

drainWakeupPipe

private void drainWakeupPipe()

processRenewQueue

private void processRenewQueue()
In preparation for performing a select operation, process all new and renewed interest registrations so that current SelectionKey interest masks are up to date. This method must not be invoked while there is a select operation in progress (because otherwise it could block indefinitely); therefore, it must be invoked only by a thread that has the exclusive right to perform a select operation.


isRenewQueueEmpty

private boolean isRenewQueueEmpty()

removeFromRenewQueue

private SelectionManager.Key removeFromRenewQueue(int[] renewMaskOut)

addOrUpdateRenewQueue

private void addOrUpdateRenewQueue(SelectionManager.Key key,
                                   int newRenewMask)

isReadyQueueEmpty

private boolean isReadyQueueEmpty()

removeFromReadyQueue

private SelectionManager.Key removeFromReadyQueue(int[] readyMaskOut)

addOrUpdateReadyQueue

private void addOrUpdateReadyQueue(SelectionManager.Key key,
                                   int newReadyMask)


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.