mirror of
https://github.com/libretro/RetroArch
synced 2025-04-09 21:45:45 +00:00
(Android) Native app glue - use pthread again
This commit is contained in:
parent
5a2c6aa5d6
commit
fe5d4e34ab
@ -22,14 +22,14 @@ struct android_app_userdata *g_android_userdata;
|
|||||||
|
|
||||||
static void free_saved_state(struct android_app* android_app)
|
static void free_saved_state(struct android_app* android_app)
|
||||||
{
|
{
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
if (android_app->savedState != NULL)
|
if (android_app->savedState != NULL)
|
||||||
{
|
{
|
||||||
free(android_app->savedState);
|
free(android_app->savedState);
|
||||||
android_app->savedState = NULL;
|
android_app->savedState = NULL;
|
||||||
android_app->savedStateSize = 0;
|
android_app->savedStateSize = 0;
|
||||||
}
|
}
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void android_app_write_cmd(struct android_app *android_app, int8_t cmd)
|
void android_app_write_cmd(struct android_app *android_app, int8_t cmd)
|
||||||
@ -65,14 +65,14 @@ static void android_app_set_input(void *data, AInputQueue* inputQueue)
|
|||||||
if (!android_app)
|
if (!android_app)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->pendingInputQueue = inputQueue;
|
android_app->pendingInputQueue = inputQueue;
|
||||||
android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
|
android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
|
||||||
|
|
||||||
while (android_app->inputQueue != android_app->pendingInputQueue)
|
while (android_app->inputQueue != android_app->pendingInputQueue)
|
||||||
scond_wait(android_app->cond, android_app->mutex);
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
|
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_set_window(void *data, ANativeWindow* window)
|
static void android_app_set_window(void *data, ANativeWindow* window)
|
||||||
@ -82,7 +82,7 @@ static void android_app_set_window(void *data, ANativeWindow* window)
|
|||||||
if (!android_app)
|
if (!android_app)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
if (android_app->pendingWindow)
|
if (android_app->pendingWindow)
|
||||||
android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
|
android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
|
||||||
|
|
||||||
@ -92,9 +92,9 @@ static void android_app_set_window(void *data, ANativeWindow* window)
|
|||||||
android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
|
android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
|
||||||
|
|
||||||
while (android_app->window != android_app->pendingWindow)
|
while (android_app->window != android_app->pendingWindow)
|
||||||
scond_wait(android_app->cond, android_app->mutex);
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
|
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_set_activity_state(void *data, int8_t cmd)
|
static void android_app_set_activity_state(void *data, int8_t cmd)
|
||||||
@ -104,34 +104,36 @@ static void android_app_set_activity_state(void *data, int8_t cmd)
|
|||||||
if (!android_app)
|
if (!android_app)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app_write_cmd(android_app, cmd);
|
android_app_write_cmd(android_app, cmd);
|
||||||
while (android_app->activityState != cmd
|
while (android_app->activityState != cmd
|
||||||
&& android_app->activityState != APP_CMD_DEAD)
|
&& android_app->activityState != APP_CMD_DEAD)
|
||||||
scond_wait(android_app->cond, android_app->mutex);
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
if (android_app->activityState == APP_CMD_DEAD)
|
if (android_app->activityState == APP_CMD_DEAD)
|
||||||
RARCH_LOG("RetroArch native thread is dead.\n");
|
RARCH_LOG("RetroArch native thread is dead.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void android_app_free(struct android_app* android_app)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
|
android_app_write_cmd(android_app, APP_CMD_DESTROY);
|
||||||
|
while (!android_app->destroyed)
|
||||||
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
|
close(android_app->msgread);
|
||||||
|
close(android_app->msgwrite);
|
||||||
|
pthread_cond_destroy(&android_app->cond);
|
||||||
|
pthread_mutex_destroy(&android_app->mutex);
|
||||||
|
free(android_app);
|
||||||
|
}
|
||||||
|
|
||||||
static void onDestroy(ANativeActivity* activity)
|
static void onDestroy(ANativeActivity* activity)
|
||||||
{
|
{
|
||||||
struct android_app *android_app = (struct android_app*)activity->instance;
|
RARCH_LOG("Destroy: %p\n", activity);
|
||||||
|
android_app_free((struct android_app*)activity->instance);
|
||||||
if (!android_app)
|
|
||||||
return;
|
|
||||||
|
|
||||||
RARCH_LOG("onDestroy: %p\n", activity);
|
|
||||||
sthread_join(android_app->thread);
|
|
||||||
RARCH_LOG("Joined with RetroArch native thread.\n");
|
|
||||||
|
|
||||||
close(android_app->msgread);
|
|
||||||
close(android_app->msgwrite);
|
|
||||||
scond_free(android_app->cond);
|
|
||||||
slock_free(android_app->mutex);
|
|
||||||
|
|
||||||
free(android_app);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onStart(ANativeActivity* activity)
|
static void onStart(ANativeActivity* activity)
|
||||||
@ -153,11 +155,11 @@ static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen)
|
|||||||
struct android_app* android_app = (struct android_app*)activity->instance;
|
struct android_app* android_app = (struct android_app*)activity->instance;
|
||||||
void* savedState = NULL;
|
void* savedState = NULL;
|
||||||
|
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->stateSaved = 0;
|
android_app->stateSaved = 0;
|
||||||
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
|
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
|
||||||
while (!android_app->stateSaved)
|
while (!android_app->stateSaved)
|
||||||
scond_wait(android_app->cond, android_app->mutex);
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
|
|
||||||
if (android_app->savedState != NULL)
|
if (android_app->savedState != NULL)
|
||||||
{
|
{
|
||||||
@ -167,7 +169,7 @@ static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen)
|
|||||||
android_app->savedStateSize = 0;
|
android_app->savedStateSize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
return savedState;
|
return savedState;
|
||||||
}
|
}
|
||||||
@ -238,7 +240,7 @@ static void onInputQueueDestroyed(ANativeActivity* activity,
|
|||||||
android_app_set_input((struct android_app*)activity->instance, NULL);
|
android_app_set_input((struct android_app*)activity->instance, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_app_entry(void *param)
|
static void *android_app_entry(void *param)
|
||||||
{
|
{
|
||||||
struct android_app* android_app = (struct android_app*)param;
|
struct android_app* android_app = (struct android_app*)param;
|
||||||
ALooper *looper = (ALooper*)ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
|
ALooper *looper = (ALooper*)ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
|
||||||
@ -246,12 +248,13 @@ static void android_app_entry(void *param)
|
|||||||
ALOOPER_EVENT_INPUT, NULL, NULL);
|
ALOOPER_EVENT_INPUT, NULL, NULL);
|
||||||
android_app->looper = looper;
|
android_app->looper = looper;
|
||||||
|
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->running = 1;
|
android_app->running = 1;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
android_main(android_app);
|
android_main(android_app);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct android_app* android_app_create(ANativeActivity* activity,
|
static struct android_app* android_app_create(ANativeActivity* activity,
|
||||||
@ -262,8 +265,8 @@ static struct android_app* android_app_create(ANativeActivity* activity,
|
|||||||
calloc(1, sizeof(*android_app));
|
calloc(1, sizeof(*android_app));
|
||||||
android_app->activity = activity;
|
android_app->activity = activity;
|
||||||
|
|
||||||
android_app->mutex = slock_new();
|
pthread_mutex_init(&android_app->mutex, NULL);
|
||||||
android_app->cond = scond_new();
|
pthread_cond_init(&android_app->cond, NULL);
|
||||||
|
|
||||||
if (savedState != NULL)
|
if (savedState != NULL)
|
||||||
{
|
{
|
||||||
@ -280,13 +283,16 @@ static struct android_app* android_app_create(ANativeActivity* activity,
|
|||||||
android_app->msgread = msgpipe[0];
|
android_app->msgread = msgpipe[0];
|
||||||
android_app->msgwrite = msgpipe[1];
|
android_app->msgwrite = msgpipe[1];
|
||||||
|
|
||||||
android_app->thread = sthread_create(android_app_entry, android_app);
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||||
|
pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
|
||||||
|
|
||||||
/* Wait for thread to start. */
|
/* Wait for thread to start. */
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
while (!android_app->running)
|
while (!android_app->running)
|
||||||
scond_wait(android_app->cond, android_app->mutex);
|
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
/* These are set only for the native activity,
|
/* These are set only for the native activity,
|
||||||
* and are reset when it ends. */
|
* and are reset when it ends. */
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
#define _ANDROID_NATIVE_APP_GLUE_H
|
#define _ANDROID_NATIVE_APP_GLUE_H
|
||||||
|
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <rthreads/rthreads.h>
|
#include <pthread.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
|
|
||||||
#include <android/configuration.h>
|
#include <android/configuration.h>
|
||||||
@ -183,13 +183,13 @@ struct android_app
|
|||||||
|
|
||||||
/* Below are "private" implementation variables of the glue code. */
|
/* Below are "private" implementation variables of the glue code. */
|
||||||
|
|
||||||
slock_t *mutex;
|
pthread_mutex_t mutex;
|
||||||
scond_t *cond;
|
pthread_cond_t cond;
|
||||||
|
|
||||||
int msgread;
|
int msgread;
|
||||||
int msgwrite;
|
int msgwrite;
|
||||||
|
|
||||||
sthread_t *thread;
|
pthread_t thread;
|
||||||
|
|
||||||
struct android_poll_source cmdPollSource;
|
struct android_poll_source cmdPollSource;
|
||||||
struct android_poll_source inputPollSource;
|
struct android_poll_source inputPollSource;
|
||||||
|
@ -125,7 +125,7 @@ static void engine_handle_cmd(void)
|
|||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case APP_CMD_INPUT_CHANGED:
|
case APP_CMD_INPUT_CHANGED:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
|
|
||||||
if (android_app->inputQueue)
|
if (android_app->inputQueue)
|
||||||
AInputQueue_detachLooper(android_app->inputQueue);
|
AInputQueue_detachLooper(android_app->inputQueue);
|
||||||
@ -140,40 +140,40 @@ static void engine_handle_cmd(void)
|
|||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_INIT_WINDOW:
|
case APP_CMD_INIT_WINDOW:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->window = android_app->pendingWindow;
|
android_app->window = android_app->pendingWindow;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
if (runloop->is_paused)
|
if (runloop->is_paused)
|
||||||
event_command(EVENT_CMD_REINIT);
|
event_command(EVENT_CMD_REINIT);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_RESUME:
|
case APP_CMD_RESUME:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->activityState = cmd;
|
android_app->activityState = cmd;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_START:
|
case APP_CMD_START:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->activityState = cmd;
|
android_app->activityState = cmd;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_PAUSE:
|
case APP_CMD_PAUSE:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->activityState = cmd;
|
android_app->activityState = cmd;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
|
|
||||||
if (!global->system.shutdown)
|
if (!global->system.shutdown)
|
||||||
{
|
{
|
||||||
@ -184,16 +184,16 @@ static void engine_handle_cmd(void)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_STOP:
|
case APP_CMD_STOP:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
android_app->activityState = cmd;
|
android_app->activityState = cmd;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_CONFIG_CHANGED:
|
case APP_CMD_CONFIG_CHANGED:
|
||||||
break;
|
break;
|
||||||
case APP_CMD_TERM_WINDOW:
|
case APP_CMD_TERM_WINDOW:
|
||||||
slock_lock(android_app->mutex);
|
pthread_mutex_lock(&android_app->mutex);
|
||||||
|
|
||||||
/* The window is being hidden or closed, clean it up. */
|
/* The window is being hidden or closed, clean it up. */
|
||||||
/* terminate display/EGL context here */
|
/* terminate display/EGL context here */
|
||||||
@ -203,8 +203,8 @@ static void engine_handle_cmd(void)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
android_app->window = NULL;
|
android_app->window = NULL;
|
||||||
scond_broadcast(android_app->cond);
|
pthread_cond_broadcast(&android_app->cond);
|
||||||
slock_unlock(android_app->mutex);
|
pthread_mutex_unlock(&android_app->mutex);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case APP_CMD_GAINED_FOCUS:
|
case APP_CMD_GAINED_FOCUS:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user