Ignore:
Timestamp:
Jan 4, 2011, 10:56:18 AM (14 years ago)
Author:
[email protected]
Message:

2011-01-04 Daniel Bates <[email protected]>

Reviewed by Adam Roben.

Extract ThreadFunctionInvocation into separate file and share between Apple Windows and Android
https://bugs.webkit.org/show_bug.cgi?id=51855

Both the Apple Windows and Android ports implement a similar adapter structure,
called ThreadFunctionInvocation and ThreadData respectively, as part of
their thread creation process. Instead, we should share such an adapter
structure and remove duplicate code.

  • JavaScriptCore.gypi: Added header wtf/ThreadFunctionInvocation.h.
  • wtf/ThreadFunctionInvocation.h: Added. (WTF::ThreadFunctionInvocation::ThreadFunctionInvocation):
  • wtf/ThreadingPthreads.cpp: Removed Android-specific structure ThreadData; Instead, use ThreadFunctionInvocation. (WTF::runThreadWithRegistration): (WTF::createThreadInternal):
  • wtf/ThreadingWin.cpp: Moved structure ThreadFunctionInvocation to its own file so that it can be shared with the Android implementation of createThreadInternal(). (WTF::wtfThreadEntryPoint): Use OwnPtr to hold passed instance of ThreadFunctionInvocation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wtf/ThreadingPthreads.cpp

    r68209 r74975  
    5151#if OS(ANDROID)
    5252#include "JNIUtility.h"
     53#include "ThreadFunctionInvocation.h"
     54#include <wtf/OwnPtr.h>
    5355#endif
    5456
     
    137139
    138140#if OS(ANDROID)
    139 // On the Android platform, threads must be registered with the VM before they run.
    140 struct ThreadData {
    141     ThreadFunction entryPoint;
    142     void* arg;
    143 };
    144 
    145141static void* runThreadWithRegistration(void* arg)
    146142{
    147     ThreadData* data = static_cast<ThreadData*>(arg);
     143    OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(static_cast<ThreadFunctionInvocation*>(arg));
    148144    JavaVM* vm = JSC::Bindings::getJavaVM();
    149145    JNIEnv* env;
    150146    void* ret = 0;
    151147    if (vm->AttachCurrentThread(&env, 0) == JNI_OK) {
    152         ret = data->entryPoint(data->arg);
     148        ret = invocation->function(invocation.data);
    153149        vm->DetachCurrentThread();
    154150    }
    155     delete data;
    156151    return ret;
    157152}
     
    160155{
    161156    pthread_t threadHandle;
    162     ThreadData* threadData = new ThreadData();
    163     threadData->entryPoint = entryPoint;
    164     threadData->arg = data;
    165 
    166     if (pthread_create(&threadHandle, 0, runThreadWithRegistration, static_cast<void*>(threadData))) {
     157
     158    // On the Android platform, threads must be registered with the VM before they run.
     159    OwnPtr<ThreadFunctionInvocation> invocation = adoptPtr(new ThreadFunctionInvocation(entryPoint, data));
     160
     161    if (pthread_create(&threadHandle, 0, runThreadWithRegistration, invocation.get())) {
    167162        LOG_ERROR("Failed to create pthread at entry point %p with data %p", entryPoint, data);
    168         delete threadData;
    169163        return 0;
    170164    }
     165
     166    // The thread will take ownership of invocation.
     167    invocation.leakPtr();
     168
    171169    return establishIdentifierForPthreadHandle(threadHandle);
    172170}
Note: See TracChangeset for help on using the changeset viewer.