Skip to content

Display and control your Android device

Add thread priority API

Romain Vimont5ee59e0f133b
diff --git a/app/src/compat.h b/app/src/compat.h
index 22563421..00cb7204 100644
--- a/app/src/compat.h
+++ b/app/src/compat.h
@@ -54,6 +54,10 @@
# define SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
#endif
+#if SDL_VERSION_ATLEAST(2, 0, 16)
+# define SCRCPY_SDL_HAS_THREAD_PRIORITY_TIME_CRITICAL
+#endif
+
#ifndef HAVE_STRDUP
char *strdup(const char *s);
#endif
diff --git a/app/src/util/thread.c b/app/src/util/thread.c
index f9687add..94921fb7 100644
--- a/app/src/util/thread.c
+++ b/app/src/util/thread.c
@@ -23,6 +23,39 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
return true;
}
+static SDL_ThreadPriority
+to_sdl_thread_priority(enum sc_thread_priority priority) {
+ switch (priority) {
+ case SC_THREAD_PRIORITY_TIME_CRITICAL:
+#ifdef SCRCPY_SDL_HAS_THREAD_PRIORITY_TIME_CRITICAL
+ return SDL_THREAD_PRIORITY_TIME_CRITICAL;
+#else
+ // fall through
+#endif
+ case SC_THREAD_PRIORITY_HIGH:
+ return SDL_THREAD_PRIORITY_HIGH;
+ case SC_THREAD_PRIORITY_NORMAL:
+ return SDL_THREAD_PRIORITY_NORMAL;
+ case SC_THREAD_PRIORITY_LOW:
+ return SDL_THREAD_PRIORITY_LOW;
+ default:
+ assert(!"Unknown thread priority");
+ return 0;
+ }
+}
+
+bool
+sc_thread_set_priority(enum sc_thread_priority priority) {
+ SDL_ThreadPriority sdl_priority = to_sdl_thread_priority(priority);
+ int r = SDL_SetThreadPriority(sdl_priority);
+ if (r) {
+ LOGD("Could not set thread priority: %s", SDL_GetError());
+ return false;
+ }
+
+ return true;
+}
+
void
sc_thread_join(sc_thread *thread, int *status) {
SDL_WaitThread(thread->thread, status);
diff --git a/app/src/util/thread.h b/app/src/util/thread.h
index 7add6f1c..4183adac 100644
--- a/app/src/util/thread.h
+++ b/app/src/util/thread.h
@@ -21,6 +21,13 @@ typedef struct sc_thread {
SDL_Thread *thread;
} sc_thread;
+enum sc_thread_priority {
+ SC_THREAD_PRIORITY_LOW,
+ SC_THREAD_PRIORITY_NORMAL,
+ SC_THREAD_PRIORITY_HIGH,
+ SC_THREAD_PRIORITY_TIME_CRITICAL,
+};
+
typedef struct sc_mutex {
SDL_mutex *mutex;
#ifndef NDEBUG
@@ -39,6 +46,9 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
void
sc_thread_join(sc_thread *thread, int *status);
+bool
+sc_thread_set_priority(enum sc_thread_priority priority);
+
bool
sc_mutex_init(sc_mutex *mutex);