|
160
|
1 /* Copyright libuv project contributors. All rights reserved.
|
|
|
2 *
|
|
|
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
4 * of this software and associated documentation files (the "Software"), to
|
|
|
5 * deal in the Software without restriction, including without limitation the
|
|
|
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
7 * sell copies of the Software, and to permit persons to whom the Software is
|
|
|
8 * furnished to do so, subject to the following conditions:
|
|
|
9 *
|
|
|
10 * The above copyright notice and this permission notice shall be included in
|
|
|
11 * all copies or substantial portions of the Software.
|
|
|
12 *
|
|
|
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
19 * IN THE SOFTWARE.
|
|
|
20 */
|
|
|
21
|
|
|
22 #include "uv.h"
|
|
|
23 #include "task.h"
|
|
|
24 #include "../src/uv-common.h"
|
|
|
25
|
|
|
26 #include <string.h>
|
|
|
27
|
|
|
28 struct semaphores {
|
|
|
29 uv_sem_t main;
|
|
|
30 uv_sem_t worker;
|
|
|
31 };
|
|
|
32
|
|
|
33 static void thread_run(void* arg) {
|
|
|
34 int r;
|
|
|
35 char thread_name[16];
|
|
|
36 struct semaphores* sem;
|
|
|
37 uv_thread_t thread;
|
|
|
38
|
|
|
39 sem = arg;
|
|
|
40
|
|
|
41 #ifdef _WIN32
|
|
|
42 /* uv_thread_self isn't defined for the main thread on Windows. */
|
|
|
43 thread = GetCurrentThread();
|
|
|
44 #else
|
|
|
45 thread = uv_thread_self();
|
|
|
46 #endif
|
|
|
47
|
|
|
48 r = uv_thread_setname("worker-thread");
|
|
|
49 ASSERT_OK(r);
|
|
|
50
|
|
|
51 uv_sem_post(&sem->worker);
|
|
|
52
|
|
|
53 r = uv_thread_getname(&thread, thread_name, sizeof(thread_name));
|
|
|
54 ASSERT_OK(r);
|
|
|
55
|
|
|
56 ASSERT_STR_EQ(thread_name, "worker-thread");
|
|
|
57
|
|
|
58 uv_sem_wait(&sem->main);
|
|
|
59 }
|
|
|
60
|
|
|
61 TEST_IMPL(thread_name) {
|
|
|
62 int r;
|
|
|
63 uv_thread_t threads[2];
|
|
|
64 char tn[UV_PTHREAD_MAX_NAMELEN_NP];
|
|
|
65 char thread_name[UV_PTHREAD_MAX_NAMELEN_NP];
|
|
|
66 char long_thread_name[UV_PTHREAD_MAX_NAMELEN_NP + 1];
|
|
|
67 struct semaphores sem;
|
|
|
68
|
|
|
69 #if defined(__ANDROID_API__) && __ANDROID_API__ < 26 || \
|
|
|
70 defined(_AIX) || \
|
|
|
71 defined(__MVS__) || \
|
|
|
72 defined(__PASE__)
|
|
|
73 RETURN_SKIP("API not available on this platform");
|
|
|
74 #endif
|
|
|
75
|
|
|
76 ASSERT_OK(uv_sem_init(&sem.main, 0));
|
|
|
77 ASSERT_OK(uv_sem_init(&sem.worker, 0));
|
|
|
78
|
|
|
79 memset(thread_name, 'a', sizeof(thread_name) - 1);
|
|
|
80 thread_name[sizeof(thread_name) - 1] = '\0';
|
|
|
81
|
|
|
82 memset(long_thread_name, 'a', sizeof(long_thread_name) - 1);
|
|
|
83 long_thread_name[sizeof(long_thread_name) - 1] = '\0';
|
|
|
84
|
|
|
85 #ifdef _WIN32
|
|
|
86 /* uv_thread_self isn't defined for the main thread on Windows. */
|
|
|
87 threads[0] = GetCurrentThread();
|
|
|
88 #else
|
|
|
89 threads[0] = uv_thread_self();
|
|
|
90 #endif
|
|
|
91
|
|
|
92 r = uv_thread_getname(&threads[0], tn, sizeof(tn));
|
|
|
93 ASSERT_OK(r);
|
|
|
94
|
|
|
95 r = uv_thread_setname(long_thread_name);
|
|
|
96 ASSERT_OK(r);
|
|
|
97
|
|
|
98 r = uv_thread_getname(&threads[0], tn, sizeof(tn));
|
|
|
99 ASSERT_OK(r);
|
|
|
100 ASSERT_STR_EQ(tn, thread_name);
|
|
|
101
|
|
|
102 r = uv_thread_setname(thread_name);
|
|
|
103 ASSERT_OK(r);
|
|
|
104
|
|
|
105 r = uv_thread_getname(&threads[0], tn, sizeof(tn));
|
|
|
106 ASSERT_OK(r);
|
|
|
107 ASSERT_STR_EQ(tn, thread_name);
|
|
|
108
|
|
|
109 r = uv_thread_getname(&threads[0], tn, 3);
|
|
|
110 ASSERT_OK(r);
|
|
|
111 ASSERT_EQ(strlen(tn), 2);
|
|
|
112 ASSERT_OK(memcmp(thread_name, tn, 2));
|
|
|
113
|
|
|
114 /* Illumos doesn't support non-ASCII thread names. */
|
|
|
115 #ifndef __illumos__
|
|
|
116 r = uv_thread_setname("~½¬{½");
|
|
|
117 ASSERT_OK(r);
|
|
|
118
|
|
|
119 r = uv_thread_getname(&threads[0], tn, sizeof(tn));
|
|
|
120 ASSERT_OK(r);
|
|
|
121 ASSERT_STR_EQ(tn, "~½¬{½");
|
|
|
122 #endif
|
|
|
123
|
|
|
124 ASSERT_OK(uv_thread_create(threads + 1, thread_run, &sem));
|
|
|
125
|
|
|
126 uv_sem_wait(&sem.worker);
|
|
|
127
|
|
|
128 r = uv_thread_getname(threads + 1, tn, sizeof(tn));
|
|
|
129 ASSERT_OK(r);
|
|
|
130
|
|
|
131 ASSERT_STR_EQ(tn, "worker-thread");
|
|
|
132
|
|
|
133 uv_sem_post(&sem.main);
|
|
|
134
|
|
|
135 ASSERT_OK(uv_thread_join(threads + 1));
|
|
|
136
|
|
|
137 uv_sem_destroy(&sem.main);
|
|
|
138 uv_sem_destroy(&sem.worker);
|
|
|
139
|
|
|
140 return 0;
|
|
|
141 }
|
|
|
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
|
|
|
182 #if defined(_AIX) || defined(__PASE__)
|
|
|
183 RETURN_SKIP("API not available on this platform");
|
|
|
184 #endif
|
|
|
185 uv_work_t req;
|
|
|
186 loop = uv_default_loop();
|
|
|
187 // Just to make sure all workers will be executed
|
|
|
188 // with the correct thread name
|
|
|
189 ASSERT_OK(uv_queue_work(loop, &req, work_cb, after_work_cb));
|
|
|
190 uv_run(loop, UV_RUN_DEFAULT);
|
|
|
191 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
192 return 0;
|
|
|
193 }
|