Add thread priority API

Expose an API to change the priority of the current thread.
pull/3757/head
Romain Vimont 1 year ago
parent 4a25f3e53b
commit 5ee59e0f13

@ -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

@ -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);

@ -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);

Loading…
Cancel
Save