- CitrineOS core extracted (CSMS OCPP 2.0.1) - OpenOCPP extracted (firmware OCPP 1.6J/2.0.1) - ShapeShifter library installed (pip install -e) - ShapeShifter specification extracted - EVerest extracted TODO updated with progress
53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright Pionix GmbH and Contributors to EVerest
|
|
#ifndef VARCONTAINER_HPP
|
|
#define VARCONTAINER_HPP
|
|
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
|
|
/*
|
|
Simple helper class for a thread safe single producer / single consumer pattern
|
|
with a queue size of one.
|
|
*/
|
|
|
|
template <class T> class VarContainer {
|
|
public:
|
|
T& operator=(T d) {
|
|
{
|
|
std::scoped_lock lock(data_mutex);
|
|
data = d;
|
|
unread_data = true;
|
|
}
|
|
condvar.notify_one();
|
|
return data;
|
|
};
|
|
|
|
void clear() {
|
|
std::scoped_lock lock(data_mutex);
|
|
unread_data = false;
|
|
};
|
|
|
|
bool wait_for(T& d, std::chrono::milliseconds timeout) {
|
|
std::unique_lock<std::mutex> lock(data_mutex);
|
|
|
|
if (condvar.wait_for(lock, timeout, [this] { return unread_data; })) {
|
|
unread_data = false;
|
|
d = data;
|
|
return true;
|
|
} else {
|
|
// Timeout occurred in wait_for
|
|
return false;
|
|
}
|
|
};
|
|
|
|
private:
|
|
T data;
|
|
std::condition_variable condvar;
|
|
std::mutex data_mutex;
|
|
bool unread_data{false};
|
|
};
|
|
|
|
#endif
|