Commit 68bfa666 authored by Peio Rigaux's avatar Peio Rigaux

added wakelocks counters to destroy powermanager only if there is no more initialized wakelocks

parent 9bdb28c3
......@@ -19,6 +19,7 @@
#include "wakelock_internal.h"
#include "belle-sip/utils.h"
#include "bctoolbox/port.h"
#include <pthread.h>
struct _WakeLock {
......@@ -29,15 +30,22 @@ struct _WakeLock {
jmethodID newWakeLockID;
jmethodID acquireID;
jmethodID releaseID;
int numberOfWakelocks;
int numberOfWakelocksAcquired;
};
typedef struct _WakeLock WakeLock;
static WakeLock ctx = {
.jvm = NULL,
.powerManager = NULL
.powerManager = NULL,
.numberOfWakelocks = 0,
.numberOfWakelocksAcquired = 0
};
bctbx_mutex_t wakeLockInitMutex = PTHREAD_MUTEX_INITIALIZER;
bctbx_mutex_t wakeLockMutex = PTHREAD_MUTEX_INITIALIZER;
static JNIEnv *get_jni_env(void);
static void jni_key_cleanup(void *data);
......@@ -52,10 +60,14 @@ static void belle_sip_set_jvm(JNIEnv *env) {
}
void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) {
bctbx_mutex_lock(&wakeLockInitMutex);
if (ctx.jvm == NULL) {
belle_sip_set_jvm(env);
}
ctx.numberOfWakelocks++;
belle_sip_debug("bellesip_wake_lock : Number of wake locks = %d", ctx.numberOfWakelocks);
if (ctx.powerManager == NULL) {
jclass powerManagerClass;
jclass wakeLockClass;
......@@ -75,13 +87,25 @@ void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) {
} else {
belle_sip_warning("bellesip_wake_lock_init(): the wakelock system has already been initialized");
}
bctbx_mutex_unlock(&wakeLockInitMutex);
}
void belle_sip_wake_lock_uninit(JNIEnv *env) {
bctbx_mutex_lock(&wakeLockInitMutex);
if (ctx.powerManager != NULL) {
(*env)->DeleteGlobalRef(env, ctx.powerManager);
ctx.powerManager = NULL;
ctx.numberOfWakelocks--;
belle_sip_debug("bellesip_wake_lock : Number of wake locks = %d", ctx.numberOfWakelocks);
if(ctx.numberOfWakelocks == 0){
(*env)->DeleteGlobalRef(env, ctx.powerManager);
ctx.powerManager = NULL;
belle_sip_message("bellesip_wake_lock_uninit(): uninitialization succeed");
} else if(ctx.numberOfWakelocks < 0){
belle_sip_warning("bellesip_wake_lock_uninit(): There is atleast one extra uninit()");
}
} else {
belle_sip_warning("bellesip_wake_lock_uninit(): the wakelock system has already been uninitialized");
}
bctbx_mutex_unlock(&wakeLockInitMutex);
}
/**
......@@ -129,6 +153,7 @@ static JNIEnv *get_jni_env(void) {
}
unsigned long wake_lock_acquire(const char *tag) {
bctbx_mutex_lock(&wakeLockMutex);
if(ctx.jvm != NULL && ctx.powerManager != NULL) {
JNIEnv *env;
if((env = get_jni_env())) {
......@@ -139,7 +164,12 @@ unsigned long wake_lock_acquire(const char *tag) {
(*env)->CallVoidMethod(env, lock, ctx.acquireID);
jobject lock2 = (*env)->NewGlobalRef(env, lock);
(*env)->DeleteLocalRef(env, lock);
ctx.numberOfWakelocksAcquired++;
belle_sip_debug("bellesip_wake_lock : Number of wake locks ACQUIRED = %d", ctx.numberOfWakelocksAcquired);
belle_sip_message("bellesip_wake_lock_acquire(): Android wake lock [%s] acquired [ref=%p]", tag, (void *)lock2);
unsigned long lock2value = (unsigned long)lock2;
belle_sip_message("bellesip_wake_lock_acquire(): cast long of wakelock %lu", lock2value);
bctbx_mutex_unlock(&wakeLockMutex);
return (unsigned long)lock2;
} else {
belle_sip_message("bellesip_wake_lock_acquire(): wake lock creation failed");
......@@ -158,6 +188,7 @@ unsigned long wake_lock_acquire(const char *tag) {
void wake_lock_release(unsigned long id) {
bctbx_mutex_lock(&wakeLockMutex);
if(ctx.jvm != NULL && ctx.powerManager != NULL) {
if(id != 0) {
jobject lock = (jobject)id;
......@@ -165,6 +196,8 @@ void wake_lock_release(unsigned long id) {
if((env = get_jni_env())) {
(*env)->CallVoidMethod(env, lock, ctx.releaseID);
belle_sip_message("bellesip_wake_lock_release(): Android wake lock released [ref=%p]", (void *)lock);
ctx.numberOfWakelocksAcquired--;
belle_sip_debug("bellesip_wake_lock : Number of wake locks ACQUIRED = %d", ctx.numberOfWakelocksAcquired);
(*env)->DeleteGlobalRef(env, lock);
} else {
belle_sip_error("bellesip_wake_lock_release(): cannot attach current thread to the JVM");
......@@ -176,4 +209,5 @@ void wake_lock_release(unsigned long id) {
else
belle_sip_error("bellesip_wake_lock_release(): cannot release wake lock. No PowerManager found");
}
bctbx_mutex_unlock(&wakeLockMutex);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment