qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Robert Foley <robert.foley@linaro.org>
To: qemu-devel@nongnu.org
Cc: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org
Subject: [PATCH v2 3/6] Add a mutex to guarantee single writer to qemu_logfile handle.
Date: Fri, 15 Nov 2019 08:10:37 -0500	[thread overview]
Message-ID: <20191115131040.2834-4-robert.foley@linaro.org> (raw)
In-Reply-To: <20191115131040.2834-1-robert.foley@linaro.org>

Also added qemu_logfile_init() for initializing the logfile mutex.

Signed-off-by: Robert Foley <robert.foley@linaro.org>
---
v2
    - In qemu_set_log() moved location of mutex lock/unlock 
      due to cleanup changes.
---
v1
    - changed qemu_logfile_init() to use __constructor__.
---
 util/log.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/util/log.c b/util/log.c
index 417d16ec66..91ebb5c924 100644
--- a/util/log.c
+++ b/util/log.c
@@ -24,8 +24,10 @@
 #include "qapi/error.h"
 #include "qemu/cutils.h"
 #include "trace/control.h"
+#include "qemu/thread.h"
 
 static char *logfilename;
+static QemuMutex qemu_logfile_mutex;
 FILE *qemu_logfile;
 int qemu_loglevel;
 static int log_append = 0;
@@ -49,6 +51,11 @@ int qemu_log(const char *fmt, ...)
     return ret;
 }
 
+static void __attribute__((__constructor__)) qemu_logfile_init(void)
+{
+    qemu_mutex_init(&qemu_logfile_mutex);
+}
+
 static bool log_uses_own_buffers;
 
 /* enable or disable low levels log */
@@ -70,7 +77,10 @@ void qemu_set_log(int log_flags)
     if (qemu_loglevel && (!is_daemonized() || logfilename)) {
         need_to_open_file = true;
     }
+    g_assert(qemu_logfile_mutex.initialized);
+    qemu_mutex_lock(&qemu_logfile_mutex);
     if (qemu_logfile && !need_to_open_file) {
+        qemu_mutex_unlock(&qemu_logfile_mutex);
         qemu_log_close();
     } else if (!qemu_logfile && need_to_open_file) {
         if (logfilename) {
@@ -105,6 +115,7 @@ void qemu_set_log(int log_flags)
 #endif
             log_append = 1;
         }
+        qemu_mutex_unlock(&qemu_logfile_mutex);
     }
 }
 
@@ -240,12 +251,15 @@ void qemu_log_flush(void)
 /* Close the log file */
 void qemu_log_close(void)
 {
+    g_assert(qemu_logfile_mutex.initialized);
+    qemu_mutex_lock(&qemu_logfile_mutex);
     if (qemu_logfile) {
         if (qemu_logfile != stderr) {
             fclose(qemu_logfile);
         }
         qemu_logfile = NULL;
     }
+    qemu_mutex_unlock(&qemu_logfile_mutex);
 }
 
 const QEMULogItem qemu_log_items[] = {
-- 
2.17.1



  parent reply	other threads:[~2019-11-15 13:14 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-15 13:10 [PATCH v2 0/6] Make the qemu_logfile handle thread safe Robert Foley
2019-11-15 13:10 ` [PATCH v2 1/6] Fix double free issue in qemu_set_log_filename() Robert Foley
2019-11-15 13:10 ` [PATCH v2 2/6] Cleaned up flow of code in qemu_set_log(), to simplify and clarify Robert Foley
2019-11-18 12:07   ` Alex Bennée
2019-11-15 13:10 ` Robert Foley [this message]
2019-11-18 12:16   ` [PATCH v2 3/6] Add a mutex to guarantee single writer to qemu_logfile handle Alex Bennée
2019-11-18 12:50     ` Robert Foley
2019-11-15 13:10 ` [PATCH v2 4/6] qemu_log_lock/unlock now preserves the " Robert Foley
2019-11-15 13:10 ` [PATCH v2 5/6] Add use of RCU for qemu_logfile Robert Foley
2019-11-18 12:22   ` Alex Bennée
2019-11-18 13:01     ` Robert Foley
2019-11-18 13:23       ` Alex Bennée
2019-11-18 14:21         ` Robert Foley
2019-11-18 16:41           ` Alex Bennée
2019-11-18 17:44             ` Robert Foley
2019-11-15 13:10 ` [PATCH v2 6/6] Added tests for close and change of logfile Robert Foley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191115131040.2834-4-robert.foley@linaro.org \
    --to=robert.foley@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=peter.puhov@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).