Skip to content

Commit

Permalink
ndk-glue: Move native activity behind lock and remove it in onDestroy
Browse files Browse the repository at this point in the history
`NativeActivity` is now behind a lock, and will be removed as soon as
`onDestroy` is called. Due to the async nature of events, make sure to
hold on to the lock received from `native_activity()` _beforehand_ if
you wish to use it during handling of `Event::Destroy`.
  • Loading branch information
MarijnS95 committed Aug 7, 2021
1 parent 6b1e160 commit fd98799
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ndk-glue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unreleased

- **Breaking:** `NativeActivity` is now behind a lock, and will be removed
as soon as `onDestroy` is called. Due to the async nature of events, make
sure to hold on to the lock received from `native_activity()` _beforehand_
if you wish to use it during handling of `Event::Destroy`.

# 0.4.0 (2021-08-02)

- Looper is now created before returning from `ANativeActivity_onCreate`, solving
Expand Down
20 changes: 15 additions & 5 deletions ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ pub fn android_log(level: Level, tag: &CStr, msg: &CStr) {
}

lazy_static! {
static ref NATIVE_ACTIVITY: RwLock<Option<NativeActivity>> = Default::default();
static ref NATIVE_WINDOW: RwLock<Option<NativeWindow>> = Default::default();
static ref INPUT_QUEUE: RwLock<Option<InputQueue>> = Default::default();
static ref CONTENT_RECT: RwLock<Rect> = Default::default();
static ref LOOPER: Mutex<Option<ForeignLooper>> = Default::default();
}

static mut NATIVE_ACTIVITY: Option<NativeActivity> = None;

pub fn native_activity() -> &'static NativeActivity {
unsafe { NATIVE_ACTIVITY.as_ref().unwrap() }
pub fn native_activity() -> RwLockReadGuard<'static, Option<NativeActivity>> {
NATIVE_ACTIVITY.read().unwrap()
}

pub fn native_window() -> RwLockReadGuard<'static, Option<NativeWindow>> {
Expand Down Expand Up @@ -114,6 +113,11 @@ pub enum Event {
SaveInstanceState,
Pause,
Stop,
/// The native activity will be stopped and destroyed after this event.
/// Due to the async nature of these events the read-lock from
/// [`native_activity()`] should be held _before_ this event is called if
/// it is still needed. The lock should be released to allow `onDestroy`
/// to return.
Destroy,
ConfigChanged,
LowMemory,
Expand Down Expand Up @@ -157,7 +161,7 @@ pub unsafe fn init(
callbacks.onLowMemory = Some(on_low_memory);

let activity = NativeActivity::from_ptr(activity);
NATIVE_ACTIVITY = Some(activity);
*NATIVE_ACTIVITY.write().unwrap() = Some(activity);

let mut logpipe: [RawFd; 2] = Default::default();
libc::pipe(logpipe.as_mut_ptr());
Expand Down Expand Up @@ -241,6 +245,12 @@ unsafe extern "C" fn on_stop(activity: *mut ANativeActivity) {

unsafe extern "C" fn on_destroy(activity: *mut ANativeActivity) {
wake(activity, Event::Destroy);
let mut native_activity_guard = NATIVE_ACTIVITY.write().unwrap();
assert_eq!(
native_activity_guard.as_ref().unwrap().ptr().as_ptr(),
activity
);
*native_activity_guard = None;
}

unsafe extern "C" fn on_configuration_changed(activity: *mut ANativeActivity) {
Expand Down

0 comments on commit fd98799

Please sign in to comment.