LCOV - code coverage report
Current view: top level - asyncFreeRtos/include/async - ModifiableLock.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 15 100.0 %
Date: 2025-01-20 13:53:09 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : /**
       4             :  * \ingroup async
       5             :  */
       6             : #ifndef GUARD_5D6895FC_B608_4C8E_87FB_B600671CA5B0
       7             : #define GUARD_5D6895FC_B608_4C8E_87FB_B600671CA5B0
       8             : 
       9             : #include "interrupts/suspendResumeAllInterrupts.h"
      10             : 
      11             : namespace async
      12             : {
      13             : /**
      14             :  * A synchronization mechanism that blocks threads from accessing a resource.
      15             :  *
      16             :  * The `ModifiableLock` class ensures mutual exclusion,
      17             :  * allowing only one thread to access a protected
      18             :  * resource or function at a time. When a thread acquires the lock,
      19             :  * any other thread attempting to acquire it is blocked until the lock is released.
      20             :  * The lock can be acquired either in the constructor or on demand using the `lock` function.
      21             :  * Similarly, the lock can be released either in the destructor or on demand using the `unlock`
      22             :  * function.
      23             :  */
      24             : class ModifiableLock final
      25             : {
      26             : public:
      27             :     ModifiableLock();
      28             :     ~ModifiableLock();
      29             : 
      30             :     void unlock();
      31             :     void lock();
      32             : 
      33             : private:
      34             :     ::OldIntEnabledStatusValueType _oldIntEnabledStatusValue;
      35             :     bool _isLocked;
      36             : };
      37             : 
      38             : /**
      39             :  * Inline implementations.
      40             :  */
      41           3 : inline ModifiableLock::ModifiableLock()
      42           3 : : _oldIntEnabledStatusValue(::getOldIntEnabledStatusValueAndSuspendAllInterrupts()), _isLocked(true)
      43             : {}
      44             : 
      45           3 : inline ModifiableLock::~ModifiableLock()
      46             : {
      47           2 :     if (_isLocked)
      48             :     {
      49           2 :         ::resumeAllInterrupts(_oldIntEnabledStatusValue);
      50             :     }
      51             : }
      52             : 
      53           3 : inline void ModifiableLock::unlock()
      54             : {
      55           3 :     if (_isLocked)
      56             :     {
      57           2 :         ::resumeAllInterrupts(_oldIntEnabledStatusValue);
      58           2 :         _isLocked = false;
      59             :     }
      60           3 : }
      61             : 
      62           2 : inline void ModifiableLock::lock()
      63             : {
      64           2 :     if (!_isLocked)
      65             :     {
      66           1 :         _oldIntEnabledStatusValue = ::getOldIntEnabledStatusValueAndSuspendAllInterrupts();
      67           1 :         _isLocked                 = true;
      68             :     }
      69           2 : }
      70             : 
      71             : } // namespace async
      72             : 
      73             : #endif // GUARD_5D6895FC_B608_4C8E_87FB_B600671CA5B0

Generated by: LCOV version 1.14