I am reading netty 4 source code. eventLoop.inEventLoop()
appears everywhere.
According to Netty in Action:
A Channel is registered for its lifetime with a single EventLoop. A single EventLoop may be assigned to one or more Channels. a channel has and only has one eventLoop/thread.
Theoretically, eventLoop.inEventLoop()
is to make sure code blocks are executed by the assigned eventLoop/thread. Furthermore, if you call something from non-io thread, eventLoop.inEventLoop()
goes to false, and will be executed by assigned eventLoop/thread.
For example, the following code goes to else block(NioSocketChannel$NioSocketChannelUnsafe(AbstractChannel$AbstractUnsafe).register(EventLoop, ChannelPromise)
) where the channel is not registered(assigned to an eventloop/thread).
if (eventLoop.inEventLoop()) {
register0(promise);
} else {
try {
eventLoop.execute(new Runnable() {
@Override
public void run() {
register0(promise);
}
});
} catch (Throwable t) {
...
}
}
I am really confused, whats the point of eventLoop.inEventLoop()
.
What does eventLoop.inEventLoop()
protect from?
Besides the above one, can you give me some more specific examples in practice to demonstrate why and how eventloop.inEventLoop() == false
? What kind of code do you call? Where do you call? How does the code make eventloop.inEventLoop() == false
?