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

Commit 8abea548 authored by Vince Leung's avatar Vince Leung
Browse files

performance: ensure PerfLock is thread safe

Use mutex locks to ensure that the global
variables have the right value.

Change-Id: I9f9f8fb2e41d9170590b7fba22bb12216f73ea00
parent 989070ca
Loading
Loading
Loading
Loading
+18 −10
Original line number Original line Diff line number Diff line
@@ -34,6 +34,7 @@


#include <dlfcn.h>
#include <dlfcn.h>
#include <limits.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>
#include <string.h>


#include <cutils/properties.h>
#include <cutils/properties.h>
@@ -49,6 +50,7 @@ static int (*cpu_setoptions)(int, int) = NULL;
static int  (*perf_lock_acq)(int, int, int[], int)  = NULL;
static int  (*perf_lock_acq)(int, int, int[], int)  = NULL;
static int  (*perf_lock_rel)(int)                   = NULL;
static int  (*perf_lock_rel)(int)                   = NULL;
static void *dlhandle                               = NULL;
static void *dlhandle                               = NULL;
static pthread_mutex_t dl_mutex                     = PTHREAD_MUTEX_INITIALIZER;


// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


@@ -117,6 +119,7 @@ org_codeaurora_performance_native_deinit(JNIEnv *env, jobject clazz)
{
{
    void (*deinit)(void);
    void (*deinit)(void);


    pthread_mutex_lock(&dl_mutex);
    if (dlhandle) {
    if (dlhandle) {
        cpu_setoptions = NULL;
        cpu_setoptions = NULL;
        perf_lock_acq  = NULL;
        perf_lock_acq  = NULL;
@@ -130,6 +133,7 @@ org_codeaurora_performance_native_deinit(JNIEnv *env, jobject clazz)
        dlclose(dlhandle);
        dlclose(dlhandle);
        dlhandle       = NULL;
        dlhandle       = NULL;
    }
    }
    pthread_mutex_unlock(&dl_mutex);
}
}


static jint
static jint
@@ -148,27 +152,29 @@ org_codeaurora_performance_native_perf_lock_acq(JNIEnv *env, jobject clazz, jint
    jint listlen = env->GetArrayLength(list);
    jint listlen = env->GetArrayLength(list);
    jint buf[listlen];
    jint buf[listlen];
    int i=0;
    int i=0;
    int ret = 0;
    env->GetIntArrayRegion(list, 0, listlen, buf);
    env->GetIntArrayRegion(list, 0, listlen, buf);


    if (dlhandle == NULL) {
    pthread_mutex_lock(&dl_mutex);
    if (perf_lock_acq == NULL) {
        org_codeaurora_performance_native_init();
        org_codeaurora_performance_native_init();
    }
    }
    if (perf_lock_acq) {
    ret = (*perf_lock_acq)(handle, duration, buf, listlen);
        return (*perf_lock_acq)(handle, duration, buf, listlen);
    pthread_mutex_unlock(&dl_mutex);
    }
    return ret;
    return 0;
}
}


static jint
static jint
org_codeaurora_performance_native_perf_lock_rel(JNIEnv *env, jobject clazz, jint handle)
org_codeaurora_performance_native_perf_lock_rel(JNIEnv *env, jobject clazz, jint handle)
{
{
    if (dlhandle == NULL) {
    int ret = 0;
    pthread_mutex_lock(&dl_mutex);
    if (perf_lock_rel == NULL) {
        org_codeaurora_performance_native_init();
        org_codeaurora_performance_native_init();
    }
    }
    if (perf_lock_rel) {
    ret = (*perf_lock_rel)(handle);
        return (*perf_lock_rel)(handle);
    pthread_mutex_unlock(&dl_mutex);
    }
    return ret;
    return 0;
}
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


@@ -182,7 +188,9 @@ static JNINativeMethod gMethods[] = {


int register_org_codeaurora_Performance(JNIEnv *env)
int register_org_codeaurora_Performance(JNIEnv *env)
{
{
    pthread_mutex_lock(&dl_mutex);
    org_codeaurora_performance_native_init();
    org_codeaurora_performance_native_init();
    pthread_mutex_unlock(&dl_mutex);


    return AndroidRuntime::registerNativeMethods(env,
    return AndroidRuntime::registerNativeMethods(env,
            "org/codeaurora/Performance", gMethods, NELEM(gMethods));
            "org/codeaurora/Performance", gMethods, NELEM(gMethods));