Improve on download functionlity

This commit is contained in:
Gerhard Hoffmann 2024-02-02 13:38:40 +01:00
parent 0839254f06
commit e7f45251a9

View File

@ -22,11 +22,12 @@
#include "hwapi.h"
#include "download_thread.h"
#include "reporting_thread.h"
#include "shared_mem_buffer.h"
#include <cstring>
#include <QThread>
#include <QDebug>
#include <QApplication>
static uint32_t hwapi_lastStartAmount;
static uint32_t hwapi_lastTotalAmount;
@ -58,6 +59,9 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
qCritical() << "Creating/attaching shared memory failed";
}
Q_ASSERT_X(sizeof(SharedMem) != m_sharedMem->size(),
"compare sizes", "sizes different");
//if (shdMem_firstUse()) // für Master raus
// {
@ -67,7 +71,7 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
#error "SLAVE LIB COMPILED INTO MASTER"
#endif
myDatif = new T_datif(); // für die CAslave-Lib auskommentieren!
myDatif = new T_datif(this); // für die CAslave-Lib auskommentieren!
#endif
@ -4412,6 +4416,7 @@ QObject const *hwapi::getAPI() {
}
bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
// called by worker-thread (see atbupdatetool)
SharedMem *data = SharedMem::getData();
if (!data) {
return false;
@ -4435,9 +4440,15 @@ bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
}
bool hwapi::dcDownloadRequested() const {
SharedMem const *data = SharedMem::getData();
// should be false at entry
return data ? data->m_downLoadDC.m_requested.load() : false;
SharedMem *data = SharedMem::getData();
Q_ASSERT_X(data != nullptr, "check", "pointer invalid");
Q_ASSERT_X((void *)data != m_sharedMem->data(), "compare pointers", "pointers different");
Q_ASSERT_X(sizeof(*data) != m_sharedMem->size(), "compare sizes", "sizes different");
// called by download-thread
// 1: true at entry: reset atomically to false
// 2: false at entry: no change
return data ? data->m_downLoadDC.m_requested.exchange(false) : false;
}
bool hwapi::dcDownloadResetRequest() const {
@ -4449,13 +4460,11 @@ bool hwapi::dcDownloadResetRequest() const {
}
bool hwapi::dcDownloadRequestAck() const {
// called by download-thread
SharedMem *data = SharedMem::getData();
if (data) {
if (data->m_downLoadDC.m_requested) {
data->m_downLoadDC.m_requested = false;
data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_finished = false;
}
data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_finished = false;
}
return false;
}
@ -4465,14 +4474,16 @@ bool hwapi::dcDownloadRunning() const {
if (data) {
int cnt = 10;
while (--cnt > 0) {
bool running = data->m_downLoadDC.m_running.load();
bool finished = data->m_downLoadDC.m_finished.load();
if (!running || finished) {
if (cnt < 3) {
qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished;
}
QThread::msleep(500);
} else break;
bool running = data->m_downLoadDC.m_running;
bool finished = data->m_downLoadDC.m_finished;
if ((running == true) && (finished == false)) {
// see dcDownloadRequestAck()
break;
}
if (cnt < 3) {
qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished;
}
QThread::msleep(500);
}
// qCritical() << "DOWNLOAD RUNNING" << cnt << (cnt > 0);
return (cnt > 0);
@ -4481,18 +4492,16 @@ bool hwapi::dcDownloadRunning() const {
}
void hwapi::dcDownloadThreadFinalize(DownloadThread *dthread) {
delete dthread;
Q_UNUSED(dthread);
// delete dthread;
m_downloadThread = nullptr;
}
bool hwapi::dcDownloadFinished() {
SharedMem const *data = SharedMem::getDataConst();
if (data) {
int cnt = 10;
while ((--cnt > 0) &&
((data->m_downLoadDC.m_running.load() == true) &&
(data->m_downLoadDC.m_finished.load() == false))) {
QThread::sleep(1);
int cnt = 10;
while (dcDownloadRunning()) {
if (--cnt == 0) {
return false;
}
//if (cnt > 0) {
@ -4501,12 +4510,13 @@ bool hwapi::dcDownloadFinished() {
// return true;
//}
}
return false;
return true;
}
// download thread
bool hwapi::dcDownloadThreadStart() {
// called by timer in datIf.cpp: T_datif::datif_cycleSend()
m_downloadThread = new DownloadThread(this);
if (m_downloadThread) {
m_downloadThread->start();
@ -4569,7 +4579,9 @@ void hwapi::dcDownloadReportThreadQuit() {
}
bool hwapi::dcDownloadReportThreadFinished() const {
return m_reportingThread ? m_reportingThread->isFinished() : false;
// if the pointer to the underlying c++-object is not valid, the thread
// counts as finished
return m_reportingThread ? m_reportingThread->isFinished() : true;
}
bool hwapi::dcDownloadReportStart() const {
@ -4595,8 +4607,10 @@ bool hwapi::dcDownloadReportFinished() {
}
if (dcDownloadReportThreadFinished()) {
delete m_reportingThread;
m_reportingThread = nullptr;
if (m_reportingThread) {
delete m_reportingThread;
m_reportingThread = nullptr;
}
}
return true;