~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/javascriptcore/JavaScriptCore/wtf/ThreadingPthreads.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007, 2009 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
#include "config.h"
 
31
#include "Threading.h"
 
32
 
 
33
#if USE(PTHREADS)
 
34
 
 
35
#include "CurrentTime.h"
 
36
#include "HashMap.h"
 
37
#include "MainThread.h"
 
38
#include "RandomNumberSeed.h"
 
39
#include "StdLibExtras.h"
 
40
#include "UnusedParam.h"
 
41
#include <errno.h>
 
42
 
 
43
#if !COMPILER(MSVC)
 
44
#include <limits.h>
 
45
#include <sys/time.h>
 
46
#endif
 
47
 
 
48
#if PLATFORM(ANDROID)
 
49
#include "jni_utility.h"
 
50
#endif
 
51
 
 
52
namespace WTF {
 
53
 
 
54
typedef HashMap<ThreadIdentifier, pthread_t> ThreadMap;
 
55
 
 
56
static Mutex* atomicallyInitializedStaticMutex;
 
57
 
 
58
#if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM)
 
59
static ThreadIdentifier mainThreadIdentifier; // The thread that was the first to call initializeThreading(), which must be the main thread.
 
60
#endif
 
61
 
 
62
static Mutex& threadMapMutex()
 
63
{
 
64
    DEFINE_STATIC_LOCAL(Mutex, mutex, ());
 
65
    return mutex;
 
66
}
 
67
 
 
68
void initializeThreading()
 
69
{
 
70
    if (!atomicallyInitializedStaticMutex) {
 
71
        atomicallyInitializedStaticMutex = new Mutex;
 
72
        threadMapMutex();
 
73
        initializeRandomNumberGenerator();
 
74
#if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM)
 
75
        mainThreadIdentifier = currentThread();
 
76
#endif
 
77
        initializeMainThread();
 
78
    }
 
79
}
 
80
 
 
81
void lockAtomicallyInitializedStaticMutex()
 
82
{
 
83
    ASSERT(atomicallyInitializedStaticMutex);
 
84
    atomicallyInitializedStaticMutex->lock();
 
85
}
 
86
 
 
87
void unlockAtomicallyInitializedStaticMutex()
 
88
{
 
89
    atomicallyInitializedStaticMutex->unlock();
 
90
}
 
91
 
 
92
static ThreadMap& threadMap()
 
93
{
 
94
    DEFINE_STATIC_LOCAL(ThreadMap, map, ());
 
95
    return map;
 
96
}
 
97
 
 
98
static ThreadIdentifier identifierByPthreadHandle(const pthread_t& pthreadHandle)
 
99
{
 
100
    MutexLocker locker(threadMapMutex());
 
101
 
 
102
    ThreadMap::iterator i = threadMap().begin();
 
103
    for (; i != threadMap().end(); ++i) {
 
104
        if (pthread_equal(i->second, pthreadHandle))
 
105
            return i->first;
 
106
    }
 
107
 
 
108
    return 0;
 
109
}
 
110
 
 
111
static ThreadIdentifier establishIdentifierForPthreadHandle(pthread_t& pthreadHandle)
 
112
{
 
113
    ASSERT(!identifierByPthreadHandle(pthreadHandle));
 
114
 
 
115
    MutexLocker locker(threadMapMutex());
 
116
 
 
117
    static ThreadIdentifier identifierCount = 1;
 
118
 
 
119
    threadMap().add(identifierCount, pthreadHandle);
 
120
 
 
121
    return identifierCount++;
 
122
}
 
123
 
 
124
static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id)
 
125
{
 
126
    MutexLocker locker(threadMapMutex());
 
127
 
 
128
    return threadMap().get(id);
 
129
}
 
130
 
 
131
static void clearPthreadHandleForIdentifier(ThreadIdentifier id)
 
132
{
 
133
    MutexLocker locker(threadMapMutex());
 
134
 
 
135
    ASSERT(threadMap().contains(id));
 
136
 
 
137
    threadMap().remove(id);
 
138
}
 
139
 
 
140
#if PLATFORM(ANDROID)
 
141
// On the Android platform, threads must be registered with the VM before they run.
 
142
struct ThreadData {
 
143
    ThreadFunction entryPoint;
 
144
    void* arg;
 
145
};
 
146
 
 
147
static void* runThreadWithRegistration(void* arg)
 
148
{
 
149
    ThreadData* data = static_cast<ThreadData*>(arg);
 
150
    JavaVM* vm = JSC::Bindings::getJavaVM();
 
151
    JNIEnv* env;
 
152
    void* ret = 0;
 
153
    if (vm->AttachCurrentThread(&env, 0) == JNI_OK) {
 
154
        ret = data->entryPoint(data->arg);
 
155
        vm->DetachCurrentThread();
 
156
    }
 
157
    delete data;
 
158
    return ret;
 
159
}
 
160
 
 
161
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
 
162
{
 
163
    pthread_t threadHandle;
 
164
    ThreadData* threadData = new ThreadData();
 
165
    threadData->entryPoint = entryPoint;
 
166
    threadData->arg = data;
 
167
 
 
168
    if (pthread_create(&threadHandle, 0, runThreadWithRegistration, static_cast<void*>(threadData))) {
 
169
        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
 
170
        return 0;
 
171
    }
 
172
    return establishIdentifierForPthreadHandle(threadHandle);
 
173
}
 
174
#else
 
175
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
 
176
{
 
177
    pthread_t threadHandle;
 
178
    if (pthread_create(&threadHandle, 0, entryPoint, data)) {
 
179
        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
 
180
        return 0;
 
181
    }
 
182
 
 
183
    return establishIdentifierForPthreadHandle(threadHandle);
 
184
}
 
185
#endif
 
186
 
 
187
void setThreadNameInternal(const char* threadName)
 
188
{
 
189
#if HAVE(PTHREAD_SETNAME_NP)
 
190
    pthread_setname_np(threadName);
 
191
#else
 
192
    UNUSED_PARAM(threadName);
 
193
#endif
 
194
}
 
195
 
 
196
int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
 
197
{
 
198
    ASSERT(threadID);
 
199
 
 
200
    pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
 
201
 
 
202
    int joinResult = pthread_join(pthreadHandle, result);
 
203
    if (joinResult == EDEADLK)
 
204
        LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
 
205
 
 
206
    clearPthreadHandleForIdentifier(threadID);
 
207
    return joinResult;
 
208
}
 
209
 
 
210
void detachThread(ThreadIdentifier threadID)
 
211
{
 
212
    ASSERT(threadID);
 
213
 
 
214
    pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
 
215
 
 
216
    pthread_detach(pthreadHandle);
 
217
 
 
218
    clearPthreadHandleForIdentifier(threadID);
 
219
}
 
220
 
 
221
ThreadIdentifier currentThread()
 
222
{
 
223
    pthread_t currentThread = pthread_self();
 
224
    if (ThreadIdentifier id = identifierByPthreadHandle(currentThread))
 
225
        return id;
 
226
    return establishIdentifierForPthreadHandle(currentThread);
 
227
}
 
228
 
 
229
bool isMainThread()
 
230
{
 
231
#if PLATFORM(DARWIN) && !PLATFORM(CHROMIUM)
 
232
    return pthread_main_np();
 
233
#else
 
234
    return currentThread() == mainThreadIdentifier;
 
235
#endif
 
236
}
 
237
 
 
238
Mutex::Mutex()
 
239
{
 
240
    pthread_mutex_init(&m_mutex, NULL);
 
241
}
 
242
 
 
243
Mutex::~Mutex()
 
244
{
 
245
    pthread_mutex_destroy(&m_mutex);
 
246
}
 
247
 
 
248
void Mutex::lock()
 
249
{
 
250
    int result = pthread_mutex_lock(&m_mutex);
 
251
    ASSERT_UNUSED(result, !result);
 
252
}
 
253
 
 
254
bool Mutex::tryLock()
 
255
{
 
256
    int result = pthread_mutex_trylock(&m_mutex);
 
257
 
 
258
    if (result == 0)
 
259
        return true;
 
260
    if (result == EBUSY)
 
261
        return false;
 
262
 
 
263
    ASSERT_NOT_REACHED();
 
264
    return false;
 
265
}
 
266
 
 
267
void Mutex::unlock()
 
268
{
 
269
    int result = pthread_mutex_unlock(&m_mutex);
 
270
    ASSERT_UNUSED(result, !result);
 
271
}
 
272
 
 
273
 
 
274
ReadWriteLock::ReadWriteLock()
 
275
{
 
276
    pthread_rwlock_init(&m_readWriteLock, NULL);
 
277
}
 
278
 
 
279
ReadWriteLock::~ReadWriteLock()
 
280
{
 
281
    pthread_rwlock_destroy(&m_readWriteLock);
 
282
}
 
283
 
 
284
void ReadWriteLock::readLock()
 
285
{
 
286
    int result = pthread_rwlock_rdlock(&m_readWriteLock);
 
287
    ASSERT_UNUSED(result, !result);
 
288
}
 
289
 
 
290
bool ReadWriteLock::tryReadLock()
 
291
{
 
292
    int result = pthread_rwlock_tryrdlock(&m_readWriteLock);
 
293
 
 
294
    if (result == 0)
 
295
        return true;
 
296
    if (result == EBUSY || result == EAGAIN)
 
297
        return false;
 
298
 
 
299
    ASSERT_NOT_REACHED();
 
300
    return false;
 
301
}
 
302
 
 
303
void ReadWriteLock::writeLock()
 
304
{
 
305
    int result = pthread_rwlock_wrlock(&m_readWriteLock);
 
306
    ASSERT_UNUSED(result, !result);
 
307
}
 
308
 
 
309
bool ReadWriteLock::tryWriteLock()
 
310
{
 
311
    int result = pthread_rwlock_trywrlock(&m_readWriteLock);
 
312
 
 
313
    if (result == 0)
 
314
        return true;
 
315
    if (result == EBUSY || result == EAGAIN)
 
316
        return false;
 
317
 
 
318
    ASSERT_NOT_REACHED();
 
319
    return false;
 
320
}
 
321
 
 
322
void ReadWriteLock::unlock()
 
323
{
 
324
    int result = pthread_rwlock_unlock(&m_readWriteLock);
 
325
    ASSERT_UNUSED(result, !result);
 
326
}
 
327
 
 
328
ThreadCondition::ThreadCondition()
 
329
 
330
    pthread_cond_init(&m_condition, NULL);
 
331
}
 
332
 
 
333
ThreadCondition::~ThreadCondition()
 
334
{
 
335
    pthread_cond_destroy(&m_condition);
 
336
}
 
337
    
 
338
void ThreadCondition::wait(Mutex& mutex)
 
339
{
 
340
    int result = pthread_cond_wait(&m_condition, &mutex.impl());
 
341
    ASSERT_UNUSED(result, !result);
 
342
}
 
343
 
 
344
bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
 
345
{
 
346
    if (absoluteTime < currentTime())
 
347
        return false;
 
348
 
 
349
    if (absoluteTime > INT_MAX) {
 
350
        wait(mutex);
 
351
        return true;
 
352
    }
 
353
 
 
354
    int timeSeconds = static_cast<int>(absoluteTime);
 
355
    int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9);
 
356
 
 
357
    timespec targetTime;
 
358
    targetTime.tv_sec = timeSeconds;
 
359
    targetTime.tv_nsec = timeNanoseconds;
 
360
 
 
361
    return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0;
 
362
}
 
363
 
 
364
void ThreadCondition::signal()
 
365
{
 
366
    int result = pthread_cond_signal(&m_condition);
 
367
    ASSERT_UNUSED(result, !result);
 
368
}
 
369
 
 
370
void ThreadCondition::broadcast()
 
371
{
 
372
    int result = pthread_cond_broadcast(&m_condition);
 
373
    ASSERT_UNUSED(result, !result);
 
374
}
 
375
 
 
376
} // namespace WTF
 
377
 
 
378
#endif // USE(PTHREADS)