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

Android: fix a concurrency issue related to al_acknowledge_drawing_halt() #1518

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/allegro5/internal/aintern_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct ALLEGRO_DISPLAY_ANDROID {
bool first_run;
bool resize_acknowledge;
bool resize_acknowledge2;
bool halt_acknowledge;
bool resumed;
bool failed;
bool is_destroy_display;
Expand Down
17 changes: 13 additions & 4 deletions src/android/android_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void android_set_display_option(ALLEGRO_DISPLAY *d, int o, int v);
static void _al_android_resize_display(ALLEGRO_DISPLAY_ANDROID *d, int width, int height);
static bool _al_android_init_display(JNIEnv *env, ALLEGRO_DISPLAY_ANDROID *display);
void _al_android_clear_current(JNIEnv *env, ALLEGRO_DISPLAY_ANDROID *d);
void _al_android_make_current(JNIEnv *env, ALLEGRO_DISPLAY_ANDROID *d);
void _al_android_make_current(JNIEnv *env, ALLEGRO_DISPLAY_ANDROID *d);

JNI_FUNC(void, AllegroSurface, nativeOnCreate, (JNIEnv *env, jobject obj))
{
Expand Down Expand Up @@ -81,8 +81,11 @@ JNI_FUNC(bool, AllegroSurface, nativeOnDestroy, (JNIEnv *env, jobject obj))
return true;
}

ALLEGRO_DEBUG("locking display event source: %p %p", d, &d->es);
// we'll wait for acknowledge_drawing_halt
display->halt_acknowledge = false;

// emit ALLEGRO_EVENT_DISPLAY_HALT_DRAWING
ALLEGRO_DEBUG("locking display event source: %p %p", d, &d->es);
_al_event_source_lock(&d->es);

if (_al_event_source_needs_to_generate_event(&d->es)) {
Expand All @@ -96,9 +99,12 @@ JNI_FUNC(bool, AllegroSurface, nativeOnDestroy, (JNIEnv *env, jobject obj))
_al_event_source_unlock(&d->es);

// wait for acknowledge_drawing_halt
ALLEGRO_DEBUG("waiting for acknowledge_drawing_halt");
al_lock_mutex(display->mutex);
al_wait_cond(display->cond, display->mutex);
while (!display->halt_acknowledge)
al_wait_cond(display->cond, display->mutex);
al_unlock_mutex(display->mutex);
ALLEGRO_DEBUG("done waiting for acknowledge_drawing_halt");

ALLEGRO_DEBUG("AllegroSurface_nativeOnDestroy end");

Expand Down Expand Up @@ -854,8 +860,11 @@ static void android_acknowledge_drawing_halt(ALLEGRO_DISPLAY *dpy)

_al_android_clear_current(_al_android_get_jnienv(), d);

/* XXX mutex? */
/* signal the condition variable */
al_lock_mutex(d->mutex);
d->halt_acknowledge = true;
al_broadcast_cond(d->cond);
al_unlock_mutex(d->mutex);

ALLEGRO_DEBUG("acknowledged drawing halt");
}
Expand Down