From 950908d553a0c7a662233ede82bb58ad7525d650 Mon Sep 17 00:00:00 2001 From: GUSTAVO CAMPOS Date: Sun, 30 Jan 2022 10:02:42 +0000 Subject: [PATCH] Addin SetDynamicNice for highest performance on threads --- README.md | 8 ++++-- atomicx/atomicx.cpp | 22 ++++++++++++++++ atomicx/atomicx.hpp | 25 +++++++++++++++++-- .../ThermalCameraDemo/ThermalCameraDemo.ino | 6 ++++- examples/Arduino/simple/simple.ino | 1 + 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dc7c64f..721d817 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AtomicX -Version 1.2.0 release +Version 1.2.1 release ![image](https://user-images.githubusercontent.com/1805792/125191254-6591cf80-e239-11eb-9e89-d7500e793cd4.png) @@ -10,6 +10,10 @@ What is AtomicX? AtomicX is a general purpose **cooperative** thread lib for emb ## Implementations from Work on progress +## Version 1.2.1 + +* Adding Dynamic Nice, now it is possible to let the kernel set the best performance for your thread, for this `SetNice(*initial nice*)` and than `SetDynamicNice(true)` in the constructor of your thread. The kernel will be able to always adjust your thread for Best performance, but, it will leave no room for sleeps between threads, increasing power consumption, it is powerful but use it carefully. + * Added `YieldNow()` the higher priority context change, it will allow other threads to work, but will, also return faster than others * **`smartSemaphore`**, Used to compliance with RII, once used in the thread context, it takes a semaphore to be initialized and expose the same methods, although it manages the local context, and ones it it gets out of context, due to leaving {} or a functions, for example the semaphore shared context is released if ever taken during the smartSemaphore instantiated object life cycle. The same is available for `mutex`, called `smartMutex`, follows the same principle. @@ -18,7 +22,7 @@ What is AtomicX? AtomicX is a general purpose **cooperative** thread lib for emb * Introducing `atomicx::Timeout`, this will help tracking a timeout over time, using methods `IsTimedout` and `GetRemaining` and `GetDurationSince`. Special use case, if the timeout value is zero, IsTimedout will always return false. -* **IMPORTAT NOTIFICATION** `atomicx::lock` has been renamed to `atomicx::mutex` for consistency, all methods are the same. +* **IMPORTANT NOTIFICATION** `atomicx::lock` has been renamed to `atomicx::mutex` for consistency, all methods are the same. * **Improvement** Added a contructor for self-manager start to define a start size and increase pace. For example: a thread starts with 150 bytes and increase pace of 10, but used stack was 200, the kernel will do 200 + 10 (increase pace) to give it room to work. The default value is (1) ```cpp diff --git a/atomicx/atomicx.cpp b/atomicx/atomicx.cpp index d33832a..80c288b 100644 --- a/atomicx/atomicx.cpp +++ b/atomicx/atomicx.cpp @@ -389,6 +389,11 @@ namespace thread ms_pCurrent->m_aStatus = aTypes::running; + if (ms_pCurrent->m_flags.dynamicNice == true) + { + ms_pCurrent->m_nice = ((ms_pCurrent->m_LastUserExecTime) + ms_pCurrent->m_nice) / 2; + } + return true; } @@ -442,8 +447,16 @@ namespace thread } } + void atomicx::SetDefaultParameters () + { + m_flags.autoStack = false; + m_flags.dynamicNice = false; + } + atomicx::atomicx(size_t nStackSize, int nStackIncreasePace) : m_context{}, m_stackSize(nStackSize), m_stackIncreasePace(nStackIncreasePace), m_stack(nullptr) { + SetDefaultParameters (); + m_flags.autoStack = true; AddThisThread(); @@ -840,4 +853,13 @@ namespace thread return m_stackIncreasePace; } + void atomicx::SetDynamicNice(bool status) + { + m_flags.dynamicNice = status; + } + + bool atomicx::IsDynamicNiceOn() + { + return m_flags.dynamicNice; + } } diff --git a/atomicx/atomicx.hpp b/atomicx/atomicx.hpp index 4cf6b0a..c063213 100644 --- a/atomicx/atomicx.hpp +++ b/atomicx/atomicx.hpp @@ -13,7 +13,7 @@ #include /* Official version */ -#define ATOMICX_VERSION "1.2.0" +#define ATOMICX_VERSION "1.2.1" #define ATOMIC_VERSION_LABEL "AtomicX v" ATOMICX_VERSION " built at " __TIMESTAMP__ using atomicx_time = uint32_t; @@ -917,7 +917,7 @@ namespace thread */ template atomicx(T (&stack)[N]) : m_context{}, m_stackSize{N}, m_stack((volatile uint8_t*) stack) { - m_flags.autoStack = false; + SetDefaultParameters(); AddThisThread(); } @@ -985,11 +985,31 @@ namespace thread */ void YieldNow (void); + /** + * @brief Set the Dynamic Nice on and off + * + * @param status True for on otherwsize off + */ + void SetDynamicNice(bool status); + + /** + * @brief Get Dynamic Nice status + * + * @return true if dynamic nice is on otherwise off + */ + bool IsDynamicNiceOn(); + /** * SPECIAL PRIVATE SECTION FOR HELPER METHODS USED BY PROCTED METHODS */ private: + /** + * @brief Set the Default Parameters for constructors + * + */ + void SetDefaultParameters (); + template void SetWaitParammeters (T& refVar, size_t nTag=0, aSubTypes asubType = aSubTypes::wait) { m_TopicId = 0; @@ -1642,6 +1662,7 @@ namespace thread struct { bool autoStack : 1; + bool dynamicNice : 1; } m_flags = {}; }; } diff --git a/examples/Arduino/ThermalCameraDemo/ThermalCameraDemo.ino b/examples/Arduino/ThermalCameraDemo/ThermalCameraDemo.ino index ae74fbd..b34dcd0 100644 --- a/examples/Arduino/ThermalCameraDemo/ThermalCameraDemo.ino +++ b/examples/Arduino/ThermalCameraDemo/ThermalCameraDemo.ino @@ -250,8 +250,11 @@ public: TextScroller (atomicx_time nNice) : atomicx(m_stack), nNumberDigits (MAX_LED_MATRIX), nOffset (0), nSpeed (2) { nIndex = (nSpeed == 0) ? nNumberDigits * (-1) : 0; + SetNice (nNice); + SetDynamicNice(true); + strMatrixText.concat (F("Welcome to AtomicX Thermal Camera demo.")); } @@ -438,7 +441,7 @@ protected: { vt100::ResetColor (); Serial.flush (); - Yield (1); + Yield (); vt100::SetLocation (12-((i) / 8), 1); Serial.print (F("\e[K")); Serial.flush (); @@ -510,6 +513,7 @@ public: Serial.flush (); SetNice (nNice); + SetDynamicNice (true); } char* GetName() diff --git a/examples/Arduino/simple/simple.ino b/examples/Arduino/simple/simple.ino index 1c26c0a..f6492c6 100644 --- a/examples/Arduino/simple/simple.ino +++ b/examples/Arduino/simple/simple.ino @@ -39,6 +39,7 @@ public: Consumer(uint32_t nNice) : atomicx (::GetStackSize(10), 10) { SetNice(nNice); + SetDynamicNice(true); } const char* GetName () override