Chromium Base MessageLoop Internals (0)
Our fancy star in this post is class base::MessageLoopCurrent
.
MessageLoopCurrent
Version: r70_3538
File: base/message_loop/message_loop_current.{h, cc}
MessageLoopCurernt is a proxy class for interactions with the MessageLoop bound to the current thread.
It is introduced to avoid direct uses of MessageLoop::current()
, quoting from original comments:
Why: Historically MessageLoop::current() gave access to the full MessageLoop API, preventing both addition of powerful owner-only APIs as well as making it harder to remove callers of deprecated APIs (that need to stick around for a few owner-only use cases and re-accrue callers after cleanup per remaining publicly available).
Because it is a light-weight proxy, it contains only a single pointer to the MessageLoop bound to the current thread.
It also comes with value semantics.
1 | class BASE_EXPORT MessageLoopCurrent { |
The constructor is protected because the class wants users to access it via explicitly Get()
:
1 | // Note it is a static member function. |
Other major functions of MessageLoopCurrent simply forward invocations to current_
, except manipulations on thread-local MessageLoop
instance.
Suprisingly, the tls for MessageLoop instance now is managed by MessageLoopCurrent, over several static functions:
1 | namespace { |
By the way, the TLS instance is exposed with a wrapping function GetTLSMessageLoop()
, preventing any direct accesses on the variable.
MessageLoopCurrentForUI and MessageLoopCurrentForIO
They are derived classes from MessageLoopCurrent, and both specialized for UI-message-loop and IO-message-loop.
Therefore, other than a pointer to the MessageLoop instance, they also contain a pointer to an associated message-pump.
1 | // ForUI extension of MessageLoopCurrent. |
MessageLoopForIO is quite similarly:
1 | // static |