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

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

2009-10-26 Steve Block <[email protected]>

Reviewed by Darin Adler.

Adds ability to disable ReadWriteLock on platforms (eg Android) that use pthreads but do not support pthread_rwlock.
https://bugs.webkit.org/show_bug.cgi?id=30713

  • wtf/Platform.h: Modified. Defines HAVE_PTHREAD_RWLOCK for all platforms currently using pthreads.
  • wtf/Threading.h: Modified. Use pthread_rwlock_t only when HAVE_PTHREAD_RWLOCK is defined.
  • wtf/ThreadingPthreads.cpp: Modified. Build ReadWriteLock methods only when HAVE_PTHREAD_RWLOCK is defined.
  • Property svn:eol-style set to native
File size: 10.6 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;
131#if HAVE(PTHREAD_RWLOCK)
132typedef pthread_rwlock_t PlatformReadWriteLock;
133#else
134typedef void* PlatformReadWriteLock;
135#endif
136typedef pthread_cond_t PlatformCondition;
137#elif PLATFORM(GTK)
138typedef GOwnPtr<GMutex> PlatformMutex;
139typedef void* PlatformReadWriteLock; // FIXME: Implement.
140typedef GOwnPtr<GCond> PlatformCondition;
141#elif PLATFORM(QT)
142typedef QT_PREPEND_NAMESPACE(QMutex)* PlatformMutex;
143typedef void* PlatformReadWriteLock; // FIXME: Implement.
144typedef QT_PREPEND_NAMESPACE(QWaitCondition)* PlatformCondition;
145#elif PLATFORM(WIN_OS)
146struct PlatformMutex {
147 CRITICAL_SECTION m_internalMutex;
148 size_t m_recursionCount;
149};
150typedef void* PlatformReadWriteLock; // FIXME: Implement.
151struct PlatformCondition {
152 size_t m_waitersGone;
153 size_t m_waitersBlocked;
154 size_t m_waitersToUnblock;
155 HANDLE m_blockLock;
156 HANDLE m_blockQueue;
157 HANDLE m_unblockLock;
158
159 bool timedWait(PlatformMutex&, DWORD durationMilliseconds);
160 void signal(bool unblockAll);
161};
162#else
163typedef void* PlatformMutex;
164typedef void* PlatformReadWriteLock;
165typedef void* PlatformCondition;
166#endif
167
168class Mutex : public Noncopyable {
169public:
170 Mutex();
171 ~Mutex();
172
173 void lock();
174 bool tryLock();
175 void unlock();
176
177public:
178 PlatformMutex& impl() { return m_mutex; }
179private:
180 PlatformMutex m_mutex;
181};
182
183typedef Locker<Mutex> MutexLocker;
184
185class ReadWriteLock : public Noncopyable {
186public:
187 ReadWriteLock();
188 ~ReadWriteLock();
189
190 void readLock();
191 bool tryReadLock();
192
193 void writeLock();
194 bool tryWriteLock();
195
196 void unlock();
197
198private:
199 PlatformReadWriteLock m_readWriteLock;
200};
201
202class ThreadCondition : public Noncopyable {
203public:
204 ThreadCondition();
205 ~ThreadCondition();
206
207 void wait(Mutex& mutex);
208 // Returns true if the condition was signaled before absoluteTime, false if the absoluteTime was reached or is in the past.
209 // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the same time zone as WTF::currentTime().
210 bool timedWait(Mutex&, double absoluteTime);
211 void signal();
212 void broadcast();
213
214private:
215 PlatformCondition m_condition;
216};
217
218#if PLATFORM(WIN_OS)
219#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
220
221#if COMPILER(MINGW) || COMPILER(MSVC7) || PLATFORM(WINCE)
222inline int atomicIncrement(int* addend) { return InterlockedIncrement(reinterpret_cast<long*>(addend)); }
223inline int atomicDecrement(int* addend) { return InterlockedDecrement(reinterpret_cast<long*>(addend)); }
224#else
225inline int atomicIncrement(int volatile* addend) { return InterlockedIncrement(reinterpret_cast<long volatile*>(addend)); }
226inline int atomicDecrement(int volatile* addend) { return InterlockedDecrement(reinterpret_cast<long volatile*>(addend)); }
227#endif
228
229#elif PLATFORM(DARWIN)
230#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
231
232inline int atomicIncrement(int volatile* addend) { return OSAtomicIncrement32Barrier(const_cast<int*>(addend)); }
233inline int atomicDecrement(int volatile* addend) { return OSAtomicDecrement32Barrier(const_cast<int*>(addend)); }
234
235#elif COMPILER(GCC) && !PLATFORM(SPARC64) // sizeof(_Atomic_word) != sizeof(int) on sparc64 gcc
236#define WTF_USE_LOCKFREE_THREADSAFESHARED 1
237
238inline int atomicIncrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, 1) + 1; }
239inline int atomicDecrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, -1) - 1; }
240
241#endif
242
243class ThreadSafeSharedBase : public Noncopyable {
244public:
245 ThreadSafeSharedBase(int initialRefCount = 1)
246 : m_refCount(initialRefCount)
247 {
248 }
249
250 void ref()
251 {
252#if USE(LOCKFREE_THREADSAFESHARED)
253 atomicIncrement(&m_refCount);
254#else
255 MutexLocker locker(m_mutex);
256 ++m_refCount;
257#endif
258 }
259
260 bool hasOneRef()
261 {
262 return refCount() == 1;
263 }
264
265 int refCount() const
266 {
267#if !USE(LOCKFREE_THREADSAFESHARED)
268 MutexLocker locker(m_mutex);
269#endif
270 return static_cast<int const volatile &>(m_refCount);
271 }
272
273protected:
274 // Returns whether the pointer should be freed or not.
275 bool derefBase()
276 {
277#if USE(LOCKFREE_THREADSAFESHARED)
278 if (atomicDecrement(&m_refCount) <= 0)
279 return true;
280#else
281 int refCount;
282 {
283 MutexLocker locker(m_mutex);
284 --m_refCount;
285 refCount = m_refCount;
286 }
287 if (refCount <= 0)
288 return true;
289#endif
290 return false;
291 }
292
293private:
294 template<class T>
295 friend class CrossThreadRefCounted;
296
297 int m_refCount;
298#if !USE(LOCKFREE_THREADSAFESHARED)
299 mutable Mutex m_mutex;
300#endif
301};
302
303template<class T> class ThreadSafeShared : public ThreadSafeSharedBase {
304public:
305 ThreadSafeShared(int initialRefCount = 1)
306 : ThreadSafeSharedBase(initialRefCount)
307 {
308 }
309
310 void deref()
311 {
312 if (derefBase())
313 delete static_cast<T*>(this);
314 }
315};
316
317// This function must be called from the main thread. It is safe to call it repeatedly.
318// 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.
319void initializeThreading();
320
321void lockAtomicallyInitializedStaticMutex();
322void unlockAtomicallyInitializedStaticMutex();
323
324} // namespace WTF
325
326using WTF::Mutex;
327using WTF::MutexLocker;
328using WTF::ThreadCondition;
329using WTF::ThreadIdentifier;
330using WTF::ThreadSafeShared;
331
332#if USE(LOCKFREE_THREADSAFESHARED)
333using WTF::atomicDecrement;
334using WTF::atomicIncrement;
335#endif
336
337using WTF::createThread;
338using WTF::currentThread;
339using WTF::isMainThread;
340using WTF::detachThread;
341using WTF::waitForThreadCompletion;
342
343#endif // Threading_h
Note: See TracBrowser for help on using the repository browser.