Skip to content

Commit

Permalink
feat: Add waker() method to AndroidAppWaker
Browse files Browse the repository at this point in the history
This commit adds a "waker()" method to AndroidAppWaker. It converts it
into an `std::task::Waker`, which is the type of waker used by
asynchronous tasks for scheduling. The goal is to allow AndroidAppWaker
to be easily used to set up an asynchronous context.

The implementation is very efficient, owing to the "static pointer"
style of coding already used. The "wake" function just calls
ALooper_wake, and cloning/dropping the waker is just a copy.

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Nov 11, 2024
1 parent 0d29930 commit 4f7010d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
Cargo.lock
22 changes: 22 additions & 0 deletions android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::ptr;
use std::ptr::NonNull;
use std::sync::Weak;
use std::sync::{Arc, Mutex, RwLock};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
Expand Down Expand Up @@ -118,6 +119,27 @@ impl AndroidAppWaker {
ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {
Expand Down
22 changes: 22 additions & 0 deletions android-activity/src/native_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::panic::AssertUnwindSafe;
use std::ptr;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
Expand Down Expand Up @@ -83,6 +84,27 @@ impl AndroidAppWaker {
ndk_sys::ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {
Expand Down

0 comments on commit 4f7010d

Please sign in to comment.