Skip to content

Commit e59e2a9

Browse files
authored
src: set a default thread name for workers (libuv#4664)
1 parent ec5a4b5 commit e59e2a9

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

docs/src/threadpool.rst

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ is 1024).
1717
.. versionchanged:: 1.45.0 threads now have an 8 MB stack instead of the
1818
(sometimes too low) platform default.
1919

20+
.. versionchanged:: 1.50.0 threads now have a default name of libuv-worker.
21+
2022
The threadpool is global and shared across all event loops. When a particular
2123
function makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`)
2224
libuv preallocates and initializes the maximum number of threads allowed by

src/threadpool.c

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static void worker(void* arg) {
5959
struct uv__queue* q;
6060
int is_slow_work;
6161

62+
uv_thread_setname("libuv-worker");
6263
uv_sem_post((uv_sem_t*) arg);
6364
arg = NULL;
6465

test/test-list.h

+2
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ TEST_DECLARE (thread_equal)
479479
TEST_DECLARE (thread_affinity)
480480
TEST_DECLARE (thread_priority)
481481
TEST_DECLARE (thread_name)
482+
TEST_DECLARE (thread_name_threadpool)
482483
TEST_DECLARE (dlerror)
483484
#if (defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))) && \
484485
!defined(__sun)
@@ -1197,6 +1198,7 @@ TASK_LIST_START
11971198
TEST_ENTRY (thread_affinity)
11981199
TEST_ENTRY (thread_priority)
11991200
TEST_ENTRY (thread_name)
1201+
TEST_ENTRY (thread_name_threadpool)
12001202
TEST_ENTRY (dlerror)
12011203
TEST_ENTRY (ip4_addr)
12021204
TEST_ENTRY (ip6_addr_link_local)

test/test-thread-name.c

+48
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,51 @@ TEST_IMPL(thread_name) {
139139

140140
return 0;
141141
}
142+
143+
#define MAX_THREADS 4
144+
145+
static void* executedThreads[MAX_THREADS] = { NULL };
146+
static int size;
147+
static uv_loop_t* loop;
148+
149+
static unsigned short int key_exists(void* key) {
150+
size_t i;
151+
for (i = 0; i < MAX_THREADS; i++) {
152+
if (executedThreads[i] == key) {
153+
return 1;
154+
}
155+
}
156+
return 0;
157+
}
158+
159+
static void work_cb(uv_work_t* req) {
160+
uv_thread_t thread = uv_thread_self();
161+
req->data = &thread;
162+
char tn[UV_PTHREAD_MAX_NAMELEN_NP];
163+
ASSERT_OK(uv_thread_getname(&thread, tn, sizeof(tn)));
164+
ASSERT_STR_EQ(tn, "libuv-worker");
165+
}
166+
167+
static void after_work_cb(uv_work_t* req, int status) {
168+
ASSERT_OK(status);
169+
if (!key_exists(req->data)) {
170+
executedThreads[size++] = req->data;
171+
}
172+
173+
if (size == MAX_THREADS) {
174+
return;
175+
}
176+
177+
uv_queue_work(loop, req, work_cb, after_work_cb);
178+
}
179+
180+
TEST_IMPL(thread_name_threadpool) {
181+
uv_work_t req;
182+
loop = uv_default_loop();
183+
// Just to make sure all workers will be executed
184+
// with the correct thread name
185+
ASSERT_OK(uv_queue_work(loop, &req, work_cb, after_work_cb));
186+
uv_run(loop, UV_RUN_DEFAULT);
187+
MAKE_VALGRIND_HAPPY(uv_default_loop());
188+
return 0;
189+
}

0 commit comments

Comments
 (0)