ScrollingThread.cpp [plain text]
#include "config.h"
#include "ScrollingThread.h"
#if ENABLE(THREADED_SCROLLING)
#include <wtf/MainThread.h>
namespace WebCore {
ScrollingThread::ScrollingThread()
: m_threadIdentifier(0)
{
}
bool ScrollingThread::isCurrentThread()
{
if (!shared().m_threadIdentifier)
return false;
return currentThread() == shared().m_threadIdentifier;
}
void ScrollingThread::dispatch(const Function<void()>& function)
{
shared().createThreadIfNeeded();
{
MutexLocker locker(shared().m_functionsMutex);
shared().m_functions.append(function);
}
shared().wakeUpRunLoop();
}
static void callFunctionOnMainThread(const Function<void()>* function)
{
callOnMainThread(*function);
delete function;
}
void ScrollingThread::dispatchBarrier(const Function<void()>& function)
{
dispatch(bind(callFunctionOnMainThread, new Function<void()>(function)));
}
ScrollingThread& ScrollingThread::shared()
{
DEFINE_STATIC_LOCAL(ScrollingThread, scrollingThread, ());
return scrollingThread;
}
void ScrollingThread::createThreadIfNeeded()
{
if (m_threadIdentifier)
return;
{
MutexLocker locker(m_initializeRunLoopConditionMutex);
m_threadIdentifier = createThread(threadCallback, this, "WebCore: Scrolling");
#if PLATFORM(MAC)
while (!m_threadRunLoop)
m_initializeRunLoopCondition.wait(m_initializeRunLoopConditionMutex);
#endif
}
}
void ScrollingThread::threadCallback(void* scrollingThread)
{
static_cast<ScrollingThread*>(scrollingThread)->threadBody();
}
void ScrollingThread::threadBody()
{
initializeRunLoop();
}
void ScrollingThread::dispatchFunctionsFromScrollingThread()
{
ASSERT(isCurrentThread());
Vector<Function<void()> > functions;
{
MutexLocker locker(m_functionsMutex);
m_functions.swap(functions);
}
for (size_t i = 0; i < functions.size(); ++i)
functions[i]();
}
}
#endif // ENABLE(THREADED_SCROLLING)