Init ST after daemon started.

This commit is contained in:
winlin 2025-10-13 10:00:51 -04:00
parent 123df8a75a
commit 31e191e9e3
3 changed files with 29 additions and 10 deletions

View File

@ -78,14 +78,10 @@ extern std::string _srs_reload_id;
extern SrsRtcBlackhole *_srs_blackhole; extern SrsRtcBlackhole *_srs_blackhole;
extern SrsDtlsCertificate *_srs_rtc_dtls_certificate; extern SrsDtlsCertificate *_srs_rtc_dtls_certificate;
bool _srs_global_initialized = false;
srs_error_t srs_global_initialize() srs_error_t srs_global_initialize()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// Root global objects.
_srs_log = new SrsFileLog();
_srs_context = new SrsThreadContext();
_srs_config = new SrsConfig();
// Initialize the global kbps statistics variables // Initialize the global kbps statistics variables
if ((err = srs_global_kbps_initialize()) != srs_success) { if ((err = srs_global_kbps_initialize()) != srs_success) {
@ -140,6 +136,9 @@ srs_error_t srs_global_initialize()
SrsRand rand; SrsRand rand;
_srs_reload_id = rand.gen_str(7); _srs_reload_id = rand.gen_str(7);
// Global initialization done
_srs_global_initialized = true;
return err; return err;
} }

View File

@ -96,10 +96,10 @@ srs_error_t do_main(int argc, char **argv, char **envp)
} }
#endif #endif
// Initialize global and thread-local variables. // Root global objects, should be created before any other global objects.
if ((err = srs_global_initialize()) != srs_success) { _srs_log = new SrsFileLog();
return srs_error_wrap(err, "global init"); _srs_context = new SrsThreadContext();
} _srs_config = new SrsConfig();
// For background context id. // For background context id.
_srs_context->set_id(_srs_context->generate_id()); _srs_context->set_id(_srs_context->generate_id());
@ -451,6 +451,14 @@ srs_error_t run_srs_server()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// Initialize global and thread-local variables.
if ((err = srs_global_initialize()) != srs_success) {
return srs_error_wrap(err, "global init");
}
// For primordial thread, share the default cid.
_srs_context->set_id(_srs_context->get_id());
_srs_server = new SrsServer(); _srs_server = new SrsServer();
// Do some system initialize. // Do some system initialize.

View File

@ -46,8 +46,14 @@ void _srs_context_destructor(void *arg)
srs_freep(cid); srs_freep(cid);
} }
extern bool _srs_global_initialized;
const SrsContextId &SrsThreadContext::get_id() const SrsContextId &SrsThreadContext::get_id()
{ {
if (!_srs_global_initialized) {
return _srs_context_default;
}
++_srs_pps_cids_get->sugar_; ++_srs_pps_cids_get->sugar_;
if (!srs_thread_self()) { if (!srs_thread_self()) {
@ -71,8 +77,14 @@ void SrsThreadContext::clear_cid()
{ {
} }
extern bool _srs_global_initialized;
const SrsContextId &srs_context_set_cid_of(srs_thread_t trd, const SrsContextId &v) const SrsContextId &srs_context_set_cid_of(srs_thread_t trd, const SrsContextId &v)
{ {
if (!_srs_global_initialized) {
_srs_context_default = v;
return v;
}
++_srs_pps_cids_set->sugar_; ++_srs_pps_cids_set->sugar_;
if (!trd) { if (!trd) {