rsdCore.cpp revision cdfdb8f2cdf4668c476cac842212892b2505ff3f
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsdCore.h"
18#include "rsdBcc.h"
19
20#include <malloc.h>
21#include "rsContext.h"
22
23#include <sys/types.h>
24#include <sys/resource.h>
25#include <sched.h>
26#include <cutils/properties.h>
27#include <cutils/sched_policy.h>
28#include <sys/syscall.h>
29#include <string.h>
30
31using namespace android;
32using namespace android::renderscript;
33
34static void Shutdown(Context *rsc);
35static void SetPriority(const Context *rsc, int32_t priority);
36
37static RsdHalFunctions FunctionTable = {
38    Shutdown,
39    NULL,
40    SetPriority,
41    {
42        rsdScriptInit,
43        rsdScriptInvokeFunction,
44        rsdScriptInvokeRoot,
45        rsdScriptInvokeForEach,
46        rsdScriptInvokeInit,
47        rsdScriptSetGlobalVar,
48        rsdScriptSetGlobalBind,
49        rsdScriptSetGlobalObj,
50        rsdScriptDestroy
51    }
52};
53
54
55
56static void * HelperThreadProc(void *vrsc) {
57    Context *rsc = static_cast<Context *>(vrsc);
58    RsHal *dc = (RsHal *)rsc->mHal.drv;
59
60
61    uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount);
62
63    //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
64
65    dc->mWorkers.mLaunchSignals[idx].init();
66    dc->mWorkers.mNativeThreadId[idx] = gettid();
67
68#if 0
69    typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
70    cpu_set_t cpuset;
71    memset(&cpuset, 0, sizeof(cpuset));
72    cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
73    int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
74              sizeof(cpuset), &cpuset);
75    LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
76#endif
77
78    int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
79    if (status) {
80        LOGE("pthread_setspecific %i", status);
81    }
82
83    while (!dc->mExit) {
84        dc->mWorkers.mLaunchSignals[idx].wait();
85        if (dc->mWorkers.mLaunchCallback) {
86           dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx);
87        }
88        android_atomic_dec(&dc->mWorkers.mRunningCount);
89        dc->mWorkers.mCompleteSignal.set();
90    }
91
92    //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
93    return NULL;
94}
95
96void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) {
97    RsHal *dc = (RsHal *)rsc->mHal.drv;
98
99    dc->mWorkers.mLaunchData = data;
100    dc->mWorkers.mLaunchCallback = cbk;
101    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
102    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
103        dc->mWorkers.mLaunchSignals[ct].set();
104    }
105    while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
106        dc->mWorkers.mCompleteSignal.wait();
107    }
108}
109
110bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
111    rsc->mHal.funcs = FunctionTable;
112
113    RsHal *dc = (RsHal *)calloc(1, sizeof(RsHal));
114    if (!rsc->mHal.drv) {
115        return false;
116    }
117    rsc->mHal.drv = dc;
118
119
120    int cpu = sysconf(_SC_NPROCESSORS_ONLN);
121    LOGV("RS Launching thread(s), reported CPU count %i", cpu);
122    if (cpu < 2) cpu = 0;
123
124    dc->mWorkers.mCount = (uint32_t)cpu;
125    dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t));
126    dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t));
127    dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount];
128    dc->mWorkers.mLaunchCallback = NULL;
129
130    dc->mWorkers.mCompleteSignal.init();
131
132    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
133    android_atomic_release_store(0, &dc->mWorkers.mLaunchCount);
134
135    int status;
136    pthread_attr_t threadAttr;
137    status = pthread_attr_init(&threadAttr);
138    if (status) {
139        LOGE("Failed to init thread attribute.");
140        return false;
141    }
142
143    for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
144        status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc);
145        if (status) {
146            dc->mWorkers.mCount = ct;
147            LOGE("Created fewer than expected number of RS threads.");
148            break;
149        }
150    }
151    while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
152        usleep(100);
153    }
154
155    pthread_attr_destroy(&threadAttr);
156    return true;
157}
158
159
160void SetPriority(const Context *rsc, int32_t priority) {
161    RsHal *dc = (RsHal *)rsc->mHal.drv;
162    for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
163        setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority);
164    }
165}
166
167void Shutdown(Context *rsc) {
168    RsHal *dc = (RsHal *)rsc->mHal.drv;
169
170    dc->mExit = true;
171    dc->mWorkers.mLaunchData = NULL;
172    dc->mWorkers.mLaunchCallback = NULL;
173    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
174    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
175        dc->mWorkers.mLaunchSignals[ct].set();
176    }
177    int status;
178    void *res;
179    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
180        status = pthread_join(dc->mWorkers.mThreadId[ct], &res);
181    }
182    rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0);
183}
184
185
186