-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
EmbeddedEventLoop.java
210 lines (182 loc) · 5.58 KB
/
EmbeddedEventLoop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2012 The Netty Project
*
* 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:
*
* https://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.
*/
package io.netty5.channel.embedded;
import io.netty5.channel.Channel;
import io.netty5.channel.EventLoop;
import io.netty5.util.concurrent.AbstractScheduledEventExecutor;
import io.netty5.util.concurrent.Future;
import io.netty5.util.internal.StringUtil;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import static java.util.Objects.requireNonNull;
final class EmbeddedEventLoop extends AbstractScheduledEventExecutor implements EventLoop {
/**
* When time is not {@link #timeFrozen frozen}, the base time to subtract from {@link System#nanoTime()}. When time
* is frozen, this variable is unused.
*
* Initialized to {@link #initialNanoTime()} so that until one of the time mutator methods is called,
* {@link #getCurrentTimeNanos()} matches the default behavior.
*/
private long startTime = initialNanoTime();
/**
* When time is frozen, the timestamp returned by {@link #getCurrentTimeNanos()}. When unfrozen, this is unused.
*/
private long frozenTimestamp;
/**
* Whether time is currently frozen.
*/
private boolean timeFrozen;
private final Queue<Runnable> tasks = new ArrayDeque<>(2);
boolean running;
private static EmbeddedChannel cast(Channel channel) {
if (channel instanceof EmbeddedChannel) {
return (EmbeddedChannel) channel;
}
throw new IllegalArgumentException("Channel of type " + StringUtil.simpleClassName(channel) + " not supported");
}
private final Unsafe unsafe = new Unsafe() {
@Override
public void register(Channel channel) {
assert inEventLoop();
cast(channel).setActive();
}
@Override
public void deregister(Channel channel) {
assert inEventLoop();
}
};
@Override
public Unsafe unsafe() {
return unsafe;
}
@Override
public EventLoop next() {
return (EventLoop) super.next();
}
@Override
public void execute(Runnable task) {
requireNonNull(task, "command");
tasks.add(task);
if (!running) {
runTasks();
}
}
void runTasks() {
boolean wasRunning = running;
try {
for (;;) {
running = true;
Runnable task = tasks.poll();
if (task == null) {
break;
}
task.run();
}
} finally {
if (!wasRunning) {
running = false;
}
}
}
long runScheduledTasks() {
long time = getCurrentTimeNanos();
boolean wasRunning = running;
try {
for (;;) {
running = true;
Runnable task = pollScheduledTask(time);
if (task == null) {
return nextScheduledTaskNano();
}
task.run();
}
} finally {
if (!wasRunning) {
running = false;
}
}
}
long nextScheduledTask() {
return nextScheduledTaskNano();
}
void cancelScheduled() {
running = true;
try {
cancelScheduledTasks();
} finally {
running = false;
}
}
@Override
protected long getCurrentTimeNanos() {
if (timeFrozen) {
return frozenTimestamp;
}
return System.nanoTime() - startTime;
}
void advanceTimeBy(long nanos) {
if (timeFrozen) {
frozenTimestamp += nanos;
} else {
// startTime is subtracted from nanoTime, so increasing the startTime will advance getCurrentTimeNanos
startTime -= nanos;
}
}
void freezeTime() {
if (!timeFrozen) {
frozenTimestamp = getCurrentTimeNanos();
timeFrozen = true;
}
}
void unfreezeTime() {
if (timeFrozen) {
// we want getCurrentTimeNanos to continue right where frozenTimestamp left off:
// getCurrentTimeNanos = nanoTime - startTime = frozenTimestamp
// then solve for startTime
startTime = System.nanoTime() - frozenTimestamp;
timeFrozen = false;
}
}
@Override
public Future<Void> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public Future<Void> terminationFuture() {
throw new UnsupportedOperationException();
}
@Override
public boolean isShuttingDown() {
return false;
}
@Override
public boolean isShutdown() {
return false;
}
@Override
public boolean isTerminated() {
return false;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
return false;
}
@Override
public boolean inEventLoop(Thread thread) {
return running;
}
}