Commit 1829a35c authored by Mihai Moldovan's avatar Mihai Moldovan

nxcomp/src/Log.h: block signals while writing out data.

Prevents race conditions caused by signal handlers while flushing out our log queue.
parent 09586d76
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <map> #include <map>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include <assert.h> #include <assert.h>
#include <stack> #include <stack>
...@@ -218,6 +219,22 @@ class NXLog ...@@ -218,6 +219,22 @@ class NXLog
*/ */
void flush(per_thread_data *pdt) void flush(per_thread_data *pdt)
{ {
/*
* Block all signals until we are dong printing data.
* Ensures that a signal handler won't interrupt us
* and overwrite the buffer data mid-print, leading
* to confusing output.
*/
sigset_t orig_signal_mask,
tmp_signal_mask;
sigemptyset(&orig_signal_mask);
/* Set up new mask to block all signals. */
sigfillset(&tmp_signal_mask);
/* Block all signals. */
pthread_sigmask(SIG_BLOCK, &tmp_signal_mask, &orig_signal_mask);
if (!pdt->buffer.empty ()) { if (!pdt->buffer.empty ()) {
const std::string str = pdt->buffer.top()->str(); const std::string str = pdt->buffer.top()->str();
...@@ -231,6 +248,9 @@ class NXLog ...@@ -231,6 +248,9 @@ class NXLog
/* Remove from stack. */ /* Remove from stack. */
pdt->buffer.pop(); pdt->buffer.pop();
} }
/* Restore old signal mask. */
pthread_sigmask(SIG_SETMASK, &orig_signal_mask, NULL);
} }
......
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