From 652cba4affb77d48887988eef1c166b5a0189774 Mon Sep 17 00:00:00 2001
From: Martin Stjernholm <mast@lysator.liu.se>
Date: Sun, 10 Oct 2010 23:21:43 +0200
Subject: [PATCH] Made the logging functions atomic to work well in threaded
 tests.

---
 lib/modules/Tools.pmod/Testsuite.pmod | 52 ++++++++++++++++-----------
 1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/lib/modules/Tools.pmod/Testsuite.pmod b/lib/modules/Tools.pmod/Testsuite.pmod
index 6eddb62814..650e28c131 100644
--- a/lib/modules/Tools.pmod/Testsuite.pmod
+++ b/lib/modules/Tools.pmod/Testsuite.pmod
@@ -39,8 +39,32 @@ void log_start (int verbosity, int on_tty)
   last_line_inplace = 0;
 }
 
+protected Thread.Mutex log_mutex = Thread.Mutex();
 protected int twiddler_counter = -1;
 
+protected void unlocked_log_msg_cont (string msg)
+{
+  if (last_line_inplace && last_line_length) {
+    write ("\n");
+    last_line_length = 0;
+  }
+
+  last_log = 0;
+  write (msg);
+  twiddler_counter = -1;
+  last_line_inplace = 0;
+
+  if (has_suffix (msg, "\n") || has_suffix (msg, "\r"))
+    last_line_length = 0;
+  else {
+    array(string) msg_lines = msg / "\n";
+    if (sizeof (msg_lines) > 1) last_line_length = 0;
+    // FIXME: This length calculation is too simplistic since it
+    // assumes 1 char == 1 position.
+    last_line_length += sizeof (msg_lines[-1]);
+  }
+}
+
 void log_msg (string msg, mixed... args)
 //! Logs a testsuite message. The message is shown regardless of the
 //! verbosity level. If the previous message was logged without a
@@ -52,6 +76,8 @@ void log_msg (string msg, mixed... args)
 {
   if (sizeof (args)) msg = sprintf (msg, @args);
 
+  Thread.MutexKey lock = log_mutex->lock();
+
   if (last_line_length) {
     write ("\n");
     last_line_length = 0;
@@ -63,7 +89,7 @@ void log_msg (string msg, mixed... args)
     last_log = 0;
   }
 
-  log_msg_cont (msg);
+  unlocked_log_msg_cont (msg);
 }
 
 void log_msg_cont (string msg, mixed... args)
@@ -73,26 +99,8 @@ void log_msg_cont (string msg, mixed... args)
 //! @[log_status].
 {
   if (sizeof (args)) msg = sprintf (msg, @args);
-
-  if (last_line_inplace && last_line_length) {
-    write ("\n");
-    last_line_length = 0;
-  }
-
-  last_log = 0;
-  write (msg);
-  twiddler_counter = -1;
-  last_line_inplace = 0;
-
-  if (has_suffix (msg, "\n") || has_suffix (msg, "\r"))
-    last_line_length = 0;
-  else {
-    array(string) msg_lines = msg / "\n";
-    if (sizeof (msg_lines) > 1) last_line_length = 0;
-    // FIXME: This length calculation is too simplistic since it
-    // assumes 1 char == 1 position.
-    last_line_length += sizeof (msg_lines[-1]);
-  }
+  Thread.MutexKey lock = log_mutex->lock();
+  unlocked_log_msg_cont (msg);
 }
 
 void log_status (string msg, mixed... args)
@@ -122,6 +130,8 @@ void log_status (string msg, mixed... args)
 {
   if (sizeof (args)) msg = sprintf (msg, @args);
 
+  Thread.MutexKey lock = log_mutex->lock();
+
   switch (verbosity) {
     case 0:
       last_log = (msg != "" && msg);
-- 
GitLab