From 7423868de4265066e5f07c413a3f36354cb49045 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 28 Aug 2020 19:23:48 +0200 Subject: [PATCH] Made Util::streamName and exitReason thread-local --- lib/config.cpp | 10 ++++++++-- lib/config.h | 5 +++-- lib/defines.h | 10 +++++----- src/input/input.cpp | 2 +- src/output/output.cpp | 6 +++--- src/output/output_http_internal.cpp | 2 +- src/output/output_rtmp.cpp | 18 +++++++++--------- src/process/process_exec.h | 2 +- src/process/process_ffmpeg.cpp | 2 +- src/process/process_livepeer.cpp | 2 +- src/utils/util_nuke.cpp | 13 +++++++------ 11 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/config.cpp b/lib/config.cpp index d9a8470b..bf92dae9 100644 --- a/lib/config.cpp +++ b/lib/config.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include // for va_list #include #include @@ -39,8 +40,13 @@ bool Util::Config::is_active = false; bool Util::Config::is_restarting = false; static Socket::Server *serv_sock_pointer = 0; uint32_t Util::printDebugLevel = DEBUG; -std::string Util::streamName; -char Util::exitReason[256] ={0}; +__thread char Util::streamName[256] = {0}; +__thread char Util::exitReason[256] ={0}; + + +void Util::setStreamName(const std::string & sn){ + strncpy(Util::streamName, sn.c_str(), 256); +} void Util::logExitReason(const char *format, ...){ if (exitReason[0]){return;} diff --git a/lib/config.h b/lib/config.h index f09eb3ee..2b1f5163 100644 --- a/lib/config.h +++ b/lib/config.h @@ -14,8 +14,9 @@ /// Contains utility code, not directly related to streaming media namespace Util{ extern uint32_t printDebugLevel; - extern std::string streamName; ///< Used by debug messages to identify the stream name - extern char exitReason[256]; + extern __thread char streamName[256]; ///< Used by debug messages to identify the stream name + void setStreamName(const std::string & sn); + extern __thread char exitReason[256]; void logExitReason(const char *format, ...); /// Deals with parsing configuration from commandline options. diff --git a/lib/defines.h b/lib/defines.h index f686bc26..9292a7b0 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -32,7 +32,7 @@ //Declare as extern so we don't have to include the whole config.h header namespace Util{ extern uint32_t printDebugLevel; - extern std::string streamName; + extern __thread char streamName[256]; } static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "INFO", "MEDIUM", @@ -53,13 +53,13 @@ static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "IN #define DEBUG_MSG(lvl, msg, ...) \ if (Util::printDebugLevel >= lvl){\ fprintf(stderr, "%s|%s|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ - getpid(), __FILE__, __LINE__, Util::streamName.c_str(), ##__VA_ARGS__); \ + getpid(), __FILE__, __LINE__, Util::streamName, ##__VA_ARGS__); \ } #else #define DEBUG_MSG(lvl, msg, ...) \ if (Util::printDebugLevel >= lvl){\ fprintf(stderr, "%s|%s|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ - getpid(), Util::streamName.c_str(), ##__VA_ARGS__); \ + getpid(), Util::streamName, ##__VA_ARGS__); \ } #endif #else @@ -67,13 +67,13 @@ static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "IN #define DEBUG_MSG(lvl, msg, ...) \ if (Util::printDebugLevel >= lvl){\ fprintf(stderr, "%s|MistProcess|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), __FILE__, \ - __LINE__, Util::streamName.c_str(), ##__VA_ARGS__); \ + __LINE__, Util::streamName, ##__VA_ARGS__); \ } #else #define DEBUG_MSG(lvl, msg, ...) \ if (Util::printDebugLevel >= lvl){\ fprintf(stderr, "%s|MistProcess|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), \ - Util::streamName.c_str(), ##__VA_ARGS__); \ + Util::streamName, ##__VA_ARGS__); \ } #endif #endif diff --git a/src/input/input.cpp b/src/input/input.cpp index d39e5dbf..51a68a37 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -310,7 +310,7 @@ namespace Mist{ int Input::boot(int argc, char *argv[]){ if (!(config->parseArgs(argc, argv))){return 1;} streamName = config->getString("streamname"); - Util::streamName = streamName; + Util::setStreamName(streamName); if (config->getBool("json")){ capa["version"] = PACKAGE_VERSION; diff --git a/src/output/output.cpp b/src/output/output.cpp index 4c7b2f57..778305b8 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -87,7 +87,7 @@ namespace Mist{ // If we have a streamname option, set internal streamname to that option if (!streamName.size() && config->hasOption("streamname")){ streamName = config->getString("streamname"); - Util::streamName = streamName; + Util::setStreamName(streamName); } /*LTS-START*/ @@ -327,7 +327,7 @@ namespace Mist{ JSON::Value strCnf = Util::getStreamConfig(streamName); if (strCnf && strCnf["fallback_stream"].asStringRef().size()){ streamName = strCnf["fallback_stream"].asStringRef(); - Util::streamName = streamName; + Util::setStreamName(streamName); INFO_MSG("Switching to configured fallback stream '%s'", streamName.c_str()); reconnect(); return; @@ -358,7 +358,7 @@ namespace Mist{ newStrm.c_str()); std::string origStream = streamName; streamName = newStrm; - Util::streamName = streamName; + Util::setStreamName(streamName); if (!Util::startInput(streamName, "", true, isPushing())){ onFail("Stream open failed (fallback stream for '" + origStream + "')", true); return; diff --git a/src/output/output_http_internal.cpp b/src/output/output_http_internal.cpp index 4efa195d..8dba3285 100644 --- a/src/output/output_http_internal.cpp +++ b/src/output/output_http_internal.cpp @@ -450,7 +450,7 @@ namespace Mist{ INFO_MSG("Falling back to default stream '%s' -> '%s'", defStrm.c_str(), newStrm.c_str()); origStreamName = streamName; streamName = newStrm; - Util::streamName = streamName; + Util::setStreamName(streamName); reconnect(); return getStatusJSON(reqHost, useragent); } diff --git a/src/output/output_rtmp.cpp b/src/output/output_rtmp.cpp index f433ff99..1d72e93c 100644 --- a/src/output/output_rtmp.cpp +++ b/src/output/output_rtmp.cpp @@ -828,11 +828,11 @@ namespace Mist{ if (streamName.find('?') != std::string::npos){ std::string tmpVars = streamName.substr(streamName.find('?') + 1); streamName = streamName.substr(0, streamName.find('?')); - Util::streamName = streamName; + Util::setStreamName(streamName); HTTP::parseVars(tmpVars, targetParams); } - Util::streamName = streamName; + Util::setStreamName(streamName); reqUrl += "/" + streamName; // LTS /*LTS-START*/ @@ -850,17 +850,17 @@ namespace Mist{ size_t lSlash = newUrl.rfind('/'); if (lSlash != std::string::npos){ streamName = newUrl.substr(lSlash + 1); - Util::streamName = streamName; + Util::setStreamName(streamName); }else{ streamName = newUrl; - Util::streamName = streamName; + Util::setStreamName(streamName); } } /*LTS-END*/ if (streamName.find('/')){ streamName = streamName.substr(0, streamName.find('/')); - Util::streamName = streamName; + Util::setStreamName(streamName); } size_t colonPos = streamName.find(':'); @@ -871,7 +871,7 @@ namespace Mist{ }else{ streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos); } - Util::streamName = streamName; + Util::setStreamName(streamName); } Util::sanitizeName(streamName); @@ -941,14 +941,14 @@ namespace Mist{ int8_t playMessageType = messageType; int32_t playStreamId = streamId; streamName = Encodings::URL::decode(amfData.getContentP(3)->StrValue()); - Util::streamName = streamName; + Util::setStreamName(streamName); reqUrl += "/" + streamName; // LTS // handle variables if (streamName.find('?') != std::string::npos){ std::string tmpVars = streamName.substr(streamName.find('?') + 1); streamName = streamName.substr(0, streamName.find('?')); - Util::streamName = streamName; + Util::setStreamName(streamName); HTTP::parseVars(tmpVars, targetParams); } @@ -960,7 +960,7 @@ namespace Mist{ }else{ streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos); } - Util::streamName = streamName; + Util::setStreamName(streamName); } Util::sanitizeName(streamName); diff --git a/src/process/process_exec.h b/src/process/process_exec.h index 17acb831..2d4dc3dd 100644 --- a/src/process/process_exec.h +++ b/src/process/process_exec.h @@ -46,7 +46,7 @@ namespace Mist{ streamName = opt["sink"].asString(); if (!streamName.size()){streamName = opt["source"].asString();} Util::streamVariables(streamName, opt["source"].asString()); - Util::streamName = opt["source"].asString() + "→" + streamName; + Util::setStreamName(opt["source"].asString() + "→" + streamName); } bool needsLock(){return false;} bool isSingular(){return false;} diff --git a/src/process/process_ffmpeg.cpp b/src/process/process_ffmpeg.cpp index 74e04dfc..1b6abb96 100644 --- a/src/process/process_ffmpeg.cpp +++ b/src/process/process_ffmpeg.cpp @@ -389,7 +389,7 @@ namespace Mist{ streamName = opt["sink"].asString(); if (!streamName.size()){streamName = opt["source"].asString();} Util::streamVariables(streamName, opt["source"].asString()); - Util::streamName = opt["source"].asString() + "→" + streamName; + Util::setStreamName(opt["source"].asString() + "→" + streamName); } std::string EncodeOutputEBML::getTrackType(int tid){return M.getType(tid);} diff --git a/src/process/process_livepeer.cpp b/src/process/process_livepeer.cpp index 21fb8896..3220a884 100644 --- a/src/process/process_livepeer.cpp +++ b/src/process/process_livepeer.cpp @@ -227,7 +227,7 @@ namespace Mist{ streamName = opt["sink"].asString(); if (!streamName.size()){streamName = opt["source"].asString();} Util::streamVariables(streamName, opt["source"].asString()); - Util::streamName = opt["source"].asString() + "→" + streamName; + Util::setStreamName(opt["source"].asString() + "→" + streamName); preRun(); }; virtual bool needsLock(){return false;} diff --git a/src/utils/util_nuke.cpp b/src/utils/util_nuke.cpp index c4fbc944..8f984864 100644 --- a/src/utils/util_nuke.cpp +++ b/src/utils/util_nuke.cpp @@ -4,6 +4,7 @@ #include #include #include +#include const char * getStateString(uint8_t state){ switch (state){ @@ -21,7 +22,7 @@ const char * getStateString(uint8_t state){ /// Gets a PID from a shared memory page, if it exists uint64_t getPidFromPage(const char * pagePattern){ char pageName[NAME_BUFFER_SIZE]; - snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); + snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName); IPC::sharedPage pidPage(pageName, 8, false, false); if (pidPage){ return *(uint64_t*)(pidPage.mapped); @@ -32,7 +33,7 @@ uint64_t getPidFromPage(const char * pagePattern){ /// Deletes a shared memory page, if it exists void nukePage(const char * pagePattern){ char pageName[NAME_BUFFER_SIZE]; - snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); + snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName); IPC::sharedPage page(pageName, 0, false, false); page.master = true; } @@ -40,7 +41,7 @@ void nukePage(const char * pagePattern){ /// Deletes a semaphore, if it exists void nukeSem(const char * pagePattern){ char pageName[NAME_BUFFER_SIZE]; - snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); + snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName); IPC::semaphore sem(pageName, O_RDWR, ACCESSPERMS, 0, true); if (sem){sem.unlink();} } @@ -51,7 +52,7 @@ int main(int argc, char **argv){ FAIL_MSG("Usage: %s STREAM_NAME", argv[0]); return 1; } - Util::streamName = argv[1]; + Util::setStreamName(argv[1]); uint8_t state = Util::getStreamStatus(Util::streamName); INFO_MSG("Current stream status: %s", getStateString(state)); size_t loops = 0; @@ -73,7 +74,7 @@ int main(int argc, char **argv){ // Scoping to clear up metadata and track providers { char pageName[NAME_BUFFER_SIZE]; - snprintf(pageName, NAME_BUFFER_SIZE, SHM_STREAM_META, Util::streamName.c_str()); + snprintf(pageName, NAME_BUFFER_SIZE, SHM_STREAM_META, Util::streamName); IPC::sharedPage streamPage(pageName, 0, false, false); if (streamPage.mapped){ streamPage.master = true; @@ -98,7 +99,7 @@ int main(int argc, char **argv){ for (uint64_t j = pages.getDeleted(); j < pages.getEndPos(); j++){ char thisPageName[NAME_BUFFER_SIZE]; snprintf(thisPageName, NAME_BUFFER_SIZE, SHM_TRACK_DATA, - Util::streamName.c_str(), i, pages.getInt("firstkey", j)); + Util::streamName, i, pages.getInt("firstkey", j)); IPC::sharedPage p(thisPageName, 0); p.master = true; }