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

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/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
1
/*
2
 
 * Copyright (C) 2007 Apple Inc. All rights reserved.
 
2
 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved.
3
3
 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4
4
 *
5
5
 * Redistribution and use in source and binary forms, with or without
26
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
 */
 
29
 
29
30
#include "config.h"
30
31
#include "Threading.h"
31
32
 
32
 
#include "StdLibExtras.h"
33
 
 
34
33
#if USE(PTHREADS)
35
34
 
 
35
#include "CurrentTime.h"
36
36
#include "HashMap.h"
37
37
#include "MainThread.h"
38
38
#include "RandomNumberSeed.h"
39
 
 
 
39
#include "StdLibExtras.h"
 
40
#include "UnusedParam.h"
40
41
#include <errno.h>
 
42
 
 
43
#if !COMPILER(MSVC)
 
44
#include <limits.h>
41
45
#include <sys/time.h>
 
46
#endif
 
47
 
 
48
#if PLATFORM(ANDROID)
 
49
#include "jni_utility.h"
 
50
#endif
42
51
 
43
52
namespace WTF {
44
53
 
46
55
 
47
56
static Mutex* atomicallyInitializedStaticMutex;
48
57
 
49
 
#if !PLATFORM(DARWIN)
 
58
#if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM)
50
59
static ThreadIdentifier mainThreadIdentifier; // The thread that was the first to call initializeThreading(), which must be the main thread.
51
60
#endif
52
61
 
62
71
        atomicallyInitializedStaticMutex = new Mutex;
63
72
        threadMapMutex();
64
73
        initializeRandomNumberGenerator();
65
 
#if !PLATFORM(DARWIN)
 
74
#if !PLATFORM(DARWIN) || PLATFORM(CHROMIUM)
66
75
        mainThreadIdentifier = currentThread();
67
76
#endif
68
77
        initializeMainThread();
108
117
    static ThreadIdentifier identifierCount = 1;
109
118
 
110
119
    threadMap().add(identifierCount, pthreadHandle);
111
 
    
 
120
 
112
121
    return identifierCount++;
113
122
}
114
123
 
115
124
static pthread_t pthreadHandleForIdentifier(ThreadIdentifier id)
116
125
{
117
126
    MutexLocker locker(threadMapMutex());
118
 
    
 
127
 
119
128
    return threadMap().get(id);
120
129
}
121
130
 
124
133
    MutexLocker locker(threadMapMutex());
125
134
 
126
135
    ASSERT(threadMap().contains(id));
127
 
    
 
136
 
128
137
    threadMap().remove(id);
129
138
}
130
139
 
131
 
ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, const char*)
132
 
{
133
 
    pthread_t threadHandle;
134
 
    if (pthread_create(&threadHandle, NULL, entryPoint, data)) {
135
 
        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
136
 
        return 0;
137
 
    }
138
 
 
139
 
    return establishIdentifierForPthreadHandle(threadHandle);
 
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
140
194
}
141
195
 
142
196
int waitForThreadCompletion(ThreadIdentifier threadID, void** result)
143
197
{
144
198
    ASSERT(threadID);
145
 
    
 
199
 
146
200
    pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
147
 
 
 
201
 
148
202
    int joinResult = pthread_join(pthreadHandle, result);
149
203
    if (joinResult == EDEADLK)
150
204
        LOG_ERROR("ThreadIdentifier %u was found to be deadlocked trying to quit", threadID);
151
 
        
 
205
 
152
206
    clearPthreadHandleForIdentifier(threadID);
153
207
    return joinResult;
154
208
}
156
210
void detachThread(ThreadIdentifier threadID)
157
211
{
158
212
    ASSERT(threadID);
159
 
    
 
213
 
160
214
    pthread_t pthreadHandle = pthreadHandleForIdentifier(threadID);
161
 
    
 
215
 
162
216
    pthread_detach(pthreadHandle);
163
 
    
 
217
 
164
218
    clearPthreadHandleForIdentifier(threadID);
165
219
}
166
220
 
174
228
 
175
229
bool isMainThread()
176
230
{
177
 
#if PLATFORM(DARWIN)
 
231
#if PLATFORM(DARWIN) && !PLATFORM(CHROMIUM)
178
232
    return pthread_main_np();
179
233
#else
180
234
    return currentThread() == mainThreadIdentifier;
193
247
 
194
248
void Mutex::lock()
195
249
{
196
 
    if (pthread_mutex_lock(&m_mutex) != 0)
197
 
        ASSERT(false);
 
250
    int result = pthread_mutex_lock(&m_mutex);
 
251
    ASSERT_UNUSED(result, !result);
198
252
}
199
 
    
 
253
 
200
254
bool Mutex::tryLock()
201
255
{
202
256
    int result = pthread_mutex_trylock(&m_mutex);
203
 
    
 
257
 
204
258
    if (result == 0)
205
259
        return true;
206
 
    else if (result == EBUSY)
 
260
    if (result == EBUSY)
207
261
        return false;
208
262
 
209
 
    ASSERT(false);
 
263
    ASSERT_NOT_REACHED();
210
264
    return false;
211
265
}
212
266
 
213
267
void Mutex::unlock()
214
268
{
215
 
    if (pthread_mutex_unlock(&m_mutex) != 0)
216
 
        ASSERT(false);
 
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);
217
326
}
218
327
 
219
328
ThreadCondition::ThreadCondition()
228
337
    
229
338
void ThreadCondition::wait(Mutex& mutex)
230
339
{
231
 
    if (pthread_cond_wait(&m_condition, &mutex.impl()) != 0)
232
 
        ASSERT(false);
 
340
    int result = pthread_cond_wait(&m_condition, &mutex.impl());
 
341
    ASSERT_UNUSED(result, !result);
233
342
}
234
343
 
235
 
bool ThreadCondition::timedWait(Mutex& mutex, double secondsToWait)
 
344
bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
236
345
{
237
 
    if (secondsToWait < 0.0) {
 
346
    if (absoluteTime < currentTime())
 
347
        return false;
 
348
 
 
349
    if (absoluteTime > INT_MAX) {
238
350
        wait(mutex);
239
351
        return true;
240
352
    }
241
353
 
242
 
    int intervalSeconds = static_cast<int>(secondsToWait);
243
 
    int intervalMicroseconds = static_cast<int>((secondsToWait - intervalSeconds) * 1000000.0);
244
 
 
245
 
    // Current time comes in sec/microsec
246
 
    timeval currentTime;
247
 
    gettimeofday(&currentTime, NULL);
248
 
 
249
 
    // Target time comes in sec/nanosec
 
354
    int timeSeconds = static_cast<int>(absoluteTime);
 
355
    int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9);
 
356
 
250
357
    timespec targetTime;
251
 
    targetTime.tv_sec = currentTime.tv_sec + intervalSeconds;
252
 
    targetTime.tv_nsec = (currentTime.tv_usec + intervalMicroseconds) * 1000;
253
 
    if (targetTime.tv_nsec > 1000000000) {
254
 
        targetTime.tv_nsec -= 1000000000;
255
 
        targetTime.tv_sec++;
256
 
    }
 
358
    targetTime.tv_sec = timeSeconds;
 
359
    targetTime.tv_nsec = timeNanoseconds;
257
360
 
258
361
    return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0;
259
362
}
260
363
 
261
364
void ThreadCondition::signal()
262
365
{
263
 
    if (pthread_cond_signal(&m_condition) != 0)
264
 
        ASSERT(false);
 
366
    int result = pthread_cond_signal(&m_condition);
 
367
    ASSERT_UNUSED(result, !result);
265
368
}
266
369
 
267
370
void ThreadCondition::broadcast()
268
371
{
269
 
    if (pthread_cond_broadcast(&m_condition) != 0)
270
 
        ASSERT(false);
 
372
    int result = pthread_cond_broadcast(&m_condition);
 
373
    ASSERT_UNUSED(result, !result);
271
374
}
272
 
    
 
375
 
273
376
} // namespace WTF
274
377
 
275
378
#endif // USE(PTHREADS)