-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreertosMutex.hpp
More file actions
43 lines (33 loc) · 877 Bytes
/
FreertosMutex.hpp
File metadata and controls
43 lines (33 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef INDEXLIB_FREERTOS_MUTEX_HPP
#define INDEXLIB_FREERTOS_MUTEX_HPP
#include "FreeRTOS.h"
#include "semphr.h"
namespace IndexLib
{
class FreertosMutex
{
public:
FreertosMutex() { _handle = xSemaphoreCreateMutex(); }
~FreertosMutex()
{
if (_handle) vSemaphoreDelete(_handle);
}
void lock() { xSemaphoreTake(_handle, portMAX_DELAY); }
void unlock() { xSemaphoreGive(_handle); }
FreertosMutex(const FreertosMutex &) = delete;
FreertosMutex &operator=(const FreertosMutex &) = delete;
private:
SemaphoreHandle_t _handle;
};
class LockGuard
{
public:
explicit LockGuard(FreertosMutex &m) : _m(m) { _m.lock(); }
~LockGuard() { _m.unlock(); }
LockGuard(const LockGuard &) = delete;
LockGuard &operator=(const LockGuard &) = delete;
private:
FreertosMutex &_m;
};
} // namespace IndexLib
#endif // INDEXLIB_FREERTOS_MUTEX_HPP