Skip to content

Commit

Permalink
Fix SDLNet Initialisation and error path (#3)
Browse files Browse the repository at this point in the history
* Initialize RawSocketSerial::thread_active to false so that it doesn't try to cleanup uninitialized objects on error.

* Reorder main to avoid thread race conditions involving pin_map if listen_on_port fails

* Initialize SDL and SDLNet so that socket can be opened
  • Loading branch information
sjasonsmith authored Aug 5, 2021
1 parent 0e047cc commit 2c1d3f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/MarlinSimulator/RawSocketSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RawSocketSerial {
}
start_listen(ip);
server_thread = std::thread(&RawSocketSerial::execute, this);
thread_active = true;
}

void stop() {
Expand Down Expand Up @@ -178,7 +179,7 @@ class RawSocketSerial {
void write(const char* str) { while (*str) tx_buffer.write(*str++); }
void write(const uint8_t* buffer, size_t size) { tx_buffer.write((uint8_t *)buffer, size); }

bool thread_active = true;
bool thread_active = false;
ServerInfo server{};
uint8_t receive_buffer[ServerInfo::max_packet_size];
uint8_t transmit_buffer[ServerInfo::max_packet_size];
Expand Down
12 changes: 10 additions & 2 deletions src/MarlinSimulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ void simulation_main() {

// Main code
int main(int, char**) {
Application app;
std::thread simulation_loop(simulation_main);
SDL_Init(0);
SDLNet_Init();

// Listen before starting simulator loop to avoid
// thread synchronization issues if listen_on_port fails
net_serial.listen_on_port(8099);

Application app;
std::thread simulation_loop(simulation_main);

while (app.active) {
app.update();
app.render();
Expand All @@ -76,5 +81,8 @@ int main(int, char**) {
simulation_loop.join();
net_serial.stop();

SDLNet_Quit();
SDL_Quit();

return 0;
}

0 comments on commit 2c1d3f2

Please sign in to comment.