This commit is contained in:
2025-11-17 18:04:40 +03:00
parent e0c8d8f39b
commit 771619b832
8 changed files with 222 additions and 149 deletions

View File

@@ -379,31 +379,32 @@ public:
// arm internal update loop
_internalUpdatingStopSource = std::stop_source{};
// _internalUpdatingStopSource = std::stop_source{};
_internalUpdatingLoopFuture = std::async(
std::launch::async,
[this](std::stop_token stoken) {
while (!(*_internalUpdatingLoopStop)) {
{
std::unique_lock ulock(*_internalUpdatingLoopMutex);
_internalUpdatingLoopCondVar->wait(ulock, [this]() -> bool { return *_dataUpdatingRequested; });
}
// _internalUpdatingLoopFuture = std::async(
// std::launch::async,
// [this](std::stop_token stoken) {
// while (!(*_internalUpdatingLoopStop)) {
// {
// std::unique_lock ulock(*_internalUpdatingLoopMutex);
// _internalUpdatingLoopCondVar->wait(ulock, [this]() -> bool { return *_dataUpdatingRequested;
// });
// }
{
std::lock_guard lock_update(*_updateMutex);
// {
// std::lock_guard lock_update(*_updateMutex);
*_isDataUpdated = false;
_lastUpdateError = _updateFunc(stoken);
}
// *_isDataUpdated = false;
// _lastUpdateError = _updateFunc(stoken);
// }
*_isDataUpdated = true;
// *_isDataUpdated = true;
// unlock all waiting threads
_updateCondVar->notify_all();
}
},
_internalUpdatingStopSource.get_token());
// // unlock all waiting threads
// _updateCondVar->notify_all();
// }
// },
// _internalUpdatingStopSource.get_token());
}
@@ -489,38 +490,38 @@ public:
// auto nn = std::this_thread::get_id();
// std::this_thread::sleep_for(_currentUpdateInterval);
std::this_thread::sleep_for(_currentUpdateInterval);
{
std::lock_guard lock{*_currentUpdateIntervalMutex};
// {
// std::lock_guard lock{*_currentUpdateIntervalMutex};
// compute it here because of possible changing _currentUpdateInterval
auto sleep_td = _currentUpdateInterval / internalUpdatingIntervalDiv;
// // compute it here because of possible changing _currentUpdateInterval
// auto sleep_td = _currentUpdateInterval / internalUpdatingIntervalDiv;
for (uint16_t i = 0; i < internalUpdatingIntervalDiv - 1; ++i) {
if (stop_token.stop_requested()) {
break;
}
// for (uint16_t i = 0; i < internalUpdatingIntervalDiv - 1; ++i) {
// if (stop_token.stop_requested()) {
// break;
// }
std::this_thread::sleep_for(sleep_td);
}
// std::this_thread::sleep_for(sleep_td);
// }
if (stop_token.stop_requested()) {
break;
}
// if (stop_token.stop_requested()) {
// break;
// }
if constexpr (std::floating_point<intv_t>) {
std::this_thread::sleep_for(sleep_td);
} else {
auto rem = _currentUpdateInterval % internalUpdatingIntervalDiv;
// if constexpr (std::floating_point<intv_t>) {
// std::this_thread::sleep_for(sleep_td);
// } else {
// auto rem = _currentUpdateInterval % internalUpdatingIntervalDiv;
if (rem.count()) {
std::this_thread::sleep_for(rem);
} else {
std::this_thread::sleep_for(sleep_td);
}
}
}
// if (rem.count()) {
// std::this_thread::sleep_for(rem);
// } else {
// std::this_thread::sleep_for(sleep_td);
// }
// }
// }
}
*_internalUpdating = false;
@@ -546,50 +547,37 @@ public:
error_t updateTelemetryData(traits::mcc_time_duration_c auto const& timeout)
{
{
std::lock_guard lock(*_internalUpdatingLoopMutex);
*_dataUpdatingRequested = true;
}
std::unique_lock ulock(*_updateMutex);
std::lock_guard thread_lock{*_updateMutex};
_internalUpdatingLoopCondVar->notify_one();
*_dataUpdatingRequested = false;
std::stop_source stop_source;
bool ok = _updateCondVar->wait_for(ulock, timeout, [this]() -> bool { return *_isDataUpdated; });
if (!ok) {
_lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
*_isDataUpdated = false;
// std::future<error_t> update_ft = std::async(std::launch::async, _updateFunc, stop_source.get_token());
// // std::future<error_t> update_ft =
// // std::async(std::launch::async, _updateFunc, _internalUpdatingStopSource.get_token());
// auto status = update_ft.wait_for(timeout);
// if (status == std::future_status::ready) {
// *_isDataUpdated = true;
// _lastUpdateError = update_ft.get();
// } else if (status == std::future_status::deferred) { // std::async was invoked in this thread, get
// result
// _lastUpdateError = update_ft.get();
// if (!_lastUpdateError) {
// *_isDataUpdated = true;
// }
// } else { // timeout
// stop_source.request_stop();
// _lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
// }
_lastUpdateError = _updateFunc(_internalUpdatingStopSource.get_token());
*_isDataUpdated = true;
}
// {
// std::lock_guard thread_lock{*_updateMutex};
// std::stop_source stop_source;
// *_isDataUpdated = false;
// // std::future<error_t> update_ft = std::async(std::launch::async, _updateFunc, stop_source.get_token());
// // // std::future<error_t> update_ft =
// // // std::async(std::launch::async, _updateFunc, _internalUpdatingStopSource.get_token());
// // auto status = update_ft.wait_for(timeout);
// // if (status == std::future_status::ready) {
// // *_isDataUpdated = true;
// // _lastUpdateError = update_ft.get();
// // } else if (status == std::future_status::deferred) { // std::async was invoked in this thread, get
// // result
// // _lastUpdateError = update_ft.get();
// // if (!_lastUpdateError) {
// // *_isDataUpdated = true;
// // }
// // } else { // timeout
// // stop_source.request_stop();
// // _lastUpdateError = MccTelemetryErrorCode::ERROR_DATA_TIMEOUT;
// // }
// *_isDataUpdated = true;
// }
// // unblock waiting threads even in the case of timeout!
// _updateCondVar->notify_all();
// unblock waiting threads even in the case of timeout!
_updateCondVar->notify_all();
// *_isDataUpdated = false;