1 | /*
|
---|
2 | * Copyright (C) 2007, 2008 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 | *
|
---|
30 | * Note: The implementations of InterlockedIncrement and InterlockedDecrement are based
|
---|
31 | * on atomic_increment and atomic_exchange_and_add from the Boost C++ Library. The license
|
---|
32 | * is virtually identical to the Apple license above but is included here for completeness.
|
---|
33 | *
|
---|
34 | * Boost Software License - Version 1.0 - August 17th, 2003
|
---|
35 | *
|
---|
36 | * Permission is hereby granted, free of charge, to any person or organization
|
---|
37 | * obtaining a copy of the software and accompanying documentation covered by
|
---|
38 | * this license (the "Software") to use, reproduce, display, distribute,
|
---|
39 | * execute, and transmit the Software, and to prepare derivative works of the
|
---|
40 | * Software, and to permit third-parties to whom the Software is furnished to
|
---|
41 | * do so, all subject to the following:
|
---|
42 | *
|
---|
43 | * The copyright notices in the Software and this entire statement, including
|
---|
44 | * the above license grant, this restriction and the following disclaimer,
|
---|
45 | * must be included in all copies of the Software, in whole or in part, and
|
---|
46 | * all derivative works of the Software, unless such copies or derivative
|
---|
47 | * works are solely in the form of machine-executable object code generated by
|
---|
48 | * a source language processor.
|
---|
49 | *
|
---|
50 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
51 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
52 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
---|
53 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
---|
54 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
---|
55 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
---|
56 | * DEALINGS IN THE SOFTWARE.
|
---|
57 | */
|
---|
58 |
|
---|
59 | #ifndef Threading_h
|
---|
60 | #define Threading_h
|
---|
61 |
|
---|
62 | #if PLATFORM(WIN_CE)
|
---|
63 | #include <windows.h>
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #include <wtf/Assertions.h>
|
---|
67 | #include <wtf/Locker.h>
|
---|
68 | #include <wtf/Noncopyable.h>
|
---|
69 |
|
---|
70 | #if PLATFORM(WIN_OS) && !PLATFORM(WIN_CE)
|
---|
71 | #include <windows.h>
|
---|
72 | #elif PLATFORM(DARWIN)
|
---|
73 | #include <libkern/OSAtomic.h>
|
---|
74 | #elif COMPILER(GCC)
|
---|
75 | #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
|
---|
76 | #include <ext/atomicity.h>
|
---|
77 | #else
|
---|
78 | #include <bits/atomicity.h>
|
---|
79 | #endif
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | #if USE(PTHREADS)
|
---|
83 | #include <pthread.h>
|
---|
84 | #elif PLATFORM(GTK)
|
---|
85 | #include <wtf/GOwnPtr.h>
|
---|
86 | typedef struct _GMutex GMutex;
|
---|
87 | typedef struct _GCond GCond;
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #if PLATFORM(QT)
|
---|
91 | #include <qglobal.h>
|
---|
92 | QT_BEGIN_NAMESPACE
|
---|
93 | class QMutex;
|
---|
94 | class QWaitCondition;
|
---|
95 | QT_END_NAMESPACE
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | #include <stdint.h>
|
---|
99 |
|
---|
100 | // For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
|
---|
101 | #define AtomicallyInitializedStatic(T, name) \
|
---|
102 | WTF::lockAtomicallyInitializedStaticMutex(); \
|
---|
103 | static T name; \
|
---|
104 | WTF::unlockAtomicallyInitializedStaticMutex();
|
---|
105 |
|
---|
106 | namespace WTF {
|
---|
107 |
|
---|
108 | typedef uint32_t ThreadIdentifier;
|
---|
109 | typedef void* (*ThreadFunction)(void* argument);
|
---|
110 |
|
---|
111 | // Returns 0 if thread creation failed.
|
---|
112 | // The thread name must be a literal since on some platforms it's passed in to the thread.
|
---|
113 | ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
|
---|
114 |
|
---|
115 | // Internal platform-specific createThread implementation.
|
---|
116 | ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadName);
|
---|
117 |
|
---|
118 | // Called in the thread during initialization.
|
---|
119 | // Helpful for platforms where the thread name must be set from within the thread.
|
---|
120 | void setThreadNameInternal(const char* threadName);
|
---|
121 |
|
---|
122 | ThreadIdentifier currentThread();
|
---|
123 | bool isMainThread();
|
---|
124 | int waitForThreadCompletion(ThreadIdentifier, void**);
|
---|
125 | void detachThread(ThreadIdentifier);
|
---|
126 |
|
---|
127 | #if USE(PTHREADS)
|
---|
128 | typedef pthread_mutex_t PlatformMutex;
|
---|
129 | typedef pthread_cond_t PlatformCondition;
|
---|
130 | #elif PLATFORM(GTK)
|
---|
131 | typedef GOwnPtr<GMutex> PlatformMutex;
|
---|
132 | typedef GOwnPtr<GCond> PlatformCondition;
|
---|
133 | #elif PLATFORM(QT)
|
---|
134 | typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex;
|
---|
135 | typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition;
|
---|
136 | #elif PLATFORM(WIN_OS)
|
---|
137 | struct PlatformMutex {
|
---|
138 | CRITICAL_SECTION m_internalMutex;
|
---|
139 | size_t m_recursionCount;
|
---|
140 | };
|
---|
141 | struct PlatformCondition {
|
---|
142 | size_t m_waitersGone;
|
---|
143 | size_t m_waitersBlocked;
|
---|
144 | size_t m_waitersToUnblock;
|
---|
145 | HANDLE m_blockLock;
|
---|
146 | HANDLE m_blockQueue;
|
---|
147 | HANDLE m_unblockLock;
|
---|
148 |
|
---|
149 | bool timedWait(PlatformMutex&, DWORD durationMilliseconds);
|
---|
150 | void signal(bool unblockAll);
|
---|
151 | };
|
---|
152 | #else
|
---|
153 | typedef void* PlatformMutex;
|
---|
154 | typedef void* PlatformCondition;
|
---|
155 | #endif
|
---|
156 |
|
---|
157 | class Mutex : Noncopyable {
|
---|
158 | public:
|
---|
159 | Mutex();
|
---|
160 | ~Mutex();
|
---|
161 |
|
---|
162 | void lock();
|
---|
163 | bool tryLock();
|
---|
164 | void unlock();
|
---|
165 |
|
---|
166 | public:
|
---|
167 | PlatformMutex& impl() { return m_mutex; }
|
---|
168 | private:
|
---|
169 | PlatformMutex m_mutex;
|
---|
170 | };
|
---|
171 |
|
---|
172 | typedef Locker<Mutex> MutexLocker;
|
---|
173 |
|
---|
174 | class ThreadCondition : Noncopyable {
|
---|
175 | public:
|
---|
176 | ThreadCondition();
|
---|
177 | ~ThreadCondition();
|
---|
178 |
|
---|
179 | void wait(Mutex& mutex);
|
---|
180 | // Returns true if the condition was signaled before absoluteTime, false if the absoluteTime was reached or is in the past.
|
---|
181 | // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the same time zone as WTF::currentTime().
|
---|
182 | bool timedWait(Mutex&, double absoluteTime);
|
---|
183 | void signal();
|
---|
184 | void broadcast();
|
---|
185 |
|
---|
186 | private:
|
---|
187 | PlatformCondition m_condition;
|
---|
188 | };
|
---|
189 |
|
---|
190 | #if PLATFORM(WIN_OS)
|
---|
191 | #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
|
---|
192 |
|
---|
193 | #if COMPILER(MINGW) || COMPILER(MSVC7) || PLATFORM(WIN_CE)
|
---|
194 | inline void atomicIncrement(int* addend) { InterlockedIncrement(reinterpret_cast<long*>(addend)); }
|
---|
195 | inline int atomicDecrement(int* addend) { return InterlockedDecrement(reinterpret_cast<long*>(addend)); }
|
---|
196 | #else
|
---|
197 | inline void atomicIncrement(int volatile* addend) { InterlockedIncrement(reinterpret_cast<long volatile*>(addend)); }
|
---|
198 | inline int atomicDecrement(int volatile* addend) { return InterlockedDecrement(reinterpret_cast<long volatile*>(addend)); }
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | #elif PLATFORM(DARWIN)
|
---|
202 | #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
|
---|
203 |
|
---|
204 | inline void atomicIncrement(int volatile* addend) { OSAtomicIncrement32Barrier(const_cast<int*>(addend)); }
|
---|
205 | inline int atomicDecrement(int volatile* addend) { return OSAtomicDecrement32Barrier(const_cast<int*>(addend)); }
|
---|
206 |
|
---|
207 | #elif COMPILER(GCC)
|
---|
208 | #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
|
---|
209 |
|
---|
210 | inline void atomicIncrement(int volatile* addend) { __gnu_cxx::__atomic_add(addend, 1); }
|
---|
211 | inline int atomicDecrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, -1) - 1; }
|
---|
212 |
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | class ThreadSafeSharedBase : Noncopyable {
|
---|
216 | public:
|
---|
217 | ThreadSafeSharedBase(int initialRefCount = 1)
|
---|
218 | : m_refCount(initialRefCount)
|
---|
219 | {
|
---|
220 | }
|
---|
221 |
|
---|
222 | void ref()
|
---|
223 | {
|
---|
224 | #if USE(LOCKFREE_THREADSAFESHARED)
|
---|
225 | atomicIncrement(&m_refCount);
|
---|
226 | #else
|
---|
227 | MutexLocker locker(m_mutex);
|
---|
228 | ++m_refCount;
|
---|
229 | #endif
|
---|
230 | }
|
---|
231 |
|
---|
232 | bool hasOneRef()
|
---|
233 | {
|
---|
234 | return refCount() == 1;
|
---|
235 | }
|
---|
236 |
|
---|
237 | int refCount() const
|
---|
238 | {
|
---|
239 | #if !USE(LOCKFREE_THREADSAFESHARED)
|
---|
240 | MutexLocker locker(m_mutex);
|
---|
241 | #endif
|
---|
242 | return static_cast<int const volatile &>(m_refCount);
|
---|
243 | }
|
---|
244 |
|
---|
245 | protected:
|
---|
246 | // Returns whether the pointer should be freed or not.
|
---|
247 | bool derefBase()
|
---|
248 | {
|
---|
249 | #if USE(LOCKFREE_THREADSAFESHARED)
|
---|
250 | if (atomicDecrement(&m_refCount) <= 0)
|
---|
251 | return true;
|
---|
252 | #else
|
---|
253 | int refCount;
|
---|
254 | {
|
---|
255 | MutexLocker locker(m_mutex);
|
---|
256 | --m_refCount;
|
---|
257 | refCount = m_refCount;
|
---|
258 | }
|
---|
259 | if (refCount <= 0)
|
---|
260 | return true;
|
---|
261 | #endif
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 |
|
---|
265 | private:
|
---|
266 | template<class T>
|
---|
267 | friend class CrossThreadRefCounted;
|
---|
268 |
|
---|
269 | int m_refCount;
|
---|
270 | #if !USE(LOCKFREE_THREADSAFESHARED)
|
---|
271 | mutable Mutex m_mutex;
|
---|
272 | #endif
|
---|
273 | };
|
---|
274 |
|
---|
275 | template<class T> class ThreadSafeShared : public ThreadSafeSharedBase {
|
---|
276 | public:
|
---|
277 | ThreadSafeShared(int initialRefCount = 1)
|
---|
278 | : ThreadSafeSharedBase(initialRefCount)
|
---|
279 | {
|
---|
280 | }
|
---|
281 |
|
---|
282 | void deref()
|
---|
283 | {
|
---|
284 | if (derefBase())
|
---|
285 | delete static_cast<T*>(this);
|
---|
286 | }
|
---|
287 | };
|
---|
288 |
|
---|
289 | // This function must be called from the main thread. It is safe to call it repeatedly.
|
---|
290 | // Darwin is an exception to this rule: it is OK to call it from any thread, the only requirement is that the calls are not reentrant.
|
---|
291 | void initializeThreading();
|
---|
292 |
|
---|
293 | void lockAtomicallyInitializedStaticMutex();
|
---|
294 | void unlockAtomicallyInitializedStaticMutex();
|
---|
295 |
|
---|
296 | } // namespace WTF
|
---|
297 |
|
---|
298 | using WTF::Mutex;
|
---|
299 | using WTF::MutexLocker;
|
---|
300 | using WTF::ThreadCondition;
|
---|
301 | using WTF::ThreadIdentifier;
|
---|
302 | using WTF::ThreadSafeShared;
|
---|
303 |
|
---|
304 | using WTF::createThread;
|
---|
305 | using WTF::currentThread;
|
---|
306 | using WTF::isMainThread;
|
---|
307 | using WTF::detachThread;
|
---|
308 | using WTF::waitForThreadCompletion;
|
---|
309 |
|
---|
310 | #endif // Threading_h
|
---|