Commit 6cf2e937 authored by Joseph Mirabel's avatar Joseph Mirabel Committed by Joseph Mirabel

Fix synchronisation of thread in RosQueuedSubscribe.

parent 3b0953a8
......@@ -91,20 +91,22 @@ namespace dynamicgraph
clear();
}
// TODO Add synchronization between:
// - writer and clear (both changes variable backIdx)
// - reader and clear (both changes variable frontIdx)
/// See comments in reader and writer for details about synchronisation.
void clear ()
{
// qmutex.lock();
// synchronize with method writer
wmutex.lock();
if (!empty()) {
if (backIdx == 0)
last = buffer[BufferSize-1];
else
last = buffer[backIdx-1];
}
// synchronize with method reader
rmutex.lock();
frontIdx = backIdx = 0;
// qmutex.unlock();
rmutex.unlock();
wmutex.unlock();
}
bool empty () const
......@@ -131,7 +133,7 @@ namespace dynamicgraph
/// Index of the slot where to write next value (does not contain valid data).
size_type backIdx;
buffer_t buffer;
// boost::mutex qmutex;
boost::mutex wmutex, rmutex;
T last;
bool init;
......
......@@ -63,27 +63,31 @@ namespace dynamicgraph
template <typename R>
void BindedSignal<T, N>::writer (const R& data)
{
// TODO: synchronize with method clear
// qmutex.lock();
// synchronize with method clear
wmutex.lock();
converter (buffer[backIdx], data);
// assert(!full());
// No need to synchronize with reader here because:
// - if the buffer was not empty, then it stays not empty,
// - if it was empty, then the current value will be used at next time. It
// means the transmission bandwidth is too low.
backIdx = (backIdx+1) % N;
if (!init) {
last = buffer[backIdx];
init = true;
}
// assert(!full());
backIdx = (backIdx+1) % N;
// TODO: synchronize with method clear
// qmutex.unlock();
wmutex.unlock();
}
template <typename T, int N>
T& BindedSignal<T, N>::reader (T& data, int time)
{
if (entity->readQueue_ == -1 || time < entity->readQueue_) {
// synchronize with method clear:
// If reading from the list cannot be done, then return last value.
bool readingIsEnabled = rmutex.try_lock();
if (!readingIsEnabled || entity->readQueue_ == -1 || time < entity->readQueue_) {
data = last;
} else {
// TODO: remove me
// qmutex.lock();
if (empty())
data = last;
else {
......@@ -91,9 +95,9 @@ namespace dynamicgraph
frontIdx = (frontIdx + 1) % N;
last = data;
}
// TODO: remove me
// qmutex.unlock();
}
if (readingIsEnabled)
rmutex.unlock();
return data;
}
} // end of namespace internal.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment