system/core/libutils/Threads.cpp

既有Dalvik虚拟机进程和线程,又有Native操作系统进程和线程,而且它们又可以同时执行Java代码和Native代码。为了理清它们之间的关系,我们将按照以下四个情景来组织本文:
        1. Dalvik虚拟机进程的创建过程;
        2. Dalvik虚拟机线程的创建过程;
        3. 只执行C/C++代码的Native线程的创建过程;
        4. 能同时执行C/C++代码和Java代码的Native线程的创建过程。
        对于上述进程和线程,Android系统都分别提供有接口来创建:
        1. Dalvik虚拟机进程可以通过android.os.Process类的静态成员函数start来创建;
        2. Dalvik虚拟机线程可以通过java.lang.Thread类的成员函数start来创建;
        3. 只执行C/C++代码的Native线程可以通过C++类Thread的成员函数run来创建;
        4. 能同时执行C/C++代码和Java代码的Native线程也可以通过C++类Thread的成员函数run来创建;

startReg()=>

androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
将全局指针gCreateThreadFn重置为javaCreateThreadEtc,否则mCanCallJava将不起作用(会导致最终都是调用androidCreateRawThreadEtc)。

system/core/include/utils/AndroidThreads.h
// Used by the Java Runtime to control how threads are created, so that
// they can be proper and lovely Java threads.
typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction,
                                        void *userData,
                                        const char* threadName,
                                        int32_t threadPriority,
                                        size_t threadStackSize,
                                        android_thread_id_t *threadId);

 

system/core/libutils/Threads.cpp
static android_create_thread_fn gCreateThreadFn = androidCreateRawThreadEtc;

int androidCreateThreadEtc(android_thread_func_t entryFunction,
                            void *userData,
                            const char* threadName,
                            int32_t threadPriority,
                            size_t threadStackSize,
                            android_thread_id_t *threadId)
{
    return gCreateThreadFn(entryFunction, userData, threadName,
        threadPriority, threadStackSize, threadId);
}

void androidSetCreateThreadFunc(android_create_thread_fn func)
{
    gCreateThreadFn = func;
}

status_t Thread::run(const char* name, int32_t priority, size_t stack)
{
    // reset status and exitPending to their default value, so we can
    // try again after an error happened (either below, or in readyToRun())
    mStatus = NO_ERROR;
    mExitPending = false;
    mThread = thread_id_t(-1);

    // hold a strong reference on ourself
    mHoldSelf = this;
    mRunning = true;

    bool res;
    if (mCanCallJava) {
        res = createThreadEtc(_threadLoop,
                this, name, priority, stack, &mThread);
    } else {
        res = androidCreateRawThreadEtc(_threadLoop,
                this, name, priority, stack, &mThread);
    }

    if (res == false) {
        mStatus = UNKNOWN_ERROR;   // something happened!
        mRunning = false;
        mThread = thread_id_t(-1);
        mHoldSelf.clear();  // "this" may have gone away after this.

        return UNKNOWN_ERROR;
    }

    // Do not refer to mStatus here: The thread is already running (may, in fact
    // already have exited with a valid mStatus result). The NO_ERROR indication
    // here merely indicates successfully starting the thread and does not
    // imply successful termination/execution.
    return NO_ERROR;

    // Exiting scope of mLock is a memory barrier and allows new thread to run
}

int androidCreateThreadEtc(android_thread_func_t entryFunction,
                            void *userData,
                            const char* threadName,
                            int32_t threadPriority,
                            size_t threadStackSize,
                            android_thread_id_t *threadId)
{
    return gCreateThreadFn(entryFunction, userData, threadName,
        threadPriority, threadStackSize, threadId);
}

 

system/core/include/utils/AndroidThreads.h
// Create thread with lots of parameters
inline bool createThreadEtc(thread_func_t entryFunction,
                            void *userData,
                            const char* threadName = "android:unnamed_thread",
                            int32_t threadPriority = PRIORITY_DEFAULT,
                            size_t threadStackSize = 0,
                            thread_id_t *threadId = 0)
{
    return androidCreateThreadEtc(entryFunction, userData, threadName,
        threadPriority, threadStackSize, threadId) ? true : false;
}



 

posted @ 2015-12-25 16:53  牧 天  阅读(804)  评论(0)    收藏  举报