X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..b80e619319b1def83d1e8b4f84042b661be1be7f:/wtf/ThreadingWin.cpp diff --git a/wtf/ThreadingWin.cpp b/wtf/ThreadingWin.cpp index 399fb38..c16be5a 100644 --- a/wtf/ThreadingWin.cpp +++ b/wtf/ThreadingWin.cpp @@ -1,6 +1,7 @@ /* * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. * Copyright (C) 2009 Google Inc. All rights reserved. + * Copyright (C) 2009 Torch Mobile, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -86,10 +87,17 @@ #include "Threading.h" #include "MainThread.h" -#if !USE(PTHREADS) && PLATFORM(WIN_OS) +#if !USE(PTHREADS) && OS(WINDOWS) #include "ThreadSpecific.h" #endif +#if !OS(WINCE) #include +#endif +#if HAVE(ERRNO_H) +#include +#else +#define NO_ERRNO +#endif #include #include #include @@ -98,7 +106,7 @@ namespace WTF { -// MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadName all come from . +// MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadNameInternal all come from . static const DWORD MS_VC_EXCEPTION = 0x406D1388; #pragma pack(push, 8) @@ -110,16 +118,12 @@ typedef struct tagTHREADNAME_INFO { } THREADNAME_INFO; #pragma pack(pop) -static void setThreadName(DWORD dwThreadID, LPCSTR szThreadName) +void initializeCurrentThreadInternal(const char* szThreadName) { - // Visual Studio has a 31-character limit on thread names. Longer names will - // be truncated silently, but we'd like callers to know about the limit. - ASSERT_ARG(szThreadName, strlen(szThreadName) <= 31); - THREADNAME_INFO info; info.dwType = 0x1000; info.szName = szThreadName; - info.dwThreadID = dwThreadID; + info.dwThreadID = GetCurrentThreadId(); info.dwFlags = 0; __try { @@ -141,8 +145,6 @@ void unlockAtomicallyInitializedStaticMutex() atomicallyInitializedStaticMutex->unlock(); } -static ThreadIdentifier mainThreadIdentifier; - static Mutex& threadMapMutex() { static Mutex mutex; @@ -151,14 +153,12 @@ static Mutex& threadMapMutex() void initializeThreading() { - if (!atomicallyInitializedStaticMutex) { - atomicallyInitializedStaticMutex = new Mutex; - threadMapMutex(); - initializeRandomNumberGenerator(); - initializeMainThread(); - mainThreadIdentifier = currentThread(); - setThreadName(mainThreadIdentifier, "Main Thread"); - } + if (atomicallyInitializedStaticMutex) + return; + + atomicallyInitializedStaticMutex = new Mutex; + threadMapMutex(); + initializeRandomNumberGenerator(); } static HashMap& threadMap() @@ -201,7 +201,7 @@ static unsigned __stdcall wtfThreadEntryPoint(void* param) void* result = invocation.function(invocation.data); -#if !USE(PTHREADS) && PLATFORM(WIN_OS) +#if !USE(PTHREADS) && OS(WINDOWS) // Do the TLS cleanup. ThreadSpecificThreadExit(); #endif @@ -214,15 +214,24 @@ ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, con unsigned threadIdentifier = 0; ThreadIdentifier threadID = 0; ThreadFunctionInvocation* invocation = new ThreadFunctionInvocation(entryPoint, data); +#if OS(WINCE) + // This is safe on WINCE, since CRT is in the core and innately multithreaded. + // On desktop Windows, need to use _beginthreadex (not available on WinCE) if using any CRT functions + HANDLE threadHandle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)wtfThreadEntryPoint, invocation, 0, (LPDWORD)&threadIdentifier); +#else HANDLE threadHandle = reinterpret_cast(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation, 0, &threadIdentifier)); +#endif if (!threadHandle) { +#if OS(WINCE) + LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, ::GetLastError()); +#elif defined(NO_ERRNO) + LOG_ERROR("Failed to create thread at entry point %p with data %p.", entryPoint, data); +#else LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno); +#endif return 0; } - if (threadName) - setThreadName(threadIdentifier, threadName); - threadID = static_cast(threadIdentifier); storeThreadHandleByIdentifier(threadIdentifier, threadHandle); @@ -250,7 +259,7 @@ int waitForThreadCompletion(ThreadIdentifier threadID, void** result) void detachThread(ThreadIdentifier threadID) { ASSERT(threadID); - + HANDLE threadHandle = threadHandleForIdentifier(threadID); if (threadHandle) CloseHandle(threadHandle); @@ -262,11 +271,6 @@ ThreadIdentifier currentThread() return static_cast(GetCurrentThreadId()); } -bool isMainThread() -{ - return currentThread() == mainThreadIdentifier; -} - Mutex::Mutex() { m_mutex.m_recursionCount = 0; @@ -457,10 +461,13 @@ bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime) if (absoluteTime < currentTime) return false; - double intervalMilliseconds = (absoluteTime - currentTime) * 1000.0; - if (intervalMilliseconds >= INT_MAX) - intervalMilliseconds = INT_MAX; + // Time is too far in the future (and would overflow unsigned long) - wait forever. + if (absoluteTime - currentTime > static_cast(INT_MAX) / 1000.0) { + wait(mutex); + return true; + } + double intervalMilliseconds = (absoluteTime - currentTime) * 1000.0; return m_condition.timedWait(mutex.impl(), static_cast(intervalMilliseconds)); }