source: webkit/trunk/JavaScriptCore/wtf/ThreadingQt.cpp@ 35419

Last change on this file since 35419 was 32949, checked in by Simon Hausmann, 17 years ago

2008-05-07 Ariya Hidayat <[email protected]>

Reviewed by Simon.

Support for isMainThread in the Qt port.

  • wtf/ThreadingQt.cpp: (WTF::initializeThreading): Adjusted. (WTF::isMainThread): Added.
File size: 5.4 KB
Line 
1/*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood ([email protected])
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#include <wtf/HashMap.h>
33#include <wtf/MathExtras.h>
34
35#include <QMutex>
36#include <QThread>
37#include <QWaitCondition>
38
39namespace WTF {
40
41class ThreadPrivate : public QThread {
42public:
43 ThreadPrivate(ThreadFunction entryPoint, void* data);
44 void run();
45 void* getReturnValue() { return m_returnValue; }
46private:
47 void* m_data;
48 ThreadFunction m_entryPoint;
49 void* m_returnValue;
50};
51
52ThreadPrivate::ThreadPrivate(ThreadFunction entryPoint, void* data)
53 : m_data(data)
54 , m_entryPoint(entryPoint)
55 , m_returnValue(0)
56{
57}
58
59void ThreadPrivate::run()
60{
61 m_returnValue = m_entryPoint(m_data);
62}
63
64
65Mutex* atomicallyInitializedStaticMutex;
66
67static ThreadIdentifier mainThreadIdentifier;
68
69static Mutex& threadMapMutex()
70{
71 static Mutex mutex;
72 return mutex;
73}
74
75static HashMap<ThreadIdentifier, QThread*>& threadMap()
76{
77 static HashMap<ThreadIdentifier, QThread*> map;
78 return map;
79}
80
81static ThreadIdentifier establishIdentifierForThread(QThread*& thread)
82{
83 MutexLocker locker(threadMapMutex());
84
85 static ThreadIdentifier identifierCount = 1;
86
87 threadMap().add(identifierCount, thread);
88
89 return identifierCount++;
90}
91
92static void clearThreadForIdentifier(ThreadIdentifier id)
93{
94 MutexLocker locker(threadMapMutex());
95
96 ASSERT(threadMap().contains(id));
97
98 threadMap().remove(id);
99}
100
101static ThreadIdentifier identifierByQthreadHandle(QThread*& thread)
102{
103 MutexLocker locker(threadMapMutex());
104
105 HashMap<ThreadIdentifier, QThread*>::iterator i = threadMap().begin();
106 for (; i != threadMap().end(); ++i) {
107 if (i->second == thread)
108 return i->first;
109 }
110
111 return 0;
112}
113
114static QThread* threadForIdentifier(ThreadIdentifier id)
115{
116 MutexLocker locker(threadMapMutex());
117
118 return threadMap().get(id);
119}
120
121void initializeThreading()
122{
123 if(!atomicallyInitializedStaticMutex) {
124 atomicallyInitializedStaticMutex = new Mutex;
125 threadMapMutex();
126 wtf_random_init();
127 mainThreadIdentifier = currentThread();
128 }
129}
130
131ThreadIdentifier createThread(ThreadFunction entryPoint, void* data)
132{
133 ThreadPrivate* thread = new ThreadPrivate(entryPoint, data);
134 if (!thread) {
135 LOG_ERROR("Failed to create thread at entry point %p with data %p", entryPoint, data);
136 return 0;
137 }
138 thread->start();
139
140 QThread* threadRef = static_cast<QThread*>(thread);
141
142 return establishIdentifierForThread(threadRef);
143}
144
145int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
146{
147 ASSERT(threadID);
148
149 QThread* thread = threadForIdentifier(threadID);
150
151 bool res = thread->wait();
152
153 clearThreadForIdentifier(threadID);
154 *result = static_cast<ThreadPrivate*>(thread)->getReturnValue();
155
156 return !res;
157}
158
159void detachThread(ThreadIdentifier)
160{
161}
162
163ThreadIdentifier currentThread()
164{
165 QThread* currentThread = QThread::currentThread();
166 if (ThreadIdentifier id = identifierByQthreadHandle(currentThread))
167 return id;
168 return establishIdentifierForThread(currentThread);
169}
170
171bool isMainThread()
172{
173 return currentThread() == mainThreadIdentifier;
174}
175
176Mutex::Mutex()
177 : m_mutex(new QMutex())
178{
179}
180
181Mutex::~Mutex()
182{
183 delete m_mutex;
184}
185
186void Mutex::lock()
187{
188 m_mutex->lock();
189}
190
191bool Mutex::tryLock()
192{
193 return m_mutex->tryLock();
194}
195
196void Mutex::unlock()
197{
198 m_mutex->unlock();
199}
200
201ThreadCondition::ThreadCondition()
202 : m_condition(new QWaitCondition())
203{
204}
205
206ThreadCondition::~ThreadCondition()
207{
208 delete m_condition;
209}
210
211void ThreadCondition::wait(Mutex& mutex)
212{
213 m_condition->wait(mutex.impl());
214}
215
216bool ThreadCondition::timedWait(Mutex& mutex, double interval)
217{
218 return m_condition->wait(mutex.impl(), interval);
219}
220
221void ThreadCondition::signal()
222{
223 m_condition->wakeOne();
224}
225
226void ThreadCondition::broadcast()
227{
228 m_condition->wakeAll();
229}
230
231} // namespace WebCore
Note: See TracBrowser for help on using the repository browser.