summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/RunLoop.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WTF/wtf/RunLoop.cpp')
-rw-r--r--Source/WTF/wtf/RunLoop.cpp50
1 files changed, 28 insertions, 22 deletions
diff --git a/Source/WTF/wtf/RunLoop.cpp b/Source/WTF/wtf/RunLoop.cpp
index 11a860fd0..66593b04e 100644
--- a/Source/WTF/wtf/RunLoop.cpp
+++ b/Source/WTF/wtf/RunLoop.cpp
@@ -26,6 +26,7 @@
#include "config.h"
#include "RunLoop.h"
+#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/ThreadSpecific.h>
@@ -37,39 +38,40 @@ static RunLoop* s_mainRunLoop;
class RunLoop::Holder {
public:
Holder()
- : m_runLoop(adoptRef(new RunLoop))
+ : m_runLoop(adoptRef(*new RunLoop))
{
}
- RunLoop* runLoop() const { return m_runLoop.get(); }
+ RunLoop& runLoop() { return m_runLoop; }
private:
- RefPtr<RunLoop> m_runLoop;
+ Ref<RunLoop> m_runLoop;
};
void RunLoop::initializeMainRunLoop()
{
if (s_mainRunLoop)
return;
- s_mainRunLoop = RunLoop::current();
+ initializeMainThread();
+ s_mainRunLoop = &RunLoop::current();
}
-RunLoop* RunLoop::current()
+RunLoop& RunLoop::current()
{
- DEFINE_STATIC_LOCAL(WTF::ThreadSpecific<RunLoop::Holder>, runLoopHolder, ());
- return runLoopHolder->runLoop();
+ static NeverDestroyed<ThreadSpecific<Holder>> runLoopHolder;
+ return runLoopHolder.get()->runLoop();
}
-RunLoop* RunLoop::main()
+RunLoop& RunLoop::main()
{
ASSERT(s_mainRunLoop);
- return s_mainRunLoop;
+ return *s_mainRunLoop;
}
bool RunLoop::isMain()
{
ASSERT(s_mainRunLoop);
- return s_mainRunLoop == RunLoop::current();
+ return s_mainRunLoop == &RunLoop::current();
}
void RunLoop::performWork()
@@ -88,22 +90,24 @@ void RunLoop::performWork()
// By only handling up to the number of functions that were in the queue when performWork() is called
// we guarantee to occasionally return from the run loop so other event sources will be allowed to spin.
- std::function<void()> function;
size_t functionsToHandle = 0;
-
{
- MutexLocker locker(m_functionQueueLock);
- functionsToHandle = m_functionQueue.size();
+ Function<void ()> function;
+ {
+ MutexLocker locker(m_functionQueueLock);
+ functionsToHandle = m_functionQueue.size();
- if (m_functionQueue.isEmpty())
- return;
+ if (m_functionQueue.isEmpty())
+ return;
- function = m_functionQueue.takeFirst();
- }
+ function = m_functionQueue.takeFirst();
+ }
- function();
+ function();
+ }
for (size_t functionsHandled = 1; functionsHandled < functionsToHandle; ++functionsHandled) {
+ Function<void ()> function;
{
MutexLocker locker(m_functionQueueLock);
@@ -120,10 +124,12 @@ void RunLoop::performWork()
}
}
-void RunLoop::dispatch(std::function<void ()> function)
+void RunLoop::dispatch(Function<void ()>&& function)
{
- MutexLocker locker(m_functionQueueLock);
- m_functionQueue.append(std::move(function));
+ {
+ MutexLocker locker(m_functionQueueLock);
+ m_functionQueue.append(WTFMove(function));
+ }
wakeUp();
}