]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Threading.h
5e5f020b81293c6fcc6fd0385fb1027a5133fcb0
[apple/javascriptcore.git] / wtf / Threading.h
1 /*
2 * Copyright (C) 2007, 2008 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 *
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 #include <wtf/Platform.h>
63
64 #if PLATFORM(WIN_CE)
65 #include <windows.h>
66 #endif
67
68 #include <wtf/Assertions.h>
69 #include <wtf/Locker.h>
70 #include <wtf/Noncopyable.h>
71
72 #if PLATFORM(WIN_OS) && !PLATFORM(WIN_CE)
73 #include <windows.h>
74 #elif PLATFORM(DARWIN)
75 #include <libkern/OSAtomic.h>
76 #elif COMPILER(GCC)
77 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
78 #include <ext/atomicity.h>
79 #else
80 #include <bits/atomicity.h>
81 #endif
82 #endif
83
84 #if USE(PTHREADS)
85 #include <pthread.h>
86 #elif PLATFORM(GTK)
87 #include <wtf/GOwnPtr.h>
88 typedef struct _GMutex GMutex;
89 typedef struct _GCond GCond;
90 #endif
91
92 #if USE(PTHREADS) && (PLATFORM(MAC) || PLATFORM(IPHONE))
93 #ifdef __OBJC__
94 @class NSThread;
95 #else
96 class NSThread;
97 #endif
98 #endif
99
100 #if PLATFORM(QT)
101 #include <qglobal.h>
102 QT_BEGIN_NAMESPACE
103 class QMutex;
104 class QWaitCondition;
105 QT_END_NAMESPACE
106 #endif
107
108 #include <stdint.h>
109
110 // For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
111 #define AtomicallyInitializedStatic(T, name) \
112 WTF::lockAtomicallyInitializedStaticMutex(); \
113 static T name; \
114 WTF::unlockAtomicallyInitializedStaticMutex();
115
116 namespace WTF {
117
118 typedef uint32_t ThreadIdentifier;
119 typedef void* (*ThreadFunction)(void* argument);
120
121 // Returns 0 if thread creation failed
122 ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
123 ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadName);
124
125 ThreadIdentifier currentThread();
126 bool isMainThread();
127 int waitForThreadCompletion(ThreadIdentifier, void**);
128 void detachThread(ThreadIdentifier);
129
130 #if USE(PTHREADS)
131 typedef pthread_mutex_t PlatformMutex;
132 typedef pthread_cond_t PlatformCondition;
133 #elif PLATFORM(GTK)
134 typedef GOwnPtr<GMutex> PlatformMutex;
135 typedef GOwnPtr<GCond> PlatformCondition;
136 #elif PLATFORM(QT)
137 typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex;
138 typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition;
139 #elif PLATFORM(WIN_OS)
140 struct PlatformMutex {
141 CRITICAL_SECTION m_internalMutex;
142 size_t m_recursionCount;
143 };
144 struct PlatformCondition {
145 size_t m_waitersGone;
146 size_t m_waitersBlocked;
147 size_t m_waitersToUnblock;
148 HANDLE m_blockLock;
149 HANDLE m_blockQueue;
150 HANDLE m_unblockLock;
151
152 bool timedWait(PlatformMutex&, DWORD durationMilliseconds);
153 void signal(bool unblockAll);
154 };
155 #else
156 typedef void* PlatformMutex;
157 typedef void* PlatformCondition;
158 #endif
159
160 class Mutex : Noncopyable {
161 public:
162 Mutex();
163 ~Mutex();
164
165 void lock();
166 bool tryLock();
167 void unlock();
168
169 public:
170 PlatformMutex& impl() { return m_mutex; }
171 private:
172 PlatformMutex m_mutex;
173 };
174
175 typedef Locker<Mutex> MutexLocker;
176
177 class ThreadCondition : Noncopyable {
178 public:
179 ThreadCondition();
180 ~ThreadCondition();
181
182 void wait(Mutex& mutex);
183 // Returns true if the condition was signaled before absoluteTime, false if the absoluteTime was reached or is in the past.
184 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the same time zone as WTF::currentTime().
185 bool timedWait(Mutex&, double absoluteTime);
186 void signal();
187 void broadcast();
188
189 private:
190 PlatformCondition m_condition;
191 };
192
193 #if PLATFORM(WIN_OS)
194 #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
195
196 #if COMPILER(MINGW) || COMPILER(MSVC7) || PLATFORM(WIN_CE)
197 inline void atomicIncrement(int* addend) { InterlockedIncrement(reinterpret_cast<long*>(addend)); }
198 inline int atomicDecrement(int* addend) { return InterlockedDecrement(reinterpret_cast<long*>(addend)); }
199 #else
200 inline void atomicIncrement(int volatile* addend) { InterlockedIncrement(reinterpret_cast<long volatile*>(addend)); }
201 inline int atomicDecrement(int volatile* addend) { return InterlockedDecrement(reinterpret_cast<long volatile*>(addend)); }
202 #endif
203
204 #elif PLATFORM(DARWIN)
205 #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
206
207 inline void atomicIncrement(int volatile* addend) { OSAtomicIncrement32Barrier(const_cast<int*>(addend)); }
208 inline int atomicDecrement(int volatile* addend) { return OSAtomicDecrement32Barrier(const_cast<int*>(addend)); }
209
210 #elif COMPILER(GCC)
211 #define WTF_USE_LOCKFREE_THREADSAFESHARED 1
212
213 inline void atomicIncrement(int volatile* addend) { __gnu_cxx::__atomic_add(addend, 1); }
214 inline int atomicDecrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, -1) - 1; }
215
216 #endif
217
218 template<class T> class ThreadSafeShared : Noncopyable {
219 public:
220 ThreadSafeShared(int initialRefCount = 1)
221 : m_refCount(initialRefCount)
222 {
223 }
224
225 void ref()
226 {
227 #if USE(LOCKFREE_THREADSAFESHARED)
228 atomicIncrement(&m_refCount);
229 #else
230 MutexLocker locker(m_mutex);
231 ++m_refCount;
232 #endif
233 }
234
235 void deref()
236 {
237 #if USE(LOCKFREE_THREADSAFESHARED)
238 if (atomicDecrement(&m_refCount) <= 0)
239 #else
240 {
241 MutexLocker locker(m_mutex);
242 --m_refCount;
243 }
244 if (m_refCount <= 0)
245 #endif
246 delete static_cast<T*>(this);
247 }
248
249 bool hasOneRef()
250 {
251 return refCount() == 1;
252 }
253
254 int refCount() const
255 {
256 #if !USE(LOCKFREE_THREADSAFESHARED)
257 MutexLocker locker(m_mutex);
258 #endif
259 return static_cast<int const volatile &>(m_refCount);
260 }
261
262 private:
263 int m_refCount;
264 #if !USE(LOCKFREE_THREADSAFESHARED)
265 mutable Mutex m_mutex;
266 #endif
267 };
268
269 // This function must be called from the main thread. It is safe to call it repeatedly.
270 // 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.
271 void initializeThreading();
272
273 #if USE(PTHREADS) && (PLATFORM(MAC) || PLATFORM(IPHONE))
274 void initializeMainNSThread();
275 NSThread *mainNSThread();
276 #endif
277
278 void lockAtomicallyInitializedStaticMutex();
279 void unlockAtomicallyInitializedStaticMutex();
280
281 } // namespace WTF
282
283 using WTF::Mutex;
284 using WTF::MutexLocker;
285 using WTF::ThreadCondition;
286 using WTF::ThreadIdentifier;
287 using WTF::ThreadSafeShared;
288
289 using WTF::createThread;
290 using WTF::currentThread;
291 using WTF::isMainThread;
292 using WTF::detachThread;
293 using WTF::waitForThreadCompletion;
294
295 #endif // Threading_h