Alive thread implemented

This commit is contained in:
2026-04-11 09:50:59 +02:00
parent 8e5381fda2
commit 52aa0cac10
10 changed files with 215 additions and 31 deletions

View File

@@ -26,13 +26,26 @@ public:
ShmQueue *result_queue;
ShmQueue *event_queue;
Rktwebview_qt *webview_handler;
bool quit;
// QThread interface
protected:
void run();
};
class Alive : public QThread
{
public:
Handler *handler;
ShmQueue *alive_queue;
bool alive_timeout;
protected:
void run();
};
static Handler *_handler;
static Alive *_alive;
static void event_cb(rkt_data_t *data)
{
@@ -58,7 +71,7 @@ int main(int argc, char *argv[])
INFO1("Starting at %s", ctime(&my_time));
}
if (argc < 6) {
if (argc < 7) {
ERROR1("%s: wrong number of arguments\n", me);
exit(1);
}
@@ -68,14 +81,16 @@ int main(int argc, char *argv[])
const char *cmd_slot_str = argv[3];
const char *res_slot_str = argv[4];
const char *evt_slot_str = argv[5];
const char *alive_slot_str = argv[6];
size_t shm_size = atoi(shm_size_str);
int cmd_slot = atoi(cmd_slot_str);
int res_slot = atoi(res_slot_str);
int evt_slot = atoi(evt_slot_str);
int alive_slot = atoi(alive_slot_str);
MKLOGSTMT(LOG_INFO, fprintf(stderr, "%s %s %s %s %s %s\n", me, shm_name, shm_size_str, cmd_slot_str, res_slot_str, evt_slot_str));
MKLOGSTMT(LOG_INFO, fprintf(stderr, "%s %s %ld %d %d %d\n", me, shm_name, shm_size, cmd_slot, res_slot, evt_slot));
MKLOGSTMT(LOG_INFO, fprintf(stderr, "%s %s %s %s %s %s %s\n", me, shm_name, shm_size_str, cmd_slot_str, res_slot_str, evt_slot_str, alive_slot_str));
MKLOGSTMT(LOG_INFO, fprintf(stderr, "%s %s %ld %d %d %d %d\n", me, shm_name, shm_size, cmd_slot, res_slot, evt_slot, alive_slot));
if (!(shm_size > 0 && cmd_slot > 0 && res_slot > 0 && evt_slot > 0)) {
ERROR1("%s: Invalid shm size or slots\n", me);
@@ -91,14 +106,38 @@ int main(int argc, char *argv[])
handler->webview_handler = new Rktwebview_qt(argc, argv);
handler->start();
Alive *alive = new Alive();
_alive = alive;
alive->handler = handler;
alive->alive_timeout = false;
alive->alive_queue = new ShmQueue(handler->shm, alive_slot, false);
alive->start();
handler->webview_handler->initApp();
handler->webview_handler->execApp();
INFO0("waiting for thread to end\n");
handler->wait();
INFO0("Handler thread stopped\n");
alive->wait();
INFO0("Alive thread stopped\n");
if (alive->alive_timeout) {
// take ownership over all queues and SHM, because we didn't get an alive message
// and probably the racket side has crashed because of that.
ERROR0("Taking ownership of shared memory, shared queues and shared semaphores\n");
handler->command_queue->takeOwnership();
handler->result_queue->takeOwnership();
handler->event_queue->takeOwnership();
handler->shm->takeOwnership();
alive->alive_queue->takeOwnership();
}
INFO0("cleaning up shm\n");
delete alive->alive_queue;
delete alive;
delete handler->webview_handler;
delete handler->command_queue;
delete handler->result_queue;
@@ -116,14 +155,28 @@ int main(int argc, char *argv[])
void Handler::run()
{
bool quit = false;
int wait_ms = 10 * 1000; // 10 seconds.
while (!quit) {
int cmd;
std::string data;
command_queue->dequeue(cmd, data, true);
bool something_came = command_queue->dequeue(cmd, data, wait_ms);
if (!something_came) {
DEBUG1("No command received last %d seconds\n", wait_ms / 1000);
cmd = CMD_NOOP;
}
QJsonObject data_obj = QJsonDocument::fromJson(data.c_str()).object();
switch(cmd) {
case CMD_NOOP: {
if (quit) {
DEBUG1("Alive timeout, quit = %d\n", quit);
webview_handler->closeAllWindows();
DEBUG0("Closed all windows\n");
webview_handler->rktQuit();
DEBUG0("Quit application\n");
}
}
break;
case CMD_QUIT: {
INFO0("Got quit message\n");
webview_handler->rktQuit();
@@ -341,4 +394,31 @@ void Handler::run()
}
}
}
DEBUG0("Exiting handler thread\n");
}
void Alive::run()
{
int wait_ms = 10 * 1000;
int ping_no;
std::string data;
bool go_on = true;
while (go_on) {
bool something_came = alive_queue->dequeue(ping_no, data, wait_ms);
if (!something_came) {
ERROR0("No alive message received, stopping alive loop and quitting rktwebview_prg\n");
handler->quit = true;
handler->command_queue->enqueue(CMD_NOOP);
alive_timeout = true;
go_on = false;
} else {
if (ping_no == CMD_ALIVE_QUIT) {
DEBUG0("Got Quit ping for alive thread\n");
go_on = false;
} else {
DEBUG1("Got alive ping: %d\n", ping_no);
}
}
}
DEBUG0("Exiting alive thread\n");
}