Commit 18460a5a authored by Julian Viereck's avatar Julian Viereck
Browse files

Tracer: Adding lock to make it threadsafe

parent 3f6d48d0
Pipeline #11266 failed with stage
in 4 minutes and 47 seconds
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <boost/function.hpp> #include <boost/function.hpp>
#include <list> #include <list>
#include <string> #include <string>
#include <mutex>
#include <dynamic-graph/entity.h> #include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-traces.h> #include <dynamic-graph/exception-traces.h>
...@@ -27,6 +28,7 @@ class DG_TRACER_DLLAPI Tracer : public Entity { ...@@ -27,6 +28,7 @@ class DG_TRACER_DLLAPI Tracer : public Entity {
protected: protected:
typedef std::list<const SignalBase<int> *> SignalList; typedef std::list<const SignalBase<int> *> SignalList;
SignalList toTraceSignals; SignalList toTraceSignals;
std::mutex files_mtx;
public: public:
enum TraceStyle { enum TraceStyle {
......
...@@ -146,6 +146,7 @@ void TracerRealTime::openFile(const SignalBase<int> &sig, ...@@ -146,6 +146,7 @@ void TracerRealTime::openFile(const SignalBase<int> &sig,
void TracerRealTime::closeFiles() { void TracerRealTime::closeFiles() {
dgDEBUGIN(15); dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
FileList::iterator iter = files.begin(); FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin(); HardFileList::iterator hardIter = hardFiles.begin();
......
...@@ -171,6 +171,7 @@ void Tracer::openFile(const SignalBase<int> &sig, const string &givenname) { ...@@ -171,6 +171,7 @@ void Tracer::openFile(const SignalBase<int> &sig, const string &givenname) {
void Tracer::closeFiles() { void Tracer::closeFiles() {
dgDEBUGIN(15); dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) { for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
std::ostream *filePtr = *iter; std::ostream *filePtr = *iter;
...@@ -193,6 +194,14 @@ void Tracer::record() { ...@@ -193,6 +194,14 @@ void Tracer::record() {
dgDEBUGIN(15); dgDEBUGIN(15);
// Ensure record() never hangs. If the attempt to acquire the lock fails,
// then closeFiles() is active and we shouldn't write to files anyways.
std::unique_lock<std::mutex> files_lock(files_mtx, std::try_to_lock);
if (!files_lock.owns_lock()) {
dgDEBUGOUT(15);
return;
}
if (files.size() != toTraceSignals.size()) { if (files.size() != toTraceSignals.size()) {
DG_THROW DG_THROW
ExceptionTraces(ExceptionTraces::NOT_OPEN, "No files open for tracing", ExceptionTraces(ExceptionTraces::NOT_OPEN, "No files open for tracing",
......
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