Skip to content

Commit 0c84c5b

Browse files
committed
ENH: Improve thread-safety by adding mutex locks to xout classes
1 parent 38c0ab0 commit 0c84c5b

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

Common/xout/xoutbase.cxx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ xoutbase & xoutbase::operator[](const char * cellname)
5555
void
5656
xoutbase::WriteBufferedData(void)
5757
{
58+
const LockGuardType mutexLock(GetRecursiveMutex());
59+
5860
/** Update the target c-streams. */
5961
for (const auto & cell : m_CTargetCells)
6062
{
@@ -77,6 +79,8 @@ xoutbase::WriteBufferedData(void)
7779
int
7880
xoutbase::AddTargetCell(const char * name, std::ostream * cell)
7981
{
82+
const LockGuardType mutexLock(GetRecursiveMutex());
83+
8084
int returndummy = 1;
8185

8286
if (this->m_XTargetCells.count(name))
@@ -102,6 +106,8 @@ xoutbase::AddTargetCell(const char * name, std::ostream * cell)
102106
int
103107
xoutbase::AddTargetCell(const char * name, Self * cell)
104108
{
109+
const LockGuardType mutexLock(GetRecursiveMutex());
110+
105111
int returndummy = 1;
106112

107113
if (this->m_CTargetCells.count(name))
@@ -127,6 +133,8 @@ xoutbase::AddTargetCell(const char * name, Self * cell)
127133
int
128134
xoutbase::RemoveTargetCell(const char * name)
129135
{
136+
const LockGuardType mutexLock(GetRecursiveMutex());
137+
130138
int returndummy = 1;
131139

132140
if (this->m_XTargetCells.erase(name) > 0)
@@ -151,6 +159,8 @@ xoutbase::RemoveTargetCell(const char * name)
151159
void
152160
xoutbase::SetTargetCells(const CStreamMapType & cellmap)
153161
{
162+
const LockGuardType mutexLock(GetRecursiveMutex());
163+
154164
this->m_CTargetCells = cellmap;
155165

156166
} // end SetTargetCells
@@ -163,6 +173,7 @@ xoutbase::SetTargetCells(const CStreamMapType & cellmap)
163173
void
164174
xoutbase::SetTargetCells(const XStreamMapType & cellmap)
165175
{
176+
const LockGuardType mutexLock(GetRecursiveMutex());
166177
this->m_XTargetCells = cellmap;
167178

168179
} // end SetTargetCells
@@ -175,7 +186,8 @@ xoutbase::SetTargetCells(const XStreamMapType & cellmap)
175186
int
176187
xoutbase::AddOutput(const char * name, std::ostream * output)
177188
{
178-
int returndummy = 1;
189+
const LockGuardType mutexLock(GetRecursiveMutex());
190+
int returndummy = 1;
179191

180192
if (this->m_XOutputs.count(name))
181193
{
@@ -199,7 +211,8 @@ xoutbase::AddOutput(const char * name, std::ostream * output)
199211
int
200212
xoutbase::AddOutput(const char * name, Self * output)
201213
{
202-
int returndummy = 1;
214+
const LockGuardType mutexLock(GetRecursiveMutex());
215+
int returndummy = 1;
203216

204217
if (this->m_COutputs.count(name))
205218
{
@@ -223,7 +236,8 @@ xoutbase::AddOutput(const char * name, Self * output)
223236
int
224237
xoutbase::RemoveOutput(const char * name)
225238
{
226-
int returndummy = 1;
239+
const LockGuardType mutexLock(GetRecursiveMutex());
240+
int returndummy = 1;
227241

228242
if (this->m_XOutputs.count(name))
229243
{
@@ -249,6 +263,7 @@ xoutbase::RemoveOutput(const char * name)
249263
void
250264
xoutbase::SetOutputs(const CStreamMapType & outputmap)
251265
{
266+
const LockGuardType mutexLock(GetRecursiveMutex());
252267
this->m_COutputs = outputmap;
253268

254269
} // end SetOutputs
@@ -261,6 +276,7 @@ xoutbase::SetOutputs(const CStreamMapType & outputmap)
261276
void
262277
xoutbase::SetOutputs(const XStreamMapType & outputmap)
263278
{
279+
const LockGuardType mutexLock(GetRecursiveMutex());
264280
this->m_XOutputs = outputmap;
265281

266282
} // end SetOutputs
@@ -275,6 +291,8 @@ xoutbase::SetOutputs(const XStreamMapType & outputmap)
275291
xoutbase &
276292
xoutbase::SelectXCell(const char * name)
277293
{
294+
const LockGuardType readMutexLock(GetRecursiveMutex());
295+
278296
const auto found = m_XTargetCells.find(name);
279297
return (found == m_XTargetCells.end()) ? *this : *(found->second);
280298

@@ -303,4 +321,12 @@ xoutbase::GetCOutputs(void)
303321

304322
} // end GetOutputs
305323

324+
325+
std::recursive_mutex &
326+
xoutbase::GetRecursiveMutex()
327+
{
328+
static std::recursive_mutex mutex;
329+
return mutex;
330+
}
331+
306332
} // end namespace xoutlibrary

Common/xout/xoutbase.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <iostream>
2222
#include <ostream>
2323
#include <map>
24+
#include <mutex>
2425
#include <string>
2526

2627
namespace xoutlibrary
@@ -148,6 +149,11 @@ class xoutbase
148149
GetXOutputs(void);
149150

150151
protected:
152+
using LockGuardType = std::lock_guard<std::recursive_mutex>;
153+
154+
static std::recursive_mutex &
155+
GetRecursiveMutex();
156+
151157
/** Default-constructor. Only to be used by its derived classes. */
152158
xoutbase() = default;
153159

@@ -169,6 +175,8 @@ class xoutbase
169175
Self &
170176
SendToTargets(const T & _arg)
171177
{
178+
const LockGuardType mutexLock(GetRecursiveMutex());
179+
172180
/** Send input to the target c-streams. */
173181
for (const auto & cell : m_CTargetCells)
174182
{

Common/xout/xoutcell.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ xoutcell::xoutcell()
4141
void
4242
xoutcell::WriteBufferedData(void)
4343
{
44+
const LockGuardType mutexLock(GetRecursiveMutex());
45+
4446
const std::string strbuf = this->m_InternalBuffer.str();
4547

4648
/** Send the string to the outputs */

Common/xout/xoutrow.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ using namespace std;
3434
void
3535
xoutrow::WriteBufferedData(void)
3636
{
37+
const LockGuardType mutexLock(GetRecursiveMutex());
38+
3739
/** Write the cell-data to the outputs, separated by tabs. */
3840
auto xit = this->m_XTargetCells.begin();
3941
auto tmpIt = xit;
@@ -65,6 +67,8 @@ xoutrow::WriteBufferedData(void)
6567
int
6668
xoutrow::AddTargetCell(const char * name)
6769
{
70+
const LockGuardType mutexLock(GetRecursiveMutex());
71+
6872
if (this->m_CellMap.count(name) == 0)
6973
{
7074
/** A new cell (type xoutcell) is created. */
@@ -98,6 +102,8 @@ xoutrow::AddTargetCell(const char * name)
98102
int
99103
xoutrow::RemoveTargetCell(const char * name)
100104
{
105+
const LockGuardType mutexLock(GetRecursiveMutex());
106+
101107
int returndummy = 1;
102108

103109
if (this->m_XTargetCells.erase(name) > 0)
@@ -122,6 +128,8 @@ xoutrow::RemoveTargetCell(const char * name)
122128
void
123129
xoutrow::SetTargetCells(const XStreamMapType & cellmap)
124130
{
131+
const LockGuardType mutexLock(GetRecursiveMutex());
132+
125133
/** Clean the this->m_CellMap (cells that are created using the
126134
* AddTarget(const char *) method.
127135
*/
@@ -143,6 +151,8 @@ xoutrow::SetTargetCells(const XStreamMapType & cellmap)
143151
int
144152
xoutrow::AddOutput(const char * name, std::ostream * output)
145153
{
154+
const LockGuardType mutexLock(GetRecursiveMutex());
155+
146156
int returndummy = 0;
147157

148158
/** Set the output in all cells. */
@@ -165,6 +175,8 @@ xoutrow::AddOutput(const char * name, std::ostream * output)
165175
int
166176
xoutrow::AddOutput(const char * name, Superclass * output)
167177
{
178+
const LockGuardType mutexLock(GetRecursiveMutex());
179+
168180
int returndummy = 0;
169181

170182
/** Set the output in all cells. */
@@ -187,6 +199,8 @@ xoutrow::AddOutput(const char * name, Superclass * output)
187199
int
188200
xoutrow::RemoveOutput(const char * name)
189201
{
202+
const LockGuardType mutexLock(GetRecursiveMutex());
203+
190204
int returndummy = 0;
191205
/** Set the output in all cells. */
192206
for (const auto & cell : this->m_XTargetCells)
@@ -208,6 +222,8 @@ xoutrow::RemoveOutput(const char * name)
208222
void
209223
xoutrow::SetOutputs(const CStreamMapType & outputmap)
210224
{
225+
const LockGuardType mutexLock(GetRecursiveMutex());
226+
211227
/** Set the output in all cells. */
212228
for (const auto & cell : this->m_XTargetCells)
213229
{
@@ -227,6 +243,8 @@ xoutrow::SetOutputs(const CStreamMapType & outputmap)
227243
void
228244
xoutrow::SetOutputs(const XStreamMapType & outputmap)
229245
{
246+
const LockGuardType mutexLock(GetRecursiveMutex());
247+
230248
/** Set the output in all cells. */
231249
for (const auto & cell : this->m_XTargetCells)
232250
{
@@ -246,6 +264,8 @@ xoutrow::SetOutputs(const XStreamMapType & outputmap)
246264
void
247265
xoutrow::WriteHeaders(void)
248266
{
267+
const LockGuardType mutexLock(GetRecursiveMutex());
268+
249269
/** Copy '*this'. */
250270
Self headerwriter;
251271
headerwriter.SetTargetCells(this->m_XTargetCells);
@@ -273,6 +293,8 @@ xoutrow::WriteHeaders(void)
273293
xoutbase &
274294
xoutrow::SelectXCell(const char * name)
275295
{
296+
const LockGuardType mutexLock(GetRecursiveMutex());
297+
276298
std::string cellname(name);
277299

278300
/** Check if the name is "WriteHeaders". Then the method

0 commit comments

Comments
 (0)