From 9f4fbdbd26be1ac3415aaa0008dca776a93f8b2e Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Apr 2021 12:39:28 +0800 Subject: [PATCH] Threads-Hybrid: Init ST for each threads 1. ST is thread-local now. 2. MUST init st for each threads. 3. Do it as early as possible. --- trunk/src/app/srs_app_threads.cpp | 25 ++++++++++++++++--------- trunk/src/app/srs_app_threads.hpp | 2 +- trunk/src/main/srs_main_server.cpp | 13 ++++++++----- trunk/src/protocol/srs_service_st.cpp | 1 + trunk/src/utest/srs_utest.cpp | 11 +++++------ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/trunk/src/app/srs_app_threads.cpp b/trunk/src/app/srs_app_threads.cpp index ce7c346c7b..f63cf108e8 100644 --- a/trunk/src/app/srs_app_threads.cpp +++ b/trunk/src/app/srs_app_threads.cpp @@ -215,22 +215,26 @@ extern const int LOG_MAX_SIZE; extern __thread char* _srs_log_data; // Setup the thread-local variables, MUST call when each thread starting. -void SrsThreadPool::setup() +srs_error_t SrsThreadPool::setup() { + srs_error_t err = srs_success; + // Initialize the log shared buffer for threads. srs_assert(!_srs_log_data); _srs_log_data = new char[LOG_MAX_SIZE]; + + // MUST init ST for each thread, because ST is thread-local now. + if ((err = srs_st_init()) != srs_success) { + return srs_error_wrap(err, "init st"); + } + + return err; } srs_error_t SrsThreadPool::initialize() { srs_error_t err = srs_success; - // TODO: FIXME: Should init ST for each thread. - if ((err = srs_st_init()) != srs_success) { - return srs_error_wrap(err, "initialize st failed"); - } - SrsThreadEntry* entry = (SrsThreadEntry*)entry_; #ifndef SRS_OSX // Load CPU affinity from config. @@ -489,13 +493,16 @@ SrsThreadEntry* SrsThreadPool::hybrid() void* SrsThreadPool::start(void* arg) { - // Initialize thread-local variables. - SrsThreadPool::setup(); - srs_error_t err = srs_success; SrsThreadEntry* entry = (SrsThreadEntry*)arg; + // Initialize thread-local variables. + if ((err = SrsThreadPool::setup()) != srs_success) { + entry->err = err; + return NULL; + } + // Set the thread local fields. entry->tid = gettid(); diff --git a/trunk/src/app/srs_app_threads.hpp b/trunk/src/app/srs_app_threads.hpp index 21e931c024..317cc99afc 100644 --- a/trunk/src/app/srs_app_threads.hpp +++ b/trunk/src/app/srs_app_threads.hpp @@ -193,7 +193,7 @@ class SrsThreadPool bool hybrid_critical_water_level(); bool hybrid_dying_water_level(); // Setup the thread-local variables. - static void setup(); + static srs_error_t setup(); // Initialize the thread pool. srs_error_t initialize(); // Execute start function with label in thread. diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index a9dad9653a..7b27144a80 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -90,7 +90,12 @@ SrsServer* _srs_server = NULL; srs_error_t do_main(int argc, char** argv) { srs_error_t err = srs_success; - + + // Initialize thread-local variables. + if ((err = SrsThreadPool::setup()) != srs_success) { + return srs_error_wrap(err, "thread init"); + } + // TODO: support both little and big endian. srs_assert(srs_is_little_endian()); @@ -223,10 +228,8 @@ srs_error_t do_main(int argc, char** argv) return err; } -int main(int argc, char** argv) { - // Initialize thread-local variables. - SrsThreadPool::setup(); - +int main(int argc, char** argv) +{ // For background context id. _srs_context->set_id(_srs_context->generate_id()); diff --git a/trunk/src/protocol/srs_service_st.cpp b/trunk/src/protocol/srs_service_st.cpp index 61f2ce36ff..1547858eb3 100644 --- a/trunk/src/protocol/srs_service_st.cpp +++ b/trunk/src/protocol/srs_service_st.cpp @@ -70,6 +70,7 @@ srs_error_t srs_st_init() return srs_error_new(ERROR_ST_SET_EPOLL, "st enable st failed, current is %s", st_get_eventsys_name()); } + // TODO: FIXME: Pass the cid of thread. // Before ST init, we might have already initialized the background cid. SrsContextId cid = _srs_context->get_id(); if (cid.empty()) { diff --git a/trunk/src/utest/srs_utest.cpp b/trunk/src/utest/srs_utest.cpp index 632a6ca45f..8770d892d3 100644 --- a/trunk/src/utest/srs_utest.cpp +++ b/trunk/src/utest/srs_utest.cpp @@ -52,11 +52,13 @@ bool _srs_in_docker = false; #include // Initialize global settings. -srs_error_t prepare_main() { +srs_error_t prepare_main() +{ srs_error_t err = srs_success; - if ((err = srs_st_init()) != srs_success) { - return srs_error_wrap(err, "init st"); + // Initialize thread-local variables. + if ((err = SrsThreadPool::setup()) != srs_success) { + return srs_error_wrap(err, "thread init"); } if ((err = _srs_rtc_dtls_certificate->initialize()) != srs_success) { @@ -73,9 +75,6 @@ srs_error_t prepare_main() { // Copy from gtest-1.6.0/src/gtest_main.cc GTEST_API_ int main(int argc, char **argv) { - // Initialize thread-local variables. - SrsThreadPool::setup(); - srs_error_t err = srs_success; if ((err = prepare_main()) != srs_success) {