2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "Threading.h"
34 #include "CurrentTime.h"
36 #include "MainThread.h"
37 #include "RandomNumberSeed.h"
38 #include <wtf/WTFThreadData.h>
40 #include <QCoreApplication>
43 #include <QWaitCondition>
47 class ThreadPrivate : public QThread {
49 ThreadPrivate(ThreadFunction entryPoint, void* data);
51 void* getReturnValue() { return m_returnValue; }
54 ThreadFunction m_entryPoint;
58 ThreadPrivate::ThreadPrivate(ThreadFunction entryPoint, void* data)
60 , m_entryPoint(entryPoint)
65 void ThreadPrivate::run()
67 m_returnValue = m_entryPoint(m_data);
70 class ThreadMonitor : public QObject {
73 static ThreadMonitor * instance()
75 static ThreadMonitor *instance = new ThreadMonitor();
82 sender()->deleteLater();
86 static Mutex* atomicallyInitializedStaticMutex;
88 static Mutex& threadMapMutex()
94 static HashMap<ThreadIdentifier, QThread*>& threadMap()
96 static HashMap<ThreadIdentifier, QThread*> map;
100 static ThreadIdentifier identifierByQthreadHandle(QThread*& thread)
102 MutexLocker locker(threadMapMutex());
104 HashMap<ThreadIdentifier, QThread*>::iterator i = threadMap().begin();
105 for (; i != threadMap().end(); ++i) {
106 if (i->second == thread)
113 static ThreadIdentifier establishIdentifierForThread(QThread*& thread)
115 ASSERT(!identifierByQthreadHandle(thread));
117 MutexLocker locker(threadMapMutex());
119 static ThreadIdentifier identifierCount = 1;
121 threadMap().add(identifierCount, thread);
123 return identifierCount++;
126 static void clearThreadForIdentifier(ThreadIdentifier id)
128 MutexLocker locker(threadMapMutex());
130 ASSERT(threadMap().contains(id));
132 threadMap().remove(id);
135 static QThread* threadForIdentifier(ThreadIdentifier id)
137 MutexLocker locker(threadMapMutex());
139 return threadMap().get(id);
142 void initializeThreading()
144 if (!atomicallyInitializedStaticMutex) {
145 // StringImpl::empty() does not construct its static string in a threadsafe fashion,
146 // so ensure it has been initialized from here.
148 atomicallyInitializedStaticMutex = new Mutex;
150 initializeRandomNumberGenerator();
152 #if ENABLE(WTF_MULTIPLE_THREADS)
153 s_dtoaP5Mutex = new Mutex;
160 void lockAtomicallyInitializedStaticMutex()
162 ASSERT(atomicallyInitializedStaticMutex);
163 atomicallyInitializedStaticMutex->lock();
166 void unlockAtomicallyInitializedStaticMutex()
168 atomicallyInitializedStaticMutex->unlock();
171 ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
173 ThreadPrivate* thread = new ThreadPrivate(entryPoint, data);
175 LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
179 QObject::connect(thread, SIGNAL(finished()), ThreadMonitor::instance(), SLOT(threadFinished()));
183 QThread* threadRef = static_cast<QThread*>(thread);
185 return establishIdentifierForThread(threadRef);
188 void initializeCurrentThreadInternal(const char*)
192 int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
196 QThread* thread = threadForIdentifier(threadID);
198 bool res = thread->wait();
200 clearThreadForIdentifier(threadID);
202 *result = static_cast<ThreadPrivate*>(thread)->getReturnValue();
207 void detachThread(ThreadIdentifier threadID)
210 clearThreadForIdentifier(threadID);
213 ThreadIdentifier currentThread()
215 QThread* currentThread = QThread::currentThread();
216 if (ThreadIdentifier id = identifierByQthreadHandle(currentThread))
218 return establishIdentifierForThread(currentThread);
223 QThread::yieldCurrentThread();
227 : m_mutex(new QMutex())
241 bool Mutex::tryLock()
243 return m_mutex->tryLock();
251 ThreadCondition::ThreadCondition()
252 : m_condition(new QWaitCondition())
256 ThreadCondition::~ThreadCondition()
261 void ThreadCondition::wait(Mutex& mutex)
263 m_condition->wait(mutex.impl());
266 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
268 double currentTime = WTF::currentTime();
270 // Time is in the past - return immediately.
271 if (absoluteTime < currentTime)
274 // Time is too far in the future (and would overflow unsigned long) - wait forever.
275 if (absoluteTime - currentTime > static_cast<double>(INT_MAX) / 1000.0) {
280 double intervalMilliseconds = (absoluteTime - currentTime) * 1000.0;
281 return m_condition->wait(mutex.impl(), static_cast<unsigned long>(intervalMilliseconds));
284 void ThreadCondition::signal()
286 m_condition->wakeOne();
289 void ThreadCondition::broadcast()
291 m_condition->wakeAll();
294 } // namespace WebCore
296 #include "ThreadingQt.moc"