Skip to content

Display and control your Android device

Simplify main thread detection

Romain Vimont70e4078b022d
diff --git a/app/src/events.c b/app/src/events.c
index 5269bdbc..3dc3dd7d 100644
--- a/app/src/events.c
+++ b/app/src/events.c
@@ -54,7 +54,7 @@ task_event_filter(void *userdata, SDL_Event *event) {
void
sc_reject_new_runnables(void) {
- assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
+ assert(sc_thread_is_main());
SDL_SetEventFilter(task_event_filter, NULL);
}
diff --git a/app/src/main.c b/app/src/main.c
index d2dbef69..1de8d874 100644
--- a/app/src/main.c
+++ b/app/src/main.c
@@ -15,7 +15,6 @@
#endif
#include "util/log.h"
#include "util/net.h"
-#include "util/thread.h"
#include "version.h"
#ifdef _WIN32
@@ -67,9 +66,6 @@ main_scrcpy(int argc, char *argv[]) {
goto end;
}
- // The current thread is the main thread
- SC_MAIN_THREAD_ID = sc_thread_get_id();
-
#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL
av_register_all();
#endif
diff --git a/app/src/receiver.c b/app/src/receiver.c
index 659f443f..89666256 100644
--- a/app/src/receiver.c
+++ b/app/src/receiver.c
@@ -44,7 +44,7 @@ sc_receiver_destroy(struct sc_receiver *receiver) {
static void
task_set_clipboard(void *userdata) {
- assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
+ assert(sc_thread_is_main());
char *text = userdata;
@@ -67,7 +67,7 @@ task_set_clipboard(void *userdata) {
static void
task_uhid_output(void *userdata) {
- assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
+ assert(sc_thread_is_main());
struct sc_uhid_output_task_data *data = userdata;
diff --git a/app/src/uhid/keyboard_uhid.c b/app/src/uhid/keyboard_uhid.c
index d8337df0..d381bfd7 100644
--- a/app/src/uhid/keyboard_uhid.c
+++ b/app/src/uhid/keyboard_uhid.c
@@ -60,7 +60,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
uint64_t ack_to_wait) {
(void) ack_to_wait;
- assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
+ assert(sc_thread_is_main());
if (event->repeat) {
// In USB HID protocol, key repeat is handled by the host (Android), so
@@ -104,7 +104,7 @@ sc_keyboard_uhid_to_sc_mod(uint8_t hid_led) {
void
sc_keyboard_uhid_process_hid_output(struct sc_keyboard_uhid *kb,
const uint8_t *data, size_t size) {
- assert(sc_thread_get_id() == SC_MAIN_THREAD_ID);
+ assert(sc_thread_is_main());
assert(size);
diff --git a/app/src/util/thread.c b/app/src/util/thread.c
index dce189bd..22e93202 100644
--- a/app/src/util/thread.c
+++ b/app/src/util/thread.c
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <SDL3/SDL_init.h>
#include <SDL3/SDL_mutex.h>
#include "util/log.h"
@@ -106,6 +107,11 @@ sc_thread_get_id(void) {
return SDL_GetCurrentThreadID();
}
+bool
+sc_thread_is_main(void) {
+ return SDL_IsMainThread();
+}
+
#ifndef NDEBUG
bool
sc_mutex_held(struct sc_mutex *mutex) {
diff --git a/app/src/util/thread.h b/app/src/util/thread.h
index 13734bdc..7f47af4d 100644
--- a/app/src/util/thread.h
+++ b/app/src/util/thread.h
@@ -39,8 +39,6 @@ typedef struct sc_cond {
SDL_Condition *cond;
} sc_cond;
-extern sc_thread_id SC_MAIN_THREAD_ID;
-
bool
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
void *userdata);
@@ -66,6 +64,9 @@ sc_mutex_unlock(sc_mutex *mutex);
sc_thread_id
sc_thread_get_id(void);
+bool
+sc_thread_is_main(void);
+
#ifndef NDEBUG
bool
sc_mutex_held(struct sc_mutex *mutex);