Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
parameter_refresher.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <condition_variable>
5#include <cstddef>
6#include <cstdint>
7#include <map>
8#include <mutex>
9#include <optional>
10#include <stop_token>
11#include <thread>
12#include <vector>
13
14namespace mm::node {
15
16class DeviceManager;
17
45 public:
48 explicit ParameterRefresher(DeviceManager& deviceManager);
49
52
55
66 void acquire(uint16_t devicePosition, uint16_t index, uint8_t subindex,
67 std::chrono::milliseconds period);
68
71 void release(uint16_t devicePosition, uint16_t index, uint8_t subindex);
72
74 void start();
75
77 void stop();
78
80 std::size_t trackedCount() const;
81
86 void pollDue();
87
88 private:
91 struct Entry {
92 uint16_t devicePosition = 0;
93 uint16_t index = 0;
94 uint8_t subindex = 0;
95 int refCount = 0;
96 std::chrono::milliseconds period{};
97 std::chrono::steady_clock::time_point nextDue{}; // default (epoch) => due immediately
98 std::chrono::milliseconds backoff{}; // 0 until a poll fails
99 };
100
103 struct Drive {
104 bool busy = false;
105 std::chrono::steady_clock::time_point openAt{}; // default (epoch) => open
106 };
107
109 struct Job {
110 uint64_t key = 0;
111 uint16_t devicePosition = 0;
112 uint16_t index = 0;
113 uint8_t subindex = 0;
114 };
115
117 static uint64_t makeKey(uint16_t devicePosition, uint16_t index, uint8_t subindex);
118
125 std::optional<Job> takeDue(std::chrono::steady_clock::time_point now,
126 std::chrono::steady_clock::time_point& wakeAt);
127
130 void poll(const Job& job);
131
134 void work(const std::stop_token& stopToken);
135
136 DeviceManager& deviceManager_;
137 mutable std::mutex mutex_;
138 std::condition_variable cv_;
139 std::map<uint64_t, Entry> entries_;
142 std::map<uint16_t, Drive> drives_;
143 std::vector<std::jthread> workers_;
144};
145
146} // namespace mm::node
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:339
Keeps a reference-counted set of SDO parameters fresh by polling them off-thread.
Definition parameter_refresher.h:44
ParameterRefresher & operator=(const ParameterRefresher &)=delete
ParameterRefresher(const ParameterRefresher &)=delete
void stop()
Stops and joins the worker threads. Idempotent.
Definition parameter_refresher.cc:104
void pollDue()
Polls the due objects on the calling thread, one pass, with no worker running.
Definition parameter_refresher.cc:193
std::size_t trackedCount() const
Number of distinct objects currently tracked (reference count > 0). For tests/UI.
Definition parameter_refresher.cc:122
void start()
Starts the worker threads. Idempotent.
Definition parameter_refresher.cc:90
void release(uint16_t devicePosition, uint16_t index, uint8_t subindex)
Drops a reference previously taken with acquire. The object stops being polled when its last referenc...
Definition parameter_refresher.cc:76
void acquire(uint16_t devicePosition, uint16_t index, uint8_t subindex, std::chrono::milliseconds period)
Adds a reference to keeping (devicePosition, index, subindex) fresh.
Definition parameter_refresher.cc:51
~ParameterRefresher()
Stops the workers (if running) and joins them.
Definition parameter_refresher.cc:44
uint8_t subindex
Definition esi_entry.cc:283
Definition bus_health_source.h:7