TinyRobotics
Loading...
Searching...
No Matches
SynchronizedQueue.h
1#pragma once
2
3
4#include "TinyRobotics/utils/AllocatorPSRAM.h"
5#include "TinyRobotics/concurrency/LockGuard.h"
6#include <list>
7
8namespace tinyrobotics {
9
10/**
11 * @brief FIFO Queue which is based on a List that is thread
12 * save.
13 * @ingroup concurrency
14 * @author Phil Schatzmann
15
16 * @tparam T
17 * @tparam TMutex
18 * @tparam TAllocator Allocator type for the underlying list (default: AllocatorPSRAM<T>)
19 */
20template <class T, class TMutex, class TAllocator = AllocatorPSRAM<T>>
22 public:
23 SynchronizedQueue() = default;
24
25 bool enqueue(T& data) {
26 LockGuard guard{mutex};;
27 return l.push_front(data);
28 }
29
30 bool peek(T& data) {
31 LockGuard guard{mutex};;
32 if (l.end()->prior == nullptr) return false;
33 data = *(l.end()->prior);
34 return true;
35 }
36
37 bool dequeue(T& data) {
38 LockGuard guard{mutex};;
39 return l.pop_back(data);
40 }
41
42 size_t size() {
43 LockGuard guard{mutex};;
44 return l.size();
45 }
46
47 bool clear() {
48 LockGuard guard{mutex};;
49 return l.clear();
50 }
51
52 bool empty() {
53 LockGuard guard{mutex};;
54 return l.empty();
55 }
56
57 protected:
58 std::list<T, TAllocator> l;
59 TMutex mutex;
60};
61
62} // namespace tinyrobotics
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition: LockGuard.h:18
FIFO Queue which is based on a List that is thread save.
Definition: SynchronizedQueue.h:21