| 1 | /* |
| 2 | * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | #include "config.h" |
| 30 | #include "Threading.h" |
| 31 | |
| 32 | #if !ENABLE(SINGLE_THREADED) |
| 33 | |
| 34 | #include "CurrentTime.h" |
| 35 | #include "HashMap.h" |
| 36 | #include "MainThread.h" |
| 37 | #include "RandomNumberSeed.h" |
| 38 | |
| 39 | #include <QtCore/qcoreapplication.h> |
| 40 | #include <QtCore/qmutex.h> |
| 41 | #include <QtCore/qthread.h> |
| 42 | #include <QtCore/qwaitcondition.h> |
| 43 | |
| 44 | namespace WTF { |
| 45 | |
| 46 | QT_USE_NAMESPACE |
| 47 | |
| 48 | class ThreadPrivate : public QThread { |
| 49 | public: |
| 50 | ThreadPrivate(ThreadFunction entryPoint, void* data); |
| 51 | void run(); |
| 52 | void* getReturnValue() { return m_returnValue; } |
| 53 | private: |
| 54 | void* m_data; |
| 55 | ThreadFunction m_entryPoint; |
| 56 | void* m_returnValue; |
| 57 | }; |
| 58 | |
| 59 | ThreadPrivate::ThreadPrivate(ThreadFunction entryPoint, void* data) |
| 60 | : m_data(data) |
| 61 | , m_entryPoint(entryPoint) |
| 62 | , m_returnValue(0) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void ThreadPrivate::run() |
| 67 | { |
| 68 | m_returnValue = m_entryPoint(m_data); |
| 69 | } |
| 70 | |
| 71 | class ThreadMonitor : public QObject { |
| 72 | Q_OBJECT |
| 73 | public: |
| 74 | static ThreadMonitor * instance() |
| 75 | { |
| 76 | static ThreadMonitor *instance = new ThreadMonitor(); |
| 77 | return instance; |
| 78 | } |
| 79 | |
| 80 | public Q_SLOTS: |
| 81 | void threadFinished() |
| 82 | { |
| 83 | sender()->deleteLater(); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | static Mutex* atomicallyInitializedStaticMutex; |
| 88 | |
| 89 | static ThreadIdentifier mainThreadIdentifier; |
| 90 | |
| 91 | static Mutex& threadMapMutex() |
| 92 | { |
| 93 | static Mutex mutex; |
| 94 | return mutex; |
| 95 | } |
| 96 | |
| 97 | static HashMap<ThreadIdentifier, QThread*>& threadMap() |
| 98 | { |
| 99 | static HashMap<ThreadIdentifier, QThread*> map; |
| 100 | return map; |
| 101 | } |
| 102 | |
| 103 | static ThreadIdentifier identifierByQthreadHandle(QThread*& thread) |
| 104 | { |
| 105 | MutexLocker locker(threadMapMutex()); |
| 106 | |
| 107 | HashMap<ThreadIdentifier, QThread*>::iterator i = threadMap().begin(); |
| 108 | for (; i != threadMap().end(); ++i) { |
| 109 | if (i->second == thread) |
| 110 | return i->first; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static ThreadIdentifier establishIdentifierForThread(QThread*& thread) |
| 117 | { |
| 118 | ASSERT(!identifierByQthreadHandle(thread)); |
| 119 | |
| 120 | MutexLocker locker(threadMapMutex()); |
| 121 | |
| 122 | static ThreadIdentifier identifierCount = 1; |
| 123 | |
| 124 | threadMap().add(key: identifierCount, mapped: thread); |
| 125 | |
| 126 | return identifierCount++; |
| 127 | } |
| 128 | |
| 129 | static void clearThreadForIdentifier(ThreadIdentifier id) |
| 130 | { |
| 131 | MutexLocker locker(threadMapMutex()); |
| 132 | |
| 133 | ASSERT(threadMap().contains(id)); |
| 134 | |
| 135 | threadMap().remove(key: id); |
| 136 | } |
| 137 | |
| 138 | static QThread* threadForIdentifier(ThreadIdentifier id) |
| 139 | { |
| 140 | MutexLocker locker(threadMapMutex()); |
| 141 | |
| 142 | return threadMap().get(key: id); |
| 143 | } |
| 144 | |
| 145 | void initializeThreading() |
| 146 | { |
| 147 | if (!atomicallyInitializedStaticMutex) { |
| 148 | atomicallyInitializedStaticMutex = new Mutex; |
| 149 | threadMapMutex(); |
| 150 | initializeRandomNumberGenerator(); |
| 151 | QThread* mainThread = QCoreApplication::instance()->thread(); |
| 152 | mainThreadIdentifier = identifierByQthreadHandle(thread&: mainThread); |
| 153 | if (!mainThreadIdentifier) |
| 154 | mainThreadIdentifier = establishIdentifierForThread(thread&: mainThread); |
| 155 | initializeMainThread(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void lockAtomicallyInitializedStaticMutex() |
| 160 | { |
| 161 | ASSERT(atomicallyInitializedStaticMutex); |
| 162 | atomicallyInitializedStaticMutex->lock(); |
| 163 | } |
| 164 | |
| 165 | void unlockAtomicallyInitializedStaticMutex() |
| 166 | { |
| 167 | atomicallyInitializedStaticMutex->unlock(); |
| 168 | } |
| 169 | |
| 170 | ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*) |
| 171 | { |
| 172 | ThreadPrivate* thread = new ThreadPrivate(entryPoint, data); |
| 173 | if (!thread) { |
| 174 | LOG_ERROR("Failed to create thread at entry point %p with data %p" , entryPoint, data); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | QObject::connect(sender: thread, SIGNAL(finished()), receiver: ThreadMonitor::instance(), SLOT(threadFinished())); |
| 179 | |
| 180 | thread->start(); |
| 181 | |
| 182 | QThread* threadRef = static_cast<QThread*>(thread); |
| 183 | |
| 184 | return establishIdentifierForThread(thread&: threadRef); |
| 185 | } |
| 186 | |
| 187 | void initializeCurrentThreadInternal(const char*) |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | int waitForThreadCompletion(ThreadIdentifier threadID, void** result) |
| 192 | { |
| 193 | ASSERT(threadID); |
| 194 | |
| 195 | QThread* thread = threadForIdentifier(id: threadID); |
| 196 | |
| 197 | bool res = thread->wait(); |
| 198 | |
| 199 | clearThreadForIdentifier(id: threadID); |
| 200 | if (result) |
| 201 | *result = static_cast<ThreadPrivate*>(thread)->getReturnValue(); |
| 202 | |
| 203 | return !res; |
| 204 | } |
| 205 | |
| 206 | void detachThread(ThreadIdentifier threadID) |
| 207 | { |
| 208 | ASSERT(threadID); |
| 209 | clearThreadForIdentifier(id: threadID); |
| 210 | } |
| 211 | |
| 212 | ThreadIdentifier currentThread() |
| 213 | { |
| 214 | QThread* currentThread = QThread::currentThread(); |
| 215 | if (ThreadIdentifier id = identifierByQthreadHandle(thread&: currentThread)) |
| 216 | return id; |
| 217 | return establishIdentifierForThread(thread&: currentThread); |
| 218 | } |
| 219 | |
| 220 | bool isMainThread() |
| 221 | { |
| 222 | return QThread::currentThread() == QCoreApplication::instance()->thread(); |
| 223 | } |
| 224 | |
| 225 | Mutex::Mutex() |
| 226 | : m_mutex(new QMutex()) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | Mutex::~Mutex() |
| 231 | { |
| 232 | delete m_mutex; |
| 233 | } |
| 234 | |
| 235 | void Mutex::lock() |
| 236 | { |
| 237 | m_mutex->lock(); |
| 238 | } |
| 239 | |
| 240 | bool Mutex::tryLock() |
| 241 | { |
| 242 | return m_mutex->tryLock(); |
| 243 | } |
| 244 | |
| 245 | void Mutex::unlock() |
| 246 | { |
| 247 | m_mutex->unlock(); |
| 248 | } |
| 249 | |
| 250 | ThreadCondition::ThreadCondition() |
| 251 | : m_condition(new QWaitCondition()) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | ThreadCondition::~ThreadCondition() |
| 256 | { |
| 257 | delete m_condition; |
| 258 | } |
| 259 | |
| 260 | void ThreadCondition::wait(Mutex& mutex) |
| 261 | { |
| 262 | m_condition->wait(lockedMutex: mutex.impl()); |
| 263 | } |
| 264 | |
| 265 | bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime) |
| 266 | { |
| 267 | double currentTime = WTF::currentTime(); |
| 268 | |
| 269 | // Time is in the past - return immediately. |
| 270 | if (absoluteTime < currentTime) |
| 271 | return false; |
| 272 | |
| 273 | // Time is too far in the future (and would overflow unsigned long) - wait forever. |
| 274 | if (absoluteTime - currentTime > static_cast<double>(INT_MAX) / 1000.0) { |
| 275 | wait(mutex); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | double intervalMilliseconds = (absoluteTime - currentTime) * 1000.0; |
| 280 | return m_condition->wait(lockedMutex: mutex.impl(), time: static_cast<unsigned long>(intervalMilliseconds)); |
| 281 | } |
| 282 | |
| 283 | void ThreadCondition::signal() |
| 284 | { |
| 285 | m_condition->wakeOne(); |
| 286 | } |
| 287 | |
| 288 | void ThreadCondition::broadcast() |
| 289 | { |
| 290 | m_condition->wakeAll(); |
| 291 | } |
| 292 | |
| 293 | } // namespace WebCore |
| 294 | |
| 295 | #include "ThreadingQt.moc" |
| 296 | |
| 297 | #endif |
| 298 | |