Changeset 43507 in webkit for trunk/JavaScriptCore/wtf/ThreadingPthreads.cpp
- Timestamp:
- May 11, 2009, 12:49:04 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/ThreadingPthreads.cpp
r43392 r43507 49 49 namespace WTF { 50 50 51 typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap; 51 bool ThreadIdentifier::operator==(const ThreadIdentifier& another) const 52 { 53 return pthread_equal(m_platformId, another.m_platformId); 54 } 55 56 bool ThreadIdentifier::operator!=(const ThreadIdentifier& another) const 57 { 58 return !pthread_equal(m_platformId, another.m_platformId); 59 } 52 60 53 61 static Mutex* atomicallyInitializedStaticMutex; … … 57 65 #endif 58 66 59 static Mutex& threadMapMutex()60 {61 DEFINE_STATIC_LOCAL(Mutex, mutex, ());62 return mutex;63 }64 65 67 void initializeThreading() 66 68 { 67 69 if (!atomicallyInitializedStaticMutex) { 68 70 atomicallyInitializedStaticMutex = new Mutex; 69 threadMapMutex();70 71 initializeRandomNumberGenerator(); 71 72 #if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM) … … 85 86 { 86 87 atomicallyInitializedStaticMutex->unlock(); 87 }88 89 static ThreadMap& threadMap()90 {91 DEFINE_STATIC_LOCAL(ThreadMap, map, ());92 return map;93 }94 95 static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle)96 {97 MutexLocker locker(threadMapMutex());98 99 ThreadMap::iterator i = threadMap().begin();100 for (; i != threadMap().end(); ++i) {101 if (pthread_equal(i->second, pthreadHandle))102 return i->first;103 }104 105 return 0;106 }107 108 static ThreadIdentifier establishIdentifierForPthreadHandle(pthread_t& pthreadHandle)109 {110 ASSERT(!identifierByPthreadHandle(pthreadHandle));111 112 MutexLocker locker(threadMapMutex());113 114 static ThreadIdentifier identifierCount = 1;115 116 threadMap().add(identifierCount, pthreadHandle);117 118 return identifierCount++;119 }120 121 static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id)122 {123 MutexLocker locker(threadMapMutex());124 125 return threadMap().get(id);126 }127 128 static void clearPthreadHandleForIdentifier(ThreadIdentifier id)129 {130 MutexLocker locker(threadMapMutex());131 132 ASSERT(threadMap().contains(id));133 134 threadMap().remove(id);135 88 } 136 89 … … 165 118 if (pthread_create(&threadHandle, 0, runThreadWithRegistration, static_cast<void*>(threadData))) { 166 119 LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data); 167 return 0;168 } 169 return establishIdentifierForPthreadHandle(threadHandle);120 return ThreadIdentifier(); 121 } 122 return ThreadIdentifier(threadHandle); 170 123 } 171 124 #else … … 175 128 if (pthread_create(&threadHandle, 0, entryPoint, data)) { 176 129 LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data); 177 return 0;178 } 179 180 return establishIdentifierForPthreadHandle(threadHandle);130 return ThreadIdentifier(); 131 } 132 133 return ThreadIdentifier(threadHandle); 181 134 } 182 135 #endif … … 193 146 int waitForThreadCompletion(ThreadIdentifier threadID, void** result) 194 147 { 195 ASSERT(threadID );196 197 pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);148 ASSERT(threadID.isValid()); 149 150 pthread_t pthreadHandle = threadID.platformId(); 198 151 199 152 int joinResult = pthread_join(pthreadHandle, result); 200 153 if (joinResult == EDEADLK) 201 LOG_ERROR("ThreadIdentifier % u was found to be deadlocked trying to quit", threadID);154 LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", pthreadHandle); 202 155 203 clearPthreadHandleForIdentifier(threadID);204 156 return joinResult; 205 157 } … … 207 159 void detachThread(ThreadIdentifier threadID) 208 160 { 209 ASSERT(threadID );210 211 pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);161 ASSERT(threadID.isValid()); 162 163 pthread_t pthreadHandle = threadID.platformId(); 212 164 213 165 pthread_detach(pthreadHandle); 214 215 clearPthreadHandleForIdentifier(threadID);216 166 } 217 167 218 168 ThreadIdentifier currentThread() 219 169 { 220 pthread_t currentThread = pthread_self(); 221 if (ThreadIdentifier id = identifierByPthreadHandle(currentThread)) 222 return id; 223 return establishIdentifierForPthreadHandle(currentThread); 170 return ThreadIdentifier(pthread_self()); 224 171 } 225 172 … … 315 262 ASSERT_UNUSED(result, !result); 316 263 } 317 264 265 // Derecated function. Safari 4 beta, until recompiled next time, uses ThreadIdentifier as uint32_t. 266 // pthread_t is a pointer. So they get a pointer casted into uint32_t from CurrentThread() 267 // and then pass it here. We cast it back to pointer. This is an ugly hack which is very temporary 268 // and will be removed once next public build of Safari 4 is released. 269 int waitForThreadCompletion(uint32_t threadID, void** result) 270 { 271 pthread_t pthreadHandle = reinterpret_cast<pthread_t>(threadID); 272 273 int joinResult = pthread_join(pthreadHandle, result); 274 if (joinResult == EDEADLK) 275 LOG_ERROR("ThreadIdentifier %p was found to be deadlocked trying to quit", pthreadHandle); 276 277 return joinResult; 278 } 279 318 280 } // namespace WTF 319 281
Note:
See TracChangeset
for help on using the changeset viewer.