mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibCore: Add a class for thread-safe promises
Since the existing Promise class is designed with deferred tasks on the main thread only, we need a new class that will ensure we can handle promises that are resolved/rejected off the main thread. This new class ensures that the callbacks are only called on the same thread that the promise is fulfilled from. If the callbacks are not set before the thread tries to fulfill the promise, it will spin until they are so that they will run on that thread.
This commit is contained in:
parent
8626404ddb
commit
fe672989a9
Notes:
sideshowbarker
2024-07-16 21:30:46 +09:00
Author: https://github.com/Zaggy1024
Commit: fe672989a9
Pull-request: https://github.com/SerenityOS/serenity/pull/19849
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/AtkinsSJ
Reviewed-by: https://github.com/kleinesfilmroellchen
Reviewed-by: https://github.com/trflynn89
7 changed files with 321 additions and 4 deletions
|
@ -17,12 +17,17 @@
|
|||
namespace Core {
|
||||
|
||||
namespace {
|
||||
Vector<EventLoop&>& event_loop_stack()
|
||||
OwnPtr<Vector<EventLoop&>>& event_loop_stack_uninitialized()
|
||||
{
|
||||
thread_local OwnPtr<Vector<EventLoop&>> s_event_loop_stack = nullptr;
|
||||
if (s_event_loop_stack == nullptr)
|
||||
s_event_loop_stack = make<Vector<EventLoop&>>();
|
||||
return *s_event_loop_stack;
|
||||
return s_event_loop_stack;
|
||||
}
|
||||
Vector<EventLoop&>& event_loop_stack()
|
||||
{
|
||||
auto& the_stack = event_loop_stack_uninitialized();
|
||||
if (the_stack == nullptr)
|
||||
the_stack = make<Vector<EventLoop&>>();
|
||||
return *the_stack;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,6 +46,12 @@ EventLoop::~EventLoop()
|
|||
}
|
||||
}
|
||||
|
||||
bool EventLoop::is_running()
|
||||
{
|
||||
auto& stack = event_loop_stack_uninitialized();
|
||||
return stack != nullptr && !stack->is_empty();
|
||||
}
|
||||
|
||||
EventLoop& EventLoop::current()
|
||||
{
|
||||
return event_loop_stack().last();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue