Skip to content

Commit

Permalink
Android: fix a concurrency issue related to al_acknowledge_drawing_ha…
Browse files Browse the repository at this point in the history
…lt(). Closes liballeg#1517
  • Loading branch information
alemart committed Dec 14, 2023
1 parent 9ced31b commit 3f4cfb6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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

0 comments on commit 3f4cfb6

Please sign in to comment.