Changeset 43392 in webkit for trunk/JavaScriptCore/wtf/ThreadingWin.cpp
- Timestamp:
- May 7, 2009, 11:47:19 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/ThreadingWin.cpp
r43366 r43392 99 99 namespace WTF { 100 100 101 bool ThreadIdentifier::operator==(const ThreadIdentifier& another) const102 {103 return m_platformId == another.m_platformId;104 }105 106 bool ThreadIdentifier::operator!=(const ThreadIdentifier& another) const107 {108 return m_platformId != another.m_platformId;109 }110 111 101 // MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadNameInternal all come from <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>. 112 102 static const DWORD MS_VC_EXCEPTION = 0x406D1388; … … 150 140 static ThreadIdentifier mainThreadIdentifier; 151 141 142 static Mutex& threadMapMutex() 143 { 144 static Mutex mutex; 145 return mutex; 146 } 147 152 148 void initializeThreading() 153 149 { 154 150 if (!atomicallyInitializedStaticMutex) { 155 151 atomicallyInitializedStaticMutex = new Mutex; 152 threadMapMutex(); 156 153 initializeRandomNumberGenerator(); 157 154 initializeMainThread(); … … 161 158 } 162 159 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 163 186 struct ThreadFunctionInvocation { 164 187 ThreadFunctionInvocation(ThreadFunction function, void* data) : function(function), data(data) {} … … 186 209 { 187 210 unsigned threadIdentifier = 0; 211 ThreadIdentifier threadID = 0; 188 212 ThreadFunctionInvocation* invocation = new ThreadFunctionInvocation(entryPoint, data); 189 213 HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation, 0, &threadIdentifier)); 190 214 if (!threadHandle) { 191 215 LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno); 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); 216 return 0; 217 } 218 219 threadID = static_cast<ThreadIdentifier>(threadIdentifier); 220 storeThreadHandleByIdentifier(threadIdentifier, threadHandle); 221 222 return threadID; 198 223 } 199 224 200 225 int waitForThreadCompletion(ThreadIdentifier threadID, void** result) 201 226 { 202 ASSERT(threadID .isValid());227 ASSERT(threadID); 203 228 204 unsigned threadIdentifier = threadID.platformId(); 205 HANDLE threadHandle = OpenThread(SYNCHRONIZE, FALSE, threadIdentifier); 206 // If thread already exited, return immediately. 229 HANDLE threadHandle = threadHandleForIdentifier(threadID); 207 230 if (!threadHandle) 208 return WAIT_OBJECT_0;231 LOG_ERROR("ThreadIdentifier %u did not correspond to an active thread when trying to quit", threadID); 209 232 210 233 DWORD joinResult = WaitForSingleObject(threadHandle, INFINITE); 211 234 if (joinResult == WAIT_FAILED) 212 LOG_ERROR("ThreadIdentifier % p was found to be deadlocked trying to quit", threadHandle);235 LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID); 213 236 214 237 CloseHandle(threadHandle); 238 clearThreadHandleForIdentifier(threadID); 215 239 216 240 return joinResult; 217 241 } 218 242 219 void detachThread(ThreadIdentifier) 220 { 243 void detachThread(ThreadIdentifier threadID) 244 { 245 ASSERT(threadID); 246 247 HANDLE threadHandle = threadHandleForIdentifier(threadID); 248 if (threadHandle) 249 CloseHandle(threadHandle); 250 clearThreadHandleForIdentifier(threadID); 221 251 } 222 252 223 253 ThreadIdentifier currentThread() 224 254 { 225 return ThreadIdentifier(GetCurrentThreadId());255 return static_cast<ThreadIdentifier>(GetCurrentThreadId()); 226 256 } 227 257 … … 441 471 } 442 472 443 // Deprecated functions. The current build of Safari 4 beta is linked with them so we need to provide444 // the implementation taking PlatformThreadIdentifier instead of ThreadIdentifier. These are not declared445 // 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 - this448 // 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 483 473 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.