Skip to content

Commit

Permalink
Threads-Hybrid: Init ST for each threads
Browse files Browse the repository at this point in the history
1. ST is thread-local now.
2. MUST init st for each threads.
3. Do it as early as possible.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 2fa3aca commit 9f4fbdb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
25 changes: 16 additions & 9 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down
1 change: 1 addition & 0 deletions trunk/src/protocol/srs_service_st.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
11 changes: 5 additions & 6 deletions trunk/src/utest/srs_utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ bool _srs_in_docker = false;
#include <srs_app_st.hpp>

// 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 9f4fbdb

Please sign in to comment.