Add thread priority API

Expose an API to change the priority of the current thread.
This commit is contained in:
Romain Vimont 2023-03-08 21:34:42 +01:00
parent 4a25f3e53b
commit 5ee59e0f13
3 changed files with 47 additions and 0 deletions

View file

@ -54,6 +54,10 @@
# define SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR # define SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
#endif #endif
#if SDL_VERSION_ATLEAST(2, 0, 16)
# define SCRCPY_SDL_HAS_THREAD_PRIORITY_TIME_CRITICAL
#endif
#ifndef HAVE_STRDUP #ifndef HAVE_STRDUP
char *strdup(const char *s); char *strdup(const char *s);
#endif #endif

View file

@ -23,6 +23,39 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
return true; 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 void
sc_thread_join(sc_thread *thread, int *status) { sc_thread_join(sc_thread *thread, int *status) {
SDL_WaitThread(thread->thread, status); SDL_WaitThread(thread->thread, status);

View file

@ -21,6 +21,13 @@ typedef struct sc_thread {
SDL_Thread *thread; SDL_Thread *thread;
} sc_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 { typedef struct sc_mutex {
SDL_mutex *mutex; SDL_mutex *mutex;
#ifndef NDEBUG #ifndef NDEBUG
@ -39,6 +46,9 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
void void
sc_thread_join(sc_thread *thread, int *status); sc_thread_join(sc_thread *thread, int *status);
bool
sc_thread_set_priority(enum sc_thread_priority priority);
bool bool
sc_mutex_init(sc_mutex *mutex); sc_mutex_init(sc_mutex *mutex);