33
33
#include " app/src/include/firebase/future.h"
34
34
#include " app/src/include/firebase/version.h"
35
35
#include " app/src/log.h"
36
+ #include " app/src/mutex.h"
36
37
#include " app/src/reference_counted_future_impl.h"
37
38
#include " app/src/util.h"
38
39
#include " app/src/util_android.h"
@@ -70,7 +71,7 @@ static const ::firebase::App* g_app = nullptr;
70
71
71
72
// Mutex used to arbitrate access to g_app between the thread calling this
72
73
// module and MessageProcessingThread().
73
- static pthread_mutex_t g_app_mutex;
74
+ static Mutex g_app_mutex;
74
75
75
76
// Global reference to the Firebase Cloud Messaging instance.
76
77
// This is initialized in messaging::Initialize() and never released
@@ -297,10 +298,9 @@ static void ConsumeEvents() {
297
298
// thread knows that it is time to quit.
298
299
static bool TerminateRequested () {
299
300
bool result;
300
- pthread_mutex_lock (& g_app_mutex);
301
+ MutexLock lock ( g_app_mutex);
301
302
// If the app is removed, Terminate() has been called.
302
303
result = !g_app;
303
- pthread_mutex_unlock (&g_app_mutex);
304
304
return result;
305
305
}
306
306
@@ -336,9 +336,11 @@ Future<void> RequestPermissionLastResult() {
336
336
337
337
// Process queued messages & tokens.
338
338
void ProcessMessages () {
339
- pthread_mutex_lock (&g_app_mutex);
340
- JNIEnv* env = g_app ? g_app->GetJNIEnv () : nullptr ;
341
- pthread_mutex_unlock (&g_app_mutex);
339
+ JNIEnv* env;
340
+ {
341
+ MutexLock lock (g_app_mutex);
342
+ env = g_app ? g_app->GetJNIEnv () : nullptr ;
343
+ }
342
344
if (HasListener () && env) {
343
345
// Check to see if there was a message in the intent that started this
344
346
// activity.
@@ -352,10 +354,12 @@ void ProcessMessages() {
352
354
// changes to that file and when messages are written it relays them to the
353
355
// OnMessage callback.
354
356
static void * MessageProcessingThread (void *) {
355
- pthread_mutex_lock (&g_app_mutex);
356
- JavaVM* jvm = g_app ? g_app->java_vm () : nullptr ;
357
- pthread_mutex_unlock (&g_app_mutex);
358
- if (jvm == nullptr ) return nullptr ;
357
+ JavaVM* jvm;
358
+ {
359
+ MutexLock lock (g_app_mutex);
360
+ jvm = g_app ? g_app->java_vm () : nullptr ;
361
+ if (jvm == nullptr ) return nullptr ;
362
+ }
359
363
360
364
// Set up inotify.
361
365
int file_descriptor = inotify_init ();
@@ -408,7 +412,6 @@ static void TerminateMessageProcessingThread() {
408
412
pthread_join (g_poll_thread, nullptr );
409
413
pthread_mutex_destroy (&g_thread_wait_mutex);
410
414
pthread_cond_destroy (&g_thread_wait_cond);
411
- pthread_mutex_destroy (&g_app_mutex);
412
415
}
413
416
414
417
static bool StringStartsWith (const char * s, const char * prefix) {
@@ -494,11 +497,19 @@ static void FireIntentMessage(JNIEnv* env) {
494
497
// when retrieving a notification via an Intent. http://b/32316101
495
498
if (g_intent_message_fired || !HasListener ()) return ;
496
499
g_intent_message_fired = true ;
500
+ jobject activity;
501
+ {
502
+ MutexLock lock (g_app_mutex);
503
+ if (!g_app) return ;
504
+ activity = env->NewLocalRef (g_app->activity ());
505
+ assert (env->ExceptionCheck () == false );
506
+ }
497
507
// Intent intent = app.getIntent();
498
508
jobject intent = env->CallObjectMethod (
499
- g_app-> activity () ,
509
+ activity,
500
510
util::activity::GetMethodId (util::activity::kGetIntent ));
501
511
assert (env->ExceptionCheck () == false );
512
+ env->DeleteLocalRef (activity);
502
513
503
514
if (intent != nullptr ) {
504
515
// Bundle bundle = intent.getExtras();
@@ -578,10 +589,10 @@ InitResult Initialize(const ::firebase::App& app, Listener* listener,
578
589
return kInitResultFailedMissingDependency ;
579
590
}
580
591
581
- g_app_mutex = PTHREAD_MUTEX_INITIALIZER;
582
- pthread_mutex_lock (& g_app_mutex);
583
- g_app = &app;
584
- pthread_mutex_unlock (&g_app_mutex);
592
+ {
593
+ MutexLock lock ( g_app_mutex);
594
+ g_app = &app;
595
+ }
585
596
g_registration_token_mutex = new Mutex ();
586
597
g_file_locker_mutex = new Mutex ();
587
598
g_pending_subscriptions = new std::vector<PendingTopic>();
@@ -666,9 +677,10 @@ void Terminate() {
666
677
internal::UnregisterTerminateOnDefaultAppDestroy ();
667
678
JNIEnv* env = g_app->GetJNIEnv ();
668
679
// Dereference the app.
669
- pthread_mutex_lock (&g_app_mutex);
670
- g_app = nullptr ;
671
- pthread_mutex_unlock (&g_app_mutex);
680
+ {
681
+ MutexLock lock (g_app_mutex);
682
+ g_app = nullptr ;
683
+ }
672
684
TerminateMessageProcessingThread ();
673
685
674
686
delete g_registration_token_mutex;
0 commit comments