diff options
author | Jason Sams <rjsams@android.com> | 2011-03-17 16:12:47 -0700 |
---|---|---|
committer | Jason Sams <rjsams@android.com> | 2011-03-17 16:14:27 -0700 |
commit | 55d2a25402319380c62a97f3f84b57e2977448d1 (patch) | |
tree | 92a149a6f2b5cac9dbb85962773a033802aa3835 /libs/rs/driver/rsdCore.cpp | |
parent | e4a06c5fc738bf219f2a495e12a637b2d0871651 (diff) | |
download | frameworks_base-55d2a25402319380c62a97f3f84b57e2977448d1.zip frameworks_base-55d2a25402319380c62a97f3f84b57e2977448d1.tar.gz frameworks_base-55d2a25402319380c62a97f3f84b57e2977448d1.tar.bz2 |
Migrate thread launch to driver.
Change-Id: If182c524cceb327547640f22f956856d291d1787
Diffstat (limited to 'libs/rs/driver/rsdCore.cpp')
-rw-r--r-- | libs/rs/driver/rsdCore.cpp | 139 |
1 files changed, 135 insertions, 4 deletions
diff --git a/libs/rs/driver/rsdCore.cpp b/libs/rs/driver/rsdCore.cpp index 79fcab5..bb6cce9 100644 --- a/libs/rs/driver/rsdCore.cpp +++ b/libs/rs/driver/rsdCore.cpp @@ -20,16 +20,29 @@ #include <malloc.h> #include "rsContext.h" +#include <sys/types.h> +#include <sys/resource.h> +#include <sched.h> +#include <cutils/properties.h> +#include <cutils/sched_policy.h> +#include <sys/syscall.h> +#include <string.h> + using namespace android; using namespace android::renderscript; +static void Shutdown(Context *rsc); +static void SetPriority(const Context *rsc, int32_t priority); + static RsdHalFunctions FunctionTable = { + Shutdown, NULL, - NULL, + SetPriority, { rsdScriptInit, rsdScriptInvokeFunction, rsdScriptInvokeRoot, + rsdScriptInvokeForEach, rsdScriptInvokeInit, rsdScriptSetGlobalVar, rsdScriptSetGlobalBind, @@ -39,16 +52,134 @@ static RsdHalFunctions FunctionTable = { }; + +static void * HelperThreadProc(void *vrsc) { + Context *rsc = static_cast<Context *>(vrsc); + RsHal *dc = (RsHal *)rsc->mHal.drv; + + + uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount); + + //LOGV("RS helperThread starting %p idx=%i", rsc, idx); + + dc->mWorkers.mLaunchSignals[idx].init(); + dc->mWorkers.mNativeThreadId[idx] = gettid(); + +#if 0 + typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t; + cpu_set_t cpuset; + memset(&cpuset, 0, sizeof(cpuset)); + cpuset.bits[idx / 64] |= 1ULL << (idx % 64); + int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx], + sizeof(cpuset), &cpuset); + 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) { + dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx); + } + android_atomic_dec(&dc->mWorkers.mRunningCount); + dc->mWorkers.mCompleteSignal.set(); + } + + //LOGV("RS helperThread exited %p idx=%i", rsc, idx); + return NULL; +} + +void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) { + RsHal *dc = (RsHal *)rsc->mHal.drv; + + dc->mWorkers.mLaunchData = data; + dc->mWorkers.mLaunchCallback = cbk; + android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount); + for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { + dc->mWorkers.mLaunchSignals[ct].set(); + } + while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) { + dc->mWorkers.mCompleteSignal.wait(); + } +} + bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) { rsc->mHal.funcs = FunctionTable; - /* - rsc->mHal.drv = (RsHal *)calloc(1, sizeof(RsHal)); + RsHal *dc = (RsHal *)calloc(1, sizeof(RsHal)); if (!rsc->mHal.drv) { return false; } - */ + rsc->mHal.drv = dc; + + int cpu = sysconf(_SC_NPROCESSORS_ONLN); + LOGV("RS Launching thread(s), reported CPU count %i", cpu); + if (cpu < 2) cpu = 0; + + dc->mWorkers.mCount = (uint32_t)cpu; + dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t)); + dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t)); + dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount]; + dc->mWorkers.mLaunchCallback = NULL; + + dc->mWorkers.mCompleteSignal.init(); + + 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) { + LOGE("Failed to init thread attribute."); + return false; + } + + for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) { + status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc); + if (status) { + dc->mWorkers.mCount = ct; + LOGE("Created fewer than expected number of RS threads."); + break; + } + } + while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) { + usleep(100); + } + + pthread_attr_destroy(&threadAttr); return true; } + +void SetPriority(const Context *rsc, int32_t priority) { + RsHal *dc = (RsHal *)rsc->mHal.drv; + for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) { + setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority); + } +} + +void Shutdown(Context *rsc) { + RsHal *dc = (RsHal *)rsc->mHal.drv; + + dc->mExit = true; + dc->mWorkers.mLaunchData = NULL; + dc->mWorkers.mLaunchCallback = NULL; + android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount); + for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { + dc->mWorkers.mLaunchSignals[ct].set(); + } + int status; + void *res; + for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { + status = pthread_join(dc->mWorkers.mThreadId[ct], &res); + } + rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0); +} + + |