From 649a36ea09c38d1b0a98ce7dc76c030b844c9175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Mon, 12 Oct 2026 16:27:14 +0200 Subject: [PATCH 1/2] accept comma separated rx_dbitlist (#1463) * accept comma separated rx_dbitlist * added counters * bumping workflow versions * bumping actions version and fixing conda-build * check brackets * updated help * updated trimen * handling thresholds --------- Co-authored-by: AliceMazzoleni99 --- slsDetectorSoftware/src/CallerSpecial.cpp | 81 ++++++++++++----------- slsSupportLib/include/sls/ToString.h | 55 ++++++++++++++- slsSupportLib/include/sls/string_utils.h | 3 + slsSupportLib/src/string_utils.cpp | 10 +++ slsSupportLib/tests/test-ToString.cpp | 20 ++++++ slsSupportLib/tests/test-string_utils.cpp | 7 ++ 6 files changed, 134 insertions(+), 42 deletions(-) diff --git a/slsDetectorSoftware/src/CallerSpecial.cpp b/slsDetectorSoftware/src/CallerSpecial.cpp index 2ece1cd04..005f29196 100644 --- a/slsDetectorSoftware/src/CallerSpecial.cpp +++ b/slsDetectorSoftware/src/CallerSpecial.cpp @@ -424,41 +424,43 @@ std::string Caller::threshold(int action) { throw RuntimeError("Not implemented for this detector\n"); } } else if (action == defs::PUT_ACTION) { + if (args.empty()) { + WrongNumberOfParameters(1); + } defs::detectorType type = det->getDetectorType().squash(); - if (type == defs::EIGER && args.size() != 1 && args.size() != 2) { - WrongNumberOfParameters(1); - } - if (type == defs::MYTHEN3 && (args.size() < 1 || args.size() > 4)) { - WrongNumberOfParameters(1); - } - bool trimbits = (cmd == "thresholdnotb") ? false : true; - std::array energy = {StringTo(args[0]), 0, 0}; - energy[1] = energy[0]; - energy[2] = energy[0]; - defs::detectorSettings sett = defs::STANDARD; - // check if argument has settings or get it - if (args.size() == 2 || args.size() == 4) { - sett = StringTo(args[args.size() - 1]); + // settings is optional, last and the only argument starting with a + // letter. The energies before it accept "a b c" as well as "a,b,c" + auto energyArgs = args; + defs::detectorSettings sett = defs::STANDARD; + if (std::isalpha(static_cast(args.back()[0]))) { + sett = StringTo(args.back()); + energyArgs.pop_back(); } else { sett = det->getSettings(std::vector{det_id}) .tsquash("Inconsistent settings between detectors"); } + auto energy = StringTo>(join(energyArgs, ',')); - // get other threshold values - if (args.size() > 2) { - energy[1] = StringTo(args[1]); - energy[2] = StringTo(args[2]); - } switch (type) { case defs::EIGER: + if (energy.size() != 1) { + throw RuntimeError("Expected one threshold energy"); + } det->setThresholdEnergy(energy[0], sett, trimbits, std::vector{det_id}); break; case defs::MYTHEN3: - det->setThresholdEnergy(energy, sett, trimbits, - std::vector{det_id}); + // one energy applies to all three counters + if (energy.size() == 1) { + energy.resize(3, energy[0]); + } + if (energy.size() != 3) { + throw RuntimeError("Expected one or three threshold energies"); + } + det->setThresholdEnergy({energy[0], energy[1], energy[2]}, sett, + trimbits, std::vector{det_id}); break; default: throw RuntimeError("Not implemented for this detector\n"); @@ -485,19 +487,16 @@ std::string Caller::trimen(int action) { auto t = det->getTrimEnergies(std::vector{det_id}); os << OutString(t) << '\n'; } else if (action == defs::PUT_ACTION) { - std::vector t(args.size()); - if (!args.empty()) { - for (size_t i = 0; i < t.size(); ++i) { - t[i] = StringTo(args[i]); - } - } + // accepts both "4500 5400" and "4500,5400", empty list clears + auto t = StringTo>(join(args, ',')); det->setTrimEnergies(t, std::vector{det_id}); - os << ToString(args) << '\n'; + os << ToString(t) << '\n'; } else { throw RuntimeError("Unknown action"); } return os.str(); } + std::string Caller::badchannels(int action) { std::ostringstream os; if (action == defs::HELP_ACTION) { @@ -1013,6 +1012,9 @@ std::string Caller::counters(int action) { "enabled. Each element in list can be 0 - 2 and must be non " "repetitive. Enabling counters sets vth dacs to remembered " "values and disabling sets them to disabled values." + "Accepts both space separated and comma separated list of indices" + "optionally enclosed in brackets. Example: 0 1 2 or 0,1,2 or " + "[0,1,2] or [0 1 2]" << '\n'; } else if (action == defs::GET_ACTION) { if (!args.empty()) { @@ -1024,15 +1026,15 @@ std::string Caller::counters(int action) { if (args.empty()) { WrongNumberOfParameters(1); } - if (std::any_of(args.cbegin(), args.cend(), [](std::string s) { - return (StringTo(s) < 0 || StringTo(s) > 2); - })) { + // accepts both "0 1 2" and "0,1,2" + auto counters = StringTo>(join(args, ',')); + if (std::any_of(counters.cbegin(), counters.cend(), + [](int val) { return (val < 0 || val > 2); })) { throw RuntimeError("Invalid counter indices list. Example: 0 1 2"); } // convert vector to counter enable mask uint32_t mask = 0; - for (size_t i = 0; i < args.size(); ++i) { - int val = StringTo(args[i]); + for (auto val : counters) { // already enabled earlier if (mask & (1 << val)) { std::ostringstream oss; @@ -1042,7 +1044,7 @@ std::string Caller::counters(int action) { mask |= (1 << val); } det->setCounterMask(mask, std::vector{det_id}); - os << ToString(args) << '\n'; + os << ToString(counters) << '\n'; } else { throw RuntimeError("Unknown action"); } @@ -1216,6 +1218,9 @@ std::string Caller::rx_dbitlist(int action) { "receiver list, the data size will be bigger if the number of " "samples is not divisible by 8 as every signal bit is padded to " "the next byte when combining all the samples in the receiver." + "Accepts both space separated and comma separated list of indices" + "optionally enclosed in brackets. Example: 0 11 37 or 3,10,15 or " + "[62,21] or [0 1 2]" << '\n'; } else if (action == defs::GET_ACTION) { if (!args.empty()) { @@ -1236,14 +1241,10 @@ std::string Caller::rx_dbitlist(int action) { } // 'none' option already covered as t is empty by default else if (args[0] != "none") { - unsigned int ntrim = args.size(); - t.resize(ntrim); - for (unsigned int i = 0; i < ntrim; ++i) { - t[i] = StringTo(args[i]); - } + t = StringTo>(join(args, ',')); } det->setRxDbitList(t, std::vector{det_id}); - os << ToString(args) << '\n'; + os << ToString(t) << '\n'; } else { throw RuntimeError("Unknown action"); } diff --git a/slsSupportLib/include/sls/ToString.h b/slsSupportLib/include/sls/ToString.h index 7035f9c38..7c692badd 100644 --- a/slsSupportLib/include/sls/ToString.h +++ b/slsSupportLib/include/sls/ToString.h @@ -1,7 +1,6 @@ // SPDX-License-Identifier: LGPL-3.0-or-other // Copyright (C) 2021 Contributors to the SLS Detector Package #pragma once - /** * \file ToString.h * @@ -353,7 +352,9 @@ T StringTo(const std::string &f, const std::string &unit) { } } -template T StringTo(const std::string &t) { +template ::value, int> = 0> +T StringTo(const std::string &t) { std::string tmp{t}; auto unit = RemoveUnit(tmp); return StringTo(tmp, unit); @@ -398,6 +399,7 @@ ToString(const T &obj) { return obj.str(); } +/** Convert vector of strings to vector of type T */ template std::vector StringTo(const std::vector &strings) { std::vector result; @@ -407,4 +409,53 @@ std::vector StringTo(const std::vector &strings) { return result; } +/** Parse comma-separated string into vector type T (e.g., + * StringTo>()) */ +template ::value && + !std::is_same_v, + int> = 0> +T StringTo(const std::string &s) { + using ElementType = typename T::value_type; + // Enclosing brackets are optional but must match. Nested pairs are + // accepted since the caller may already have wrapped the list once. + std::string list = s; + auto strip = [](std::string &str) { + auto first = str.find_first_not_of(' '); + if (first == std::string::npos) { + str.clear(); + return; + } + str = str.substr(first, str.find_last_not_of(' ') - first + 1); + }; + strip(list); + while (list.size() >= 2 && list.front() == '[' && list.back() == ']') { + list = list.substr(1, list.size() - 2); + strip(list); + } + if (list.find_first_of("[]") != std::string::npos) { + throw RuntimeError("Mismatched brackets in list '" + s + "'"); + } + + T res; + std::istringstream ss(list); + std::string item; + while (std::getline(ss, item, ',')) { + item.erase(std::remove_if(item.begin(), item.end(), + [](char c) { return c == '"' || c == ' '; }), + item.end()); + + try { + if (item.empty()) + continue; + auto val = StringTo(item); + res.push_back(val); + } catch (const std::exception &e) { + throw RuntimeError("Could not convert '" + item + + "' to the container element type. " + + std::string(e.what())); + } + } + return res; +} + } // namespace sls diff --git a/slsSupportLib/include/sls/string_utils.h b/slsSupportLib/include/sls/string_utils.h index abeed79a7..d6afefebb 100644 --- a/slsSupportLib/include/sls/string_utils.h +++ b/slsSupportLib/include/sls/string_utils.h @@ -85,6 +85,9 @@ implementation should not be used in a performance critical place. */ std::vector split(const std::string &strToSplit, char delimeter); +/** Join strings with the specified delimiter, e.g. {"a", "b"} -> "a,b" */ +std::string join(const std::vector &strings, char delimeter); + std::string RemoveUnit(std::string &str); bool is_int(const std::string &s); diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 36d161e4e..1f4c7635d 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -24,6 +24,16 @@ std::vector split(const std::string &strToSplit, char delimeter) { return splittedStrings; } +std::string join(const std::vector &strings, char delimeter) { + std::string joined; + for (const auto &s : strings) { + if (!joined.empty()) + joined += delimeter; + joined += s; + } + return joined; +} + std::string RemoveUnit(std::string &str) { auto it = str.begin(); while (it != str.end()) { diff --git a/slsSupportLib/tests/test-ToString.cpp b/slsSupportLib/tests/test-ToString.cpp index 0c35bdfc0..194f04442 100644 --- a/slsSupportLib/tests/test-ToString.cpp +++ b/slsSupportLib/tests/test-ToString.cpp @@ -415,4 +415,24 @@ TEST_CASE("string to timingInfoDecoder") { defs::timingInfoDecoder::SHINE); } + +TEST_CASE("Convert string to vector of ints") { + REQUIRE(StringTo>("1, 2, 3") == std::vector{1, 2, 3}); + REQUIRE(StringTo>(" 4 ,5,6 ") == std::vector{4, 5, 6}); + REQUIRE(StringTo>("[8]") == std::vector{8}); + REQUIRE(StringTo>("9, ") == std::vector{9}); + REQUIRE(StringTo>("") == std::vector{}); + REQUIRE(StringTo>("[]") == std::vector{}); + REQUIRE(StringTo>(" [ [1, 2] ] ") == + std::vector{1, 2}); +} + +TEST_CASE("Mismatched brackets in string to vector throws") { + REQUIRE_THROWS(StringTo>("[1, 2")); + REQUIRE_THROWS(StringTo>("1, 2]")); + REQUIRE_THROWS(StringTo>("[[1, 2]")); + REQUIRE_THROWS(StringTo>("1]2")); + REQUIRE_THROWS(StringTo>("[1], [2]")); +} + } // namespace sls diff --git a/slsSupportLib/tests/test-string_utils.cpp b/slsSupportLib/tests/test-string_utils.cpp index 58f431f54..eb27f6de9 100644 --- a/slsSupportLib/tests/test-string_utils.cpp +++ b/slsSupportLib/tests/test-string_utils.cpp @@ -52,6 +52,13 @@ TEST_CASE("split a string without end delimiter") { REQUIRE(r[2] == "filibom"); } +TEST_CASE("join strings") { + REQUIRE(join({"abra", "kadabra", "filibom"}, '+') == + "abra+kadabra+filibom"); + REQUIRE(join({"abra"}, '+') == "abra"); + REQUIRE(join({}, '+') == ""); +} + TEST_CASE("Remove char from string") { char str[] = "sometest"; removeChar(str, 'e'); From c87cc7e515689800666fa7e5ed505e4e822fae57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Tue, 13 Oct 2026 08:23:56 +0200 Subject: [PATCH 2/2] Mac compatible version of gettid() (#1585) * added getThreadId --- .../matterhornServer/src/MatterhornApp.cpp | 22 +++--------- slsReceiverSoftware/src/Arping.cpp | 9 ++--- slsReceiverSoftware/src/ClientInterface.cpp | 11 ++---- .../src/FrameSynchronizerApp.cpp | 18 ++++------ slsReceiverSoftware/src/MultiReceiverApp.cpp | 21 +++++------ slsReceiverSoftware/src/Receiver.cpp | 6 ---- slsReceiverSoftware/src/ReceiverApp.cpp | 15 ++++---- slsReceiverSoftware/src/ThreadObject.cpp | 9 ++--- slsSupportLib/include/sls/thread_utils.h | 36 +++++++++++++++++++ 9 files changed, 70 insertions(+), 77 deletions(-) create mode 100644 slsSupportLib/include/sls/thread_utils.h diff --git a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp index 6551bf2f8..b7b6f1235 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp @@ -3,6 +3,7 @@ #include "helpers/Helpers.hpp" #include "sls/logger.h" #include "sls/sls_detector_exceptions.h" +#include "sls/thread_utils.h" #include "sls/versionAPI.h" #include @@ -12,20 +13,6 @@ #include #include -// gettid added in glibc 2.30 -#if defined(__APPLE__) -#include -#include -static inline uint64_t gettid() { - uint64_t tid = 0; - pthread_threadid_np(nullptr, &tid); - return tid; -} -#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - using namespace sls; pid_t pid = -1; @@ -90,7 +77,7 @@ int main(int argc, char *argv[]) { // also return EXIT_FAILURE } LOG(TLogLevel::logINFOBLUE) - << "Exiting Stop Server [ Tid: " << gettid() << " ]"; + << "Exiting Stop Server [ Tid: " << getThreadId() << " ]"; LOG(sls::logINFO) << "Exiting Stop Server"; } else if (pid > 0) { // parent @@ -108,7 +95,7 @@ int main(int argc, char *argv[]) { } catch (...) { LOG(logERROR) << "Some Error occured in Control Server, exiting"; LOG(sls::logINFOBLUE) - << "Exiting Control Server [ Tid: " << gettid() << " ]"; + << "Exiting Control Server [ Tid: " << getThreadId() << " ]"; LOG(sls::logINFO) << "Exiting Detector Server"; kill(pid, SIGINT); // tell child to exit waitpid(pid, nullptr, 0); // wait for child to exit @@ -116,7 +103,8 @@ int main(int argc, char *argv[]) { } waitpid(pid, nullptr, 0); // wait for child to exit LOG(sls::logINFOBLUE) - << "Exiting Detector Control Server [ Tid: " << gettid() << " ]"; + << "Exiting Detector Control Server [ Tid: " << getThreadId() + << " ]"; LOG(sls::logINFO) << "Exiting Detector Server"; } else { LOG(sls::logERROR) diff --git a/slsReceiverSoftware/src/Arping.cpp b/slsReceiverSoftware/src/Arping.cpp index 7633e041b..28eccfc60 100644 --- a/slsReceiverSoftware/src/Arping.cpp +++ b/slsReceiverSoftware/src/Arping.cpp @@ -2,6 +2,7 @@ // Copyright (C) 2021 Contributors to the SLS Detector Package #include "Arping.h" +#include "sls/thread_utils.h" #include #include @@ -11,12 +12,6 @@ namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - void func(int signum) { wait(NULL); } Arping::Arping() {} @@ -59,7 +54,7 @@ void Arping::StartProcess() { childPid = fork(); // child process if (childPid == 0) { - LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << gettid() + LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << getThreadId() << " ]"; ProcessExecution(); } diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index 9cc94ef3b..a88688b77 100644 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -7,6 +7,7 @@ #include "sls/sls_detector_exceptions.h" #include "sls/string_utils.h" +#include "sls/thread_utils.h" #include "sls/versionAPI.h" #include @@ -27,12 +28,6 @@ namespace sls { using ns = std::chrono::nanoseconds; using Interface = ServerInterface; -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - std::mutex ClientInterface::callbackMutex; ClientInterface::~ClientInterface() { @@ -47,7 +42,7 @@ ClientInterface::ClientInterface(uint16_t portNumber) : detType(GENERIC), portNumber(portNumber), server(portNumber) { validatePortNumber(portNumber); functionTable(); - parentThreadId = gettid(); + parentThreadId = getThreadId(); tcpThread = make_unique(&ClientInterface::startTCPServer, this); } @@ -79,7 +74,7 @@ void ClientInterface::registerCallBackRawDataReady( } void ClientInterface::startTCPServer() { - tcpThreadId = gettid(); + tcpThreadId = getThreadId(); LOG(logINFOBLUE) << "Created [ TCP server Tid: " << tcpThreadId << "]"; LOG(logINFO) << "SLS Receiver starting TCP Server on port " << portNumber << '\n'; diff --git a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp index fc48814a9..d513c522a 100644 --- a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp +++ b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp @@ -12,6 +12,7 @@ #include "sls/logger.h" #include "sls/network_utils.h" #include "sls/sls_detector_defs.h" +#include "sls/thread_utils.h" #include //SIGINT #include @@ -31,12 +32,6 @@ std::vector threads; sls::TLogLevel printHeadersLevel = sls::logDEBUG; -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - /** Define Colors to print data call back in different colors for different * recievers */ #define PRINT_IN_COLOR(c, f, ...) \ @@ -522,7 +517,8 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']'; + LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId() + << ']'; // close files on ctrl+c sls::setupSignalHandler(SIGINT, sigInterruptHandler); @@ -548,8 +544,8 @@ int main(int argc, char *argv[]) { sem_t *semaphore = &semaphores[i]; threads.emplace_back( [i, semaphore, port, user_data, &threadException]() { - LOG(sls::logINFOBLUE) - << "Thread " << i << " [ Tid: " << gettid() << ']'; + LOG(sls::logINFOBLUE) << "Thread " << i << " [ Tid: " + << sls::getThreadId() << ']'; try { sls::Receiver receiver(port); receiver.registerCallBackStartAcquisition( @@ -567,8 +563,8 @@ int main(int argc, char *argv[]) { threadException = std::current_exception(); raise(SIGINT); } - LOG(sls::logINFOBLUE) - << "Exiting Thread " << i << " [ Tid: " << gettid() << " ]"; + LOG(sls::logINFOBLUE) << "Exiting Thread " << i << " [ Tid: " + << sls::getThreadId() << " ]"; }); } diff --git a/slsReceiverSoftware/src/MultiReceiverApp.cpp b/slsReceiverSoftware/src/MultiReceiverApp.cpp index a1bc4cb83..003b66c4f 100644 --- a/slsReceiverSoftware/src/MultiReceiverApp.cpp +++ b/slsReceiverSoftware/src/MultiReceiverApp.cpp @@ -9,6 +9,7 @@ #include "sls/logger.h" #include "sls/network_utils.h" #include "sls/sls_detector_defs.h" +#include "sls/thread_utils.h" #include //SIGINT #include @@ -16,12 +17,6 @@ #include //wait #include -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - /** Define Colors to print data call back in different colors for different * recievers */ #define PRINT_IN_COLOR(c, f, ...) \ @@ -156,7 +151,8 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']'; + LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId() + << ']'; // close files on ctrl+c sls::setupSignalHandler(SIGINT, sigInterruptHandler); @@ -182,7 +178,8 @@ int main(int argc, char *argv[]) { /** - if child process */ else if (pid == 0) { LOG(sls::logINFOBLUE) - << "Child process " << i << " [ Tid: " << gettid() << ']'; + << "Child process " << i << " [ Tid: " << sls::getThreadId() + << ']'; try { uint16_t port = m.port + i; @@ -213,13 +210,13 @@ int main(int argc, char *argv[]) { // each child process gets a copy of the semaphore sem_wait(&semaphore); sem_destroy(&semaphore); - LOG(sls::logINFOBLUE) - << "Exiting Child Process [ Tid: " << gettid() << ']'; + LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: " + << sls::getThreadId() << ']'; exit(EXIT_SUCCESS); } catch (...) { sem_destroy(&semaphore); - LOG(sls::logINFOBLUE) - << "Exiting Child Process [ Tid: " << gettid() << " ]"; + LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: " + << sls::getThreadId() << " ]"; exit(EXIT_FAILURE); } } diff --git a/slsReceiverSoftware/src/Receiver.cpp b/slsReceiverSoftware/src/Receiver.cpp index c9a299b19..f7372ba0e 100644 --- a/slsReceiverSoftware/src/Receiver.cpp +++ b/slsReceiverSoftware/src/Receiver.cpp @@ -19,12 +19,6 @@ namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - Receiver::~Receiver() = default; Receiver::Receiver(uint16_t port) { diff --git a/slsReceiverSoftware/src/ReceiverApp.cpp b/slsReceiverSoftware/src/ReceiverApp.cpp index 92ff84b50..9a382b577 100644 --- a/slsReceiverSoftware/src/ReceiverApp.cpp +++ b/slsReceiverSoftware/src/ReceiverApp.cpp @@ -8,17 +8,12 @@ #include "sls/logger.h" #include "sls/network_utils.h" #include "sls/sls_detector_defs.h" +#include "sls/thread_utils.h" #include //SIGINT #include #include -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - sem_t semaphore; /** @@ -44,7 +39,8 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]"; + LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId() + << " ]"; // close files on ctrl+c sls::setupSignalHandler(SIGINT, sigInterruptHandler); @@ -60,10 +56,11 @@ int main(int argc, char *argv[]) { sem_destroy(&semaphore); } catch (...) { sem_destroy(&semaphore); - LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]"; + LOG(sls::logINFOBLUE) + << "Exiting [ Tid: " << sls::getThreadId() << " ]"; throw; } - LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]"; + LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << sls::getThreadId() << " ]"; LOG(sls::logINFO) << "Exiting Receiver"; return EXIT_SUCCESS; } diff --git a/slsReceiverSoftware/src/ThreadObject.cpp b/slsReceiverSoftware/src/ThreadObject.cpp index 183b31ece..746a03c28 100644 --- a/slsReceiverSoftware/src/ThreadObject.cpp +++ b/slsReceiverSoftware/src/ThreadObject.cpp @@ -7,17 +7,12 @@ #include "ThreadObject.h" #include "sls/container_utils.h" +#include "sls/thread_utils.h" #include #include namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - ThreadObject::ThreadObject(int index, std::string type) : index(index), type(type) { LOG(logDEBUG) << type << " thread created: " << index; @@ -46,7 +41,7 @@ void ThreadObject::StartRunning() { runningFlag = true; } void ThreadObject::StopRunning() { runningFlag = false; } void ThreadObject::RunningThread() { - threadId = gettid(); + threadId = getThreadId(); LOG(logINFOBLUE) << "Created [ " << type << "Thread " << index << ", Tid: " << threadId << "]"; while (!killThread) { diff --git a/slsSupportLib/include/sls/thread_utils.h b/slsSupportLib/include/sls/thread_utils.h new file mode 100644 index 000000000..1fc29fcef --- /dev/null +++ b/slsSupportLib/include/sls/thread_utils.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: LGPL-3.0-or-other +// Copyright (C) 2021 Contributors to the SLS Detector Package +#pragma once + +/** + * @file thread_utils.h + * @short id of the calling thread + */ + +#include // pid_t + +#if defined(__APPLE__) +#include +#include +#elif defined(__linux__) +#include +#include +#else +#error "getThreadId is not implemented for this platform" +#endif + +namespace sls { + +/** Kernel thread id of the calling thread. On macOS the 64-bit id is + * truncated to pid_t. */ +inline pid_t getThreadId() noexcept { +#if defined(__APPLE__) + std::uint64_t tid = 0; + pthread_threadid_np(nullptr, &tid); + return static_cast(tid); +#else + return static_cast(::syscall(SYS_gettid)); +#endif +} + +} // namespace sls