Skip to content

Commit

Permalink
fix: implement proper thread locking for serial manipulations
Browse files Browse the repository at this point in the history
implemented serial_mutex to lock serial changes when receiving data or when ConnectionWindow changes the connection. Furthermore, port is now automatically closed when error occurs, e.g. when disconnecting device while receiving.
  • Loading branch information
vtx22 committed Mar 9, 2025
1 parent 1358582 commit 20d467c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/ConnectionWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "ConnectionWindow.hpp"

ConnectionWindow::ConnectionWindow(Serial *sp) : Window(ICON_FA_NETWORK_WIRED " Connection", nullptr), _sp(sp)
ConnectionWindow::ConnectionWindow(DataHandler *data_handler, Serial *sp) : Window(ICON_FA_NETWORK_WIRED " Connection", data_handler), _sp(sp)
{
_com_ports.push_back("COM-");
}

void ConnectionWindow::update_content()
{
std::lock_guard<std::mutex> lock(_data_handler->get_serial_mutex());

ImGui::SeparatorText("Settings");
_port_open = _sp->get_open();

if (_port_open)
{
Expand Down Expand Up @@ -170,6 +172,7 @@ void ConnectionWindow::update_content()
std::cout << "Closing COM port ..." << std::endl;

_sp->close();

ImGui::InsertNotification({ImGuiToastType::Success, SPARQ_NOTIFY_DURATION_OK, "COM port closed successfully!"});
_port_open = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectionWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class ConnectionWindow : public Window
{
public:
ConnectionWindow(Serial *sp);
ConnectionWindow(DataHandler *data_handler, Serial *sp);

void update_content();

Expand Down
14 changes: 11 additions & 3 deletions src/DataHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ void DataHandler::receiver_loop()
{
while (_running)
{
std::unique_lock<std::mutex> serial_lock(_serial_mutex);
sparq_message_t message = receive_message();
serial_lock.unlock();

if (message.valid)
{
std::unique_lock<std::mutex> lock(_data_mutex);

std::lock_guard<std::mutex> data_lock(_data_mutex);

if (message.message_type == sparq_message_type_t::STRING)
{
Expand All @@ -35,8 +39,6 @@ void DataHandler::receiver_loop()
{
add_to_datasets(message);
}

lock.unlock();
}

std::this_thread::sleep_for(SPARQ_RECEIVE_LOOP_DELAY);
Expand Down Expand Up @@ -256,6 +258,12 @@ sparq_message_t DataHandler::receive_message()

// Read everything thats available
int len = _sp->read(_serial_buffer.data(), SPARQ_MAX_MESSAGE_LENGTH * 2);

if (len <= 0)
{
return message;
}

// Append to message buffer
_message_buffer.insert(_message_buffer.end(), _serial_buffer.begin(), _serial_buffer.begin() + len);

Expand Down
3 changes: 2 additions & 1 deletion src/DataHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class DataHandler
sparq_plot_settings_t plot_settings;

std::mutex &get_data_mutex() { return _data_mutex; }
std::mutex &get_serial_mutex() { return _serial_mutex; }

private:
uint32_t current_absolute_sample = 0;
Expand All @@ -82,5 +83,5 @@ class DataHandler

std::thread _receive_thread;
std::atomic<bool> _running = true;
std::mutex _data_mutex;
std::mutex _data_mutex, _serial_mutex;
};
2 changes: 1 addition & 1 deletion src/cpp-serial-win
Submodule cpp-serial-win updated 1 files
+2 −0 src/serial.cpp
2 changes: 1 addition & 1 deletion src/sparq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void SPARQ::object_init()
static ConsoleWindow console_window;
static DataHandler data_handler(&serial_port, &console_window);

static ConnectionWindow connection_window(&serial_port);
static ConnectionWindow connection_window(&data_handler, &serial_port);
static PlottingWindow plotting_window(&data_handler);
static DataWindow data_window(&data_handler);
static MeasureWindow measure_window(&data_handler);
Expand Down
2 changes: 1 addition & 1 deletion src/sparq_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#define SPARQ_VERSION "v0.4.0"
#define SPARQ_VERSION "v0.4.1"

#define SPARQ_MAX_FPS 120
#define SPARQ_FONT "./assets/roboto.ttf"
Expand Down

0 comments on commit 20d467c

Please sign in to comment.