2011-11-09 15:25:14 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright 2009 Red Hat, Inc.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Red Hat licenses this file to you under the Apache License, version 2.0
|
|
|
|
|
|
* (the "License"); you may not use this file except in compliance with the
|
|
|
|
|
|
* License. You may obtain a copy of the License at:
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
|
* under the License.
|
|
|
|
|
|
*/
|
2012-05-20 15:56:52 +02:00
|
|
|
|
package akka.util.internal;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Set;
|
2013-01-22 15:11:49 +01:00
|
|
|
|
import java.util.Iterator;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
import java.util.concurrent.ThreadFactory;
|
2013-01-22 15:11:49 +01:00
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
|
|
|
2013-03-05 16:19:54 +01:00
|
|
|
|
import akka.dispatch.sysmsg.SystemMessage;
|
2012-12-17 20:44:59 +01:00
|
|
|
|
import akka.util.Helpers;
|
2012-09-21 14:50:06 +02:00
|
|
|
|
import scala.concurrent.duration.Duration;
|
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration;
|
2012-08-06 13:44:07 +02:00
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
|
import akka.util.Unsafe;
|
|
|
|
|
|
|
2011-11-09 15:25:14 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* A {@link Timer} optimized for approximated I/O timeout scheduling.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <h3>Tick Duration</h3>
|
|
|
|
|
|
*
|
|
|
|
|
|
* As described with 'approximated', this timer does not execute the scheduled
|
2012-05-20 15:56:52 +02:00
|
|
|
|
* {@link TimerTask} on time. {@link HashedWheelTimer}, on every tick, will
|
2011-11-09 15:25:14 +01:00
|
|
|
|
* check if there are any {@link TimerTask}s behind the schedule and execute
|
|
|
|
|
|
* them.
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
* You can increase or decrease the accuracy of the execution timing by
|
|
|
|
|
|
* specifying smaller or larger tick duration in the constructor. In most
|
|
|
|
|
|
* network applications, I/O timeout does not need to be accurate. Therefore,
|
|
|
|
|
|
* the default tick duration is 100 milliseconds and you will not need to try
|
|
|
|
|
|
* different configurations in most cases.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <h3>Ticks per Wheel (Wheel Size)</h3>
|
|
|
|
|
|
*
|
2012-05-20 15:56:52 +02:00
|
|
|
|
* {@link HashedWheelTimer} maintains a data structure called 'wheel'.
|
2011-11-09 15:25:14 +01:00
|
|
|
|
* To put simply, a wheel is a hash table of {@link TimerTask}s whose hash
|
|
|
|
|
|
* function is 'dead line of the task'. The default number of ticks per wheel
|
|
|
|
|
|
* (i.e. the size of the wheel) is 512. You could specify a larger value
|
|
|
|
|
|
* if you are going to schedule a lot of timeouts.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <h3>Do not create many instances.</h3>
|
|
|
|
|
|
*
|
2012-05-20 15:56:52 +02:00
|
|
|
|
* {@link HashedWheelTimer} creates a new thread whenever it is instantiated and
|
2011-11-09 15:25:14 +01:00
|
|
|
|
* started. Therefore, you should make sure to create only one instance and
|
|
|
|
|
|
* share it across your application. One of the common mistakes, that makes
|
|
|
|
|
|
* your application unresponsive, is to create a new instance in
|
|
|
|
|
|
* {@link ChannelPipelineFactory}, which results in the creation of a new thread
|
|
|
|
|
|
* for every connection.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <h3>Implementation Details</h3>
|
|
|
|
|
|
*
|
2012-05-20 15:56:52 +02:00
|
|
|
|
* {@link HashedWheelTimer} is based on
|
2011-11-09 15:25:14 +01:00
|
|
|
|
* <a href="http://cseweb.ucsd.edu/users/varghese/">George Varghese</a> and
|
|
|
|
|
|
* Tony Lauck's paper,
|
|
|
|
|
|
* <a href="http://cseweb.ucsd.edu/users/varghese/PAPERS/twheel.ps.Z">'Hashed
|
|
|
|
|
|
* and Hierarchical Timing Wheels: data structures to efficiently implement a
|
|
|
|
|
|
* timer facility'</a>. More comprehensive slides are located
|
|
|
|
|
|
* <a href="http://www.cse.wustl.edu/~cdgill/courses/cs6874/TimingWheels.ppt">here</a>.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
|
|
|
|
|
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
|
|
|
|
|
* @version $Rev: 2297 $, $Date: 2010-06-07 10:50:02 +0900 (Mon, 07 Jun 2010) $
|
2011-11-24 14:30:03 +01:00
|
|
|
|
*
|
|
|
|
|
|
* The original implementation has been slightly altered to fit the specific requirements of Akka.
|
2011-12-06 09:27:32 +01:00
|
|
|
|
*
|
|
|
|
|
|
* Specifically: it is required to throw an IllegalStateException if a job
|
|
|
|
|
|
* cannot be queued. If no such exception is thrown, the job must be executed
|
|
|
|
|
|
* (or returned upon stop()).
|
2011-11-09 15:25:14 +01:00
|
|
|
|
*/
|
2013-04-07 20:07:26 +02:00
|
|
|
|
@Deprecated
|
2011-11-09 15:25:14 +01:00
|
|
|
|
public class HashedWheelTimer implements Timer {
|
|
|
|
|
|
private final Worker worker = new Worker();
|
|
|
|
|
|
final Thread workerThread;
|
2011-12-03 21:17:22 +01:00
|
|
|
|
boolean shutdown = false;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
final long tickDuration;
|
|
|
|
|
|
final Set<HashedWheelTimeout>[] wheel;
|
|
|
|
|
|
final int mask;
|
|
|
|
|
|
final ReadWriteLock lock = new ReentrantReadWriteLock();
|
|
|
|
|
|
volatile int wheelCursor;
|
2011-11-10 16:31:50 +01:00
|
|
|
|
private LoggingAdapter logger;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new timer.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param threadFactory a {@link java.util.concurrent.ThreadFactory} that creates a
|
|
|
|
|
|
* background {@link Thread} which is dedicated to
|
|
|
|
|
|
* {@link TimerTask} execution.
|
2011-11-23 11:07:16 +01:00
|
|
|
|
* @param duration the duration between ticks
|
2011-11-09 15:25:14 +01:00
|
|
|
|
* @param ticksPerWheel the size of the wheel
|
|
|
|
|
|
*/
|
|
|
|
|
|
public HashedWheelTimer(
|
2011-11-10 16:31:50 +01:00
|
|
|
|
LoggingAdapter logger,
|
2011-11-09 15:25:14 +01:00
|
|
|
|
ThreadFactory threadFactory,
|
2011-11-23 11:07:16 +01:00
|
|
|
|
Duration duration,
|
|
|
|
|
|
int ticksPerWheel) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
if (threadFactory == null) {
|
|
|
|
|
|
throw new NullPointerException("threadFactory");
|
|
|
|
|
|
}
|
2011-11-23 11:07:16 +01:00
|
|
|
|
if (duration == null) {
|
|
|
|
|
|
throw new NullPointerException("duration");
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
2011-11-23 11:07:16 +01:00
|
|
|
|
if (duration.toNanos() <= 0) {
|
2011-11-24 14:30:03 +01:00
|
|
|
|
throw new IllegalArgumentException("duration must be greater than 0 ns: " + duration.toNanos());
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (ticksPerWheel <= 0) {
|
2011-11-24 14:30:03 +01:00
|
|
|
|
throw new IllegalArgumentException("ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-10 16:31:50 +01:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
2011-11-09 15:25:14 +01:00
|
|
|
|
// Normalize ticksPerWheel to power of two and initialize the wheel.
|
|
|
|
|
|
wheel = createWheel(ticksPerWheel);
|
|
|
|
|
|
mask = wheel.length - 1;
|
|
|
|
|
|
|
2011-11-24 14:30:03 +01:00
|
|
|
|
// Convert to standardized tickDuration
|
|
|
|
|
|
this.tickDuration = duration.toNanos();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
// Prevent overflow.
|
2011-11-24 14:30:03 +01:00
|
|
|
|
if (tickDuration == Long.MAX_VALUE || tickDuration >= Long.MAX_VALUE / wheel.length) {
|
|
|
|
|
|
throw new IllegalArgumentException("tickDuration is too long: " + tickDuration + ' ' + duration.unit());
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-10 16:31:50 +01:00
|
|
|
|
workerThread = threadFactory.newThread(worker);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private static Set<HashedWheelTimeout>[] createWheel(final int ticksPerWheel) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
if (ticksPerWheel <= 0) {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
throw new IllegalArgumentException("ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (ticksPerWheel > 1073741824) {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
throw new IllegalArgumentException("ticksPerWheel may not be greater than 2^30: " + ticksPerWheel);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
final Set<HashedWheelTimeout>[] wheel = new Set[normalizeTicksPerWheel(ticksPerWheel)];
|
2011-11-09 15:25:14 +01:00
|
|
|
|
for (int i = 0; i < wheel.length; i ++) {
|
2013-01-22 15:11:49 +01:00
|
|
|
|
wheel[i] = Collections.newSetFromMap(new ConcurrentHashMap<HashedWheelTimeout, Boolean>(16, 0.95f, 4));
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
return wheel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int normalizeTicksPerWheel(int ticksPerWheel) {
|
|
|
|
|
|
int normalizedTicksPerWheel = 1;
|
|
|
|
|
|
while (normalizedTicksPerWheel < ticksPerWheel) {
|
|
|
|
|
|
normalizedTicksPerWheel <<= 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return normalizedTicksPerWheel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Starts the background thread explicitly. The background thread will
|
|
|
|
|
|
* start automatically on demand even if you did not call this method.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws IllegalStateException if this timer has been
|
|
|
|
|
|
* {@linkplain #stop() stopped} already
|
|
|
|
|
|
*/
|
|
|
|
|
|
public synchronized void start() {
|
2011-12-03 21:17:22 +01:00
|
|
|
|
lock.readLock().lock();
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (shutdown) {
|
|
|
|
|
|
throw new IllegalStateException("cannot be started once stopped");
|
|
|
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
2011-12-03 21:17:22 +01:00
|
|
|
|
if (!workerThread.isAlive()) {
|
|
|
|
|
|
workerThread.start();
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
lock.readLock().unlock();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized Set<Timeout> stop() {
|
|
|
|
|
|
if (Thread.currentThread() == workerThread) {
|
|
|
|
|
|
throw new IllegalStateException(
|
|
|
|
|
|
HashedWheelTimer.class.getSimpleName() +
|
|
|
|
|
|
".stop() cannot be called from " +
|
|
|
|
|
|
TimerTask.class.getSimpleName());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-03 21:17:22 +01:00
|
|
|
|
lock.writeLock().lock();
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (shutdown) {
|
|
|
|
|
|
return Collections.emptySet();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
shutdown = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
lock.writeLock().unlock();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boolean interrupted = false;
|
|
|
|
|
|
while (workerThread.isAlive()) {
|
|
|
|
|
|
workerThread.interrupt();
|
|
|
|
|
|
try {
|
|
|
|
|
|
workerThread.join(100);
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
interrupted = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (interrupted) {
|
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
|
|
|
|
|
|
for (Set<HashedWheelTimeout> bucket: wheel) {
|
|
|
|
|
|
unprocessedTimeouts.addAll(bucket);
|
|
|
|
|
|
bucket.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Collections.unmodifiableSet(unprocessedTimeouts);
|
|
|
|
|
|
}
|
2011-12-03 21:17:22 +01:00
|
|
|
|
|
|
|
|
|
|
public HashedWheelTimeout createTimeout(TimerTask task, long time) {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
return new HashedWheelTimeout(this, task, time);
|
2011-12-03 21:17:22 +01:00
|
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
2012-09-18 09:58:30 +02:00
|
|
|
|
public Timeout newTimeout(TimerTask task, FiniteDuration delay) {
|
2011-11-24 14:36:13 +01:00
|
|
|
|
final long currentTime = System.nanoTime();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
if (task == null) {
|
|
|
|
|
|
throw new NullPointerException("task");
|
|
|
|
|
|
}
|
2011-11-23 11:07:16 +01:00
|
|
|
|
if (delay == null) {
|
|
|
|
|
|
throw new NullPointerException("delay");
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!workerThread.isAlive()) {
|
|
|
|
|
|
start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-03 21:17:22 +01:00
|
|
|
|
HashedWheelTimeout timeout = createTimeout(task, currentTime + delay.toNanos());
|
2011-11-24 14:30:03 +01:00
|
|
|
|
scheduleTimeout(timeout, delay.toNanos());
|
2011-11-09 15:25:14 +01:00
|
|
|
|
return timeout;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void scheduleTimeout(HashedWheelTimeout timeout, long delay) {
|
|
|
|
|
|
// Prepare the required parameters to schedule the timeout object.
|
2012-11-14 16:19:54 +01:00
|
|
|
|
long relativeIndex = (delay + tickDuration - 1) / tickDuration;
|
|
|
|
|
|
// if the previous line had an overflow going on, then we’ll just schedule this timeout
|
|
|
|
|
|
// one tick early; that shouldn’t matter since we’re talking 270 years here
|
|
|
|
|
|
if (relativeIndex < 0) relativeIndex = delay / tickDuration;
|
|
|
|
|
|
if (relativeIndex == 0) relativeIndex = 1;
|
2013-01-10 10:08:23 -08:00
|
|
|
|
// if an integral number of wheel rotations, schedule one tick earlier
|
|
|
|
|
|
if ((relativeIndex & mask) == 0) relativeIndex--;
|
2012-08-06 15:05:01 +02:00
|
|
|
|
final long remainingRounds = relativeIndex / wheel.length;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
// Add the timeout to the wheel.
|
|
|
|
|
|
lock.readLock().lock();
|
|
|
|
|
|
try {
|
2011-12-03 21:17:22 +01:00
|
|
|
|
if (shutdown) throw new IllegalStateException("cannot enqueue after shutdown");
|
2012-08-06 13:44:07 +02:00
|
|
|
|
final int stopIndex = (int) ((wheelCursor + relativeIndex) & mask);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
timeout.stopIndex = stopIndex;
|
|
|
|
|
|
timeout.remainingRounds = remainingRounds;
|
|
|
|
|
|
wheel[stopIndex].add(timeout);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
lock.readLock().unlock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final class Worker implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
private long startTime;
|
|
|
|
|
|
private long tick;
|
|
|
|
|
|
|
|
|
|
|
|
Worker() {
|
|
|
|
|
|
super();
|
|
|
|
|
|
}
|
2011-12-03 21:17:22 +01:00
|
|
|
|
|
|
|
|
|
|
private boolean shutdown() {
|
|
|
|
|
|
lock.readLock().lock();
|
|
|
|
|
|
try {
|
|
|
|
|
|
return shutdown;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
lock.readLock().unlock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
public void run() {
|
2011-11-24 14:36:13 +01:00
|
|
|
|
startTime = System.nanoTime();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
tick = 1;
|
|
|
|
|
|
|
2011-12-03 21:17:22 +01:00
|
|
|
|
while (!shutdown()) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
final long deadline = waitForNextTick();
|
2012-11-14 16:19:54 +01:00
|
|
|
|
if (deadline > Long.MIN_VALUE)
|
2012-08-06 13:44:07 +02:00
|
|
|
|
notifyExpiredTimeouts(fetchExpiredTimeouts(deadline));
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private ArrayList<HashedWheelTimeout> fetchExpiredTimeouts(long deadline) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
// Find the expired timeouts and decrease the round counter
|
|
|
|
|
|
// if necessary. Note that we don't send the notification
|
|
|
|
|
|
// immediately to make sure the listeners are called without
|
|
|
|
|
|
// an exclusive lock.
|
|
|
|
|
|
lock.writeLock().lock();
|
|
|
|
|
|
try {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
final int newWheelCursor = wheelCursor = wheelCursor + 1 & mask;
|
2013-01-22 15:11:49 +01:00
|
|
|
|
return fetchExpiredTimeouts(wheel[newWheelCursor], deadline);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
} finally {
|
|
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-22 15:11:49 +01:00
|
|
|
|
private ArrayList<HashedWheelTimeout> fetchExpiredTimeouts(final Iterable<HashedWheelTimeout> it, final long deadline) {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
final ArrayList<HashedWheelTimeout> expiredTimeouts = new ArrayList<HashedWheelTimeout>();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
List<HashedWheelTimeout> slipped = null;
|
2013-01-22 15:11:49 +01:00
|
|
|
|
Iterator<HashedWheelTimeout> i = it.iterator();
|
2011-11-09 15:25:14 +01:00
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
|
HashedWheelTimeout timeout = i.next();
|
|
|
|
|
|
if (timeout.remainingRounds <= 0) {
|
|
|
|
|
|
i.remove();
|
2012-11-14 16:19:54 +01:00
|
|
|
|
if (timeout.deadline - deadline <= 0) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
expiredTimeouts.add(timeout);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Handle the case where the timeout is put into a wrong
|
|
|
|
|
|
// place, usually one tick earlier. For now, just add
|
|
|
|
|
|
// it to a temporary list - we will reschedule it in a
|
|
|
|
|
|
// separate loop.
|
2012-08-06 13:44:07 +02:00
|
|
|
|
if (slipped == null)
|
2011-11-09 15:25:14 +01:00
|
|
|
|
slipped = new ArrayList<HashedWheelTimeout>();
|
2012-08-06 13:44:07 +02:00
|
|
|
|
|
2011-11-09 15:25:14 +01:00
|
|
|
|
slipped.add(timeout);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
timeout.remainingRounds -= 1;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reschedule the slipped timeouts.
|
|
|
|
|
|
if (slipped != null) {
|
|
|
|
|
|
for (HashedWheelTimeout timeout: slipped) {
|
|
|
|
|
|
scheduleTimeout(timeout, timeout.deadline - deadline);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-08-06 13:44:07 +02:00
|
|
|
|
return expiredTimeouts;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private void notifyExpiredTimeouts(ArrayList<HashedWheelTimeout> expiredTimeouts) {
|
2011-11-09 15:25:14 +01:00
|
|
|
|
// Notify the expired timeouts.
|
|
|
|
|
|
for (int i = expiredTimeouts.size() - 1; i >= 0; i --) {
|
|
|
|
|
|
expiredTimeouts.get(i).expire();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clean up the temporary list.
|
|
|
|
|
|
expiredTimeouts.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-14 16:19:54 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* calculate goal nanoTime from startTime and current tick number,
|
|
|
|
|
|
* then wait until that goal has been reached.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return Long.MIN_VALUE if received a shutdown request, current time otherwise (with Long.MIN_VALUE changed by +1)
|
|
|
|
|
|
*/
|
2011-11-09 15:25:14 +01:00
|
|
|
|
private long waitForNextTick() {
|
|
|
|
|
|
long deadline = startTime + tickDuration * tick;
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
2011-11-24 14:36:13 +01:00
|
|
|
|
final long currentTime = System.nanoTime();
|
2012-05-29 01:43:39 +02:00
|
|
|
|
|
2012-08-06 15:05:01 +02:00
|
|
|
|
long sleepTimeMs = (deadline - currentTime + 999999) / 1000000;
|
|
|
|
|
|
|
|
|
|
|
|
if (sleepTimeMs <= 0) {
|
|
|
|
|
|
tick += 1;
|
2012-11-14 16:19:54 +01:00
|
|
|
|
if (currentTime == Long.MIN_VALUE) return -Long.MAX_VALUE;
|
|
|
|
|
|
else return currentTime;
|
2012-08-06 15:05:01 +02:00
|
|
|
|
}
|
2012-05-29 01:43:39 +02:00
|
|
|
|
|
|
|
|
|
|
// Check if we run on windows, as if thats the case we will need
|
|
|
|
|
|
// to round the sleepTime as workaround for a bug that only affect
|
|
|
|
|
|
// the JVM if it runs on windows.
|
|
|
|
|
|
//
|
|
|
|
|
|
// See https://github.com/netty/netty/issues/356
|
2012-12-17 20:44:59 +01:00
|
|
|
|
if (Helpers.isWindows()) {
|
2012-08-06 15:05:01 +02:00
|
|
|
|
sleepTimeMs = (sleepTimeMs / 10) * 10;
|
2012-05-29 01:43:39 +02:00
|
|
|
|
}
|
2012-08-06 15:05:01 +02:00
|
|
|
|
|
2011-11-09 15:25:14 +01:00
|
|
|
|
try {
|
2012-08-06 15:05:01 +02:00
|
|
|
|
Thread.sleep(sleepTimeMs);
|
2011-11-09 15:25:14 +01:00
|
|
|
|
} catch (InterruptedException e) {
|
2011-12-03 21:17:22 +01:00
|
|
|
|
if (shutdown()) {
|
2012-11-14 16:19:54 +01:00
|
|
|
|
return Long.MIN_VALUE;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private static final class HashedWheelTimeout implements Timeout {
|
|
|
|
|
|
private static final long _stateOffset;
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
|
try {
|
|
|
|
|
|
_stateOffset = Unsafe.instance.objectFieldOffset(HashedWheelTimeout.class.getDeclaredField("_state"));
|
|
|
|
|
|
} catch(Throwable t){
|
|
|
|
|
|
throw new ExceptionInInitializerError(t);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
private static final int ST_INIT = 0;
|
|
|
|
|
|
private static final int ST_CANCELLED = 1;
|
|
|
|
|
|
private static final int ST_EXPIRED = 2;
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private final HashedWheelTimer parent;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
private final TimerTask task;
|
|
|
|
|
|
final long deadline;
|
|
|
|
|
|
volatile int stopIndex;
|
|
|
|
|
|
volatile long remainingRounds;
|
2012-08-06 13:44:07 +02:00
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
|
private volatile int _state = ST_INIT;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
HashedWheelTimeout(HashedWheelTimer parent, TimerTask task, long deadline) {
|
|
|
|
|
|
this.parent = parent;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
this.task = task;
|
|
|
|
|
|
this.deadline = deadline;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Timer getTimer() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
return parent;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TimerTask getTask() {
|
|
|
|
|
|
return task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-06 13:44:07 +02:00
|
|
|
|
private final int state() {
|
|
|
|
|
|
return Unsafe.instance.getIntVolatile(this, _stateOffset);
|
|
|
|
|
|
}
|
|
|
|
|
|
private final boolean updateState(int old, int future) {
|
|
|
|
|
|
return Unsafe.instance.compareAndSwapInt(this, _stateOffset, old, future);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-14 23:21:51 +01:00
|
|
|
|
public boolean cancel() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
if (updateState(ST_INIT, ST_CANCELLED)) {
|
|
|
|
|
|
parent.wheel[stopIndex].remove(this);
|
2013-01-14 23:21:51 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
} else return false;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isCancelled() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
return state() == ST_CANCELLED;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isExpired() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
return state() != ST_INIT;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void expire() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
if (updateState(ST_INIT, ST_EXPIRED)) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
task.run(this);
|
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
|
parent.logger.warning(
|
|
|
|
|
|
"An exception was thrown by " +
|
|
|
|
|
|
TimerTask.class.getSimpleName() + ".", t);
|
|
|
|
|
|
}
|
2011-11-09 15:25:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-22 15:11:49 +01:00
|
|
|
|
@Override public final int hashCode() {
|
|
|
|
|
|
return System.identityHashCode(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override public final boolean equals(final Object that) {
|
|
|
|
|
|
return this == that;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-09 15:25:14 +01:00
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
2012-08-06 13:44:07 +02:00
|
|
|
|
final long currentTime = System.nanoTime();
|
|
|
|
|
|
final long remaining = deadline - currentTime;
|
2011-11-09 15:25:14 +01:00
|
|
|
|
|
|
|
|
|
|
StringBuilder buf = new StringBuilder(192);
|
2012-08-06 13:44:07 +02:00
|
|
|
|
buf.append("HashedWheelTimeout");
|
2011-11-09 15:25:14 +01:00
|
|
|
|
buf.append('(');
|
|
|
|
|
|
|
|
|
|
|
|
buf.append("deadline: ");
|
|
|
|
|
|
if (remaining > 0) {
|
|
|
|
|
|
buf.append(remaining);
|
2011-12-09 12:16:13 +01:00
|
|
|
|
buf.append(" ns later, ");
|
2011-11-09 15:25:14 +01:00
|
|
|
|
} else if (remaining < 0) {
|
|
|
|
|
|
buf.append(-remaining);
|
2011-12-09 12:16:13 +01:00
|
|
|
|
buf.append(" ns ago, ");
|
2011-11-09 15:25:14 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
buf.append("now, ");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isCancelled()) {
|
|
|
|
|
|
buf.append (", cancelled");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return buf.append(')').toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-11-24 14:30:03 +01:00
|
|
|
|
|