nodejs/deps/uv/test/test-thread.c

307 lines
8.1 KiB
C
Raw Permalink Normal View History

2011-12-16 13:35:08 -08:00
/* Copyright Joyent, Inc. and other Node 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 <stdio.h>
#include <stdlib.h>
2012-01-23 13:35:36 +01:00
#include <string.h> /* memset */
2011-12-29 00:18:23 +01:00
#ifdef __POSIX__
#include <pthread.h>
#endif
2011-12-29 00:18:23 +01:00
struct getaddrinfo_req {
uv_thread_t thread_id;
unsigned int counter;
uv_loop_t* loop;
uv_getaddrinfo_t handle;
};
struct fs_req {
uv_thread_t thread_id;
unsigned int counter;
uv_loop_t* loop;
uv_fs_t handle;
};
2012-01-16 17:24:53 -08:00
2012-09-14 02:56:41 +02:00
struct test_thread {
2012-01-16 17:24:53 -08:00
uv_thread_t thread_id;
int thread_called;
2012-01-16 17:24:53 -08:00
};
2011-12-29 00:18:23 +01:00
static void getaddrinfo_do(struct getaddrinfo_req* req);
static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
int status,
struct addrinfo* res);
static void fs_do(struct fs_req* req);
static void fs_cb(uv_fs_t* handle);
2011-12-16 13:35:08 -08:00
static int thread_called;
2013-08-29 15:04:27 +02:00
static uv_key_t tls_key;
2011-12-16 13:35:08 -08:00
2011-12-29 00:18:23 +01:00
static void getaddrinfo_do(struct getaddrinfo_req* req) {
int r;
r = uv_getaddrinfo(req->loop,
&req->handle,
getaddrinfo_cb,
"localhost",
NULL,
NULL);
ASSERT_OK(r);
2011-12-29 00:18:23 +01:00
}
static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
int status,
struct addrinfo* res) {
struct getaddrinfo_req* req;
ASSERT_OK(status);
2011-12-29 00:18:23 +01:00
req = container_of(handle, struct getaddrinfo_req, handle);
uv_freeaddrinfo(res);
if (--req->counter)
getaddrinfo_do(req);
}
static void fs_do(struct fs_req* req) {
int r;
r = uv_fs_stat(req->loop, &req->handle, ".", fs_cb);
ASSERT_OK(r);
2011-12-29 00:18:23 +01:00
}
static void fs_cb(uv_fs_t* handle) {
struct fs_req* req = container_of(handle, struct fs_req, handle);
2012-01-16 17:24:53 -08:00
uv_fs_req_cleanup(handle);
2011-12-29 00:18:23 +01:00
if (--req->counter)
fs_do(req);
}
static void do_work(void* arg) {
struct getaddrinfo_req getaddrinfo_reqs[4];
struct fs_req fs_reqs[4];
uv_loop_t loop;
2011-12-29 00:18:23 +01:00
size_t i;
2012-09-14 02:56:41 +02:00
struct test_thread* thread = arg;
2011-12-29 00:18:23 +01:00
ASSERT_OK(uv_loop_init(&loop));
2011-12-29 00:18:23 +01:00
for (i = 0; i < ARRAY_SIZE(getaddrinfo_reqs); i++) {
struct getaddrinfo_req* req = getaddrinfo_reqs + i;
req->counter = 4;
req->loop = &loop;
2011-12-29 00:18:23 +01:00
getaddrinfo_do(req);
}
for (i = 0; i < ARRAY_SIZE(fs_reqs); i++) {
struct fs_req* req = fs_reqs + i;
req->counter = 4;
req->loop = &loop;
2011-12-29 00:18:23 +01:00
fs_do(req);
}
ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
ASSERT_OK(uv_loop_close(&loop));
2012-01-16 17:24:53 -08:00
thread->thread_called = 1;
2011-12-29 00:18:23 +01:00
}
2011-12-16 13:35:08 -08:00
static void thread_entry(void* arg) {
ASSERT_PTR_EQ(arg, (void *) 42);
2011-12-16 13:35:08 -08:00
thread_called++;
}
TEST_IMPL(thread_create) {
uv_thread_t tid;
int r;
r = uv_thread_create(&tid, thread_entry, (void *) 42);
ASSERT_OK(r);
2011-12-16 13:35:08 -08:00
r = uv_thread_join(&tid);
ASSERT_OK(r);
2011-12-16 13:35:08 -08:00
ASSERT_EQ(1, thread_called);
2011-12-16 13:35:08 -08:00
return 0;
}
2011-12-20 20:55:14 +01:00
2011-12-29 00:18:23 +01:00
/* Hilariously bad test name. Run a lot of tasks in the thread pool and verify
* that each "finished" callback is run in its originating thread.
*/
TEST_IMPL(threadpool_multiple_event_loops) {
/* TODO(gengjiawen): Fix test on QEMU. */
#if defined(__QEMU__)
RETURN_SKIP("Test does not currently work in QEMU");
#endif
2012-09-14 02:56:41 +02:00
struct test_thread threads[8];
2011-12-29 00:18:23 +01:00
size_t i;
int r;
2012-01-16 17:24:53 -08:00
memset(threads, 0, sizeof(threads));
2011-12-29 00:18:23 +01:00
for (i = 0; i < ARRAY_SIZE(threads); i++) {
2012-01-16 17:24:53 -08:00
r = uv_thread_create(&threads[i].thread_id, do_work, &threads[i]);
ASSERT_OK(r);
2011-12-29 00:18:23 +01:00
}
for (i = 0; i < ARRAY_SIZE(threads); i++) {
2012-01-16 17:24:53 -08:00
r = uv_thread_join(&threads[i].thread_id);
ASSERT_OK(r);
ASSERT_EQ(1, threads[i].thread_called);
2011-12-29 00:18:23 +01:00
}
return 0;
}
2013-08-29 15:04:27 +02:00
static void tls_thread(void* arg) {
ASSERT_NULL(uv_key_get(&tls_key));
2013-08-29 15:04:27 +02:00
uv_key_set(&tls_key, arg);
ASSERT_PTR_EQ(arg, uv_key_get(&tls_key));
2013-08-29 15:04:27 +02:00
uv_key_set(&tls_key, NULL);
ASSERT_NULL(uv_key_get(&tls_key));
2013-08-29 15:04:27 +02:00
}
TEST_IMPL(thread_local_storage) {
char name[] = "main";
uv_thread_t threads[2];
ASSERT_OK(uv_key_create(&tls_key));
ASSERT_NULL(uv_key_get(&tls_key));
2013-08-29 15:04:27 +02:00
uv_key_set(&tls_key, name);
ASSERT_PTR_EQ(name, uv_key_get(&tls_key));
ASSERT_OK(uv_thread_create(threads + 0, tls_thread, threads + 0));
ASSERT_OK(uv_thread_create(threads + 1, tls_thread, threads + 1));
ASSERT_OK(uv_thread_join(threads + 0));
ASSERT_OK(uv_thread_join(threads + 1));
2013-08-29 15:04:27 +02:00
uv_key_delete(&tls_key);
return 0;
}
static void thread_check_stack(void* arg) {
#if defined(__APPLE__)
size_t expected;
expected = arg == NULL ? 0 : ((uv_thread_options_t*)arg)->stack_size;
/* 512 kB is the default stack size of threads other than the main thread
* on MacOS. */
if (expected == 0)
expected = 512 * 1024;
ASSERT_GE(pthread_get_stacksize_np(pthread_self()), expected);
#elif defined(__linux__) && defined(__GLIBC__)
size_t expected;
struct rlimit lim;
size_t stack_size;
pthread_attr_t attr;
ASSERT_OK(getrlimit(RLIMIT_STACK, &lim));
if (lim.rlim_cur == RLIM_INFINITY)
lim.rlim_cur = 2 << 20; /* glibc default. */
ASSERT_OK(pthread_getattr_np(pthread_self(), &attr));
ASSERT_OK(pthread_attr_getstacksize(&attr, &stack_size));
expected = arg == NULL ? 0 : ((uv_thread_options_t*)arg)->stack_size;
if (expected == 0)
expected = (size_t)lim.rlim_cur;
ASSERT_GE(stack_size, expected);
ASSERT_OK(pthread_attr_destroy(&attr));
#endif
}
TEST_IMPL(thread_stack_size) {
uv_thread_t thread;
ASSERT_OK(uv_thread_create(&thread, thread_check_stack, NULL));
ASSERT_OK(uv_thread_join(&thread));
return 0;
}
TEST_IMPL(thread_stack_size_explicit) {
uv_thread_t thread;
uv_thread_options_t options;
options.flags = UV_THREAD_HAS_STACK_SIZE;
options.stack_size = 1024 * 1024;
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
options.stack_size = 8 * 1024 * 1024; /* larger than most default os sizes */
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
options.stack_size = 0;
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
options.stack_size = 42;
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
#ifdef PTHREAD_STACK_MIN
options.stack_size = PTHREAD_STACK_MIN - 42; /* unaligned size */
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
options.stack_size = PTHREAD_STACK_MIN / 2 - 42; /* unaligned size */
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
#endif
/* unaligned size, should be larger than PTHREAD_STACK_MIN */
options.stack_size = 1234567;
ASSERT_OK(uv_thread_create_ex(&thread, &options,
thread_check_stack, &options));
ASSERT_OK(uv_thread_join(&thread));
return 0;
}
static void thread_detach_cb(void* arg) {}
TEST_IMPL(thread_detach) {
uv_thread_t thread;
ASSERT_OK(uv_thread_create(&thread, thread_detach_cb, NULL));
ASSERT_OK(uv_thread_detach(&thread));
return 0;
}