Changeset 43366 in webkit for trunk/JavaScriptCore/wtf/ThreadingWin.cpp
- Timestamp:
- May 7, 2009, 2:24:36 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/ThreadingWin.cpp
r41626 r43366 99 99 namespace WTF { 100 100 101 bool ThreadIdentifier::operator==(const ThreadIdentifier& another) const 102 { 103 return m_platformId == another.m_platformId; 104 } 105 106 bool ThreadIdentifier::operator!=(const ThreadIdentifier& another) const 107 { 108 return m_platformId != another.m_platformId; 109 } 110 101 111 // MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadNameInternal all come from <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>. 102 112 static const DWORD MS_VC_EXCEPTION = 0x406D1388; … … 140 150 static ThreadIdentifier mainThreadIdentifier; 141 151 142 static Mutex& threadMapMutex()143 {144 static Mutex mutex;145 return mutex;146 }147 148 152 void initializeThreading() 149 153 { 150 154 if (!atomicallyInitializedStaticMutex) { 151 155 atomicallyInitializedStaticMutex = new Mutex; 152 threadMapMutex();153 156 initializeRandomNumberGenerator(); 154 157 initializeMainThread(); … … 158 161 } 159 162 160 static HashMap<DWORD, HANDLE>& threadMap()161 {162 static HashMap<DWORD, HANDLE> map;163 return map;164 }165 166 static void storeThreadHandleByIdentifier(DWORD threadID, HANDLE threadHandle)167 {168 MutexLocker locker(threadMapMutex());169 ASSERT(!threadMap().contains(threadID));170 threadMap().add(threadID, threadHandle);171 }172 173 static HANDLE threadHandleForIdentifier(ThreadIdentifier id)174 {175 MutexLocker locker(threadMapMutex());176 return threadMap().get(id);177 }178 179 static void clearThreadHandleForIdentifier(ThreadIdentifier id)180 {181 MutexLocker locker(threadMapMutex());182 ASSERT(threadMap().contains(id));183 threadMap().remove(id);184 }185 186 163 struct ThreadFunctionInvocation { 187 164 ThreadFunctionInvocation(ThreadFunction function, void* data) : function(function), data(data) {} … … 209 186 { 210 187 unsigned threadIdentifier = 0; 211 ThreadIdentifier threadID = 0;212 188 ThreadFunctionInvocation* invocation = new ThreadFunctionInvocation(entryPoint, data); 213 189 HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation, 0, &threadIdentifier)); 214 190 if (!threadHandle) { 215 191 LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno); 216 return 0; 217 } 218 219 threadID = static_cast<ThreadIdentifier>(threadIdentifier); 220 storeThreadHandleByIdentifier(threadIdentifier, threadHandle); 221 222 return threadID; 192 return ThreadIdentifier(); 193 } 194 // Closes handle only, since we don't need it. The new thread runs until it exits. 195 CloseHandle(threadHandle); 196 197 return ThreadIdentifier(threadIdentifier); 223 198 } 224 199 225 200 int waitForThreadCompletion(ThreadIdentifier threadID, void** result) 226 201 { 227 ASSERT(threadID );202 ASSERT(threadID.isValid()); 228 203 229 HANDLE threadHandle = threadHandleForIdentifier(threadID); 204 unsigned threadIdentifier = threadID.platformId(); 205 HANDLE threadHandle = OpenThread(SYNCHRONIZE, FALSE, threadIdentifier); 206 // If thread already exited, return immediately. 230 207 if (!threadHandle) 231 LOG_ERROR("ThreadIdentifier %u did not correspond to an active thread when trying to quit", threadID);208 return WAIT_OBJECT_0; 232 209 233 210 DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE); 234 211 if (joinResult == WAIT_FAILED) 235 LOG_ERROR("ThreadIdentifier % u was found to be deadlocked trying to quit", threadID);212 LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", threadHandle); 236 213 237 214 CloseHandle(threadHandle); 238 clearThreadHandleForIdentifier(threadID);239 215 240 216 return joinResult; 241 217 } 242 218 243 void detachThread(ThreadIdentifier threadID) 244 { 245 ASSERT(threadID); 246 247 HANDLE threadHandle = threadHandleForIdentifier(threadID); 248 if (threadHandle) 249 CloseHandle(threadHandle); 250 clearThreadHandleForIdentifier(threadID); 219 void detachThread(ThreadIdentifier) 220 { 251 221 } 252 222 253 223 ThreadIdentifier currentThread() 254 224 { 255 return static_cast<ThreadIdentifier>(GetCurrentThreadId());225 return ThreadIdentifier(GetCurrentThreadId()); 256 226 } 257 227 … … 471 441 } 472 442 443 // Deprecated functions. The current build of Safari 4 beta is linked with them so we need to provide 444 // the implementation taking PlatformThreadIdentifier instead of ThreadIdentifier. These are not declared 445 // in .h file so the next time Safari is built it will 'automatically' switch to new functions. 446 // Then deprecated ones can be removed from this file and from JavaScriptCore.def and WebKit.def files. 447 // In WebKit.def file, these functions are 'renamed' so their name does not have 'Deprecated' part - this 448 // is to avoid having 2 functions with only different type of return value. 449 void detachThreadDeprecated(PlatformThreadIdentifier) 450 { 451 } 452 453 int waitForThreadCompletionDeprecated(PlatformThreadIdentifier threadID, void** result) 454 { 455 ASSERT(threadID); 456 457 unsigned threadIdentifier = static_cast<unsigned>(threadID); 458 HANDLE threadHandle = OpenThread(SYNCHRONIZE, FALSE, threadIdentifier); 459 // If thread already exited, return immediately. 460 if (!threadHandle) 461 return WAIT_OBJECT_0; 462 463 DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE); 464 if (joinResult == WAIT_FAILED) 465 LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", threadHandle); 466 467 CloseHandle(threadHandle); 468 469 return joinResult; 470 } 471 472 PlatformThreadIdentifier currentThreadDeprecated() 473 { 474 return static_cast<PlatformThreadIdentifier>(GetCurrentThreadId()); 475 } 476 477 PlatformThreadIdentifier createThreadDeprecated(ThreadFunction entryPoint, void* data, const char* name) 478 { 479 ThreadIdentifier threadID = createThread(entryPoint, data, name); 480 return static_cast<PlatformThreadIdentifier>(threadID.platformId()); 481 } 482 473 483 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.