libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
mutex.cpp
1#include "mutex.hpp"
2
3#include <pthread.h>
4
5#include "libxr_def.hpp"
6#include "libxr_system.hpp"
7
8using namespace LibXR;
9
10Mutex::Mutex() : mutex_handle_(PTHREAD_MUTEX_INITIALIZER) {}
11
12Mutex::~Mutex() { pthread_mutex_destroy(&mutex_handle_); }
13
14ErrorCode Mutex::Lock() {
15 if (pthread_mutex_lock(&mutex_handle_) != 0) {
16 return ErrorCode::BUSY;
17 }
18 return ErrorCode::OK;
19}
20
21ErrorCode Mutex::TryLock() {
22 if (pthread_mutex_trylock(&mutex_handle_) != 0) {
23 return ErrorCode::BUSY;
24 }
25 return ErrorCode::OK;
26}
27
28void Mutex::Unlock() { pthread_mutex_unlock(&mutex_handle_); }
libxr_mutex_handle mutex_handle_
互斥锁句柄 (Handle for the mutex).
Definition mutex.hpp:85
Mutex()
构造函数,初始化互斥锁 (Constructor to initialize the mutex).
Definition mutex.cpp:10
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
ErrorCode TryLock()
尝试加锁,如果锁已被占用,则立即返回失败 (Attempt to lock the mutex, returning immediately if already locked).
Definition mutex.cpp:21
~Mutex()
析构函数,销毁互斥锁 (Destructor to destroy the mutex).
Definition mutex.cpp:12
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
LibXR 命名空间
Definition ch32_gpio.hpp:9