From 82cbc848fbd9b177a3e08b23cba04d14dc5fe199 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 14 Jul 2021 09:57:12 -0700 Subject: [PATCH] [tasklet] declare `Scheduler` as nested type of `Tasklet` (#6815) This commit also inlines simple constructors. --- src/core/api/tasklet_api.cpp | 4 +- src/core/common/instance.hpp | 4 +- src/core/common/tasklet.cpp | 21 ++------- src/core/common/tasklet.hpp | 84 +++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 61 deletions(-) diff --git a/src/core/api/tasklet_api.cpp b/src/core/api/tasklet_api.cpp index 5f80c6422..d39099e40 100644 --- a/src/core/api/tasklet_api.cpp +++ b/src/core/api/tasklet_api.cpp @@ -47,7 +47,7 @@ void otTaskletsProcess(otInstance *aInstance) Instance &instance = *static_cast(aInstance); VerifyOrExit(otInstanceIsInitialized(aInstance)); - instance.Get().ProcessQueuedTasklets(); + instance.Get().ProcessQueuedTasklets(); exit: return; @@ -59,7 +59,7 @@ bool otTaskletsArePending(otInstance *aInstance) Instance &instance = *static_cast(aInstance); VerifyOrExit(otInstanceIsInitialized(aInstance)); - retval = instance.Get().AreTaskletsPending(); + retval = instance.Get().AreTaskletsPending(); exit: return retval; diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 0fd222d71..8dd1966ea 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -333,7 +333,7 @@ private: // Tasklet and Timer Schedulers are first to ensure other // objects/classes can use them from their constructors. - TaskletScheduler mTaskletScheduler; + Tasklet::Scheduler mTaskletScheduler; TimerMilliScheduler mTimerMilliScheduler; #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE TimerMicroScheduler mTimerMicroScheduler; @@ -945,7 +945,7 @@ template <> inline Mac::SubMac &Instance::Get(void) #endif // OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE -template <> inline TaskletScheduler &Instance::Get(void) +template <> inline Tasklet::Scheduler &Instance::Get(void) { return mTaskletScheduler; } diff --git a/src/core/common/tasklet.cpp b/src/core/common/tasklet.cpp index 81dd84ff2..a7dc136cd 100644 --- a/src/core/common/tasklet.cpp +++ b/src/core/common/tasklet.cpp @@ -34,34 +34,19 @@ #include "tasklet.hpp" #include "common/code_utils.hpp" -#include "common/debug.hpp" -#include "common/instance.hpp" #include "common/locator_getters.hpp" -#include "net/ip6.hpp" namespace ot { -Tasklet::Tasklet(Instance &aInstance, Handler aHandler) - : InstanceLocator(aInstance) - , mHandler(aHandler) - , mNext(nullptr) -{ -} - void Tasklet::Post(void) { if (!IsPosted()) { - Get().PostTasklet(*this); + Get().PostTasklet(*this); } } -TaskletScheduler::TaskletScheduler(void) - : mTail(nullptr) -{ -} - -void TaskletScheduler::PostTasklet(Tasklet &aTasklet) +void Tasklet::Scheduler::PostTasklet(Tasklet &aTasklet) { // Tasklets are saved in a circular singly linked list. @@ -79,7 +64,7 @@ void TaskletScheduler::PostTasklet(Tasklet &aTasklet) } } -void TaskletScheduler::ProcessQueuedTasklets(void) +void Tasklet::Scheduler::ProcessQueuedTasklets(void) { Tasklet *tail = mTail; diff --git a/src/core/common/tasklet.hpp b/src/core/common/tasklet.hpp index 9d9b9e5be..6899e57d7 100644 --- a/src/core/common/tasklet.hpp +++ b/src/core/common/tasklet.hpp @@ -63,9 +63,46 @@ class TaskletScheduler; */ class Tasklet : public InstanceLocator { - friend class TaskletScheduler; - public: + /** + * This class implements the tasklet scheduler. + * + */ + class Scheduler : private NonCopyable + { + friend class Tasklet; + + public: + /** + * This constructor initializes the object. + * + */ + Scheduler(void) + : mTail(nullptr) + { + } + + /** + * This method indicates whether or not there are tasklets pending. + * + * @retval TRUE If there are tasklets pending. + * @retval FALSE If there are no tasklets pending. + * + */ + bool AreTaskletsPending(void) const { return mTail != nullptr; } + + /** + * This method processes all tasklets queued when this is called. + * + */ + void ProcessQueuedTasklets(void); + + private: + void PostTasklet(Tasklet &aTasklet); + + Tasklet *mTail; // A circular singly linked-list + }; + /** * This function reference is called when the tasklet is run. * @@ -81,7 +118,12 @@ public: * @param[in] aHandler A pointer to a function that is called when the tasklet is run. * */ - Tasklet(Instance &aInstance, Handler aHandler); + Tasklet(Instance &aInstance, Handler aHandler) + : InstanceLocator(aInstance) + , mHandler(aHandler) + , mNext(nullptr) + { + } /** * This method puts the tasklet on the tasklet scheduler run queue. @@ -145,42 +187,6 @@ private: void *mContext; }; -/** - * This class implements the tasklet scheduler. - * - */ -class TaskletScheduler : private NonCopyable -{ - friend class Tasklet; - -public: - /** - * This constructor initializes the object. - * - */ - TaskletScheduler(void); - - /** - * This method indicates whether or not there are tasklets pending. - * - * @retval TRUE If there are tasklets pending. - * @retval FALSE If there are no tasklets pending. - * - */ - bool AreTaskletsPending(void) const { return mTail != nullptr; } - - /** - * This method processes all tasklets queued when this is called. - * - */ - void ProcessQueuedTasklets(void); - -private: - void PostTasklet(Tasklet &aTasklet); - - Tasklet *mTail; // A circular singly linked-list -}; - /** * @} *