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 |
|
---|
39 | namespace WTF {
|
---|
40 |
|
---|
41 | class ThreadPrivate : public QThread {
|
---|
42 | public:
|
---|
43 | ThreadPrivate(ThreadFunction entryPoint, void* data);
|
---|
44 | void run();
|
---|
45 | void* getReturnValue() { return m_returnValue; }
|
---|
46 | private:
|
---|
47 | void* m_data;
|
---|
48 | ThreadFunction m_entryPoint;
|
---|
49 | void* m_returnValue;
|
---|
50 | };
|
---|
51 |
|
---|
52 | ThreadPrivate::ThreadPrivate(ThreadFunction entryPoint, void* data)
|
---|
53 | : m_data(data)
|
---|
54 | , m_entryPoint(entryPoint)
|
---|
55 | , m_returnValue(0)
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | void ThreadPrivate::run()
|
---|
60 | {
|
---|
61 | m_returnValue = m_entryPoint(m_data);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | Mutex* atomicallyInitializedStaticMutex;
|
---|
66 |
|
---|
67 | static ThreadIdentifier mainThreadIdentifier;
|
---|
68 |
|
---|
69 | static Mutex& threadMapMutex()
|
---|
70 | {
|
---|
71 | static Mutex mutex;
|
---|
72 | return mutex;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static HashMap<ThreadIdentifier, QThread*>& threadMap()
|
---|
76 | {
|
---|
77 | static HashMap<ThreadIdentifier, QThread*> map;
|
---|
78 | return map;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static 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 |
|
---|
92 | static void clearThreadForIdentifier(ThreadIdentifier id)
|
---|
93 | {
|
---|
94 | MutexLocker locker(threadMapMutex());
|
---|
95 |
|
---|
96 | ASSERT(threadMap().contains(id));
|
---|
97 |
|
---|
98 | threadMap().remove(id);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static 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 |
|
---|
114 | static QThread* threadForIdentifier(ThreadIdentifier id)
|
---|
115 | {
|
---|
116 | MutexLocker locker(threadMapMutex());
|
---|
117 |
|
---|
118 | return threadMap().get(id);
|
---|
119 | }
|
---|
120 |
|
---|
121 | void initializeThreading()
|
---|
122 | {
|
---|
123 | if(!atomicallyInitializedStaticMutex) {
|
---|
124 | atomicallyInitializedStaticMutex = new Mutex;
|
---|
125 | threadMapMutex();
|
---|
126 | wtf_random_init();
|
---|
127 | mainThreadIdentifier = currentThread();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | ThreadIdentifier 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 |
|
---|
145 | int 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 |
|
---|
159 | void detachThread(ThreadIdentifier)
|
---|
160 | {
|
---|
161 | }
|
---|
162 |
|
---|
163 | ThreadIdentifier currentThread()
|
---|
164 | {
|
---|
165 | QThread* currentThread = QThread::currentThread();
|
---|
166 | if (ThreadIdentifier id = identifierByQthreadHandle(currentThread))
|
---|
167 | return id;
|
---|
168 | return establishIdentifierForThread(currentThread);
|
---|
169 | }
|
---|
170 |
|
---|
171 | bool isMainThread()
|
---|
172 | {
|
---|
173 | return currentThread() == mainThreadIdentifier;
|
---|
174 | }
|
---|
175 |
|
---|
176 | Mutex::Mutex()
|
---|
177 | : m_mutex(new QMutex())
|
---|
178 | {
|
---|
179 | }
|
---|
180 |
|
---|
181 | Mutex::~Mutex()
|
---|
182 | {
|
---|
183 | delete m_mutex;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void Mutex::lock()
|
---|
187 | {
|
---|
188 | m_mutex->lock();
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool Mutex::tryLock()
|
---|
192 | {
|
---|
193 | return m_mutex->tryLock();
|
---|
194 | }
|
---|
195 |
|
---|
196 | void Mutex::unlock()
|
---|
197 | {
|
---|
198 | m_mutex->unlock();
|
---|
199 | }
|
---|
200 |
|
---|
201 | ThreadCondition::ThreadCondition()
|
---|
202 | : m_condition(new QWaitCondition())
|
---|
203 | {
|
---|
204 | }
|
---|
205 |
|
---|
206 | ThreadCondition::~ThreadCondition()
|
---|
207 | {
|
---|
208 | delete m_condition;
|
---|
209 | }
|
---|
210 |
|
---|
211 | void ThreadCondition::wait(Mutex& mutex)
|
---|
212 | {
|
---|
213 | m_condition->wait(mutex.impl());
|
---|
214 | }
|
---|
215 |
|
---|
216 | bool ThreadCondition::timedWait(Mutex& mutex, double interval)
|
---|
217 | {
|
---|
218 | return m_condition->wait(mutex.impl(), interval);
|
---|
219 | }
|
---|
220 |
|
---|
221 | void ThreadCondition::signal()
|
---|
222 | {
|
---|
223 | m_condition->wakeOne();
|
---|
224 | }
|
---|
225 |
|
---|
226 | void ThreadCondition::broadcast()
|
---|
227 | {
|
---|
228 | m_condition->wakeAll();
|
---|
229 | }
|
---|
230 |
|
---|
231 | } // namespace WebCore
|
---|