Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be8ac6ac authored by Jason Sams's avatar Jason Sams
Browse files

Move TLS behind hal.

Change-Id: I9e84acb3736bc98fa5fb0720bddb13a030285319
parent 0ec8c6f9
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -56,8 +56,9 @@ struct DrvScript {

};


static Script * setTLS(Script *sc) {
    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey);
    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey);
    rsAssert(tls);
    Script *old = tls->mScript;
    tls->mScript = sc;
@@ -133,12 +134,13 @@ bool rsdScriptInit(const Context *rsc,
                     uint32_t flags) {
    //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);

    pthread_mutex_lock(&rsdgInitMutex);
    char *cachePath = NULL;
    uint32_t objectSlotCount = 0;

    DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
    if (drv == NULL) {
        return false;
        goto error;
    }
    script->mHal.drv = drv;

@@ -159,20 +161,20 @@ bool rsdScriptInit(const Context *rsc,
                  (char const *)drv->mScriptText,
                  drv->mScriptTextLength, 0) != 0) {
        LOGE("bcc: FAILS to read bitcode");
        return NULL;
        goto error;
    }

#if 1
    if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) {
        LOGE("bcc: FAILS to link bitcode");
        return NULL;
        goto error;
    }
#endif
    cachePath = genCacheFileName(cacheDir, resName, ".oBCC");

    if (bccPrepareExecutable(drv->mBccScript, cachePath, 0) != 0) {
        LOGE("bcc: FAILS to prepare executable");
        return NULL;
        goto error;
    }

    free(cachePath);
@@ -234,10 +236,12 @@ bool rsdScriptInit(const Context *rsc,
    script->mHal.info.root = drv->mRoot;


    pthread_mutex_unlock(&rsdgInitMutex);
    return true;

error:

    pthread_mutex_unlock(&rsdgInitMutex);
    free(drv);
    return false;

+36 −6
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ static RsdHalFunctions FunctionTable = {

};

pthread_key_t rsdgThreadTLSKey = 0;
uint32_t rsdgThreadTLSKeyCount = 0;
pthread_mutex_t rsdgInitMutex = PTHREAD_MUTEX_INITIALIZER;


static void * HelperThreadProc(void *vrsc) {
@@ -87,6 +90,11 @@ static void * HelperThreadProc(void *vrsc) {
    dc->mWorkers.mLaunchSignals[idx].init();
    dc->mWorkers.mNativeThreadId[idx] = gettid();

    int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
    if (status) {
        LOGE("pthread_setspecific %i", status);
    }

#if 0
    typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
    cpu_set_t cpuset;
@@ -97,11 +105,6 @@ static void * HelperThreadProc(void *vrsc) {
    LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
#endif

    int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
    if (status) {
        LOGE("pthread_setspecific %i", status);
    }

    while (!dc->mExit) {
        dc->mWorkers.mLaunchSignals[idx].wait();
        if (dc->mWorkers.mLaunchCallback) {
@@ -139,6 +142,25 @@ bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
    }
    rsc->mHal.drv = dc;

    pthread_mutex_lock(&rsdgInitMutex);
    if (!rsdgThreadTLSKeyCount) {
        int status = pthread_key_create(&rsdgThreadTLSKey, NULL);
        if (status) {
            LOGE("Failed to init thread tls key.");
            pthread_mutex_unlock(&rsdgInitMutex);
            return false;
        }
    }
    rsdgThreadTLSKeyCount++;
    pthread_mutex_unlock(&rsdgInitMutex);

    dc->mTlsStruct.mContext = rsc;
    dc->mTlsStruct.mScript = NULL;
    int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
    if (status) {
        LOGE("pthread_setspecific %i", status);
    }


    int cpu = sysconf(_SC_NPROCESSORS_ONLN);
    LOGV("RS Launching thread(s), reported CPU count %i", cpu);
@@ -155,7 +177,6 @@ bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
    android_atomic_release_store(0, &dc->mWorkers.mLaunchCount);

    int status;
    pthread_attr_t threadAttr;
    status = pthread_attr_init(&threadAttr);
    if (status) {
@@ -203,6 +224,15 @@ void Shutdown(Context *rsc) {
        status = pthread_join(dc->mWorkers.mThreadId[ct], &res);
    }
    rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0);

    // Global structure cleanup.
    pthread_mutex_lock(&rsdgInitMutex);
    --rsdgThreadTLSKeyCount;
    if (!rsdgThreadTLSKeyCount) {
        pthread_key_delete(rsdgThreadTLSKey);
    }
    pthread_mutex_unlock(&rsdgInitMutex);

}

+10 −0
Original line number Diff line number Diff line
@@ -34,6 +34,11 @@ typedef struct RsdSymbolTableRec {
    bool threadable;
} RsdSymbolTable;

typedef struct ScriptTLSStructRec {
    android::renderscript::Context * mContext;
    android::renderscript::Script * mScript;
} ScriptTLSStruct;

typedef struct RsdHalRec {
    uint32_t version_major;
    uint32_t version_minor;
@@ -53,9 +58,14 @@ typedef struct RsdHalRec {
    Workers mWorkers;
    bool mExit;

    ScriptTLSStruct mTlsStruct;

    RsdGL gl;
} RsdHal;

extern pthread_key_t rsdgThreadTLSKey;
extern uint32_t rsdgThreadTLSKeyCount;
extern pthread_mutex_t rsdgInitMutex;


void rsdLaunchThreads(android::renderscript::Context *rsc, WorkerCallback_t cbk, void *data);
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ using namespace android;
using namespace android::renderscript;

#define GET_TLS()  ScriptTLSStruct * tls = \
    (ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
    (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey); \
    Context * rsc = tls->mContext; \
    ScriptC * sc = (ScriptC *) tls->mScript

+80 −112
Original line number Diff line number Diff line
@@ -39,13 +39,9 @@
using namespace android;
using namespace android::renderscript;

pthread_key_t Context::gThreadTLSKey = 0;
uint32_t Context::gThreadTLSKeyCount = 0;
pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;



bool Context::initGLThread() {
    pthread_mutex_lock(&gInitMutex);
    LOGV("initGLThread start %p", this);
@@ -277,18 +273,11 @@ void * Context::threadProc(void *vrsc) {
    rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
    rsc->props.mLogVisual = getProp("debug.rs.visual");

     rsc->mTlsStruct = new ScriptTLSStruct;
     if (!rsc->mTlsStruct) {
         LOGE("Error allocating tls storage");
         rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
    if (!rsdHalInit(rsc, 0, 0)) {
        LOGE("Hal init failed");
        return NULL;
    }
     rsc->mTlsStruct->mContext = rsc;
     rsc->mTlsStruct->mScript = NULL;
     int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
     if (status) {
         LOGE("pthread_setspecific %i", status);
     }
    rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);

    if (!rsc->initGLThread()) {
        rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
@@ -347,7 +336,6 @@ void * Context::threadProc(void *vrsc) {
        rsc->deinitEGL();
        pthread_mutex_unlock(&gInitMutex);
    }
     delete rsc->mTlsStruct;

    LOGV("%p, RS Thread exited", rsc);
    return NULL;
@@ -429,16 +417,6 @@ bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
    int status;
    pthread_attr_t threadAttr;

    if (!gThreadTLSKeyCount) {
        status = pthread_key_create(&gThreadTLSKey, NULL);
        if (status) {
            LOGE("Failed to init thread tls key.");
            pthread_mutex_unlock(&gInitMutex);
            return false;
        }
    }
    gThreadTLSKeyCount++;

    pthread_mutex_unlock(&gInitMutex);

    // Global init done at this point.
@@ -454,12 +432,6 @@ bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
    timerInit();
    timerSet(RS_TIMER_INTERNAL);

    if (!rsdHalInit(this, 0, 0)) {
        LOGE("Hal init failed");
        return false;
    }
    mHal.funcs.setPriority(this, mThreadPriority);

    status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
    if (status) {
        LOGE("Failed to start rs context thread.");
@@ -499,10 +471,6 @@ Context::~Context() {
    pthread_mutex_lock(&gInitMutex);
    if (mDev) {
        mDev->removeContext(this);
        --gThreadTLSKeyCount;
        if (!gThreadTLSKeyCount) {
            pthread_key_delete(gThreadTLSKey);
        }
        mDev = NULL;
    }
    pthread_mutex_unlock(&gInitMutex);
Loading