From dca67c4b9e64ea52623ffac05bd57d2ec1762376 Mon Sep 17 00:00:00 2001 From: Jacob Su Date: Mon, 18 Mar 2024 22:40:21 +0800 Subject: [PATCH] issue #3996: impl gettid for macOS & cygwin64. --- trunk/configure | 2 +- trunk/src/app/srs_app_threads.cpp | 9 ++++-- trunk/src/utest/srs_utest_thread_pool.cpp | 36 +++++++++++++++++++++++ trunk/src/utest/srs_utest_thread_pool.hpp | 15 ++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 trunk/src/utest/srs_utest_thread_pool.cpp create mode 100644 trunk/src/utest/srs_utest_thread_pool.hpp diff --git a/trunk/configure b/trunk/configure index 24fe192c962..9358f7339c8 100755 --- a/trunk/configure +++ b/trunk/configure @@ -464,7 +464,7 @@ if [[ $SRS_UTEST == YES ]]; then MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_kernel" "srs_utest_core" "srs_utest_config" "srs_utest_rtmp" "srs_utest_http" "srs_utest_avc" "srs_utest_reload" "srs_utest_mp4" "srs_utest_service" "srs_utest_app" "srs_utest_rtc" "srs_utest_config2" - "srs_utest_protocol" "srs_utest_protocol2" "srs_utest_kernel2") + "srs_utest_protocol" "srs_utest_protocol2" "srs_utest_kernel2" "srs_utest_thread_pool") if [[ $SRS_SRT == YES ]]; then MODULE_FILES+=("srs_utest_srt") fi diff --git a/trunk/src/app/srs_app_threads.cpp b/trunk/src/app/srs_app_threads.cpp index b42a163be13..ecef96e41af 100644 --- a/trunk/src/app/srs_app_threads.cpp +++ b/trunk/src/app/srs_app_threads.cpp @@ -36,9 +36,14 @@ using namespace std; #include #include -#if defined(SRS_OSX) || defined(SRS_CYGWIN64) +#if defined(SRS_OSX) pid_t gettid() { - return 0; + uint64_t tid; + return pthread_threadid_np(NULL, &tid) ? 0 : tid; + } +#elif defined(SRS_CYGWIN64) + pid_t gettid() { + return (unsigned int) pthread_self(); } #else #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 diff --git a/trunk/src/utest/srs_utest_thread_pool.cpp b/trunk/src/utest/srs_utest_thread_pool.cpp new file mode 100644 index 00000000000..4cd4e458c73 --- /dev/null +++ b/trunk/src/utest/srs_utest_thread_pool.cpp @@ -0,0 +1,36 @@ +// +// Copyright (c) 2013-2024 The SRS Authors +// +// SPDX-License-Identifier: MIT +// +#include +#include + +static srs_error_t dummy_loop(void*) { + return srs_success; +} + + +VOID TEST(ThreadPoolTest, tid) { + SrsThreadPool* thread_pool_1 = new SrsThreadPool(); + SrsThreadPool* thread_pool_2 = new SrsThreadPool(); + + EXPECT_TRUE(thread_pool_1->execute("hybrid", dummy_loop, (void*)NULL) == srs_success); + EXPECT_TRUE(thread_pool_2->execute("hybrid", dummy_loop, (void*)NULL) == srs_success); + usleep(100); + + EXPECT_GT(thread_pool_1->hybrid()->tid, 0); + EXPECT_GT(thread_pool_2->hybrid()->tid, 0); + + EXPECT_NE(thread_pool_1->hybrid()->tid, thread_pool_2->hybrid()->tid); + + SrsThreadEntry* thread_entry_1 = thread_pool_1->hybrid(); + SrsThreadEntry* thread_entry_2 = thread_pool_2->hybrid(); + + // Workaround SrsThreadEntry Memory leak in SrsThreadPool, Maybe SrsThreadPool need to improve. + srs_freep(thread_entry_1); + srs_freep(thread_entry_2); + + srs_freep(thread_pool_1); + srs_freep(thread_pool_2); +} diff --git a/trunk/src/utest/srs_utest_thread_pool.hpp b/trunk/src/utest/srs_utest_thread_pool.hpp new file mode 100644 index 00000000000..81ea5a34d6c --- /dev/null +++ b/trunk/src/utest/srs_utest_thread_pool.hpp @@ -0,0 +1,15 @@ +// +// Copyright (c) 2013-2024 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#ifndef SRS_UTEST_THREAD_POOL +#define SRS_UTEST_THREAD_POOL + +#include + +#include + +#endif // SRS_UTEST_THREAD_POOL +