2009-01-19 16:05:04 +01:00
|
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
|
* Copyright 2012 The Netty Project
|
2009-01-19 16:05:04 +01:00
|
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
|
* The Netty Project 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:
|
2009-01-19 16:05:04 +01:00
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-19 16:05:04 +01:00
|
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
2009-01-19 16:05:04 +01:00
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
|
package io.netty.util;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
2013-01-11 06:03:27 +01:00
|
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2013-02-26 23:54:25 +01:00
|
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-05-15 10:10:54 +02:00
|
|
|
|
|
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;
|
2012-07-07 06:52:54 +02:00
|
|
|
|
import java.util.Iterator;
|
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;
|
2011-08-12 08:20:43 +02: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-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.
|
|
|
|
|
*
|
2010-06-07 03:50:02 +02:00
|
|
|
|
* <h3>Do not create many instances.</h3>
|
|
|
|
|
*
|
|
|
|
|
* {@link HashedWheelTimer} creates a new thread whenever it is instantiated and
|
|
|
|
|
* started. Therefore, you should make sure to create only one instance and
|
|
|
|
|
* share it across your application. One of the common mistakes, that makes
|
2011-12-28 11:44:04 +01:00
|
|
|
|
* your application unresponsive, is to create a new instance for every connection.
|
2010-06-07 03:50:02 +02:00
|
|
|
|
*
|
2009-06-17 08:37:36 +02:00
|
|
|
|
* <h3>Implementation Details</h3>
|
|
|
|
|
*
|
|
|
|
|
* {@link HashedWheelTimer} is based on
|
2009-10-30 01:04:48 +01:00
|
|
|
|
* <a href="http://cseweb.ucsd.edu/users/varghese/">George Varghese</a> and
|
2009-06-17 08:37:36 +02:00
|
|
|
|
* Tony Lauck's paper,
|
2009-10-30 01:04:48 +01:00
|
|
|
|
* <a href="http://cseweb.ucsd.edu/users/varghese/PAPERS/twheel.ps.Z">'Hashed
|
2009-06-17 08:37:36 +02:00
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
public class HashedWheelTimer implements Timer {
|
|
|
|
|
|
2009-01-20 05:39:11 +01:00
|
|
|
|
static final InternalLogger logger =
|
2013-03-12 08:43:46 +01:00
|
|
|
|
InternalLoggerFactory.getInstance(HashedWheelTimer.class);
|
2009-01-20 05:39:11 +01:00
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
|
private static final ResourceLeakDetector<HashedWheelTimer> leakDetector =
|
|
|
|
|
new ResourceLeakDetector<HashedWheelTimer>(
|
|
|
|
|
HashedWheelTimer.class, 1, Runtime.getRuntime().availableProcessors() * 4);
|
2009-02-13 13:41:46 +01:00
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
|
private final ResourceLeak leak = leakDetector.open(this);
|
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;
|
2013-03-16 04:27:33 +01:00
|
|
|
|
|
|
|
|
|
public static final int WORKER_STATE_INIT = 0;
|
|
|
|
|
public static final int WORKER_STATE_STARTED = 1;
|
|
|
|
|
public static final int WORKER_STATE_SHUTDOWN = 2;
|
2012-10-16 22:36:36 +02:00
|
|
|
|
final AtomicInteger workerState = new AtomicInteger(); // 0 - init, 1 - started, 2 - shut down
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
|
|
|
|
final long tickDuration;
|
|
|
|
|
final Set<HashedWheelTimeout>[] wheel;
|
|
|
|
|
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}
|
2012-09-30 22:23:14 +02:00
|
|
|
|
* @throws NullPointerException if {@code unit} is {@code null}
|
|
|
|
|
* @throws IllegalArgumentException if {@code tickDuration} is <= 0
|
2009-06-17 08:37:36 +02:00
|
|
|
|
*/
|
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
|
2012-09-30 22:23:14 +02:00
|
|
|
|
* @throws NullPointerException if {@code unit} is {@code null}
|
|
|
|
|
* @throws IllegalArgumentException if either of {@code tickDuration} and {@code ticksPerWheel} is <= 0
|
2009-06-17 08:37:36 +02:00
|
|
|
|
*/
|
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.
|
2012-09-30 22:23:14 +02:00
|
|
|
|
* @throws NullPointerException if {@code threadFactory} is {@code null}
|
2009-06-17 08:37:36 +02:00
|
|
|
|
*/
|
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}
|
2012-09-30 22:23:14 +02:00
|
|
|
|
* @throws NullPointerException if either of {@code threadFactory} and {@code unit} is {@code null}
|
|
|
|
|
* @throws IllegalArgumentException if {@code tickDuration} is <= 0
|
2009-06-17 08:37:36 +02:00
|
|
|
|
*/
|
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
|
2012-09-30 22:23:14 +02:00
|
|
|
|
* @throws NullPointerException if either of {@code threadFactory} and {@code unit} is {@code null}
|
|
|
|
|
* @throws IllegalArgumentException if either of {@code tickDuration} and {@code ticksPerWheel} is <= 0
|
2009-06-17 08:37:36 +02:00
|
|
|
|
*/
|
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) {
|
2012-09-30 22:23:14 +02:00
|
|
|
|
throw new IllegalArgumentException("tickDuration must be greater than 0: " + tickDuration);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
2009-01-20 13:17:56 +01:00
|
|
|
|
if (ticksPerWheel <= 0) {
|
2012-09-30 22:23:14 +02:00
|
|
|
|
throw new IllegalArgumentException("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);
|
|
|
|
|
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.
|
2012-09-30 22:23:14 +02:00
|
|
|
|
if (tickDuration == Long.MAX_VALUE || tickDuration >= Long.MAX_VALUE / wheel.length) {
|
|
|
|
|
throw new IllegalArgumentException("tickDuration is too long: " + tickDuration + ' ' + unit);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-04 11:21:47 +02:00
|
|
|
|
workerThread = threadFactory.newThread(worker);
|
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 ++) {
|
2012-05-15 10:10:54 +02:00
|
|
|
|
wheel[i] = Collections.newSetFromMap(
|
2013-02-27 00:54:51 +01:00
|
|
|
|
PlatformDependent.<HashedWheelTimeout, Boolean>newConcurrentHashMap());
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
2009-01-20 08:57:45 +01:00
|
|
|
|
return wheel;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
2012-10-16 22:36:36 +02:00
|
|
|
|
public void start() {
|
|
|
|
|
switch (workerState.get()) {
|
2013-03-16 04:27:33 +01:00
|
|
|
|
case WORKER_STATE_INIT:
|
|
|
|
|
if (workerState.compareAndSet(WORKER_STATE_INIT, WORKER_STATE_STARTED)) {
|
2013-03-12 08:43:46 +01:00
|
|
|
|
workerThread.start();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-03-16 04:27:33 +01:00
|
|
|
|
case WORKER_STATE_STARTED:
|
2013-03-12 08:43:46 +01:00
|
|
|
|
break;
|
2013-03-16 04:27:33 +01:00
|
|
|
|
case WORKER_STATE_SHUTDOWN:
|
2013-03-12 08:43:46 +01:00
|
|
|
|
throw new IllegalStateException("cannot be started once stopped");
|
|
|
|
|
default:
|
2013-03-16 04:27:33 +01:00
|
|
|
|
throw new Error("Invalid WorkerState");
|
2009-02-11 06:05:20 +01:00
|
|
|
|
}
|
2009-01-20 12:04:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2012-10-16 22:36:36 +02:00
|
|
|
|
public Set<Timeout> stop() {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
if (Thread.currentThread() == workerThread) {
|
|
|
|
|
throw new IllegalStateException(
|
|
|
|
|
HashedWheelTimer.class.getSimpleName() +
|
2013-03-12 08:43:46 +01:00
|
|
|
|
".stop() cannot be called from " +
|
|
|
|
|
TimerTask.class.getSimpleName());
|
2011-01-31 06:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-16 04:27:33 +01:00
|
|
|
|
if (!workerState.compareAndSet(WORKER_STATE_STARTED, WORKER_STATE_SHUTDOWN)) {
|
2012-10-16 22:36:36 +02:00
|
|
|
|
// workerState can be 0 or 2 at this moment - let it always be 2.
|
2013-03-16 04:27:33 +01:00
|
|
|
|
workerState.set(WORKER_STATE_SHUTDOWN);
|
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-09-04 06:21:56 +02:00
|
|
|
|
boolean interrupted = false;
|
2009-01-20 12:04:27 +01:00
|
|
|
|
while (workerThread.isAlive()) {
|
|
|
|
|
workerThread.interrupt();
|
|
|
|
|
try {
|
|
|
|
|
workerThread.join(100);
|
|
|
|
|
} catch (InterruptedException e) {
|
2009-09-04 06:21:56 +02:00
|
|
|
|
interrupted = true;
|
2009-01-20 12:04:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-01-20 13:14:29 +01:00
|
|
|
|
|
2009-09-04 06:21:56 +02:00
|
|
|
|
if (interrupted) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
|
leak.close();
|
2009-02-13 13:41:46 +01:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-16 22:36:36 +02:00
|
|
|
|
start();
|
2011-01-31 06:48:01 +01:00
|
|
|
|
|
2009-02-09 11:58:30 +01:00
|
|
|
|
delay = unit.toMillis(delay);
|
2011-01-31 06:48:01 +01:00
|
|
|
|
HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delay);
|
|
|
|
|
scheduleTimeout(timeout, delay);
|
|
|
|
|
return timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scheduleTimeout(HashedWheelTimeout timeout, long delay) {
|
2013-03-12 08:43:46 +01:00
|
|
|
|
// Prepare the required parameters to schedule the timeout object.
|
2013-03-16 04:27:33 +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;
|
|
|
|
|
}
|
|
|
|
|
if ((relativeIndex & mask) == 0) {
|
|
|
|
|
relativeIndex--;
|
|
|
|
|
}
|
|
|
|
|
final long remainingRounds = relativeIndex / wheel.length;
|
2009-02-02 04:42:05 +01:00
|
|
|
|
|
|
|
|
|
// Add the timeout to the wheel.
|
2009-01-19 16:05:04 +01:00
|
|
|
|
lock.readLock().lock();
|
|
|
|
|
try {
|
2013-03-16 04:27:33 +01:00
|
|
|
|
if (workerState.get() == WORKER_STATE_SHUTDOWN) {
|
|
|
|
|
throw new IllegalStateException("Cannot enqueue after shutdown");
|
|
|
|
|
}
|
|
|
|
|
final int stopIndex = (int) ((wheelCursor + relativeIndex) & mask);
|
2011-01-31 06:48:01 +01:00
|
|
|
|
timeout.stopIndex = stopIndex;
|
|
|
|
|
timeout.remainingRounds = remainingRounds;
|
|
|
|
|
wheel[stopIndex].add(timeout);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
} finally {
|
|
|
|
|
lock.readLock().unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2009-01-20 12:07:30 +01:00
|
|
|
|
public void run() {
|
2009-01-19 16:05:04 +01:00
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts =
|
2013-03-12 08:43:46 +01:00
|
|
|
|
new ArrayList<HashedWheelTimeout>();
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
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
|
|
|
|
|
2013-03-16 04:27:33 +01:00
|
|
|
|
while (workerState.get() == WORKER_STATE_STARTED) {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
final long deadline = waitForNextTick();
|
2013-03-12 08:43:46 +01:00
|
|
|
|
if (deadline > 0) {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
fetchExpiredTimeouts(expiredTimeouts, deadline);
|
|
|
|
|
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(
|
2011-01-31 06:48:01 +01:00
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts, long deadline) {
|
2009-01-19 16:05:04 +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 {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
int newWheelCursor = wheelCursor = wheelCursor + 1 & mask;
|
2012-07-07 06:52:54 +02:00
|
|
|
|
fetchExpiredTimeouts(expiredTimeouts, wheel[newWheelCursor].iterator(), deadline);
|
2009-01-20 08:57:45 +01:00
|
|
|
|
} finally {
|
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fetchExpiredTimeouts(
|
|
|
|
|
List<HashedWheelTimeout> expiredTimeouts,
|
2012-07-07 06:52:54 +02:00
|
|
|
|
Iterator<HashedWheelTimeout> i, long deadline) {
|
2009-01-20 08:57:45 +01:00
|
|
|
|
|
2011-01-31 06:48:01 +01:00
|
|
|
|
List<HashedWheelTimeout> slipped = null;
|
2009-01-20 10:03:31 +01:00
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
HashedWheelTimeout timeout = i.next();
|
2009-02-11 06:05:20 +01:00
|
|
|
|
if (timeout.remainingRounds <= 0) {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
i.remove();
|
2013-03-12 08:43:46 +01:00
|
|
|
|
if (timeout.deadline <= deadline) {
|
2009-02-11 06:05:20 +01:00
|
|
|
|
expiredTimeouts.add(timeout);
|
2009-01-20 10:03:31 +01:00
|
|
|
|
} else {
|
2011-01-31 06:48:01 +01:00
|
|
|
|
// 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.
|
|
|
|
|
if (slipped == null) {
|
2012-11-12 01:05:16 +01:00
|
|
|
|
slipped = new ArrayList<HashedWheelTimeout>();
|
2011-01-31 06:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
slipped.add(timeout);
|
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
|
|
|
|
}
|
2011-01-31 06:48:01 +01:00
|
|
|
|
|
|
|
|
|
// Reschedule the slipped timeouts.
|
|
|
|
|
if (slipped != null) {
|
|
|
|
|
for (HashedWheelTimeout timeout: slipped) {
|
|
|
|
|
scheduleTimeout(timeout, timeout.deadline - deadline);
|
|
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-16 04:27:33 +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-01-31 06:48:01 +01:00
|
|
|
|
private long waitForNextTick() {
|
2013-03-12 08:43:46 +01:00
|
|
|
|
long deadline = startTime + tickDuration * tick;
|
2011-01-31 06:48:01 +01:00
|
|
|
|
|
2009-01-19 16:05:04 +01:00
|
|
|
|
for (;;) {
|
2013-03-12 08:43:46 +01:00
|
|
|
|
final long currentTime = System.currentTimeMillis();
|
2013-03-16 04:27:33 +01:00
|
|
|
|
long sleepTimeMs = (deadline - currentTime + 999999) / 1000000;
|
|
|
|
|
|
|
|
|
|
if (sleepTimeMs <= 0) {
|
|
|
|
|
tick += 1;
|
|
|
|
|
if (currentTime == Long.MIN_VALUE) {
|
|
|
|
|
return -Long.MAX_VALUE;
|
|
|
|
|
} else {
|
|
|
|
|
return currentTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-23 15:12:04 +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
|
2013-01-11 06:03:27 +01:00
|
|
|
|
if (PlatformDependent.isWindows()) {
|
2013-03-16 04:27:33 +01:00
|
|
|
|
sleepTimeMs = (sleepTimeMs / 10) * 10;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2013-03-16 04:27:33 +01:00
|
|
|
|
Thread.sleep(sleepTimeMs);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
} catch (InterruptedException e) {
|
2013-03-16 04:27:33 +01:00
|
|
|
|
if (workerState.get() == WORKER_STATE_SHUTDOWN) {
|
|
|
|
|
return Long.MIN_VALUE;
|
2009-01-20 08:57:45 +01:00
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final class HashedWheelTimeout implements Timeout {
|
|
|
|
|
|
2011-08-12 07:03:48 +02:00
|
|
|
|
private static final int ST_INIT = 0;
|
|
|
|
|
private static final int ST_CANCELLED = 1;
|
|
|
|
|
private static final int ST_EXPIRED = 2;
|
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
|
private final TimerTask task;
|
2009-02-02 04:42:05 +01:00
|
|
|
|
final long deadline;
|
2011-01-31 06:48:01 +01:00
|
|
|
|
volatile int stopIndex;
|
2009-02-02 04:42:05 +01:00
|
|
|
|
volatile long remainingRounds;
|
2011-08-12 07:03:48 +02:00
|
|
|
|
private final AtomicInteger state = new AtomicInteger(ST_INIT);
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
2011-01-31 06:48:01 +01:00
|
|
|
|
HashedWheelTimeout(TimerTask task, long deadline) {
|
2009-01-20 05:29:58 +01:00
|
|
|
|
this.task = task;
|
2009-02-02 04:42:05 +01:00
|
|
|
|
this.deadline = deadline;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2012-12-26 05:50:01 +01:00
|
|
|
|
public Timer timer() {
|
2009-06-17 08:37:36 +02:00
|
|
|
|
return HashedWheelTimer.this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2012-12-26 05:50:01 +01:00
|
|
|
|
public TimerTask task() {
|
2009-01-20 05:29:58 +01:00
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2012-06-29 13:59:48 +02:00
|
|
|
|
public boolean cancel() {
|
2011-08-12 07:03:48 +02:00
|
|
|
|
if (!state.compareAndSet(ST_INIT, ST_CANCELLED)) {
|
2012-06-29 13:59:48 +02:00
|
|
|
|
return false;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
2012-05-15 10:10:54 +02:00
|
|
|
|
|
2009-02-02 04:42:05 +01:00
|
|
|
|
wheel[stopIndex].remove(this);
|
2012-06-29 13:59:48 +02:00
|
|
|
|
return true;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2009-01-19 16:05:04 +01:00
|
|
|
|
public boolean isCancelled() {
|
2011-08-12 07:03:48 +02:00
|
|
|
|
return state.get() == ST_CANCELLED;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
|
@Override
|
2009-01-19 16:05:04 +01:00
|
|
|
|
public boolean isExpired() {
|
2011-08-12 07:03:48 +02:00
|
|
|
|
return state.get() != ST_INIT;
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void expire() {
|
2011-08-12 07:03:48 +02:00
|
|
|
|
if (!state.compareAndSet(ST_INIT, ST_EXPIRED)) {
|
2009-01-20 05:29:58 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2009-01-19 16:05:04 +01:00
|
|
|
|
|
2009-01-20 05:29:58 +01:00
|
|
|
|
try {
|
|
|
|
|
task.run(this);
|
|
|
|
|
} catch (Throwable t) {
|
2012-02-17 10:37:41 +01:00
|
|
|
|
if (logger.isWarnEnabled()) {
|
2013-03-12 08:43:46 +01:00
|
|
|
|
logger.warn("An exception was thrown by " + TimerTask.class.getSimpleName() + '.', t);
|
2012-02-17 10:37:41 +01:00
|
|
|
|
}
|
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()) {
|
2012-01-11 12:16:14 +01:00
|
|
|
|
buf.append(", cancelled");
|
2009-01-19 16:05:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.append(')').toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|