1
0
mirror of https://github.com/libuv/libuv synced 2025-03-28 21:13:16 +00:00

src: set a default thread name for workers (#4664)

This commit is contained in:
Rafael Gonzaga 2025-01-08 09:58:28 -03:00 committed by GitHub
parent ec5a4b54f7
commit e59e2a9e49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 0 deletions

View File

@ -17,6 +17,8 @@ is 1024).
.. versionchanged:: 1.45.0 threads now have an 8 MB stack instead of the
(sometimes too low) platform default.
.. versionchanged:: 1.50.0 threads now have a default name of libuv-worker.
The threadpool is global and shared across all event loops. When a particular
function makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`)
libuv preallocates and initializes the maximum number of threads allowed by

View File

@ -59,6 +59,7 @@ static void worker(void* arg) {
struct uv__queue* q;
int is_slow_work;
uv_thread_setname("libuv-worker");
uv_sem_post((uv_sem_t*) arg);
arg = NULL;

View File

@ -479,6 +479,7 @@ TEST_DECLARE (thread_equal)
TEST_DECLARE (thread_affinity)
TEST_DECLARE (thread_priority)
TEST_DECLARE (thread_name)
TEST_DECLARE (thread_name_threadpool)
TEST_DECLARE (dlerror)
#if (defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))) && \
!defined(__sun)
@ -1197,6 +1198,7 @@ TASK_LIST_START
TEST_ENTRY (thread_affinity)
TEST_ENTRY (thread_priority)
TEST_ENTRY (thread_name)
TEST_ENTRY (thread_name_threadpool)
TEST_ENTRY (dlerror)
TEST_ENTRY (ip4_addr)
TEST_ENTRY (ip6_addr_link_local)

View File

@ -139,3 +139,51 @@ TEST_IMPL(thread_name) {
return 0;
}
#define MAX_THREADS 4
static void* executedThreads[MAX_THREADS] = { NULL };
static int size;
static uv_loop_t* loop;
static unsigned short int key_exists(void* key) {
size_t i;
for (i = 0; i < MAX_THREADS; i++) {
if (executedThreads[i] == key) {
return 1;
}
}
return 0;
}
static void work_cb(uv_work_t* req) {
uv_thread_t thread = uv_thread_self();
req->data = &thread;
char tn[UV_PTHREAD_MAX_NAMELEN_NP];
ASSERT_OK(uv_thread_getname(&thread, tn, sizeof(tn)));
ASSERT_STR_EQ(tn, "libuv-worker");
}
static void after_work_cb(uv_work_t* req, int status) {
ASSERT_OK(status);
if (!key_exists(req->data)) {
executedThreads[size++] = req->data;
}
if (size == MAX_THREADS) {
return;
}
uv_queue_work(loop, req, work_cb, after_work_cb);
}
TEST_IMPL(thread_name_threadpool) {
uv_work_t req;
loop = uv_default_loop();
// Just to make sure all workers will be executed
// with the correct thread name
ASSERT_OK(uv_queue_work(loop, &req, work_cb, after_work_cb));
uv_run(loop, UV_RUN_DEFAULT);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}