Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : /**
4 : * \ingroup async
5 : */
6 : #pragma once
7 :
8 : #include "interrupts/suspendResumeAllInterrupts.h"
9 :
10 : namespace async
11 : {
12 : /**
13 : * A synchronization mechanism that blocks threads from accessing a resource.
14 : *
15 : * The Lock class ensures mutual exclusion, allowing only one thread to access a
16 : * protected resource or function at a time. When a thread acquires the lock,
17 : * any other thread attempting to acquire it is blocked until the lock is released.
18 : * The lock is automatically released in the destructor (RAII idiom).
19 : */
20 : class Lock
21 : {
22 : public:
23 : Lock();
24 : ~Lock();
25 :
26 : private:
27 : OldIntEnabledStatusValueType _oldIntEnabledStatusValue;
28 : };
29 :
30 : /**
31 : * Inline implementations.
32 : */
33 459 : inline Lock::Lock()
34 459 : : _oldIntEnabledStatusValue(getOldIntEnabledStatusValueAndSuspendAllInterrupts())
35 459 : {}
36 :
37 459 : inline Lock::~Lock() { resumeAllInterrupts(_oldIntEnabledStatusValue); }
38 :
39 : } // namespace async
|