From 840970e8f72b8352f09901fe29e7eaf5204aedb9 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 30 Mar 2024 11:54:29 -0400 Subject: [PATCH] Internals: Add VL_MT_SAFE flags --- include/verilatedos.h | 14 +++++++------- include/verilatedos_c.h | 12 +++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/verilatedos.h b/include/verilatedos.h index 2bbfbb69a..e4e2baab1 100644 --- a/include/verilatedos.h +++ b/include/verilatedos.h @@ -616,35 +616,35 @@ static inline double VL_ROUND(double n) { namespace VlOs { -extern uint64_t memUsageBytes(); ///< Return memory usage in bytes, or 0 if not implemented +extern uint64_t memUsageBytes() VL_MT_SAFE; ///< Return memory usage in bytes, or 0 if unknown // Internal: Record CPU time, starting point on construction, and current delta from that class DeltaCpuTime final { double m_start{}; // Time constructed at - static double gettime(); + static double gettime() VL_MT_SAFE; public: // Construct, and if startit is true, start() timer explicit DeltaCpuTime(bool startit) { if (startit) start(); } - void start() { m_start = gettime(); } // Start timer; record current time - double deltaTime() const { // Return time between now and start() + void start() VL_MT_SAFE { m_start = gettime(); } // Start timer; record current time + double deltaTime() const VL_MT_SAFE { // Return time between now and start() return (m_start == 0.0) ? 0.0 : gettime() - m_start; } }; // Internal: Record wall time, starting point on construction, and current delta from that class DeltaWallTime final { double m_start{}; // Time constructed at - static double gettime(); + static double gettime() VL_MT_SAFE; public: // Construct, and if startit is true, start() timer explicit DeltaWallTime(bool startit) { if (startit) start(); } - void start() { m_start = gettime(); } // Start timer; record current time - double deltaTime() const { // Return time between now and start() + void start() VL_MT_SAFE { m_start = gettime(); } // Start timer; record current time + double deltaTime() const VL_MT_SAFE { // Return time between now and start() return (m_start == 0.0) ? 0.0 : gettime() - m_start; } }; diff --git a/include/verilatedos_c.h b/include/verilatedos_c.h index 2178984f4..dc7778e34 100644 --- a/include/verilatedos_c.h +++ b/include/verilatedos_c.h @@ -38,7 +38,7 @@ namespace VlOs { //========================================================================= // VlOs::VlGetCpuTime/VlGetWallTime implementation -double DeltaCpuTime::gettime() { +double DeltaCpuTime::gettime() VL_MT_SAFE { #if defined(_WIN32) || defined(__MINGW32__) FILETIME lpCreationTime, lpExitTime, lpKernelTime, lpUserTime; if (0 @@ -51,11 +51,12 @@ double DeltaCpuTime::gettime() { #else // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) timespec ts; - if (0 != clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) return ts.tv_sec + ts.tv_nsec * 1e-9; + if (0 != clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) // MT-Safe + return ts.tv_sec + ts.tv_nsec * 1e-9; #endif return 0.0; } -double DeltaWallTime::gettime() { +double DeltaWallTime::gettime() VL_MT_SAFE { #if defined(_WIN32) || defined(__MINGW32__) FILETIME ft; // contains number of 0.1us intervals since the beginning of 1601 UTC. GetSystemTimeAsFileTime(&ft); @@ -65,7 +66,8 @@ double DeltaWallTime::gettime() { #else // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) timespec ts; - if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) return ts.tv_sec + ts.tv_nsec * 1e-9; + if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) // MT-Safe + return ts.tv_sec + ts.tv_nsec * 1e-9; return 0.0; #endif } @@ -73,7 +75,7 @@ double DeltaWallTime::gettime() { //========================================================================= // VlOs::memUsageBytes implementation -uint64_t memUsageBytes() { +uint64_t memUsageBytes() VL_MT_SAFE { #if defined(_WIN32) || defined(__MINGW32__) const HANDLE process = GetCurrentProcess(); PROCESS_MEMORY_COUNTERS pmc;