From b721d11b1ad73d098e478372917e5b16925e697b Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 14 Feb 2024 11:30:02 -0500 Subject: [PATCH] cwThread.h/cpp : Added stateTimeOutMicros() and pauseMicros(). --- cwThread.cpp | 23 +++++++++++++++++++---- cwThread.h | 3 +++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cwThread.cpp b/cwThread.cpp index 593563d..1154ef2 100644 --- a/cwThread.cpp +++ b/cwThread.cpp @@ -37,7 +37,7 @@ namespace cw inline thread_t* _handleToPtr(handle_t h) { return handleToPtr(h); } // Called from client thread to wait for the internal thread to transition to a specified state. - rc_t _waitForState( thread_t* p, unsigned stateId ) + rc_t _waitForState( thread_t* p, stateId_t stateId ) { unsigned waitTimeMicroSecs = 0; stateId_t curStateId; @@ -54,7 +54,7 @@ namespace cw waitTimeMicroSecs += p->sleepMicros; }while( waitTimeMicroSecs < p->stateMicros ); - + return curStateId==stateId ? kOkRC : kTimeOutRC; } @@ -143,6 +143,7 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s rc = cwLogSysError(kOpFailRC,sysRC,"Thread attribute init failed."); } else + { /* // Creating the thread in a detached state should prevent it from leaking memory when @@ -160,6 +161,7 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s p->stateId = kNotInitThId; rc = cwLogSysError(kOpFailRC,sysRC,"Thread create failed."); } + } hRef.set(p); return rc; @@ -205,7 +207,7 @@ cw::rc_t cw::thread::pause( handle_t h, unsigned cmdFlags ) thread_t* p = _handleToPtr(h); stateId_t curStateId = p->stateId.load(std::memory_order_acquire); bool isPausedFl = curStateId == kPausedThId; - unsigned waitId; + stateId_t waitId; if( isPausedFl == pauseFl ) return kOkRC; @@ -225,7 +227,7 @@ cw::rc_t cw::thread::pause( handle_t h, unsigned cmdFlags ) rc = _waitForState(p,waitId); if( rc != kOkRC ) - cwLogError(rc,"Thread timed out waiting for '%s'.", pauseFl ? "pause" : "un-pause"); + cwLogError(rc,"Thread timed out waiting for '%s'. pauseMicros:%i stateMicros:%i sleepMicros:%i", pauseFl ? "pause" : "un-pause",p->pauseMicros,p->stateMicros,p->sleepMicros); return rc; @@ -256,6 +258,19 @@ cw::thread::thread_id_t cw::thread::id() return id.u.id; } +unsigned cw::thread::stateTimeOutMicros( handle_t h) +{ + thread_t* p = _handleToPtr(h); + return p->stateMicros; +} + +unsigned cw::thread::pauseMicros( handle_t h ) +{ + thread_t* p = _handleToPtr(h); + return p->pauseMicros; +} + + namespace cw { bool _threadTestCb( void* p ) diff --git a/cwThread.h b/cwThread.h index 0346cb7..7a12181 100644 --- a/cwThread.h +++ b/cwThread.h @@ -35,6 +35,9 @@ namespace cw // Return the thread id of the calling context. thread_id_t id(); + + unsigned stateTimeOutMicros( handle_t h); + unsigned pauseMicros( handle_t h ); } rc_t threadTest(); }