Commit 0f808355daf46e66dbaa924d82c0b17bf19d8142

Authored by Calvin Bulla
1 parent 497fec6f
Exists in master and in 1 other branch dev

Added Thread Safety for Commands

Showing 2 changed files with 15 additions and 4 deletions   Show diff stats
app/src/inoChannel.cpp
... ... @@ -9,28 +9,33 @@ void inoChannel::exit() {
9 9 serial.close();
10 10 }
11 11  
  12 +void inoChannel::addCommand(const inoCommand & cmd) {
  13 + commandsMtx.lock();
  14 + commands.push(cmd);
  15 + commandsMtx.unlock();
  16 +}
  17 +
12 18 void inoChannel::identify() {
13 19 inoCommand cmd;
14 20  
15 21 cmd.resize(17, 0);
16   -
17 22 cmd[0] = HW_HELO;
18 23 for(int i = 0; i < 16 && i < device.size(); i++) {
19 24 cmd[i+1] = device[i];
20 25 }
21   - commands.push(cmd);
  26 + addCommand(cmd);
22 27 }
23 28  
24 29 void inoChannel::startStream() {
25 30 inoCommand cmd;
26 31 cmd.push_back(HW_START_STREAM);
27   - commands.push(cmd);
  32 + addCommand(cmd);
28 33 }
29 34  
30 35 void inoChannel::stopStream() {
31 36 inoCommand cmd;
32 37 cmd.push_back(HW_STOP_STREAM);
33   - commands.push(cmd);
  38 + addCommand(cmd);
34 39 }
35 40  
36 41 bool inoChannel::checkSystem() {
... ... @@ -76,6 +81,7 @@ void inoChannel::threadedFunction() {
76 81 break;
77 82 }
78 83 }
  84 + commandsMtx.lock();
79 85 if(commands.size() > 1) {
80 86 inoCommand cmd = commands.front();
81 87 if(serial.writeBytes(cmd.data(), cmd.size())) {
... ... @@ -84,5 +90,6 @@ void inoChannel::threadedFunction() {
84 90 ofLogError() << "Unable to send command";
85 91 }
86 92 }
  93 + commandsMtx.unlock();
87 94 }
88 95 }
... ...
app/src/inoChannel.h
... ... @@ -19,7 +19,11 @@ public:
19 19 ofEvent<string> statusChange;
20 20  
21 21 private:
  22 +
  23 + inline void addCommand(const inoCommand & cmd);
  24 +
22 25 ofSerial serial;
23 26 string device;
24 27 queue<inoCommand> commands;
  28 + ofMutex commandsMtx;
25 29 };
... ...