Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
sync.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <freertos/FreeRTOS.h>
13#include <freertos/portmacro.h>
14#include <freertos/semphr.h>
15
16namespace vsync
17{
24{
25public:
26 explicit MutexLock(SemaphoreHandle_t mutex) : _mutex(mutex)
27 {
28 if (this->_mutex != nullptr)
29 {
30 xSemaphoreTake(this->_mutex, portMAX_DELAY);
31 }
32 }
34 {
35 if (this->_mutex != nullptr)
36 {
37 xSemaphoreGive(this->_mutex);
38 }
39 }
40 MutexLock(const MutexLock&) = delete;
41 MutexLock& operator=(const MutexLock&) = delete;
42
43private:
44 SemaphoreHandle_t _mutex;
45};
46
53template <typename T> class SpinGuarded
54{
55public:
56 SpinGuarded() = default;
57 explicit SpinGuarded(const T& initial) : _value(initial) {}
58 SpinGuarded(const SpinGuarded&) = delete;
59 SpinGuarded& operator=(const SpinGuarded&) = delete;
60
62 T load() const
63 {
64 const Section section(&this->_lock);
65 return this->_value;
66 }
67
69 void store(const T& value)
70 {
71 const Section section(&this->_lock);
72 this->_value = value;
73 }
74
76 template <typename F> decltype(auto) update(F&& f)
77 {
78 const Section section(&this->_lock);
79 return f(this->_value);
80 }
81
83 template <typename F> decltype(auto) inspect(F&& f) const
84 {
85 const Section section(&this->_lock);
86 return f(this->_value);
87 }
88
89private:
91 class Section
92 {
93 public:
94 explicit Section(portMUX_TYPE* lock) : _lock(lock) { portENTER_CRITICAL(this->_lock); }
95 ~Section() { portEXIT_CRITICAL(this->_lock); }
96 Section(const Section&) = delete;
97 Section& operator=(const Section&) = delete;
98
99 private:
100 portMUX_TYPE* _lock;
101 };
102
103 mutable portMUX_TYPE _lock = portMUX_INITIALIZER_UNLOCKED;
104 T _value{};
105};
106} // namespace vsync
Scoped lock on a FreeRTOS mutex; takes it forever on construction and gives it back on destruction.
Definition sync.h:24
A value of type T that owns its own spinlock; every access is one short critical section.
Definition sync.h:54
decltype(auto) inspect(F &&f) const
Runs f(const T&) under the lock and returns its result; use it to copy out one field.
Definition sync.h:83
T load() const
Returns a copy of the value, made under the lock.
Definition sync.h:62
decltype(auto) update(F &&f)
Runs f(T&) under the lock and returns its result (void is fine).
Definition sync.h:76
void store(const T &value)
Replaces the value under the lock.
Definition sync.h:69