Skip to content

Display and control your Android device

Replace run-on-main-thread implementation

Romain Vimont3fcb01082306
diff --git a/app/src/events.c b/app/src/events.c
index 3dc3dd7d..b7a3a7a3 100644
--- a/app/src/events.c
+++ b/app/src/events.c
@@ -5,6 +5,9 @@
#include "util/log.h"
#include "util/thread.h"
+static sc_mutex mutex;
+static bool stopped;
+
bool
sc_push_event_impl(uint32_t type, void *ptr, const char *name) {
SDL_Event event = {
@@ -23,29 +26,29 @@ sc_push_event_impl(uint32_t type, void *ptr, const char *name) {
}
bool
-sc_post_to_main_thread(sc_runnable_fn run, void *userdata) {
- SDL_Event event = {
- .user = {
- .type = SC_EVENT_RUN_ON_MAIN_THREAD,
- .data1 = run,
- .data2 = userdata,
- },
- };
- bool ok = SDL_PushEvent(&event);
- if (!ok) {
- LOGW("Could not post runnable to main thread: %s", SDL_GetError());
- return false;
- }
+sc_main_thread_init(void) {
+ stopped = false;
+ return sc_mutex_init(&mutex);
+}
- return true;
+void
+sc_main_thread_destroy(void) {
+ sc_mutex_destroy(&mutex);
}
-static bool SDLCALL
-task_event_filter(void *userdata, SDL_Event *event) {
- (void) userdata;
+bool
+sc_run_on_main_thread(sc_runnable_fn run, void *userdata, bool wait_complete) {
+ assert(!sc_thread_is_main());
- if (event->type == SC_EVENT_RUN_ON_MAIN_THREAD) {
- // Reject this event type from now on
+ sc_mutex_lock(&mutex);
+ if (stopped) {
+ sc_mutex_unlock(&mutex);
+ return false;
+ }
+ bool ok = SDL_RunOnMainThread(run, userdata, wait_complete);
+ sc_mutex_unlock(&mutex);
+ if (!ok) {
+ LOGW("Could not run on main thread: %s", SDL_GetError());
return false;
}
@@ -53,8 +56,11 @@ task_event_filter(void *userdata, SDL_Event *event) {
}
void
-sc_reject_new_runnables(void) {
- assert(sc_thread_is_main());
+sc_main_thread_stop(void) {
+ sc_mutex_lock(&mutex);
+ stopped = true;
+ sc_mutex_unlock(&mutex);
- SDL_SetEventFilter(task_event_filter, NULL);
+ // Run all remaining runnables on the main thread
+ SDL_PumpEvents();
}
diff --git a/app/src/events.h b/app/src/events.h
index c82741af..0c7b4c37 100644
--- a/app/src/events.h
+++ b/app/src/events.h
@@ -6,11 +6,11 @@
#include <stdbool.h>
#include <stdint.h>
#include <SDL3/SDL_events.h>
+#include <SDL3/SDL_init.h>
enum {
SC_EVENT_NEW_FRAME = SDL_EVENT_USER,
SC_EVENT_OPEN_WINDOW,
- SC_EVENT_RUN_ON_MAIN_THREAD,
SC_EVENT_DEVICE_DISCONNECTED,
SC_EVENT_SERVER_CONNECTION_FAILED,
SC_EVENT_SERVER_CONNECTED,
@@ -29,12 +29,19 @@ sc_push_event_impl(uint32_t type, void *ptr, const char *name);
#define sc_push_event(TYPE) sc_push_event_impl(TYPE, NULL, # TYPE)
#define sc_push_event_with_data(TYPE, PTR) sc_push_event_impl(TYPE, PTR, # TYPE)
-typedef void (*sc_runnable_fn)(void *userdata);
+typedef SDL_MainThreadCallback sc_runnable_fn;
bool
-sc_post_to_main_thread(sc_runnable_fn run, void *userdata);
+sc_main_thread_init(void);
void
-sc_reject_new_runnables(void);
+sc_main_thread_destroy(void);
+
+bool
+sc_run_on_main_thread(sc_runnable_fn run, void *userdata, bool wait_complete);
+
+// Reject new runnables after this call
+void
+sc_main_thread_stop(void);
#endif
diff --git a/app/src/main.c b/app/src/main.c
index 1de8d874..7327e07d 100644
--- a/app/src/main.c
+++ b/app/src/main.c
@@ -8,6 +8,7 @@
#include <SDL3/SDL.h>
#include "cli.h"
+#include "events.h"
#include "options.h"
#include "scrcpy.h"
#ifdef HAVE_USB
@@ -83,12 +84,20 @@ main_scrcpy(int argc, char *argv[]) {
sc_log_configure();
+ if (!sc_main_thread_init()) {
+ ret = SCRCPY_EXIT_FAILURE;
+ goto net_cleanup;
+ }
+
#ifdef HAVE_USB
ret = args.opts.otg ? scrcpy_otg(&args.opts) : scrcpy(&args.opts);
#else
ret = scrcpy(&args.opts);
#endif
+ sc_main_thread_destroy();
+
+net_cleanup:
net_cleanup();
end:
diff --git a/app/src/receiver.c b/app/src/receiver.c
index 89666256..be0be0c7 100644
--- a/app/src/receiver.c
+++ b/app/src/receiver.c
@@ -85,7 +85,7 @@ process_msg(struct sc_receiver *receiver, struct sc_device_msg *msg) {
// Take ownership of the text (do not destroy the msg)
char *text = msg->clipboard.text;
- bool ok = sc_post_to_main_thread(task_set_clipboard, text);
+ bool ok = sc_run_on_main_thread(task_set_clipboard, text, false);
if (!ok) {
LOGW("Could not post clipboard to main thread");
free(text);
@@ -139,14 +139,13 @@ process_msg(struct sc_receiver *receiver, struct sc_device_msg *msg) {
// It is guaranteed that these pointers will still be valid when
// the main thread will process them (the main thread will stop
- // processing SC_EVENT_RUN_ON_MAIN_THREAD on exit, when everything
- // gets deinitialized)
+ // processing on exit, when everything gets deinitialized)
data->uhid_devices = receiver->uhid_devices;
data->id = msg->uhid_output.id;
data->data = msg->uhid_output.data; // take ownership
data->size = msg->uhid_output.size;
- bool ok = sc_post_to_main_thread(task_uhid_output, data);
+ bool ok = sc_run_on_main_thread(task_uhid_output, data, false);
if (!ok) {
LOGW("Could not post UHID output to main thread");
free(data->data);
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 10b98460..5af45cfb 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -140,12 +140,6 @@ event_loop(struct scrcpy *s, bool has_screen) {
case SDL_EVENT_QUIT:
LOGD("User requested to quit");
return SCRCPY_EXIT_SUCCESS;
- case SC_EVENT_RUN_ON_MAIN_THREAD: {
- sc_runnable_fn run = event.user.data1;
- void *userdata = event.user.data2;
- run(userdata);
- break;
- }
default:
if (has_screen) {
sc_screen_handle_event(&s->screen, &event);
@@ -156,22 +150,6 @@ event_loop(struct scrcpy *s, bool has_screen) {
return SCRCPY_EXIT_FAILURE;
}
-static void
-terminate_runnables_on_event_loop(void) {
- sc_reject_new_runnables();
-
- SDL_Event event;
- while (SDL_PeepEvents(&event, 1, SDL_GETEVENT,
- SC_EVENT_RUN_ON_MAIN_THREAD,
- SC_EVENT_RUN_ON_MAIN_THREAD) == 1) {
- assert(event.type == SC_EVENT_RUN_ON_MAIN_THREAD);
- // Make sure all posted runnables are run, to avoid memory leaks
- sc_runnable_fn run = event.user.data1;
- void *userdata = event.user.data2;
- run(userdata);
- }
-}
-
// Return true on success, false on error
static bool
await_for_server(bool *connected) {
@@ -902,7 +880,11 @@ aoa_complete:
}
ret = event_loop(s, options->window);
- terminate_runnables_on_event_loop();
+
+ // Reject all new runnables, and execute the pending ones now
+ // (they could access memory that will be cleaned up below)
+ sc_main_thread_stop();
+
disconnected = ret == SCRCPY_EXIT_DISCONNECTED;
end: