Top Description Fields Constructors Methods
org.glassfish.grizzly.nio

public final Class SelectorRunner

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
java.io.IOException, java.nio.channels.CancelledKeyException, .ClosedSelectorException, .SelectableChannel, .SelectionKey, .Selector, java.util.ArrayDeque, .ConcurrentModificationException, .Iterator, .Map, .Queue, .Set, .WeakHashMap, java.util.concurrent.ConcurrentLinkedQueue, .TimeUnit, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, .AtomicReference, java.util.logging.Level, .Logger, org.glassfish.grizzly.Connection, .Grizzly, .IOEvent, .IOStrategy, .Transport.State, org.glassfish.grizzly.localization.LogMessages, org.glassfish.grizzly.threadpool.Threads, org.glassfish.grizzly.utils.StateHolder

Class is responsible for processing certain (single) SelectorHandler
Author
Alexey Stashok

Field Summary

Modifier and TypeField and Description
private Queue<SelectorHandlerTask>
private volatile int
private int
private final Queue<SelectorHandlerTask>
pack-priv volatile boolean
private boolean
private Iterator<SelectionKey>
private SelectionKey
private int
private int
private long
lastSpinTimestamp

Selector spin discovering

private static final Logger
private final Queue<SelectorHandlerTask>
private final Queue<SelectorHandlerTask>
private Set<SelectionKey>
private final AtomicInteger
private Selector
private Thread
private final AtomicBoolean
private final Map<Selector, Long>
private final AtomicReference<Transport.State>
private static final String
private final NIOTransport

Constructor Summary

AccessConstructor and Description
private
SelectorRunner(final NIOTransport transport, final Selector selector)

Method Summary

Modifier and TypeMethod and Description
private void
pack-priv void
private void
addThreadNameMarker(final Thread currentThread)

pack-priv SelectionKey
pack-priv void
checkSelectorSpin(final boolean hasSelectedKeys, final int spinRateThreshold)

public static SelectorRunner
create(final NIOTransport transport)

protected boolean
doSelect()

This method handle the processing of all Selector's interest op (OP_ACCEPT,OP_READ,OP_WRITE,OP_CONNECT) by delegating to its Handler.

private void
dropConnectionDueToException(final SelectionKey
SelectionKey, which was processed, when the Exception occurred
key
,
final String
error description
description
,
final Exception
Exception occurred
e
,
final Level
logger Level to use, if transport is in running state
runLogLevel
,
final Level
logger Level to use, if transport is not in running state
stoppedLogLevel
)

Notify transport about the Exception happened, log it and cancel the SelectionKey, if it is not null

public int

Returns:

number of SelectionKeys, which were selected last time.
getLastSelectedKeysCount
()

Number of SelectionKeys, which were selected last time.

public Queue<SelectorHandlerTask>
public Queue<SelectorHandlerTask>
public Thread
public Selector
public Transport.State
public NIOTransport
pack-priv int

Returns:

current spin rate (emptyspins/sec).
incSpinCounter
()

Increments spin counter and returns current spin rate (emptyspins/sec).

private boolean
pack-priv boolean
private boolean
private boolean
public Queue<SelectorHandlerTask>
public void
private void
removeThreadNameMarker(final Thread currentThread)

pack-priv void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

private void
setRunnerThread(final Thread runnerThread)

pack-priv void
setSelector(final Selector
the new Selector to be associated with this SelectorRunner.
selector
)

Sets the Selector, associated with the runner.

private void
public synchronized void
public synchronized void
stop()

protected void
private void
pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentPostponedTasksback to summary
private Queue<SelectorHandlerTask> currentPostponedTasks
dumbVolatileback to summary
private volatile int dumbVolatile
emptySpinCounterback to summary
private int emptySpinCounter
evenPostponedTasksback to summary
private final Queue<SelectorHandlerTask> evenPostponedTasks
hasPendingTasksback to summary
pack-priv volatile boolean hasPendingTasks
isResumeback to summary
private boolean isResume
iteratorback to summary
private Iterator<SelectionKey> iterator
keyback to summary
private SelectionKey key
keyReadyOpsback to summary
private int keyReadyOps
lastSelectedKeysCountback to summary
private int lastSelectedKeysCount
lastSpinTimestampback to summary
private long lastSpinTimestamp

Selector spin discovering

LOGGERback to summary
private static final Logger LOGGER
oddPostponedTasksback to summary
private final Queue<SelectorHandlerTask> oddPostponedTasks
pendingTasksback to summary
private final Queue<SelectorHandlerTask> pendingTasks
readyKeySetback to summary
private Set<SelectionKey> readyKeySet
runnerThreadActivityCounterback to summary
private final AtomicInteger runnerThreadActivityCounter
selectorback to summary
private Selector selector
selectorRunnerThreadback to summary
private Thread selectorRunnerThread
selectorWakeupFlagback to summary
private final AtomicBoolean selectorWakeupFlag
spinnedSelectorsHistoryback to summary
private final Map<Selector, Long> spinnedSelectorsHistory
stateHolderback to summary
private final AtomicReference<Transport.State> stateHolder
THREAD_MARKERback to summary
private static final String THREAD_MARKER
transportback to summary
private final NIOTransport transport

Constructor Detail

SelectorRunnerback to summary
private SelectorRunner(final NIOTransport transport, final Selector selector)

Method Detail

abortTasksInQueueback to summary
private void abortTasksInQueue(final Queue<SelectorHandlerTask> taskQueue)
addPendingTaskback to summary
pack-priv void addPendingTask(final SelectorHandlerTask task)
addThreadNameMarkerback to summary
private void addThreadNameMarker(final Thread currentThread)
checkIfSpinnedKeyback to summary
pack-priv SelectionKey checkIfSpinnedKey(final SelectionKey key)
checkSelectorSpinback to summary
pack-priv void checkSelectorSpin(final boolean hasSelectedKeys, final int spinRateThreshold) throws IOException
createback to summary
public static SelectorRunner create(final NIOTransport transport) throws IOException
doSelectback to summary
protected boolean doSelect()

This method handle the processing of all Selector's interest op (OP_ACCEPT,OP_READ,OP_WRITE,OP_CONNECT) by delegating to its Handler. By default, all java.nio.channels.Selector operations are implemented using SelectorHandler. All SelectionKey operations are implemented by SelectionKeyHandler. Finally, ProtocolChain creation/re-use are implemented by InstanceHandler.

dropConnectionDueToExceptionback to summary
private void dropConnectionDueToException(final SelectionKey key, final String description, final Exception e, final Level runLogLevel, final Level stoppedLogLevel)

Notify transport about the Exception happened, log it and cancel the SelectionKey, if it is not null

Parameters
key:SelectionKey

SelectionKey, which was processed, when the Exception occurred

description:String

error description

e:Exception

Exception occurred

runLogLevel:Level

logger Level to use, if transport is in running state

stoppedLogLevel:Level

logger Level to use, if transport is not in running state

getLastSelectedKeysCountback to summary
public int getLastSelectedKeysCount()

Number of SelectionKeys, which were selected last time. Operation is not thread-safe.

Returns:int

number of SelectionKeys, which were selected last time.

getPendingTasksback to summary
public Queue<SelectorHandlerTask> getPendingTasks()
getPostponedTasksback to summary
public Queue<SelectorHandlerTask> getPostponedTasks()
getRunnerThreadback to summary
public Thread getRunnerThread()
getSelectorback to summary
public Selector getSelector()
getStateback to summary
public Transport.State getState()
getTransportback to summary
public NIOTransport getTransport()
incSpinCounterback to summary
pack-priv int incSpinCounter()

Increments spin counter and returns current spin rate (emptyspins/sec).

Returns:int

current spin rate (emptyspins/sec).

isRunningback to summary
private boolean isRunning()
isStopback to summary
pack-priv boolean isStop()
iterateKeyEventsback to summary
private boolean iterateKeyEvents() throws IOException
iterateKeysback to summary
private boolean iterateKeys()
obtainPostponedTasksback to summary
public Queue<SelectorHandlerTask> obtainPostponedTasks()
postponeback to summary
public void postpone()
removeThreadNameMarkerback to summary
private void removeThreadNameMarker(final Thread currentThread)
resetSpinCounterback to summary
pack-priv void resetSpinCounter()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
setRunnerThreadback to summary
private void setRunnerThread(final Thread runnerThread)
setSelectorback to summary
pack-priv void setSelector(final Selector selector)

Sets the Selector, associated with the runner. The method should be called from the runner thread.

Parameters
selector:Selector

the new Selector to be associated with this SelectorRunner.

shutdownSelectorback to summary
private void shutdownSelector()
startback to summary
public synchronized void start()
stopback to summary
public synchronized void stop()
switchToNewSelectorback to summary
protected void switchToNewSelector() throws IOException
wakeupSelectorback to summary
private void wakeupSelector()
workaroundSelectorSpinback to summary
pack-priv void workaroundSelectorSpin() throws IOException