blob: 6435e1663bd356c313ae596b8ae7bda8d2f12c47 [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SENSORS_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SENSORS_H_
#include <atomic>
#include <chrono> // NOLINT
#include <cstdint>
#include <limits>
#include <utility>
namespace milotic_tlbmc {
class IpcSensor {
public:
IpcSensor() : value_(std::numeric_limits<float>::infinity()), timestamp_(0) {
static_assert(std::atomic<float>::is_always_lock_free);
static_assert(std::atomic<uint64_t>::is_always_lock_free);
}
std::pair<float, uint64_t> GetValue() const { return {value_, timestamp_}; }
void SetValue(float value) {
value_ = value;
timestamp_ = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
void SetValue(float value, uint64_t timestamp) {
value_ = value;
timestamp_ = timestamp;
}
private:
// Our application does not need strong consistency but collect time serials
// data. It's fine that timestamp is not updated atomically with the value.
std::atomic<float> value_;
// The timestamp is in milliseconds.
std::atomic<uint64_t> timestamp_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SENSORS_H_