source: webkit/trunk/JavaScriptCore/wtf/Threading.h@ 48265

Last change on this file since 48265 was 48161, checked in by [email protected], 16 years ago

2009-09-08 Laszlo Gombos <Laszlo Gombos>

Reviewed by Simon Hausmann.

Build fix when USE(LOCKFREE_THREADSAFESHARED) is not defined
https://bugs.webkit.org/show_bug.cgi?id=29011

  • wtf/Threading.h: Use LOCKFREE_THREADSAFESHARED guard for atomicIncrement and atomicDecrement
  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
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#include "Platform.h"
63
64#if PLATFORM(WINCE)
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(WINCE)
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>
88typedef struct _GMutex GMutex;
89typedef struct _GCond GCond;
90#endif
91
92#if PLATFORM(QT)
93#include <qglobal.h>
94QT_BEGIN_NAMESPACE
95class QMutex;
96class QWaitCondition;
97QT_END_NAMESPACE
98#endif
99
100#include <stdint.h>
101
102// For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
103#define AtomicallyInitializedStatic(T, name) \
104 WTF::lockAtomicallyInitializedStaticMutex(); \
105 static T name; \
106 WTF::unlockAtomicallyInitializedStaticMutex();
107
108namespace WTF {
109
110typedef uint32_t ThreadIdentifier;
111typedef void* (*ThreadFunction)(void* argument);
112
113// Returns 0 if thread creation failed.
114// The thread name must be a literal since on some platforms it's passed in to the thread.
115ThreadIdentifier createThread(ThreadFunction, void*, const char* threadName);
116
117// Internal platform-specific createThread implementation.
118ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadName);
119
120// Called in the thread during initialization.
121// Helpful for platforms where the thread name must be set from within the thread.
122void setThreadNameInternal(const char* threadName);
123
124ThreadIdentifier currentThread();
125bool isMainThread();
126int waitForThreadCompletion(ThreadIdentifier, void**);
127void detachThread(ThreadIdentifier);
128
129#if USE(PTHREADS)
130typedef pthread_mutex_t PlatformMutex;
131typedef pthread_rwlock_t PlatformReadWriteLock;
132typedef pthread_cond_t PlatformCondition;
133#elif PLATFORM(GTK)
134typedef GOwnPtr<GMutex> PlatformMutex;
135typedef void* PlatformReadWriteLock; // FIXME: Implement.
136typedef GOwnPtr<GCond> PlatformCondition;
137#elif PLATFORM(QT)
138typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex;
139typedef void* PlatformReadWriteLock; // FIXME: Implement.
140typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition;
141#elif PLATFORM(WIN_OS)
142struct PlatformMutex {
143 CRITICAL_SECTION m_internalMutex;
144 size_t m_recursionCount;
145};
146typedef void* PlatformReadWriteLock; // FIXME: Implement.
147struct PlatformCondition {
148 size_t m_waitersGone;
149 size_t m_waitersBlocked;
150 size_t m_waitersToUnblock;
151 HANDLE m_blockLock;
152 HANDLE m_blockQueue;
153 HANDLE m_unblockLock;
154
155 bool timedWait(PlatformMutex&, DWORD durationMilliseconds);
156 void signal(bool unblockAll);
157};
158#else
159typedef void* PlatformMutex;
160typedef void* PlatformReadWriteLock;
161typedef void* PlatformCondition;
162#endif
163
164class Mutex : public Noncopyable {
165public:
166 Mutex();
167 ~Mutex();
168
169 void lock();
170 bool tryLock();
171 void unlock();
172
173public:
174 PlatformMutex& impl() { return m_mutex; }
175private:
176 PlatformMutex m_mutex;
177};
178
179typedef Locker<Mutex> MutexLocker;
180
181class ReadWriteLock : public Noncopyable {
182public:
183 ReadWriteLock();
184 ~ReadWriteLock();
185
186 void readLock();
187 bool tryReadLock();
188
189 void writeLock();
190 bool tryWriteLock();
191
192 void unlock();
193
194private:
195 PlatformReadWriteLock m_readWriteLock;
196};
197
198class ThreadCondition : public Noncopyable {
199public:
200 ThreadCondition();
201 ~ThreadCondition();
202
203 void wait(Mutex& mutex);
204 // Returns true if the condition was signaled before absoluteTime, false if the absoluteTime was reached or is in the past.
205 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the same time zone as WTF::currentTime().
206 bool timedWait(Mutex&, double absoluteTime);
207 void signal();
208 void broadcast();
209
210private:
211 PlatformCondition m_condition;
212};
213
214#if PLATFORM(WIN_OS)
215#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
216
217#if COMPILER(MINGW) || COMPILER(MSVC7) || PLATFORM(WINCE)
218inline int atomicIncrement(int* addend) { return InterlockedIncrement(reinterpret_cast<long*>(addend)); }
219inline int atomicDecrement(int* addend) { return InterlockedDecrement(reinterpret_cast<long*>(addend)); }
220#else
221inline int atomicIncrement(int volatile* addend) { return InterlockedIncrement(reinterpret_cast<long volatile*>(addend)); }
222inline int atomicDecrement(int volatile* addend) { return InterlockedDecrement(reinterpret_cast<long volatile*>(addend)); }
223#endif
224
225#elif PLATFORM(DARWIN)
226#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
227
228inline int atomicIncrement(int volatile* addend) { return OSAtomicIncrement32Barrier(const_cast<int*>(addend)); }
229inline int atomicDecrement(int volatile* addend) { return OSAtomicDecrement32Barrier(const_cast<int*>(addend)); }
230
231#elif COMPILER(GCC)
232#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
233
234inline int atomicIncrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, 1) + 1; }
235inline int atomicDecrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, -1) - 1; }
236
237#endif
238
239class ThreadSafeSharedBase : public Noncopyable {
240public:
241 ThreadSafeSharedBase(int initialRefCount = 1)
242 : m_refCount(initialRefCount)
243 {
244 }
245
246 void ref()
247 {
248#if USE(LOCKFREE_THREADSAFESHARED)
249 atomicIncrement(&m_refCount);
250#else
251 MutexLocker locker(m_mutex);
252 ++m_refCount;
253#endif
254 }
255
256 bool hasOneRef()
257 {
258 return refCount() == 1;
259 }
260
261 int refCount() const
262 {
263#if !USE(LOCKFREE_THREADSAFESHARED)
264 MutexLocker locker(m_mutex);
265#endif
266 return static_cast<int const volatile &>(m_refCount);
267 }
268
269protected:
270 // Returns whether the pointer should be freed or not.
271 bool derefBase()
272 {
273#if USE(LOCKFREE_THREADSAFESHARED)
274 if (atomicDecrement(&m_refCount) <= 0)
275 return true;
276#else
277 int refCount;
278 {
279 MutexLocker locker(m_mutex);
280 --m_refCount;
281 refCount = m_refCount;
282 }
283 if (refCount <= 0)
284 return true;
285#endif
286 return false;
287 }
288
289private:
290 template<class T>
291 friend class CrossThreadRefCounted;
292
293 int m_refCount;
294#if !USE(LOCKFREE_THREADSAFESHARED)
295 mutable Mutex m_mutex;
296#endif
297};
298
299template<class T> class ThreadSafeShared : public ThreadSafeSharedBase {
300public:
301 ThreadSafeShared(int initialRefCount = 1)
302 : ThreadSafeSharedBase(initialRefCount)
303 {
304 }
305
306 void deref()
307 {
308 if (derefBase())
309 delete static_cast<T*>(this);
310 }
311};
312
313// This function must be called from the main thread. It is safe to call it repeatedly.
314// 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.
315void initializeThreading();
316
317void lockAtomicallyInitializedStaticMutex();
318void unlockAtomicallyInitializedStaticMutex();
319
320} // namespace WTF
321
322using WTF::Mutex;
323using WTF::MutexLocker;
324using WTF::ThreadCondition;
325using WTF::ThreadIdentifier;
326using WTF::ThreadSafeShared;
327
328#if USE(LOCKFREE_THREADSAFESHARED)
329using WTF::atomicDecrement;
330using WTF::atomicIncrement;
331#endif
332
333using WTF::createThread;
334using WTF::currentThread;
335using WTF::isMainThread;
336using WTF::detachThread;
337using WTF::waitForThreadCompletion;
338
339#endif // Threading_h
Note: See TracBrowser for help on using the repository browser.