Skip to content

Commit 7b0b8f6

Browse files
committed
Use return value of fd-mux Loop().
Make the return value of Loop() more meaningfuil (a boolean returning tru if everything was fine - opposite to the integer returned before). Use that in machine-control (previously, machine control was actually not (anymore?) looking at that result. The 'ret' value was only initialized, never set. Rename the static volatile signal state variables in fd-mux to have prefix `s_` for improved readability.
1 parent 8fa4c94 commit 7b0b8f6

3 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/common/fd-mux.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
#include "common/logging.h"
3232

3333
// A signal that should trigger exiting the loop
34-
static volatile sig_atomic_t caught_exit_trigger_signal = 0;
34+
static volatile sig_atomic_t s_caught_exit_trigger_signal = 0;
3535

3636
// A signal that is harmless for our context and shall be ignored.
37-
static volatile sig_atomic_t caught_ignored_signal = 0;
37+
static volatile sig_atomic_t s_caught_ignored_signal = 0;
3838

3939
static void receive_signal(int signo) {
4040
static const char msg[] = "Caught signal. Shutting down ASAP.\n";
41-
if (!caught_exit_trigger_signal) { // only print message once.
41+
if (!s_caught_exit_trigger_signal) { // only print message once.
4242
write(STDERR_FILENO, msg, sizeof(msg));
4343
}
44-
caught_exit_trigger_signal = 1;
44+
s_caught_exit_trigger_signal = 1;
4545
}
4646

47-
static void ignore_signal(int signo) { caught_ignored_signal = 1; }
47+
static void ignore_signal(int signo) { s_caught_ignored_signal = 1; }
4848

4949
static void arm_signal_handler() {
50-
caught_exit_trigger_signal = 0;
51-
caught_ignored_signal = 0;
50+
s_caught_exit_trigger_signal = 0;
51+
s_caught_ignored_signal = 0;
5252

5353
struct sigaction sa = {};
5454
sa.sa_handler = receive_signal;
@@ -136,7 +136,7 @@ bool FDMultiplexer::SingleCycle(unsigned int timeout_ms) {
136136

137137
int fds_ready = select(maxfd + 1, &read_fds, &write_fds, nullptr, &timeout);
138138
if (fds_ready < 0) {
139-
if (!caught_exit_trigger_signal) perror("select() failed");
139+
if (!s_caught_exit_trigger_signal) perror("select() failed");
140140
return false;
141141
}
142142

@@ -154,17 +154,17 @@ bool FDMultiplexer::SingleCycle(unsigned int timeout_ms) {
154154
return true;
155155
}
156156

157-
int FDMultiplexer::Loop() {
157+
bool FDMultiplexer::Loop() {
158158
const unsigned timeout = idle_ms_;
159159

160160
arm_signal_handler();
161-
while (SingleCycle(timeout) && !caught_exit_trigger_signal) {
162-
if (caught_ignored_signal) {
161+
while (SingleCycle(timeout) && !s_caught_exit_trigger_signal) {
162+
if (s_caught_ignored_signal) {
163163
Log_info("Caught SIGPIPE. Ignored.\n");
164-
caught_ignored_signal = 0;
164+
s_caught_ignored_signal = 0;
165165
}
166166
}
167167
disarm_signal_handler();
168168

169-
return caught_exit_trigger_signal ? 1 : 0;
169+
return !s_caught_exit_trigger_signal;
170170
}

src/common/fd-mux.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ class FDMultiplexer {
4646
void RunOnIdle(const Handler &handler);
4747

4848
// Run the main loop. Blocks while there is still a filedescriptor
49-
// registered (return 0) or until a signal is triggered (return 1).
50-
int Loop();
49+
// registered.
50+
// Return `true` on sucess, `false` if we exited due to some signal.
51+
bool Loop();
5152

5253
protected:
5354
// Run a single cycle resulting in exactly one call of a handler function.

src/machine-control.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <unistd.h>
3232

3333
#include <cmath>
34+
#include <cstdlib>
3435
#include <memory>
3536
#include <string>
3637
#include <string_view>
@@ -613,7 +614,6 @@ int main(int argc, char *argv[]) {
613614
new GCodeParser(parser_cfg, machine_control->ParseEventReceiver());
614615
GCodeStreamer *const streamer = new GCodeStreamer(
615616
&event_server, parser, machine_control->ParseEventReceiver());
616-
int ret = 0;
617617
if (has_filename) {
618618
const char *filename = argv[optind];
619619
send_file_to_machine(machine_control, streamer, filename);
@@ -627,20 +627,20 @@ int main(int argc, char *argv[]) {
627627
machine_control);
628628
}
629629

630-
event_server.Loop(); // Run service until Ctrl-C or all sockets closed.
630+
// Run service until Ctrl-C or all sockets closed.
631+
const bool clean_exit = event_server.Loop();
631632
Log_info("Exiting.");
632633

633634
delete streamer;
634635
delete parser;
635636
delete machine_control;
636637

637-
const bool caught_signal = (ret == 1); // ?
638-
if (caught_signal) {
638+
if (!clean_exit) {
639639
Log_info(
640640
"Caught signal: immediate exit. "
641641
"Skipping potential remaining queue.");
642642
}
643-
motion_backend->Shutdown(!caught_signal);
643+
motion_backend->Shutdown(clean_exit);
644644

645645
delete motion_backend;
646646
delete pru_hw_interface;
@@ -650,5 +650,5 @@ int main(int argc, char *argv[]) {
650650
parser_cfg.SaveParams();
651651

652652
Log_info("Shutdown.");
653-
return ret;
653+
return clean_exit ? EXIT_SUCCESS : EXIT_FAILURE;
654654
}

0 commit comments

Comments
 (0)