This source file includes following definitions.
- initializeCurrentThreadInternal
- lockAtomicallyInitializedStaticMutex
- unlockAtomicallyInitializedStaticMutex
- threadMapMutex
- initializeThreading
- threadMap
- storeThreadHandleByIdentifier
- threadHandleForIdentifier
- clearThreadHandleForIdentifier
- wtfThreadEntryPoint
- createThreadInternal
- waitForThreadCompletion
- detachThread
- yield
- currentThread
- lock
- tryLock
- unlock
- timedWait
- signal
- wait
- timedWait
- signal
- broadcast
- absoluteTimeToWaitTimeoutInterval
#include "config.h"
#include "Threading.h"
#if OS(WIN)
#include "wtf/CurrentTime.h"
#include "wtf/DateMath.h"
#include "wtf/HashMap.h"
#include "wtf/MainThread.h"
#include "wtf/MathExtras.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/ThreadFunctionInvocation.h"
#include "wtf/ThreadSpecific.h"
#include "wtf/ThreadingPrimitives.h"
#include "wtf/WTFThreadData.h"
#include "wtf/dtoa.h"
#include "wtf/dtoa/cached-powers.h"
#include <errno.h>
#include <process.h>
#include <windows.h>
namespace WTF {
static const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO {
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
} THREADNAME_INFO;
#pragma pack(pop)
void initializeCurrentThreadInternal(const char* szThreadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = GetCurrentThreadId();
info.dwFlags = 0;
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
} __except (EXCEPTION_CONTINUE_EXECUTION) {
}
}
static Mutex* atomicallyInitializedStaticMutex;
void lockAtomicallyInitializedStaticMutex()
{
ASSERT(atomicallyInitializedStaticMutex);
atomicallyInitializedStaticMutex->lock();
}
void unlockAtomicallyInitializedStaticMutex()
{
atomicallyInitializedStaticMutex->unlock();
}
static Mutex& threadMapMutex()
{
static Mutex mutex;
return mutex;
}
void initializeThreading()
{
ASSERT(!atomicallyInitializedStaticMutex);
StringImpl::empty();
atomicallyInitializedStaticMutex = new Mutex;
threadMapMutex();
wtfThreadData();
s_dtoaP5Mutex = new Mutex;
initializeDates();
}
static HashMap<DWORD, HANDLE>& threadMap()
{
static HashMap<DWORD, HANDLE>* gMap;
if (!gMap)
gMap = new HashMap<DWORD, HANDLE>();
return *gMap;
}
static void storeThreadHandleByIdentifier(DWORD threadID, HANDLE threadHandle)
{
MutexLocker locker(threadMapMutex());
ASSERT(!threadMap().contains(threadID));
threadMap().add(threadID, threadHandle);
}
static HANDLE threadHandleForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
return threadMap().get(id);
}
static void clearThreadHandleForIdentifier(ThreadIdentifier id)
{
MutexLocker locker(threadMapMutex());
ASSERT(threadMap().contains(id));
threadMap().remove(id);
}
static unsigned __stdcall wtfThreadEntryPoint(void* param)
{
OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(static_cast<ThreadFunctionInvocation*>(param));
invocation->function(invocation->data);
ThreadSpecificThreadExit();
return 0;
}
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char* threadName)
{
unsigned threadIdentifier = 0;
ThreadIdentifier threadID = 0;
OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(new ThreadFunctionInvocation(entryPoint, data));
HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation.get(), 0, &threadIdentifier));
if (!threadHandle) {
WTF_LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno);
return 0;
}
ThreadFunctionInvocation* ALLOW_UNUSED leakedInvocation = invocation.leakPtr();
threadID = static_cast<ThreadIdentifier>(threadIdentifier);
storeThreadHandleByIdentifier(threadIdentifier, threadHandle);
return threadID;
}
int waitForThreadCompletion(ThreadIdentifier threadID)
{
ASSERT(threadID);
HANDLE threadHandle = threadHandleForIdentifier(threadID);
if (!threadHandle)
WTF_LOG_ERROR("ThreadIdentifier %u did not correspond to an active thread when trying to quit", threadID);
DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE);
if (joinResult == WAIT_FAILED)
WTF_LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
CloseHandle(threadHandle);
clearThreadHandleForIdentifier(threadID);
return joinResult;
}
void detachThread(ThreadIdentifier threadID)
{
ASSERT(threadID);
HANDLE threadHandle = threadHandleForIdentifier(threadID);
if (threadHandle)
CloseHandle(threadHandle);
clearThreadHandleForIdentifier(threadID);
}
void yield()
{
::Sleep(1);
}
ThreadIdentifier currentThread()
{
return static_cast<ThreadIdentifier>(GetCurrentThreadId());
}
Mutex::Mutex()
{
m_mutex.m_recursionCount = 0;
InitializeCriticalSection(&m_mutex.m_internalMutex);
}
Mutex::~Mutex()
{
DeleteCriticalSection(&m_mutex.m_internalMutex);
}
void Mutex::lock()
{
EnterCriticalSection(&m_mutex.m_internalMutex);
++m_mutex.m_recursionCount;
}
bool Mutex::tryLock()
{
DWORD result = TryEnterCriticalSection(&m_mutex.m_internalMutex);
if (result != 0) {
if (m_mutex.m_recursionCount > 0) {
LeaveCriticalSection(&m_mutex.m_internalMutex);
return false;
}
++m_mutex.m_recursionCount;
return true;
}
return false;
}
void Mutex::unlock()
{
ASSERT(m_mutex.m_recursionCount);
--m_mutex.m_recursionCount;
LeaveCriticalSection(&m_mutex.m_internalMutex);
}
bool PlatformCondition::timedWait(PlatformMutex& mutex, DWORD durationMilliseconds)
{
DWORD res = WaitForSingleObject(m_blockLock, INFINITE);
ASSERT_UNUSED(res, res == WAIT_OBJECT_0);
++m_waitersBlocked;
res = ReleaseSemaphore(m_blockLock, 1, 0);
ASSERT_UNUSED(res, res);
--mutex.m_recursionCount;
LeaveCriticalSection(&mutex.m_internalMutex);
bool timedOut = (WaitForSingleObject(m_blockQueue, durationMilliseconds) == WAIT_TIMEOUT);
res = WaitForSingleObject(m_unblockLock, INFINITE);
ASSERT_UNUSED(res, res == WAIT_OBJECT_0);
int signalsLeft = m_waitersToUnblock;
if (m_waitersToUnblock)
--m_waitersToUnblock;
else if (++m_waitersGone == (INT_MAX / 2)) {
res = WaitForSingleObject(m_blockLock, INFINITE);
ASSERT_UNUSED(res, res == WAIT_OBJECT_0);
m_waitersBlocked -= m_waitersGone;
res = ReleaseSemaphore(m_blockLock, 1, 0);
ASSERT_UNUSED(res, res);
m_waitersGone = 0;
}
res = ReleaseMutex(m_unblockLock);
ASSERT_UNUSED(res, res);
if (signalsLeft == 1) {
res = ReleaseSemaphore(m_blockLock, 1, 0);
ASSERT_UNUSED(res, res);
}
EnterCriticalSection (&mutex.m_internalMutex);
++mutex.m_recursionCount;
return !timedOut;
}
void PlatformCondition::signal(bool unblockAll)
{
unsigned signalsToIssue = 0;
DWORD res = WaitForSingleObject(m_unblockLock, INFINITE);
ASSERT_UNUSED(res, res == WAIT_OBJECT_0);
if (m_waitersToUnblock) {
if (!m_waitersBlocked) {
res = ReleaseMutex(m_unblockLock);
ASSERT_UNUSED(res, res);
return;
}
if (unblockAll) {
signalsToIssue = m_waitersBlocked;
m_waitersToUnblock += m_waitersBlocked;
m_waitersBlocked = 0;
} else {
signalsToIssue = 1;
++m_waitersToUnblock;
--m_waitersBlocked;
}
} else if (m_waitersBlocked > m_waitersGone) {
res = WaitForSingleObject(m_blockLock, INFINITE);
ASSERT_UNUSED(res, res == WAIT_OBJECT_0);
if (m_waitersGone != 0) {
m_waitersBlocked -= m_waitersGone;
m_waitersGone = 0;
}
if (unblockAll) {
signalsToIssue = m_waitersBlocked;
m_waitersToUnblock = m_waitersBlocked;
m_waitersBlocked = 0;
} else {
signalsToIssue = 1;
m_waitersToUnblock = 1;
--m_waitersBlocked;
}
} else {
res = ReleaseMutex(m_unblockLock);
ASSERT_UNUSED(res, res);
return;
}
res = ReleaseMutex(m_unblockLock);
ASSERT_UNUSED(res, res);
if (signalsToIssue) {
res = ReleaseSemaphore(m_blockQueue, signalsToIssue, 0);
ASSERT_UNUSED(res, res);
}
}
static const long MaxSemaphoreCount = static_cast<long>(~0UL >> 1);
ThreadCondition::ThreadCondition()
{
m_condition.m_waitersGone = 0;
m_condition.m_waitersBlocked = 0;
m_condition.m_waitersToUnblock = 0;
m_condition.m_blockLock = CreateSemaphore(0, 1, 1, 0);
m_condition.m_blockQueue = CreateSemaphore(0, 0, MaxSemaphoreCount, 0);
m_condition.m_unblockLock = CreateMutex(0, 0, 0);
if (!m_condition.m_blockLock || !m_condition.m_blockQueue || !m_condition.m_unblockLock) {
if (m_condition.m_blockLock)
CloseHandle(m_condition.m_blockLock);
if (m_condition.m_blockQueue)
CloseHandle(m_condition.m_blockQueue);
if (m_condition.m_unblockLock)
CloseHandle(m_condition.m_unblockLock);
}
}
ThreadCondition::~ThreadCondition()
{
CloseHandle(m_condition.m_blockLock);
CloseHandle(m_condition.m_blockQueue);
CloseHandle(m_condition.m_unblockLock);
}
void ThreadCondition::wait(Mutex& mutex)
{
m_condition.timedWait(mutex.impl(), INFINITE);
}
bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
{
DWORD interval = absoluteTimeToWaitTimeoutInterval(absoluteTime);
if (!interval) {
return false;
}
return m_condition.timedWait(mutex.impl(), interval);
}
void ThreadCondition::signal()
{
m_condition.signal(false);
}
void ThreadCondition::broadcast()
{
m_condition.signal(true);
}
DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime)
{
double currentTime = WTF::currentTime();
if (absoluteTime < currentTime)
return 0;
if (absoluteTime - currentTime > static_cast<double>(INT_MAX) / 1000.0)
return INFINITE;
return static_cast<DWORD>((absoluteTime - currentTime) * 1000.0);
}
}
#endif