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 @@ ...@@ -19,6 +19,7 @@
#include "wakelock_internal.h" #include "wakelock_internal.h"
#include "belle-sip/utils.h" #include "belle-sip/utils.h"
#include "bctoolbox/port.h"
#include <pthread.h> #include <pthread.h>
struct _WakeLock { struct _WakeLock {
...@@ -29,15 +30,22 @@ struct _WakeLock { ...@@ -29,15 +30,22 @@ struct _WakeLock {
jmethodID newWakeLockID; jmethodID newWakeLockID;
jmethodID acquireID; jmethodID acquireID;
jmethodID releaseID; jmethodID releaseID;
int numberOfWakelocks;
int numberOfWakelocksAcquired;
}; };
typedef struct _WakeLock WakeLock; typedef struct _WakeLock WakeLock;
static WakeLock ctx = { static WakeLock ctx = {
.jvm = NULL, .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 JNIEnv *get_jni_env(void);
static void jni_key_cleanup(void *data); static void jni_key_cleanup(void *data);
...@@ -52,10 +60,14 @@ static void belle_sip_set_jvm(JNIEnv *env) { ...@@ -52,10 +60,14 @@ static void belle_sip_set_jvm(JNIEnv *env) {
} }
void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) { void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) {
bctbx_mutex_lock(&wakeLockInitMutex);
if (ctx.jvm == NULL) { if (ctx.jvm == NULL) {
belle_sip_set_jvm(env); belle_sip_set_jvm(env);
} }
ctx.numberOfWakelocks++;
belle_sip_debug("bellesip_wake_lock : Number of wake locks = %d", ctx.numberOfWakelocks);
if (ctx.powerManager == NULL) { if (ctx.powerManager == NULL) {
jclass powerManagerClass; jclass powerManagerClass;
jclass wakeLockClass; jclass wakeLockClass;
...@@ -75,13 +87,25 @@ void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) { ...@@ -75,13 +87,25 @@ void belle_sip_wake_lock_init(JNIEnv *env, jobject pm) {
} else { } else {
belle_sip_warning("bellesip_wake_lock_init(): the wakelock system has already been initialized"); 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) { void belle_sip_wake_lock_uninit(JNIEnv *env) {
bctbx_mutex_lock(&wakeLockInitMutex);
if (ctx.powerManager != NULL) { if (ctx.powerManager != NULL) {
(*env)->DeleteGlobalRef(env, ctx.powerManager); ctx.numberOfWakelocks--;
ctx.powerManager = NULL; 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) { ...@@ -129,6 +153,7 @@ static JNIEnv *get_jni_env(void) {
} }
unsigned long wake_lock_acquire(const char *tag) { unsigned long wake_lock_acquire(const char *tag) {
bctbx_mutex_lock(&wakeLockMutex);
if(ctx.jvm != NULL && ctx.powerManager != NULL) { if(ctx.jvm != NULL && ctx.powerManager != NULL) {
JNIEnv *env; JNIEnv *env;
if((env = get_jni_env())) { if((env = get_jni_env())) {
...@@ -139,7 +164,12 @@ unsigned long wake_lock_acquire(const char *tag) { ...@@ -139,7 +164,12 @@ unsigned long wake_lock_acquire(const char *tag) {
(*env)->CallVoidMethod(env, lock, ctx.acquireID); (*env)->CallVoidMethod(env, lock, ctx.acquireID);
jobject lock2 = (*env)->NewGlobalRef(env, lock); jobject lock2 = (*env)->NewGlobalRef(env, lock);
(*env)->DeleteLocalRef(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); 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; return (unsigned long)lock2;
} else { } else {
belle_sip_message("bellesip_wake_lock_acquire(): wake lock creation failed"); belle_sip_message("bellesip_wake_lock_acquire(): wake lock creation failed");
...@@ -158,6 +188,7 @@ unsigned long wake_lock_acquire(const char *tag) { ...@@ -158,6 +188,7 @@ unsigned long wake_lock_acquire(const char *tag) {
void wake_lock_release(unsigned long id) { void wake_lock_release(unsigned long id) {
bctbx_mutex_lock(&wakeLockMutex);
if(ctx.jvm != NULL && ctx.powerManager != NULL) { if(ctx.jvm != NULL && ctx.powerManager != NULL) {
if(id != 0) { if(id != 0) {
jobject lock = (jobject)id; jobject lock = (jobject)id;
...@@ -165,6 +196,8 @@ void wake_lock_release(unsigned long id) { ...@@ -165,6 +196,8 @@ void wake_lock_release(unsigned long id) {
if((env = get_jni_env())) { if((env = get_jni_env())) {
(*env)->CallVoidMethod(env, lock, ctx.releaseID); (*env)->CallVoidMethod(env, lock, ctx.releaseID);
belle_sip_message("bellesip_wake_lock_release(): Android wake lock released [ref=%p]", (void *)lock); 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); (*env)->DeleteGlobalRef(env, lock);
} else { } else {
belle_sip_error("bellesip_wake_lock_release(): cannot attach current thread to the JVM"); 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) { ...@@ -176,4 +209,5 @@ void wake_lock_release(unsigned long id) {
else else
belle_sip_error("bellesip_wake_lock_release(): cannot release wake lock. No PowerManager found"); 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