summaryrefslogtreecommitdiffhomepage
path: root/tracker-hatire
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-05-04 07:22:13 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-05-04 07:22:13 +0200
commit7f1f658fec4a1f50d78dc8b8c4efe22bb6a9636b (patch)
tree74fb6dd72ef461e57d007676a72b34777e3e0309 /tracker-hatire
parent38dd6e55d20adfd830d834c394fc6ce7373a4805 (diff)
tracker/hatire: remove race
Don't prepend existing remaining data racing with add new data. Expose the lock as a public member.
Diffstat (limited to 'tracker-hatire')
-rwxr-xr-x[-rw-r--r--]tracker-hatire/ftnoir_tracker_hat.cpp72
-rwxr-xr-x[-rw-r--r--]tracker-hatire/thread.cpp7
-rwxr-xr-x[-rw-r--r--]tracker-hatire/thread.hpp15
3 files changed, 51 insertions, 43 deletions
diff --git a/tracker-hatire/ftnoir_tracker_hat.cpp b/tracker-hatire/ftnoir_tracker_hat.cpp
index 6b76ddba..48585f6c 100644..100755
--- 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
index 26bdc14d..83b712c2 100644..100755
--- 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
index dd0d8c14..c4ce4761 100644..100755
--- a/tracker-hatire/thread.hpp
+++ b/tracker-hatire/thread.hpp
@@ -22,6 +22,12 @@ enum results
# include <QTimer>
#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;
};