From 8de24ad5b515bdbe2bab37acfd01ec59a7c50425 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 24 May 2023 17:32:06 +0800 Subject: [PATCH] add file src/tests/test_thread_local.c --- .gitignore | 1 + src/tests/Makefile | 3 +- src/tests/test_thread_local.c | 93 +++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/tests/test_thread_local.c diff --git a/.gitignore b/.gitignore index 6c44ddd..aef4ce1 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,7 @@ src/tests/test_queue_perf src/tests/test_normalize_path src/tests/test_sorted_array src/tests/test_sorted_queue +src/tests/test_thread_local # other *.swp diff --git a/src/tests/Makefile b/src/tests/Makefile index c4f78ef..a3c7040 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -10,7 +10,8 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo test_json_parser test_pthread_lock test_uniq_skiplist test_split_string \ test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \ test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \ - test_queue_perf test_normalize_path test_sorted_array test_sorted_queue + test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \ + test_thread_local all: $(ALL_PRGS) .c: diff --git a/src/tests/test_thread_local.c b/src/tests/test_thread_local.c new file mode 100644 index 0000000..e133824 --- /dev/null +++ b/src/tests/test_thread_local.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2020 YuQing <384681@qq.com> + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the Lesser GNU General Public License, version 3 + * or later ("LGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include "fastcommon/logger.h" +#include "fastcommon/shared_func.h" +#include "fastcommon/pthread_func.h" + +#define THREAD_COUNT 16 + +static pthread_key_t key; +volatile int running_count; + +static void destroy(void *ptr) +{ + printf("destroy ptr: %p\n", ptr); +} + +static void test_fetch(void *ptr) +{ + int i; + + for (i=0; i<10000000; i++) { + if (pthread_getspecific(key) != ptr) { + logError("pthread_getspecific fail"); + } + } +} + +static void *thread_run(void *args) +{ + void *ptr; + + ptr = pthread_getspecific(key); + if (ptr == NULL) { + ptr = malloc(64); + pthread_setspecific(key, ptr); + printf("create ptr: %p\n", ptr); + } + + fc_sleep_ms(1); + test_fetch(ptr); + + __sync_fetch_and_sub(&running_count, 1); + return NULL; +} + +int main(int argc, char *argv[]) +{ + pthread_t tid; + int64_t start_time; + int i; + int result; + + log_init(); + + start_time = get_current_time_ms(); + if ((result=pthread_key_create(&key, destroy)) != 0) { + logError("pthread_key_create fail"); + return result; + } + + running_count = THREAD_COUNT; + for (i=0; i