Skip to content

Commit 66b1647

Browse files
committed
issue #3996: impl gettid for macOS & cygwin64.
1 parent 954b1b7 commit 66b1647

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

trunk/configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ if [[ $SRS_UTEST == YES ]]; then
464464
MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_kernel" "srs_utest_core"
465465
"srs_utest_config" "srs_utest_rtmp" "srs_utest_http" "srs_utest_avc" "srs_utest_reload"
466466
"srs_utest_mp4" "srs_utest_service" "srs_utest_app" "srs_utest_rtc" "srs_utest_config2"
467-
"srs_utest_protocol" "srs_utest_protocol2" "srs_utest_kernel2")
467+
"srs_utest_protocol" "srs_utest_protocol2" "srs_utest_kernel2" "srs_utest_thread_pool")
468468
if [[ $SRS_SRT == YES ]]; then
469469
MODULE_FILES+=("srs_utest_srt")
470470
fi

trunk/src/app/srs_app_threads.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ using namespace std;
3636
#include <unistd.h>
3737
#include <fcntl.h>
3838

39-
#if defined(SRS_OSX) || defined(SRS_CYGWIN64)
39+
#if defined(SRS_OSX)
40+
pid_t gettid() {
41+
uint64_t tid;
42+
return pthread_threadid_np(NULL, &tid) ? 0 : tid;
43+
}
44+
#elif defined(SRS_CYGWIN64)
4045
pid_t gettid() {
4146
return 0;
4247
}
@@ -550,6 +555,12 @@ SrsThreadPool::~SrsThreadPool()
550555
::close(pid_fd);
551556
pid_fd = -1;
552557
}
558+
559+
while(!threads_.empty()) {
560+
SrsThreadEntry* entry = threads_.back();
561+
srs_freep(entry);
562+
threads_.pop_back();
563+
}
553564
}
554565

555566
// Setup the thread-local variables, MUST call when each thread starting.
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright (c) 2013-2024 The SRS Authors
3+
//
4+
// SPDX-License-Identifier: MIT
5+
//
6+
#include <srs_utest_thread_pool.hpp>
7+
#include <pthread.h>
8+
9+
static srs_error_t dummy_loop(void*) {
10+
return srs_success;
11+
}
12+
13+
#ifndef SRS_CYGWIN64
14+
15+
VOID TEST(ThreadPoolTest, tid) {
16+
srs_error_t err;
17+
SrsThreadPool* thread_pool_1 = new SrsThreadPool();
18+
SrsThreadPool* thread_pool_2 = new SrsThreadPool();
19+
20+
EXPECT_TRUE((err = thread_pool_1->initialize()) == srs_success);
21+
srs_freep(err);
22+
23+
EXPECT_TRUE((err = thread_pool_2->initialize()) == srs_success);
24+
srs_freep(err);
25+
26+
EXPECT_TRUE((err = thread_pool_1->execute("hybrid", dummy_loop, (void*)NULL)) == srs_success);
27+
srs_freep(err);
28+
29+
EXPECT_TRUE((err = thread_pool_2->execute("hybrid", dummy_loop, (void*)NULL)) == srs_success);
30+
srs_freep(err);
31+
32+
err = thread_pool_1->run();
33+
srs_freep(err);
34+
err = thread_pool_2->run();
35+
srs_freep(err);
36+
37+
EXPECT_GT(thread_pool_1->hybrid()->tid, 0);
38+
EXPECT_GT(thread_pool_2->hybrid()->tid, 0);
39+
40+
EXPECT_NE(thread_pool_1->hybrid()->tid, thread_pool_2->hybrid()->tid);
41+
42+
srs_freep(thread_pool_1);
43+
srs_freep(thread_pool_2);
44+
}
45+
46+
#endif
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright (c) 2013-2024 The SRS Authors
3+
//
4+
// SPDX-License-Identifier: MIT
5+
//
6+
7+
#ifndef SRS_UTEST_THREAD_POOL
8+
#define SRS_UTEST_THREAD_POOL
9+
10+
#include <srs_utest.hpp>
11+
12+
#include <srs_app_threads.hpp>
13+
14+
#endif // SRS_UTEST_THREAD_POOL
15+

0 commit comments

Comments
 (0)