diff options
author | Ryan Liptak <squeek502@hotmail.com> | 2025-05-15 00:16:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-15 09:16:25 +0200 |
commit | 2b76a4fafa91dd531e35084ab544d9cd8a3e2d79 (patch) | |
tree | 5802548c907ce5909dd87c800ebc88038432bf06 | |
parent | a3b8cb9cc0410b3f40972179b1b430e3a450002a (diff) | |
download | libuv-2b76a4fafa91dd531e35084ab544d9cd8a3e2d79.tar.gz libuv-2b76a4fafa91dd531e35084ab544d9cd8a3e2d79.zip |
unix: fix uv_thread_{get,set}priority error codes (#4782)
pthread_getschedparam and pthread_setschedparam do not use errno but
instead return the error code directly.
https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_getschedparam.html
Also include a fix to return UV_ESRCH instead of UV_EBADF on Windows,
like Unix systems do.
-rw-r--r-- | src/unix/core.c | 6 | ||||
-rw-r--r-- | src/win/util.c | 18 | ||||
-rw-r--r-- | test/test-thread-priority.c | 6 |
3 files changed, 23 insertions, 7 deletions
diff --git a/src/unix/core.c b/src/unix/core.c index bd51b69b..1dde27bd 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1656,7 +1656,7 @@ int uv_thread_getpriority(uv_thread_t tid, int* priority) { r = pthread_getschedparam(tid, &policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); #ifdef __linux__ if (SCHED_OTHER == policy && pthread_equal(tid, pthread_self())) { @@ -1709,7 +1709,7 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { r = pthread_getschedparam(tid, &policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); #ifdef __linux__ /** @@ -1757,7 +1757,7 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { param.sched_priority = prio; r = pthread_setschedparam(tid, policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); } return 0; diff --git a/src/win/util.c b/src/win/util.c index 57061bf8..da1238d3 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1519,20 +1519,26 @@ int uv_os_setpriority(uv_pid_t pid, int priority) { } int uv_thread_getpriority(uv_thread_t tid, int* priority) { + DWORD err; int r; if (priority == NULL) return UV_EINVAL; r = GetThreadPriority(tid); - if (r == THREAD_PRIORITY_ERROR_RETURN) - return uv_translate_sys_error(GetLastError()); + if (r == THREAD_PRIORITY_ERROR_RETURN) { + err = GetLastError(); + if (err == ERROR_INVALID_HANDLE) + return UV_ESRCH; + return uv_translate_sys_error(err); + } *priority = r; return 0; } int uv_thread_setpriority(uv_thread_t tid, int priority) { + DWORD err; int r; switch (priority) { @@ -1555,8 +1561,12 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { return 0; } - if (r == 0) - return uv_translate_sys_error(GetLastError()); + if (r == 0) { + err = GetLastError(); + if (err == ERROR_INVALID_HANDLE) + return UV_ESRCH; + return uv_translate_sys_error(err); + } return 0; } diff --git a/test/test-thread-priority.c b/test/test-thread-priority.c index 0aaf2977..f1e4f9a7 100644 --- a/test/test-thread-priority.c +++ b/test/test-thread-priority.c @@ -101,5 +101,11 @@ TEST_IMPL(thread_priority) { uv_sem_destroy(&sem); + /* Now that the thread no longer exists, verify that the relevant error is returned */ +#if !defined(__ANDROID__) + ASSERT_EQ(UV_ESRCH, uv_thread_getpriority(task_id, &priority)); + ASSERT_EQ(UV_ESRCH, uv_thread_setpriority(task_id, UV_THREAD_PRIORITY_LOWEST)); +#endif + return 0; }
\ No newline at end of file |