-
Notifications
You must be signed in to change notification settings - Fork 10
/
ThreadSafeQueue.h
171 lines (139 loc) · 3.98 KB
/
ThreadSafeQueue.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef SAFE_QUEUE
#define SAFE_QUEUE
#include <queue>
#if defined(_STM32_DEF_)
// A simple, non-threadsafe queue for non-multithreading CPUs.
template <class T>
class SafeQueue
{
public:
SafeQueue(int Size) : q(),NumElements(0)
{}
// Add an element to the queue.
void Enqueue(T t)
{
q.push(t);
NumElements++;
}
// Get the "front"-element.
// If the queue is empty, wait till a element is avaiable.
bool Dequeue(T* pVal)
{
if(!q.empty())
{
*pVal = q.front();
q.pop();
NumElements--;
return true;
}
return false;
}
int NumElements;
private:
std::queue<T> q;
};
#endif
#if defined(xxx)
// A threadsafe-queue for CPUs supporting std::mutex
// Inspired by: https://stackoverflow.com/questions/15278343/c11-thread-safe-queue
#include <mutex>
template <class T>
class SafeQueue
{
public:
SafeQueue(int Size) : q(), m(), NumElements(0)
{}
// Add an element to the queue.
void Enqueue(T t)
{
std::lock_guard<std::mutex> lock(m);
q.push(t);
NumElements++;
}
// Get the "front"-element.
// If the queue is empty, wait till a element is avaiable.
bool Dequeue(T* pVal)
{
std::unique_lock<std::mutex> lock(m);
if(!q.empty())
{
*pVal = q.front();
q.pop();
NumElements--;
return true;
}
return false;
}
int NumElements;
private:
std::queue<T> q;
mutable std::mutex m;
};
#endif
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32)
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
template <class T>
class SafeQueue
{
public:
SafeQueue(int Size=16)
{
Queue = xQueueCreate( Size, sizeof( T ) );
NumElements = 0;
QueueSema = xSemaphoreCreateCounting( 1, 0 );
}
// Add an element to the queue.
// ISR, absolutely no printing to serial!!
void Enqueue(T t)
{
xSemaphoreTakeFromISR(QueueSema, 0);
xQueueSendFromISR(Queue, &t, 0);
NumElements++;
xSemaphoreGiveFromISR(QueueSema, 0);
}
// Get the "front"-element.
// If the queue is empty, wait till an element is avaiable.
// ISR, absolutely no printing to serial!!
bool Dequeue(T* pVal)
{
xSemaphoreTakeFromISR(QueueSema, 0);
if(xQueueReceiveFromISR(Queue, pVal, 0))
{
NumElements--;
xSemaphoreGiveFromISR(QueueSema, 0);
return true;
}
xSemaphoreGiveFromISR(QueueSema, 0);
return false;
}
int NumElements;
private:
QueueHandle_t Queue;
SemaphoreHandle_t QueueSema;
};
#endif
#if 0
template<class T>
T volatile_copy(T const volatile& source) {
static_assert(std::is_trivially_copyable_v<T>, "Input must be trivially copyable");
T dest;
auto* dest_ptr = dynamic_cast<char*>(&dest);
auto* source_ptr = dynamic_cast<char const volatile*>(&source);
for(int i = 0; i < sizeof(T); i++) {
dest_ptr[i] = source_ptr[i];
}
return dest;
}
template<class T>
void volatile_assign(T volatile& dest, T const& source) {
static_assert(std::is_trivially_copyable_v<T>, "Input must be trivially copyable");
auto* source_ptr = dynamic_cast<char*>(&source);
auto* dest_ptr = dynamic_cast<char volatile*>(&dest);
for(int i = 0; i < sizeof(T); i++) {
dest_ptr[i] = source_ptr[i];
}
}
#endif
#endif