diff --git a/cpp-client/deephaven/dhcore/CMakeLists.txt b/cpp-client/deephaven/dhcore/CMakeLists.txt index 4209f7cc974..6186051d5f0 100644 --- a/cpp-client/deephaven/dhcore/CMakeLists.txt +++ b/cpp-client/deephaven/dhcore/CMakeLists.txt @@ -31,6 +31,7 @@ set(ALL_FILES src/ticking/ticking.cc src/utility/cython_support.cc src/utility/utility.cc + src/utility/utility_platform_specific.cc include/private/deephaven/dhcore/ticking/immer_table_state.h include/private/deephaven/dhcore/ticking/index_decoder.h diff --git a/cpp-client/deephaven/dhcore/include/public/deephaven/dhcore/utility/utility.h b/cpp-client/deephaven/dhcore/include/public/deephaven/dhcore/utility/utility.h index 4d177b0146b..354c3d18fbe 100644 --- a/cpp-client/deephaven/dhcore/include/public/deephaven/dhcore/utility/utility.h +++ b/cpp-client/deephaven/dhcore/include/public/deephaven/dhcore/utility/utility.h @@ -197,6 +197,12 @@ TimePointToStr( */ std::string Basename(std::string_view path); +/** + * Returns the current thread ID as a string. + * @return The current thread ID as a string. + */ +[[nodiscard]] std::string GetTidAsString(); + template [[nodiscard]] std::string TypeName(const T& t) { return demangle(typeid(t).name()); diff --git a/cpp-client/deephaven/dhcore/src/utility/utility_platform_specific.cc b/cpp-client/deephaven/dhcore/src/utility/utility_platform_specific.cc new file mode 100644 index 00000000000..35e75d6c9d0 --- /dev/null +++ b/cpp-client/deephaven/dhcore/src/utility/utility_platform_specific.cc @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending + */ +#include +#include "deephaven/dhcore/utility/utility.h" + +// for GetTidAsString +#if defined(__linux__) +#include +#include +#elif defined(_WIN32) +#include +#endif + +namespace deephaven::dhcore::utility { +[[nodiscard]] std::string GetTidAsString() { +#if defined(__linux__) + const pid_t tid = syscall(__NR_gettid); // this is more portable than gettid(). + return std::to_string(tid); +#elif defined(_WIN32) + auto tid = GetCurrentThreadId(); +return std::to_string(tid); +#else +#error "Don't have a way to getting thread id on your platform" +#endif +} +} // namespace deephaven::dhcore::utility diff --git a/cpp-client/deephaven/tests/src/utility_test.cc b/cpp-client/deephaven/tests/src/utility_test.cc index 371494fd059..dd600761796 100644 --- a/cpp-client/deephaven/tests/src/utility_test.cc +++ b/cpp-client/deephaven/tests/src/utility_test.cc @@ -7,6 +7,7 @@ using deephaven::dhcore::utility::Base64Encode; using deephaven::dhcore::utility::Basename; using deephaven::dhcore::utility::EpochMillisToStr; +using deephaven::dhcore::utility::GetTidAsString; using deephaven::dhcore::utility::ObjectId; namespace deephaven::client::tests { @@ -41,4 +42,13 @@ TEST_CASE("Basename", "[utility]") { CHECK(Basename(R"(C:\Users\kosak\)").empty()); #endif } + +// This isn't much of a test, but if it can compile on all supported +// platforms (Linux and Windows) then that is at least a sanity check +// (that the entry point exists). For now we just visuallyi spot-check +// that ireturns the right value. +TEST_CASE("ThreadId", "[utility]") { + auto tid = GetTidAsString(); + fmt::println("This should be my thread id: {}", tid); +} } // namespace deephaven::client::tests