Ref T292, Ref T285 minor adjustments in worker/cache

This commit is contained in:
Klaus Basan
2018-07-26 21:40:52 +02:00
parent 7a58cc2222
commit 4f3cb6447f
2 changed files with 15 additions and 8 deletions

View File

@@ -129,7 +129,7 @@ namespace BlackMisc
constexpr auto ready = std::future_status::ready; constexpr auto ready = std::future_status::ready;
constexpr auto zero = std::chrono::seconds::zero(); constexpr auto zero = std::chrono::seconds::zero();
auto future = m_revision.promiseLoadedValue(key, getTimestampSync(key)); std::future<void> future = m_revision.promiseLoadedValue(key, getTimestampSync(key));
if (future.valid()) if (future.valid())
{ {
std::future_status s {}; std::future_status s {};
@@ -137,6 +137,10 @@ namespace BlackMisc
while (s != ready && m_revision.isNewerValueAvailable(key, getTimestampSync(key))); while (s != ready && m_revision.isNewerValueAvailable(key, getTimestampSync(key)));
if (s != ready) { s = future.wait_for(zero); } if (s != ready) { s = future.wait_for(zero); }
if (s != ready) { return false; } if (s != ready) { return false; }
//! \todo KB 2018-07 In datastore with consolidation "on" I see many of these exceptions. Is that a normal state?
// maybe this happens if a cache is written and this takes a while, maybe we can
// use a write in prgress flag or such?
try { future.get(); } try { future.get(); }
catch (const std::future_error &) { return false; } // broken promise catch (const std::future_error &) { return false; } // broken promise
return true; return true;

View File

@@ -12,6 +12,7 @@
#include "blackmisc/verify.h" #include "blackmisc/verify.h"
#include <future> #include <future>
#include <QPointer>
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
#include <windows.h> #include <windows.h>
@@ -67,7 +68,7 @@ namespace BlackMisc
if (typeId != QMetaType::Void) { worker->m_result = CVariant(typeId, nullptr); } if (typeId != QMetaType::Void) { worker->m_result = CVariant(typeId, nullptr); }
QString ownerName = owner->objectName().isEmpty() ? owner->metaObject()->className() : owner->objectName(); const QString ownerName = owner->objectName().isEmpty() ? owner->metaObject()->className() : owner->objectName();
thread->setObjectName(ownerName + ":" + name); thread->setObjectName(ownerName + ":" + name);
worker->setObjectName(name); worker->setObjectName(name);
@@ -126,7 +127,7 @@ namespace BlackMisc
m_owner(owner), m_name(name) m_owner(owner), m_name(name)
{ {
Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO, "Empty name"); Q_ASSERT_X(!name.isEmpty(), Q_FUNC_INFO, "Empty name");
setObjectName(m_name); this->setObjectName(m_name);
m_updateTimer.setObjectName(m_name + ":timer"); m_updateTimer.setObjectName(m_name + ":timer");
} }
@@ -137,7 +138,7 @@ namespace BlackMisc
// avoid message "QObject: Cannot create children for a parent that is in a different thread" // avoid message "QObject: Cannot create children for a parent that is in a different thread"
Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(m_owner), Q_FUNC_INFO, "Needs to be started in owner thread"); Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(m_owner), Q_FUNC_INFO, "Needs to be started in owner thread");
emit aboutToStart(); emit this->aboutToStart();
setStarted(); setStarted();
auto *thread = new CRegularThread(m_owner); auto *thread = new CRegularThread(m_owner);
@@ -193,8 +194,10 @@ namespace BlackMisc
// shift in correct thread // shift in correct thread
if (!this->isFinished()) if (!this->isFinished())
{ {
QTimer::singleShot(0, this, [this, updateTimeSecs] QPointer<CContinuousWorker> myself(this);
QTimer::singleShot(0, this, [ = ]
{ {
if (!myself) { return; }
if (this->isFinished()) { return; } if (this->isFinished()) { return; }
this->startUpdating(updateTimeSecs); this->startUpdating(updateTimeSecs);
}); });
@@ -205,19 +208,19 @@ namespace BlackMisc
// here in correct timer thread // here in correct timer thread
if (updateTimeSecs < 0) if (updateTimeSecs < 0)
{ {
setEnabled(false); this->setEnabled(false);
m_updateTimer.stop(); m_updateTimer.stop();
} }
else else
{ {
setEnabled(true); this->setEnabled(true);
m_updateTimer.start(1000 * updateTimeSecs); m_updateTimer.start(1000 * updateTimeSecs);
} }
} }
void CContinuousWorker::finish() void CContinuousWorker::finish()
{ {
setFinished(); this->setFinished();
auto *ownThread = thread(); auto *ownThread = thread();
moveToThread(ownThread->thread()); // move worker back to the thread which constructed it, so there is no race on deletion moveToThread(ownThread->thread()); // move worker back to the thread which constructed it, so there is no race on deletion