From 7f1f658fec4a1f50d78dc8b8c4efe22bb6a9636b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 4 May 2016 07:22:13 +0200 Subject: tracker/hatire: remove race Don't prepend existing remaining data racing with add new data. Expose the lock as a public member. --- tracker-hatire/ftnoir_tracker_hat.cpp | 72 ++++++++++++++++++----------------- tracker-hatire/thread.cpp | 7 +--- tracker-hatire/thread.hpp | 15 ++++++-- 3 files changed, 51 insertions(+), 43 deletions(-) mode change 100644 => 100755 tracker-hatire/ftnoir_tracker_hat.cpp mode change 100644 => 100755 tracker-hatire/thread.cpp mode change 100644 => 100755 tracker-hatire/thread.hpp diff --git a/tracker-hatire/ftnoir_tracker_hat.cpp b/tracker-hatire/ftnoir_tracker_hat.cpp old mode 100644 new mode 100755 index 6b76ddba..48585f6c --- a/tracker-hatire/ftnoir_tracker_hat.cpp +++ b/tracker-hatire/ftnoir_tracker_hat.cpp @@ -85,46 +85,50 @@ void hatire::send_serial_command(const QByteArray& x) // void hatire::data(double *data) { - QByteArray dataRead(t.flush_data_read()); - - while (dataRead.length() >= 30) { - t.Log(dataRead.toHex()); - if (dataRead.startsWith(Begin) && dataRead.mid(28,2) == End) - { // .Begin==0xAAAA .End==0x5555 - QDataStream datastream(dataRead.left(30)); - if (ts.bBigEndian) datastream.setByteOrder(QDataStream::BigEndian ); - else datastream.setByteOrder(QDataStream::LittleEndian); - datastream>>ArduinoData; - frame_cnt++; - if (ArduinoData.Code <= 1000) - { - HAT=ArduinoData; - new_frame=true; + QMutexLocker l(&t.data_mtx); + + QByteArray dataRead(t.flush_data_read_nolock()); + + while (dataRead.length() >= 30) + { + t.Log(dataRead.toHex()); + if (dataRead.startsWith(Begin) && dataRead.mid(28,2) == End) + { // .Begin==0xAAAA .End==0x5555 + QDataStream datastream(dataRead.left(30)); + if (ts.bBigEndian) datastream.setByteOrder(QDataStream::BigEndian ); + else datastream.setByteOrder(QDataStream::LittleEndian); + datastream>>ArduinoData; + frame_cnt++; + if (ArduinoData.Code <= 1000) + { + HAT=ArduinoData; + new_frame=true; + } + else + { + emit t.serial_debug_info(dataRead.mid(4,24)) ; + } + dataRead.remove(0,30); } else { - emit t.serial_debug_info(dataRead.mid(4,24)) ; - } - dataRead.remove(0,30); + bool ok = true; + // resynchro trame + int index = dataRead.indexOf(Begin); + if (index==-1) { + ok = false; + index=dataRead.length(); + } + emit t.serial_debug_info(dataRead.mid(0,index)) ; + dataRead.remove(0,index); + CptError++; + qDebug() << QTime::currentTime() << "hatire resync stream" << "index" << index << "ok" << ok; + } } - else - { - bool ok = true; - // resynchro trame - int index = dataRead.indexOf(Begin); - if (index==-1) { - ok = false; - index=dataRead.length(); - } - emit t.serial_debug_info(dataRead.mid(0,index)) ; - dataRead.remove(0,index); - CptError++; - qDebug() << QTime::currentTime() << "hatire resync stream" << "index" << index << "ok" << ok; - } - } - t.prepend_unread_data(dataRead); + t.prepend_unread_data_nolock(dataRead); + } if (CptError > 50) { diff --git a/tracker-hatire/thread.cpp b/tracker-hatire/thread.cpp old mode 100644 new mode 100755 index 26bdc14d..83b712c2 --- a/tracker-hatire/thread.cpp +++ b/tracker-hatire/thread.cpp @@ -275,16 +275,13 @@ void hatire_thread::on_serial_read() portable::sleep(ms); } -void hatire_thread::prepend_unread_data(const QByteArray &data) +void hatire_thread::prepend_unread_data_nolock(const QByteArray &data) { - QMutexLocker lck(&data_mtx); data_read.prepend(data); } -QByteArray hatire_thread::flush_data_read() +QByteArray hatire_thread::flush_data_read_nolock() { - QMutexLocker lck(&data_mtx); - constexpr int packet_len = 30; if (data_read.length() < 4 * packet_len) diff --git a/tracker-hatire/thread.hpp b/tracker-hatire/thread.hpp old mode 100644 new mode 100755 index dd0d8c14..c4ce4761 --- a/tracker-hatire/thread.hpp +++ b/tracker-hatire/thread.hpp @@ -22,6 +22,12 @@ enum results # include #endif +#ifdef __GNUC__ +# define unused(t, i) t __attribute__((unused)) i +#else +# define unused(t, i) t i +#endif + struct thread_settings { QByteArray sCmdStart; @@ -94,7 +100,6 @@ class hatire_thread : public QThread QByteArray data_read; serial_t com_port; thread_settings s; - QMutex data_mtx; void run() override; @@ -102,7 +107,7 @@ private slots: void on_serial_read(); void teardown_serial(); - void sendcmd_impl(const QByteArray& cmd) + void sendcmd_impl(unused(const QByteArray, &cmd)) { #ifndef HATIRE_DEBUG_LOGFILE QByteArray Msg; @@ -160,8 +165,10 @@ public: ~hatire_thread() override; hatire_thread(); - void prepend_unread_data(const QByteArray& data); + void prepend_unread_data_nolock(const QByteArray& data); - QByteArray flush_data_read(); + QByteArray flush_data_read_nolock(); void Log(const QString& message); + + QMutex data_mtx; }; -- cgit v1.2.3