From 47c7f1b648396f96c6824b264d5c912a72e38edb Mon Sep 17 00:00:00 2001 From: Maciej Godek Date: Sun, 21 Mar 2021 23:56:29 +0100 Subject: [PATCH] Notify event pipe before releasing NativeActivity resources The design of ndk-glue seems to imply that the user of a `NativeActivity` resource, e.g. `NativeWindow` obtained from `ndk_glue::native_window()`, should hold a read lock on the resource as long as they are using it. Therefore, ndk-glue's `NativeActivity` callbacks related to resource release should: (1) notify the user of upcoming resource release, (2) acquire a write lock on the handle, waiting for all read locks to be dropped, (3) drop the handle, (4) return from the callback. This allows the user to react and correctly release various objects derived from the resource (e.g. swapchains/surfaces from `NativeWindow`) before it goes away. Currently, the order is 2-3-1-4, which can lead to a deadlock (if the user holds on to a read guard) or a race condition (if they drop the read guard early). This commit fixes the order. --- ndk-glue/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ndk-glue/src/lib.rs b/ndk-glue/src/lib.rs index 886bd031..958d49e0 100644 --- a/ndk-glue/src/lib.rs +++ b/ndk-glue/src/lib.rs @@ -260,8 +260,8 @@ unsafe extern "C" fn on_window_destroyed( activity: *mut ANativeActivity, _window: *mut ANativeWindow, ) { - *NATIVE_WINDOW.write().unwrap() = None; wake(activity, Event::WindowDestroyed); + *NATIVE_WINDOW.write().unwrap() = None; } unsafe extern "C" fn on_input_queue_created( @@ -279,10 +279,12 @@ unsafe extern "C" fn on_input_queue_destroyed( activity: *mut ANativeActivity, queue: *mut AInputQueue, ) { + wake(activity, Event::InputQueueDestroyed); + let mut input_queue_guard = INPUT_QUEUE.write().unwrap(); + assert_eq!(input_queue_guard.as_ref().unwrap().ptr().as_ptr(), queue); let input_queue = InputQueue::from_ptr(NonNull::new(queue).unwrap()); input_queue.detach_looper(); - *INPUT_QUEUE.write().unwrap() = None; - wake(activity, Event::InputQueueDestroyed); + *input_queue_guard = None; } unsafe extern "C" fn on_content_rect_changed(activity: *mut ANativeActivity, rect: *const ARect) {