Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate System::Timer to BitMapObjectPool #11487

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/system/SystemTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,45 @@ namespace System {
*******************************************************************************
*/

ObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> Timer::sPool;
BitMapObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> Timer::sPool;
Stats::count_t Timer::mNumInUse = 0;
Stats::count_t Timer::mHighWatermark = 0;

Timer * Timer::New(System::Layer & systemLayer, System::Clock::Timeout delay, TimerCompleteCallback onComplete, void * appState)
{
Timer * timer = Timer::sPool.TryCreate();
Timer * timer = Timer::sPool.CreateObject();
if (timer == nullptr)
{
ChipLogError(chipSystemLayer, "Timer pool EMPTY");
}
else
{
timer->AppState = appState;
timer->mAppState = appState;
timer->mSystemLayer = &systemLayer;
timer->mAwakenTime = SystemClock().GetMonotonicTimestamp() + delay;
if (!__sync_bool_compare_and_swap(&timer->mOnComplete, nullptr, onComplete))
{
chipDie();
}
#if CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
static_assert(CHIP_SYSTEM_CONFIG_NUM_TIMERS < CHIP_SYS_STATS_COUNT_MAX, "Stats count is too small");
if (++mNumInUse > mHighWatermark)
{
mHighWatermark = mNumInUse;
}
#endif // CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
}
return timer;
}

void Timer::Release()
{
Timer::sPool.ReleaseObject(this);
#if CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
--mNumInUse;
#endif // CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
}

void Timer::Clear()
{
TimerCompleteCallback lOnComplete = this->mOnComplete;
Expand All @@ -107,7 +124,7 @@ void Timer::Clear()
VerifyOrReturn(__sync_bool_compare_and_swap(&mOnComplete, lOnComplete, nullptr));

// Since this thread changed the state of mOnComplete, release the timer.
AppState = nullptr;
mAppState = nullptr;
mSystemLayer = nullptr;
}

Expand All @@ -116,15 +133,15 @@ void Timer::HandleComplete()
// Save information needed to perform the callback.
Layer * lLayer = this->mSystemLayer;
const TimerCompleteCallback lOnComplete = this->mOnComplete;
void * lAppState = this->AppState;
void * lAppState = this->mAppState;

// Check if timer is armed
VerifyOrReturn(lOnComplete != nullptr, );
// Atomically disarm if the value has not changed.
VerifyOrReturn(__sync_bool_compare_and_swap(&this->mOnComplete, lOnComplete, nullptr), );

// Since this thread changed the state of mOnComplete, release the timer.
AppState = nullptr;
mAppState = nullptr;
mSystemLayer = nullptr;
this->Release();

Expand Down Expand Up @@ -193,7 +210,7 @@ Timer * Timer::List::Remove(TimerCompleteCallback aOnComplete, void * aAppState)
Timer * previous = nullptr;
for (Timer * timer = mHead; timer != nullptr; timer = timer->mNextTimer)
{
if (timer->mOnComplete == aOnComplete && timer->AppState == aAppState)
if (timer->mOnComplete == aOnComplete && timer->mAppState == aAppState)
{
if (previous == nullptr)
{
Expand Down
22 changes: 16 additions & 6 deletions src/system/SystemTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@

// Include dependent headers
#include <lib/support/DLLUtil.h>
#include <lib/support/Pool.h>

#include <system/SystemClock.h>
#include <system/SystemError.h>
#include <system/SystemLayer.h>
#include <system/SystemMutex.h>
#include <system/SystemObject.h>
#include <system/SystemStats.h>

#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
Expand All @@ -57,7 +58,7 @@ using TimerCompleteCallback = void (*)(Layer * aLayer, void * appState);
/**
* This is an Object-pool based class that System::Layer implementations can use to assist in providing timer functions.
*/
class DLL_EXPORT Timer : public Object
class DLL_EXPORT Timer
{
public:
/**
Expand Down Expand Up @@ -185,11 +186,16 @@ class DLL_EXPORT Timer : public Object
Timer() = default;

/**
* Obtain a new timer from the system object pool.
* Obtain a new timer from the object pool.
*/
static Timer * New(System::Layer & systemLayer, System::Clock::Timeout delay, TimerCompleteCallback onComplete,
void * appState);

/**
* Return a timer to the object pool.
*/
void Release();

/**
* Return the expiration time.
*/
Expand All @@ -215,20 +221,24 @@ class DLL_EXPORT Timer : public Object
/**
* Read timer pool statistics.
*/
static void GetStatistics(chip::System::Stats::count_t & aNumInUse, chip::System::Stats::count_t & aHighWatermark)
static void GetStatistics(Stats::count_t & aNumInUse, Stats::count_t & aHighWatermark)
{
sPool.GetStatistics(aNumInUse, aHighWatermark);
aNumInUse = mNumInUse;
aHighWatermark = mHighWatermark;
}

private:
friend class LayerImplLwIP;
static ObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> sPool;
static BitMapObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> sPool;
static Stats::count_t mNumInUse;
static Stats::count_t mHighWatermark;

TimerCompleteCallback mOnComplete;
Clock::Timestamp mAwakenTime;
Timer * mNextTimer;

Layer * mSystemLayer;
void * mAppState;

#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
friend class LayerImplSelect;
Expand Down