Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the threading model for lifecycle events #10

Open
SpexGuy opened this issue Nov 29, 2021 · 0 comments
Open

Improve the threading model for lifecycle events #10

SpexGuy opened this issue Nov 29, 2021 · 0 comments

Comments

@SpexGuy
Copy link
Contributor

SpexGuy commented Nov 29, 2021

Lifecycle events (onStart, onPause, onNativeWindowDestroyed, etc) come from threads that are disjoint from the main "background" thread, which runs independently. The way synchronization between these threads is done in the example is simply with a lock. This has two problems:

  1. If the lock is dropped and then taken again quickly by the main thread, waiting event threads may be starved out. Mutexes on Android are not "fair", and will not necessarily prioritize threads that have been waiting longer. This is unlikely to deadlock the application, but could cause a few extra frames of latency that are unnecessary.
  2. For calls like onNativeWindowDestroyed, it is important that the background thread does not continue to use the window after this function returns. This means that if the bg thread has access to the window, the event thread must pause and wait for the background thread to acknowledge the event before returning.

The "official" native app glue in the apk has a nicer model that might be worth taking a look at, where events are put into a queue and then the event thread waits for a signal from the background thread that the event has been processed. You can find it in ndk/sources/android/native_app_glue/android_native_app_glue.c (apache 2.0 licensed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant