2009-01-19 16:05:04 +01:00
|
|
|
/*
|
|
|
|
* JBoss, Home of Professional Open Source
|
|
|
|
*
|
|
|
|
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
|
|
*/
|
2009-04-03 19:37:49 +02:00
|
|
|
package org.jboss.netty.util;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2009-01-20 13:14:29 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2009-01-19 16:05:04 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2009-01-20 13:17:56 +01:00
|
|
|
import java.util.concurrent.Executors;
|
2009-01-20 12:04:27 +01:00
|
|
|
import java.util.concurrent.ThreadFactory;
|
2009-01-19 16:05:04 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2009-01-20 12:04:27 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2009-01-20 12:35:04 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2009-01-19 16:05:04 +01:00
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
|
2009-01-20 05:39:11 +01:00
|
|
|
import org.jboss.netty.logging.InternalLogger;
|
|
|
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
2009-04-03 09:41:54 +02:00
|
|
|
import org.jboss.netty.util.internal.ConcurrentIdentityHashMap;
|
|
|
|
import org.jboss.netty.util.internal.MapBackedSet;
|
|
|
|
import org.jboss.netty.util.internal.ReusableIterator;
|
2009-01-20 05:37:26 +01:00
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
/**
|
2009-06-17 08:37:36 +02: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
|
|
|
|
* {@link TimerTask} on time. {@link HashedWheelTimer}, on every tick, will
|
|
|
|
* 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>
|
|
|
|
*
|
|
|
|
* {@link HashedWheelTimer} maintains a data structure called 'wheel'.
|
|
|
|
* 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>Implementation Details</h3>
|
|
|
|
*
|
|
|
|
* {@link HashedWheelTimer} is based on
|
|
|
|
* <a href="http://cseweb.ucsd.edu/users/varghese/>George Varghese</a> and
|
|
|
|
* Tony Lauck's paper,
|
|
|
|
* <a href="http://www-cse.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>.
|
|
|
|
*
|
2009-01-19 16:05:04 +01:00
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*/
|
|
|
|
public class HashedWheelTimer implements Timer {
|
|
|
|
|
2009-01-20 05:39:11 +01:00
|
|
|
static final InternalLogger logger =
|
|
|
|
InternalLoggerFactory.getInstance(HashedWheelTimer.class);
|
2009-01-20 12:35:04 +01:00
|
|
|
private static final AtomicInteger id = new AtomicInteger();
|
2009-01-20 05:39:11 +01:00
|
|
|
|
2009-02-13 14:28:35 +01:00
|
|
|
// I'd say 64 active timer threads are obvious misuse.
|
|
|
|
private static final int MISUSE_WARNING_THRESHOLD = 64;
|
2009-02-13 13:41:46 +01:00
|
|
|
private static final AtomicInteger activeInstances = new AtomicInteger();
|
|
|
|
private static final AtomicBoolean loggedMisuseWarning = new AtomicBoolean();
|
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
private final Worker worker = new Worker();
|
2009-01-20 13:53:41 +01:00
|
|
|
final Thread workerThread;
|
2009-01-20 12:04:27 +01:00
|
|
|
final AtomicBoolean shutdown = new AtomicBoolean();
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
private final long roundDuration;
|
2009-01-19 16:05:04 +01:00
|
|
|
final long tickDuration;
|
|
|
|
final Set<HashedWheelTimeout>[] wheel;
|
2009-01-20 08:57:45 +01:00
|
|
|
final ReusableIterator<HashedWheelTimeout>[] iterators;
|
2009-01-19 16:05:04 +01:00
|
|
|
final int mask;
|
|
|
|
final ReadWriteLock lock = new ReentrantReadWriteLock();
|
|
|
|
volatile int wheelCursor;
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer with the default thread factory
|
|
|
|
* ({@link Executors#defaultThreadFactory()}), default tick duration, and
|
|
|
|
* default number of ticks per wheel.
|
|
|
|
*/
|
2009-01-20 13:17:56 +01:00
|
|
|
public HashedWheelTimer() {
|
|
|
|
this(Executors.defaultThreadFactory());
|
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer with the default thread factory
|
|
|
|
* ({@link Executors#defaultThreadFactory()}) and default number of ticks
|
|
|
|
* per wheel.
|
|
|
|
*
|
|
|
|
* @param tickDuration the duration between tick
|
|
|
|
* @param unit the time unit of the {@code tickDuration}
|
|
|
|
*/
|
2009-02-13 05:29:56 +01:00
|
|
|
public HashedWheelTimer(long tickDuration, TimeUnit unit) {
|
|
|
|
this(Executors.defaultThreadFactory(), tickDuration, unit);
|
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer with the default thread factory
|
|
|
|
* ({@link Executors#defaultThreadFactory()}).
|
|
|
|
*
|
|
|
|
* @param tickDuration the duration between tick
|
|
|
|
* @param unit the time unit of the {@code tickDuration}
|
|
|
|
* @param ticksPerWheel the size of the wheel
|
|
|
|
*/
|
2009-02-13 05:29:56 +01:00
|
|
|
public HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel) {
|
2009-01-20 13:17:56 +01:00
|
|
|
this(Executors.defaultThreadFactory(), tickDuration, unit, ticksPerWheel);
|
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer with the default tick duration and default number of
|
|
|
|
* ticks per wheel.
|
|
|
|
*
|
|
|
|
* @param threadFactory a {@link ThreadFactory} that creates a
|
|
|
|
* background {@link Thread} which is dedicated to
|
|
|
|
* {@link TimerTask} execution.
|
|
|
|
*/
|
2009-01-20 13:18:50 +01:00
|
|
|
public HashedWheelTimer(ThreadFactory threadFactory) {
|
2009-02-13 05:29:56 +01:00
|
|
|
this(threadFactory, 100, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer with the default number of ticks per wheel.
|
|
|
|
*
|
|
|
|
* @param threadFactory a {@link ThreadFactory} that creates a
|
|
|
|
* background {@link Thread} which is dedicated to
|
|
|
|
* {@link TimerTask} execution.
|
|
|
|
* @param tickDuration the duration between tick
|
|
|
|
* @param unit the time unit of the {@code tickDuration}
|
|
|
|
*/
|
2009-02-13 05:29:56 +01:00
|
|
|
public HashedWheelTimer(
|
|
|
|
ThreadFactory threadFactory, long tickDuration, TimeUnit unit) {
|
|
|
|
this(threadFactory, tickDuration, unit, 512);
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* Creates a new timer.
|
|
|
|
*
|
|
|
|
* @param threadFactory a {@link ThreadFactory} that creates a
|
|
|
|
* background {@link Thread} which is dedicated to
|
|
|
|
* {@link TimerTask} execution.
|
|
|
|
* @param tickDuration the duration between tick
|
|
|
|
* @param unit the time unit of the {@code tickDuration}
|
|
|
|
* @param ticksPerWheel the size of the wheel
|
|
|
|
*/
|
2009-01-19 16:05:04 +01:00
|
|
|
public HashedWheelTimer(
|
2009-01-20 12:04:27 +01:00
|
|
|
ThreadFactory threadFactory,
|
2009-01-19 16:05:04 +01:00
|
|
|
long tickDuration, TimeUnit unit, int ticksPerWheel) {
|
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
if (threadFactory == null) {
|
|
|
|
throw new NullPointerException("threadFactory");
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
if (unit == null) {
|
|
|
|
throw new NullPointerException("unit");
|
|
|
|
}
|
|
|
|
if (tickDuration <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"tickDuration must be greater than 0: " + tickDuration);
|
|
|
|
}
|
2009-01-20 13:17:56 +01:00
|
|
|
if (ticksPerWheel <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
2009-01-20 13:32:20 +01:00
|
|
|
"ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
2009-01-20 13:17:56 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
// Normalize ticksPerWheel to power of two and initialize the wheel.
|
|
|
|
wheel = createWheel(ticksPerWheel);
|
2009-01-20 08:57:45 +01:00
|
|
|
iterators = createIterators(wheel);
|
2009-01-19 16:05:04 +01:00
|
|
|
mask = wheel.length - 1;
|
|
|
|
|
2009-02-09 12:04:28 +01:00
|
|
|
// Convert tickDuration to milliseconds.
|
2009-02-09 11:58:30 +01:00
|
|
|
this.tickDuration = tickDuration = unit.toMillis(tickDuration);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
// Prevent overflow.
|
|
|
|
if (tickDuration == Long.MAX_VALUE ||
|
|
|
|
tickDuration >= Long.MAX_VALUE / wheel.length) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"tickDuration is too long: " +
|
|
|
|
tickDuration + ' ' + unit);
|
|
|
|
}
|
|
|
|
|
2009-01-20 08:57:45 +01:00
|
|
|
roundDuration = tickDuration * wheel.length;
|
2009-01-20 12:04:27 +01:00
|
|
|
|
2009-01-20 12:35:04 +01:00
|
|
|
workerThread = threadFactory.newThread(new ThreadRenamingRunnable(
|
|
|
|
worker, "Hashed wheel timer #" + id.incrementAndGet()));
|
2009-02-13 13:41:46 +01:00
|
|
|
|
|
|
|
// Misuse check
|
|
|
|
int activeInstances = HashedWheelTimer.activeInstances.incrementAndGet();
|
|
|
|
if (activeInstances >= MISUSE_WARNING_THRESHOLD &&
|
|
|
|
loggedMisuseWarning.compareAndSet(false, true)) {
|
2009-02-13 14:28:35 +01:00
|
|
|
logger.debug(
|
|
|
|
"There are too many active " +
|
|
|
|
HashedWheelTimer.class.getSimpleName() + " instances (" +
|
|
|
|
activeInstances + ") - you should share the small number " +
|
|
|
|
"of instances to avoid excessive resource consumption.");
|
2009-02-13 13:41:46 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Set<HashedWheelTimeout>[] createWheel(int ticksPerWheel) {
|
|
|
|
if (ticksPerWheel <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
|
|
|
}
|
|
|
|
if (ticksPerWheel > 1073741824) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"ticksPerWheel may not be greater than 2^30: " + ticksPerWheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticksPerWheel = normalizeTicksPerWheel(ticksPerWheel);
|
2009-01-20 08:57:45 +01:00
|
|
|
Set<HashedWheelTimeout>[] wheel = new Set[ticksPerWheel];
|
|
|
|
for (int i = 0; i < wheel.length; i ++) {
|
2009-01-20 12:04:27 +01:00
|
|
|
wheel[i] = new MapBackedSet<HashedWheelTimeout>(
|
|
|
|
new ConcurrentIdentityHashMap<HashedWheelTimeout, Boolean>(16, 0.95f, 4));
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
return wheel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static ReusableIterator<HashedWheelTimeout>[] createIterators(Set<HashedWheelTimeout>[] wheel) {
|
|
|
|
ReusableIterator<HashedWheelTimeout>[] iterators = new ReusableIterator[wheel.length];
|
|
|
|
for (int i = 0; i < wheel.length; i ++) {
|
|
|
|
iterators[i] = (ReusableIterator<HashedWheelTimeout>) wheel[i].iterator();
|
|
|
|
}
|
|
|
|
return iterators;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int normalizeTicksPerWheel(int ticksPerWheel) {
|
|
|
|
int normalizedTicksPerWheel = 1;
|
|
|
|
while (normalizedTicksPerWheel < ticksPerWheel) {
|
|
|
|
normalizedTicksPerWheel <<= 1;
|
|
|
|
}
|
|
|
|
return normalizedTicksPerWheel;
|
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-02-11 06:05:20 +01:00
|
|
|
public synchronized void start() {
|
|
|
|
if (shutdown.get()) {
|
|
|
|
throw new IllegalStateException("cannot be started once stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!workerThread.isAlive()) {
|
|
|
|
workerThread.start();
|
|
|
|
}
|
2009-01-20 12:04:27 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 06:05:20 +01:00
|
|
|
public synchronized Set<Timeout> stop() {
|
2009-01-20 12:04:27 +01:00
|
|
|
if (!shutdown.compareAndSet(false, true)) {
|
2009-01-20 13:14:29 +01:00
|
|
|
return Collections.emptySet();
|
2009-01-20 12:04:27 +01:00
|
|
|
}
|
2009-01-20 13:14:29 +01:00
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
while (workerThread.isAlive()) {
|
|
|
|
workerThread.interrupt();
|
|
|
|
try {
|
|
|
|
workerThread.join(100);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 13:14:29 +01:00
|
|
|
|
2009-02-13 13:41:46 +01:00
|
|
|
activeInstances.decrementAndGet();
|
|
|
|
|
2009-01-20 13:14:29 +01:00
|
|
|
Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
|
|
|
|
for (Set<HashedWheelTimeout> bucket: wheel) {
|
|
|
|
unprocessedTimeouts.addAll(bucket);
|
|
|
|
bucket.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Collections.unmodifiableSet(unprocessedTimeouts);
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
2009-02-02 04:42:05 +01:00
|
|
|
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
|
2009-02-09 11:58:30 +01:00
|
|
|
final long currentTime = System.currentTimeMillis();
|
2009-02-02 04:42:05 +01:00
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
if (task == null) {
|
|
|
|
throw new NullPointerException("task");
|
|
|
|
}
|
|
|
|
if (unit == null) {
|
|
|
|
throw new NullPointerException("unit");
|
|
|
|
}
|
|
|
|
|
2009-02-09 11:58:30 +01:00
|
|
|
delay = unit.toMillis(delay);
|
2009-02-08 07:57:40 +01:00
|
|
|
if (delay < tickDuration) {
|
|
|
|
delay = tickDuration;
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
if (!workerThread.isAlive()) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2009-02-02 04:42:05 +01:00
|
|
|
// Prepare the required parameters to create the timeout object.
|
2009-01-19 16:05:04 +01:00
|
|
|
HashedWheelTimeout timeout;
|
2009-02-02 04:42:05 +01:00
|
|
|
final long lastRoundDelay = delay % roundDuration;
|
|
|
|
final long lastTickDelay = delay % tickDuration;
|
|
|
|
final long relativeIndex =
|
|
|
|
lastRoundDelay / tickDuration + (lastTickDelay != 0? 1 : 0);
|
|
|
|
final long deadline = currentTime + delay;
|
|
|
|
|
|
|
|
final long remainingRounds =
|
|
|
|
delay / roundDuration - (delay % roundDuration == 0? 1 : 0);
|
|
|
|
|
|
|
|
// Add the timeout to the wheel.
|
2009-01-19 16:05:04 +01:00
|
|
|
lock.readLock().lock();
|
|
|
|
try {
|
2009-02-02 04:42:05 +01:00
|
|
|
timeout =
|
|
|
|
new HashedWheelTimeout(
|
|
|
|
task, deadline,
|
|
|
|
(int) (wheelCursor + relativeIndex & mask),
|
|
|
|
remainingRounds);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-02-02 04:42:05 +01:00
|
|
|
wheel[timeout.stopIndex].add(timeout);
|
2009-01-19 16:05:04 +01:00
|
|
|
} finally {
|
|
|
|
lock.readLock().unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class Worker implements Runnable {
|
|
|
|
|
2009-01-20 10:03:31 +01:00
|
|
|
private long startTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
private long tick;
|
|
|
|
|
|
|
|
Worker() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2009-01-20 12:07:30 +01:00
|
|
|
public void run() {
|
2009-01-19 16:05:04 +01:00
|
|
|
List<HashedWheelTimeout> expiredTimeouts =
|
|
|
|
new ArrayList<HashedWheelTimeout>();
|
|
|
|
|
2009-02-09 11:58:30 +01:00
|
|
|
startTime = System.currentTimeMillis();
|
2009-01-20 12:04:27 +01:00
|
|
|
tick = 1;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
while (!shutdown.get()) {
|
|
|
|
waitForNextTick();
|
|
|
|
fetchExpiredTimeouts(expiredTimeouts);
|
|
|
|
notifyExpiredTimeouts(expiredTimeouts);
|
2009-01-20 10:03:31 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
private void fetchExpiredTimeouts(
|
2009-01-19 16:05:04 +01:00
|
|
|
List<HashedWheelTimeout> expiredTimeouts) {
|
|
|
|
|
|
|
|
// 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 {
|
2009-01-20 08:57:45 +01:00
|
|
|
int oldBucketHead = wheelCursor;
|
|
|
|
int newBucketHead = oldBucketHead + 1 & mask;
|
2009-01-19 16:05:04 +01:00
|
|
|
wheelCursor = newBucketHead;
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
|
|
ReusableIterator<HashedWheelTimeout> i = iterators[oldBucketHead];
|
|
|
|
fetchExpiredTimeouts(expiredTimeouts, i);
|
|
|
|
} finally {
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchExpiredTimeouts(
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts,
|
2009-01-20 09:38:07 +01:00
|
|
|
ReusableIterator<HashedWheelTimeout> i) {
|
2009-01-20 08:57:45 +01:00
|
|
|
|
2009-06-05 19:05:42 +02:00
|
|
|
long currentDeadline = System.currentTimeMillis() + tickDuration;
|
2009-01-20 10:03:31 +01:00
|
|
|
i.rewind();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
HashedWheelTimeout timeout = i.next();
|
2009-02-11 06:05:20 +01:00
|
|
|
if (timeout.remainingRounds <= 0) {
|
2009-06-05 19:05:42 +02:00
|
|
|
if (timeout.deadline < currentDeadline) {
|
2009-02-11 06:05:20 +01:00
|
|
|
i.remove();
|
|
|
|
expiredTimeouts.add(timeout);
|
2009-01-20 10:03:31 +01:00
|
|
|
} else {
|
2009-02-11 06:05:20 +01:00
|
|
|
// A rare case where a timeout is put for the next
|
|
|
|
// round: just wait for the next round.
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
2009-02-11 06:05:20 +01:00
|
|
|
} else {
|
|
|
|
timeout.remainingRounds --;
|
2009-01-20 10:03:31 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyExpiredTimeouts(
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts) {
|
|
|
|
// Notify the expired timeouts.
|
|
|
|
for (int i = expiredTimeouts.size() - 1; i >= 0; i --) {
|
|
|
|
expiredTimeouts.get(i).expire();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the temporary list.
|
|
|
|
expiredTimeouts.clear();
|
|
|
|
}
|
|
|
|
|
2009-01-20 12:04:27 +01:00
|
|
|
private void waitForNextTick() {
|
2009-01-19 16:05:04 +01:00
|
|
|
for (;;) {
|
2009-02-09 11:58:30 +01:00
|
|
|
final long currentTime = System.currentTimeMillis();
|
2009-01-19 16:05:04 +01:00
|
|
|
final long sleepTime = tickDuration * tick - (currentTime - startTime);
|
|
|
|
|
|
|
|
if (sleepTime <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2009-02-09 12:29:17 +01:00
|
|
|
Thread.sleep(sleepTime);
|
2009-01-19 16:05:04 +01:00
|
|
|
} catch (InterruptedException e) {
|
2009-01-20 12:04:27 +01:00
|
|
|
if (shutdown.get()) {
|
|
|
|
return;
|
2009-01-20 08:57:45 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the tick if overflow is expected.
|
|
|
|
if (tickDuration * tick > Long.MAX_VALUE - tickDuration) {
|
2009-02-09 11:58:30 +01:00
|
|
|
startTime = System.currentTimeMillis();
|
2009-01-19 16:05:04 +01:00
|
|
|
tick = 1;
|
|
|
|
} else {
|
|
|
|
// Increase the tick if overflow is not likely to happen.
|
|
|
|
tick ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class HashedWheelTimeout implements Timeout {
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
private final TimerTask task;
|
2009-02-02 04:42:05 +01:00
|
|
|
final int stopIndex;
|
|
|
|
final long deadline;
|
|
|
|
volatile long remainingRounds;
|
2009-01-20 05:29:58 +01:00
|
|
|
private volatile boolean cancelled;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-02-02 04:42:05 +01:00
|
|
|
HashedWheelTimeout(
|
|
|
|
TimerTask task, long deadline, int stopIndex, long remainingRounds) {
|
2009-01-20 05:29:58 +01:00
|
|
|
this.task = task;
|
2009-02-02 04:42:05 +01:00
|
|
|
this.deadline = deadline;
|
|
|
|
this.stopIndex = stopIndex;
|
|
|
|
this.remainingRounds = remainingRounds;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
2009-06-17 08:37:36 +02:00
|
|
|
public Timer getTimer() {
|
|
|
|
return HashedWheelTimer.this;
|
|
|
|
}
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
public TimerTask getTask() {
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
2009-02-05 07:55:14 +01:00
|
|
|
if (isExpired()) {
|
2009-01-19 16:05:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-01-20 05:29:58 +01:00
|
|
|
|
2009-01-20 13:43:28 +01:00
|
|
|
cancelled = true;
|
|
|
|
|
|
|
|
// Might be called more than once, but doesn't matter.
|
2009-02-02 04:42:05 +01:00
|
|
|
wheel[stopIndex].remove(this);
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isCancelled() {
|
2009-01-20 05:29:58 +01:00
|
|
|
return cancelled;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isExpired() {
|
2009-02-09 11:58:30 +01:00
|
|
|
return cancelled || System.currentTimeMillis() > deadline;
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void expire() {
|
2009-01-20 05:29:58 +01:00
|
|
|
if (cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
try {
|
|
|
|
task.run(this);
|
|
|
|
} catch (Throwable t) {
|
2009-01-20 05:39:11 +01:00
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by " +
|
|
|
|
TimerTask.class.getSimpleName() + ".", t);
|
2009-01-20 05:29:58 +01:00
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2009-02-09 11:58:30 +01:00
|
|
|
long currentTime = System.currentTimeMillis();
|
2009-01-20 05:35:33 +01:00
|
|
|
long remaining = deadline - currentTime;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
2009-01-20 05:35:33 +01:00
|
|
|
StringBuilder buf = new StringBuilder(192);
|
|
|
|
buf.append(getClass().getSimpleName());
|
|
|
|
buf.append('(');
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
buf.append("deadline: ");
|
|
|
|
if (remaining > 0) {
|
2009-02-09 12:29:17 +01:00
|
|
|
buf.append(remaining);
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append(" ms later, ");
|
2009-01-19 16:05:04 +01:00
|
|
|
} else if (remaining < 0) {
|
2009-02-09 12:29:17 +01:00
|
|
|
buf.append(-remaining);
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append(" ms ago, ");
|
2009-01-19 16:05:04 +01:00
|
|
|
} else {
|
2009-01-20 05:29:58 +01:00
|
|
|
buf.append("now, ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCancelled()) {
|
|
|
|
buf.append (", cancelled");
|
2009-01-19 16:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.append(')').toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|