Changeset 43392 in webkit for trunk/JavaScriptCore/wtf/ThreadingPthreads.cpp
- Timestamp:
- May 7, 2009, 11:47:19 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/ThreadingPthreads.cpp
r43366 r43392 49 49 namespace WTF { 50 50 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 } 51 typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap; 60 52 61 53 static Mutex* atomicallyInitializedStaticMutex; … … 65 57 #endif 66 58 59 static Mutex& threadMapMutex() 60 { 61 DEFINE_STATIC_LOCAL(Mutex, mutex, ()); 62 return mutex; 63 } 64 67 65 void initializeThreading() 68 66 { 69 67 if (!atomicallyInitializedStaticMutex) { 70 68 atomicallyInitializedStaticMutex = new Mutex; 69 threadMapMutex(); 71 70 initializeRandomNumberGenerator(); 72 71 #if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM) … … 86 85 { 87 86 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); 88 135 } 89 136 … … 118 165 if (pthread_create(&threadHandle, 0, runThreadWithRegistration, static_cast<void*>(threadData))) { 119 166 LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data); 120 return ThreadIdentifier();121 } 122 return ThreadIdentifier(threadHandle);167 return 0; 168 } 169 return establishIdentifierForPthreadHandle(threadHandle); 123 170 } 124 171 #else … … 128 175 if (pthread_create(&threadHandle, 0, entryPoint, data)) { 129 176 LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data); 130 return ThreadIdentifier();131 } 132 133 return ThreadIdentifier(threadHandle);177 return 0; 178 } 179 180 return establishIdentifierForPthreadHandle(threadHandle); 134 181 } 135 182 #endif … … 146 193 int waitForThreadCompletion(ThreadIdentifier threadID, void** result) 147 194 { 148 ASSERT(threadID .isValid());149 150 pthread_t pthreadHandle = threadID.platformId();195 ASSERT(threadID); 196 197 pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID); 151 198 152 199 int joinResult = pthread_join(pthreadHandle, result); 153 200 if (joinResult == EDEADLK) 154 LOG_ERROR("ThreadIdentifier % p was found to be deadlocked trying to quit", pthreadHandle);201 LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID); 155 202 203 clearPthreadHandleForIdentifier(threadID); 156 204 return joinResult; 157 205 } … … 159 207 void detachThread(ThreadIdentifier threadID) 160 208 { 161 ASSERT(threadID .isValid());162 163 pthread_t pthreadHandle = threadID.platformId();209 ASSERT(threadID); 210 211 pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID); 164 212 165 213 pthread_detach(pthreadHandle); 214 215 clearPthreadHandleForIdentifier(threadID); 166 216 } 167 217 168 218 ThreadIdentifier currentThread() 169 219 { 170 return ThreadIdentifier(pthread_self()); 220 pthread_t currentThread = pthread_self(); 221 if (ThreadIdentifier id = identifierByPthreadHandle(currentThread)) 222 return id; 223 return establishIdentifierForPthreadHandle(currentThread); 171 224 } 172 225
Note:
See TracChangeset
for help on using the changeset viewer.