mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-02-11 15:38:40 +01:00
cleaning up properly , semaphore leaks, child process/thread throwing handled
This commit is contained in:
@@ -28,7 +28,8 @@ ParsedOptions CommandLineOptions::parse(int argc, char *argv[]) {
|
||||
MultiReceiverOptions multi;
|
||||
FrameSyncOptions frame;
|
||||
|
||||
auto optString = buildOptString();
|
||||
// leading '+' to stop parsing at first non-option argument (to allow for deprecated options)
|
||||
auto optString = "+" + buildOptString();
|
||||
auto longOptions = buildOptionList();
|
||||
|
||||
optind = 0; // reset getopt
|
||||
@@ -52,7 +53,7 @@ ParsedOptions CommandLineOptions::parse(int argc, char *argv[]) {
|
||||
handleAppSpecificOption(opt, optarg, base, multi, frame);
|
||||
break;
|
||||
default:
|
||||
throw sls::RuntimeError("Invalid arguments." + getHelpMessage());
|
||||
throw sls::RuntimeError("Invalidddd arguments." + getHelpMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,7 +238,7 @@ void CommandLineOptions::handleAppSpecificOption(int opt, const char *optarg,
|
||||
}
|
||||
|
||||
case 't':
|
||||
LOG(sls::logWARNING) << "Deprecated option 't' and '--rx_tcport'. Use "
|
||||
LOG(sls::logWARNING) << "Deprecated option '-t' and '--rx_tcport'. Use "
|
||||
"'p' or '--port' instead.";
|
||||
base.port = parsePort(optarg);
|
||||
break;
|
||||
|
||||
@@ -503,16 +503,15 @@ void sigInterruptHandler(int p) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
CommandLineOptions cli(AppType::SingleReceiver);
|
||||
CommandLineOptions cli(AppType::FrameSynchronizer);
|
||||
ParsedOptions opts;
|
||||
try {
|
||||
opts = cli.parse(argc, argv);
|
||||
} catch (sls::RuntimeError &e) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto &o = std::get<CommonOptions>(opts);
|
||||
auto &f = std::get<FrameSyncOptions>(opts);
|
||||
if (o.versionRequested || o.helpRequested) {
|
||||
if (f.versionRequested || f.helpRequested) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -525,7 +524,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
semaphores.resize(f.numReceivers);
|
||||
for (auto &s : semaphores) {
|
||||
sem_init(&s, 1, 0);
|
||||
sem_init(&s, 0, 0);
|
||||
}
|
||||
|
||||
FrameStatus stat{true, false, f.numReceivers};
|
||||
@@ -537,24 +536,35 @@ int main(int argc, char *argv[]) {
|
||||
std::thread combinerThread(Correlate, &stat);
|
||||
|
||||
for (int i = 0; i != f.numReceivers; ++i) {
|
||||
uint16_t port = o.port + i;
|
||||
uint16_t port = f.port + i;
|
||||
sem_t *semaphore = &semaphores[i];
|
||||
threads.emplace_back([i, semaphore, port, user_data]() {
|
||||
sls::Receiver receiver(port);
|
||||
receiver.registerCallBackStartAcquisition(StartAcquisitionCallback,
|
||||
user_data);
|
||||
receiver.registerCallBackAcquisitionFinished(
|
||||
AcquisitionFinishedCallback, user_data);
|
||||
receiver.registerCallBackRawDataReady(GetDataCallback, user_data);
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Thread " << i << " [ Tid: " << gettid() << ']';
|
||||
try {
|
||||
sls::Receiver receiver(port);
|
||||
receiver.registerCallBackStartAcquisition(StartAcquisitionCallback,
|
||||
user_data);
|
||||
receiver.registerCallBackAcquisitionFinished(
|
||||
AcquisitionFinishedCallback, user_data);
|
||||
receiver.registerCallBackRawDataReady(GetDataCallback, user_data);
|
||||
|
||||
/** - as long as no Ctrl+C */
|
||||
// each child shares the common semaphore
|
||||
sem_wait(semaphore);
|
||||
sem_destroy(semaphore);
|
||||
|
||||
// clean up frames
|
||||
if (i == 0)
|
||||
/** - as long as no Ctrl+C */
|
||||
// each child shares the common semaphore
|
||||
sem_wait(semaphore);
|
||||
} catch (...) {
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Thread " << i << " [ Tid: " << gettid() << " ]";
|
||||
for (auto &s : semaphores)
|
||||
sem_destroy(&s);
|
||||
cleanup();
|
||||
if (global_frame_status)
|
||||
sem_destroy(&(global_frame_status->available));
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Thread " << i << " [ Tid: " << gettid() << " ]";
|
||||
sem_destroy(semaphore);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -562,6 +572,7 @@ int main(int argc, char *argv[]) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
cleanup();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(stat.mtx);
|
||||
stat.terminate = true;
|
||||
|
||||
@@ -143,16 +143,15 @@ void sigInterruptHandler(int signal) {
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
CommandLineOptions cli(AppType::SingleReceiver);
|
||||
CommandLineOptions cli(AppType::MultiReceiver);
|
||||
ParsedOptions opts;
|
||||
try {
|
||||
opts = cli.parse(argc, argv);
|
||||
} catch (sls::RuntimeError &e) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto& o = std::get<CommonOptions>(opts);
|
||||
auto &m = std::get<MultiReceiverOptions>(opts);
|
||||
if (o.versionRequested || o.helpRequested) {
|
||||
if (m.versionRequested || m.helpRequested) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -185,7 +184,7 @@ int main(int argc, char *argv[]) {
|
||||
<< "Child process " << i << " [ Tid: " << gettid() << ']';
|
||||
|
||||
try {
|
||||
uint16_t port = o.port + i;
|
||||
uint16_t port = m.port + i;
|
||||
sls::Receiver receiver(port);
|
||||
|
||||
/** - register callbacks. remember to set file write enable
|
||||
@@ -215,6 +214,7 @@ int main(int argc, char *argv[]) {
|
||||
sem_destroy(&semaphore);
|
||||
|
||||
} catch (...) {
|
||||
sem_destroy(&semaphore);
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Child Process [ Tid: " << gettid() << " ]";
|
||||
throw;
|
||||
@@ -250,10 +250,6 @@ int main(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// child closed
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Child Process [ Tid: " << childPid << ']';
|
||||
}
|
||||
|
||||
std::cout << "Goodbye!\n";
|
||||
|
||||
@@ -29,8 +29,10 @@ Receiver::~Receiver() = default;
|
||||
|
||||
Receiver::Receiver(uint16_t port) {
|
||||
validatePortNumber(port);
|
||||
//#ifdef SLS_USE_TESTS
|
||||
if (port == 9999) throw RuntimeError("throwing for 9999 test");
|
||||
//#endif
|
||||
tcpipInterface = make_unique<ClientInterface>(port);
|
||||
if (port == 1957) throw RuntimeError("throwing for 1957");
|
||||
}
|
||||
|
||||
std::string Receiver::getReceiverVersion() {
|
||||
|
||||
@@ -58,7 +58,9 @@ int main(int argc, char *argv[]) {
|
||||
sem_wait(&semaphore);
|
||||
sem_destroy(&semaphore);
|
||||
} catch (...) {
|
||||
// pass
|
||||
sem_destroy(&semaphore);
|
||||
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
|
||||
throw;
|
||||
}
|
||||
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFO) << "Exiting Receiver";
|
||||
|
||||
Reference in New Issue
Block a user