Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inverter.cpp: avoid mess with simultaneous read/write operations #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sources/inverter-cli/inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void cInverter::poll() {
ups_qpiws_changed = true;
}
}

if (quit_thread) return;
sleep(5);
}
}
Expand Down
10 changes: 8 additions & 2 deletions sources/inverter-cli/inverter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ___INVERTER_H
#define ___INVERTER_H

#include <atomic>
#include <thread>
#include <mutex>

Expand All @@ -16,6 +17,8 @@ class cInverter {

std::string device;
std::mutex m;
std::thread t1;
std::atomic_bool quit_thread{false};

void SetMode(char newmode);
bool CheckCRC(unsigned char *buff, int len);
Expand All @@ -26,8 +29,11 @@ class cInverter {
cInverter(std::string devicename, int qpiri, int qpiws, int qmod, int qpigs);
void poll();
void runMultiThread() {
std::thread t1(&cInverter::poll, this);
t1.detach();
t1 = std::thread(&cInverter::poll, this);
}
void terminateThread() {
quit_thread = true;
t1.join();
}

string *GetQpiriStatus();
Expand Down
14 changes: 11 additions & 3 deletions sources/inverter-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <sys/file.h>

#include "main.h"
#include "tools.h"
Expand Down Expand Up @@ -171,13 +172,17 @@ int main(int argc, char* argv[]) {
runOnce = true;
}
lprintf("INVERTER: Debug set");
const char *settings;

// Get the rest of the settings from the conf file
if( access( "./inverter.conf", F_OK ) != -1 ) { // file exists
getSettingsFile("./inverter.conf");
settings = "./inverter.conf";
} else { // file doesn't exist
getSettingsFile("/etc/inverter/inverter.conf");
settings = "/etc/inverter/inverter.conf";
}
getSettingsFile(settings);
int fd = open(settings, O_RDWR);
while (flock(fd, LOCK_EX)) sleep(1);

bool ups_status_changed(false);
ups = new cInverter(devicename,qpiri,qpiws,qmod,qpigs);
Expand Down Expand Up @@ -282,6 +287,7 @@ int main(int argc, char* argv[]) {
delete reply2;

if(runOnce) {
ups->terminateThread();
// Do once and exit instead of loop endlessly
lprintf("INVERTER: All queries complete, exiting loop.");
exit(0);
Expand All @@ -292,7 +298,9 @@ int main(int argc, char* argv[]) {
sleep(1);
}

if (ups)
if (ups) {
ups->terminateThread();
delete ups;
}
return 0;
}