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