Skip to content

Commit 6de2dc5

Browse files
Googlera-maurice
authored andcommitted
Resubmit fix for undefined mutex behavior in Admob destructor on Android.
originally sumbitted as cl/279825051 Tests run are implemented using pthread and are executed and built only on phones. PiperOrigin-RevId: 281148306
1 parent 905942c commit 6de2dc5

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

admob/src/android/banner_view_internal_android.cc

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "admob/src/android/banner_view_internal_android.h"
18+
1719
#include <assert.h>
1820
#include <jni.h>
1921

@@ -23,13 +25,13 @@
2325
#include "admob/admob_resources.h"
2426
#include "admob/src/android/ad_request_converter.h"
2527
#include "admob/src/android/admob_android.h"
26-
#include "admob/src/android/banner_view_internal_android.h"
2728
#include "admob/src/common/admob_common.h"
2829
#include "admob/src/include/firebase/admob.h"
2930
#include "admob/src/include/firebase/admob/banner_view.h"
3031
#include "admob/src/include/firebase/admob/types.h"
3132
#include "app/src/assert.h"
3233
#include "app/src/mutex.h"
34+
#include "app/src/semaphore.h"
3335
#include "app/src/util_android.h"
3436

3537
namespace firebase {
@@ -56,24 +58,25 @@ BannerViewInternalAndroid::BannerViewInternalAndroid(BannerView* base)
5658
env->DeleteLocalRef(helper_ref);
5759
}
5860

61+
62+
void DestroyOnDeleteCallback(const Future<void>& result,
63+
void* sem_data) {
64+
if (sem_data != nullptr) {
65+
Semaphore* semaphore = static_cast<Semaphore*>(sem_data);
66+
semaphore->Post();
67+
}
68+
}
69+
5970
BannerViewInternalAndroid::~BannerViewInternalAndroid() {
6071
JNIEnv* env = ::firebase::admob::GetJNI();
6172

62-
// Destroy the banner view so all pending futures / callbacks complete.
63-
{
64-
Mutex mutex(Mutex::kModeNonRecursive);
65-
mutex.Acquire();
66-
Destroy().OnCompletion(
67-
[](const Future<void>&, void* mutex) {
68-
reinterpret_cast<Mutex*>(mutex)->Release();
69-
},
70-
&mutex);
71-
// Acquire a second Mutex lock to block until the Future for the last call
72-
// to Destroy() completes at which point the lambda function in OnCompletion
73-
// is called and the Mutex lock is released.
74-
mutex.Acquire();
75-
mutex.Release();
76-
}
73+
DestroyInternalData();
74+
75+
Semaphore semaphore(0);
76+
InvokeNullary(kBannerViewFnDestroyOnDelete, banner_view_helper::kDestroy)
77+
.OnCompletion(DestroyOnDeleteCallback, &semaphore);
78+
79+
semaphore.Wait();
7780
env->DeleteGlobalRef(helper_);
7881
helper_ = nullptr;
7982
}
@@ -131,8 +134,7 @@ Future<void> BannerViewInternalAndroid::Resume() {
131134
}
132135

133136
Future<void> BannerViewInternalAndroid::Destroy() {
134-
// The bounding box is zeroed on destroy.
135-
bounding_box_ = {};
137+
DestroyInternalData();
136138
return InvokeNullary(kBannerViewFnDestroy, banner_view_helper::kDestroy);
137139
}
138140

@@ -214,6 +216,11 @@ Future<void> BannerViewInternalAndroid::InvokeNullary(
214216
return GetLastResult(fn);
215217
}
216218

219+
void BannerViewInternalAndroid::DestroyInternalData() {
220+
// The bounding box is zeroed on destroy.
221+
bounding_box_ = {};
222+
}
223+
217224
} // namespace internal
218225
} // namespace admob
219226
} // namespace firebase

admob/src/android/banner_view_internal_android.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class BannerViewInternalAndroid : public BannerViewInternal {
7878
// the future callback pointer.
7979
Future<void> InvokeNullary(BannerViewFn fn,
8080
banner_view_helper::Method method);
81+
82+
// Cleans up any C++ side data before invoking the Android SDK to do the same.
83+
void DestroyInternalData();
8184
};
8285

8386
} // namespace internal

admob/src/common/banner_view_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum BannerViewFn {
3535
kBannerViewFnPause,
3636
kBannerViewFnResume,
3737
kBannerViewFnDestroy,
38+
kBannerViewFnDestroyOnDelete,
3839
kBannerViewFnMoveTo,
3940
kBannerViewFnCount
4041
};

0 commit comments

Comments
 (0)