libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
mutex.hpp
1#pragma once
2
3#include "libxr_def.hpp"
4#include "libxr_system.hpp"
5
6namespace LibXR
7{
8
17class Mutex
18{
19 public:
24 Mutex();
25
30 ~Mutex();
31
38 ErrorCode Lock();
39
48 ErrorCode TryLock();
49
54 void Unlock();
55
66 ErrorCode TryLockInCallback(bool in_isr);
67
74 void UnlockFromCallback(bool in_isr);
75
85
95 {
96 public:
102 LockGuard(Mutex &mutex) : mutex_(mutex) { mutex_.Lock(); }
103
109
110 private:
112 };
113
123 {
124 public:
136
142 {
143 if (success_ == ErrorCode::OK)
144 {
146 }
147 }
148
155 bool Locked() { return success_ == ErrorCode::OK; }
156
157 private:
159 ErrorCode success_;
160 bool in_isr_;
161 };
162
163 private:
165};
166
167} // namespace LibXR
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
Definition mutex.hpp:95
Mutex & mutex_
被管理的互斥锁 (Reference to the managed mutex).
Definition mutex.hpp:111
LockGuard(Mutex &mutex)
构造函数,自动加锁 (Constructor automatically locking the mutex).
Definition mutex.hpp:102
~LockGuard()
析构函数,自动解锁 (Destructor automatically unlocking the mutex).
Definition mutex.hpp:108
回调(ISR)上下文中的互斥锁管理类 (Lock management class for ISR context).
Definition mutex.hpp:123
~LockGuardInCallback()
析构函数,自动释放锁(如果成功加锁) (Destructor automatically unlocking the mutex if successfully locked).
Definition mutex.hpp:141
ErrorCode success_
记录加锁的状态 (Stores the lock acquisition status).
Definition mutex.hpp:159
bool Locked()
检查是否成功加锁 (Check if the mutex was successfully locked).
Definition mutex.hpp:155
bool in_isr_
记录是否处于 ISR 上下文 (Indicates if in ISR context).
Definition mutex.hpp:160
LockGuardInCallback(Mutex &mutex, bool in_isr)
构造函数,尝试在 ISR 上下文中加锁 (Constructor attempting to lock the mutex inside ISR context).
Definition mutex.hpp:132
Mutex & mutex_
被管理的互斥锁 (Reference to the managed mutex).
Definition mutex.hpp:158
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode TryLockInCallback(bool in_isr)
在回调(ISR)中尝试加锁 (Attempt to lock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:29
libxr_mutex_handle mutex_handle_
互斥锁句柄 (Handle for the mutex).
Definition mutex.hpp:164
Mutex()
构造函数,初始化互斥锁 (Constructor to initialize the mutex).
Definition mutex.cpp:9
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
ErrorCode TryLock()
尝试加锁,如果锁已被占用,则立即返回失败 (Attempt to lock the mutex, returning immediately if already locked).
Definition mutex.cpp:20
~Mutex()
析构函数,销毁互斥锁 (Destructor to destroy the mutex).
Definition mutex.cpp:11
void UnlockFromCallback(bool in_isr)
在回调(ISR)中解锁 (Unlock the mutex inside an interrupt service routine (ISR)).
Definition mutex.cpp:49
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
SemaphoreHandle_t libxr_mutex_handle
互斥锁句柄类型定义 Mutex handle type definition