qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, Hans de Goede <hdegoede@redhat.com>
Subject: [Qemu-devel] [PATCH 1/3] qemu-char: Add qemu_chr_fe_claim / _release helper functions
Date: Wed, 27 Mar 2013 20:29:39 +0100	[thread overview]
Message-ID: <1364412581-3672-2-git-send-email-hdegoede@redhat.com> (raw)
In-Reply-To: <1364412581-3672-1-git-send-email-hdegoede@redhat.com>

Add qemu_chr_fe_claim / _release helper functions for properly dealing with
avail_connections.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 hw/qdev-properties-system.c |  5 ++---
 include/char/char.h         | 29 +++++++++++++++++++++++++++++
 qemu-char.c                 | 23 +++++++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/hw/qdev-properties-system.c b/hw/qdev-properties-system.c
index 12a87d5..e27d708 100644
--- a/hw/qdev-properties-system.c
+++ b/hw/qdev-properties-system.c
@@ -123,11 +123,10 @@ static int parse_chr(DeviceState *dev, const char *str, void **ptr)
     if (chr == NULL) {
         return -ENOENT;
     }
-    if (chr->avail_connections < 1) {
+    if (qemu_chr_fe_claim(chr) != 0) {
         return -EEXIST;
     }
     *ptr = chr;
-    --chr->avail_connections;
     return 0;
 }
 
@@ -140,7 +139,7 @@ static void release_chr(Object *obj, const char *name, void *opaque)
 
     if (chr) {
         qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL);
-        ++chr->avail_connections;
+        qemu_chr_fe_release(chr);
     }
 }
 
diff --git a/include/char/char.h b/include/char/char.h
index 1457e80..920a5a0 100644
--- a/include/char/char.h
+++ b/include/char/char.h
@@ -188,6 +188,35 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg);
 int qemu_chr_fe_get_msgfd(CharDriverState *s);
 
 /**
+ * @qemu_chr_fe_claim:
+ *
+ * Claim a backend before using it, should be called before calling
+ * qemu_chr_add_handlers(). 
+ *
+ * Returns: -1 if the backend is already in use by another frontend, 0 on
+ *          success.
+ */
+int qemu_chr_fe_claim(CharDriverState *s);
+
+/**
+ * @qemu_chr_fe_claim_no_fail:
+ *
+ * Like qemu_chr_fe_claim, but will exit qemu with an error when the
+ * backend is already in use.
+ */
+void qemu_chr_fe_claim_no_fail(CharDriverState *s);
+
+/**
+ * @qemu_chr_fe_claim:
+ *
+ * Release a backend for use by another frontend.
+ *
+ * Returns: -1 if the backend is already in use by another frontend, 0 on
+ *          success.
+ */
+void qemu_chr_fe_release(CharDriverState *s);
+
+/**
  * @qemu_chr_be_can_write:
  *
  * Determine how much data the front end can currently accept.  This function
diff --git a/qemu-char.c b/qemu-char.c
index edf3779..da6cbe2 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -3418,6 +3418,29 @@ int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
     return tag;
 }
 
+int qemu_chr_fe_claim(CharDriverState *s)
+{
+    if (s->avail_connections < 1) {
+        return -1;
+    }
+    s->avail_connections--;
+    return 0;
+}
+
+void qemu_chr_fe_claim_no_fail(CharDriverState *s)
+{
+    if (qemu_chr_fe_claim(s) != 0) {
+        fprintf(stderr, "%s: error chardev \"%s\" already used\n",
+                __func__, s->label);
+        exit(1);
+    }
+}
+
+void qemu_chr_fe_release(CharDriverState *s)
+{
+    s->avail_connections++;
+}
+
 void qemu_chr_delete(CharDriverState *chr)
 {
     QTAILQ_REMOVE(&chardevs, chr, next);
-- 
1.8.1.4

  reply	other threads:[~2013-03-27 19:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-27 19:29 [Qemu-devel] [PATCH 0/3] chardev-frontends: Explicitly check, inc and dec avail_connections v2 Hans de Goede
2013-03-27 19:29 ` Hans de Goede [this message]
2013-03-27 19:29 ` [Qemu-devel] [PATCH 2/3] qemu-char: Call fe_claim / fe_release when not using qdev chr properties Hans de Goede
2013-03-27 19:29 ` [Qemu-devel] [PATCH 3/3] ipoctal232: Convert to use chardev properties directly Hans de Goede
2013-03-27 21:18   ` Anthony Liguori
2013-03-28  6:18     ` Paolo Bonzini
2013-03-28 11:42       ` Hans de Goede
2013-03-28 11:39         ` Paolo Bonzini
2013-04-01 21:38   ` Alberto Garcia
2013-04-05 12:51 ` [Qemu-devel] [PATCH 0/3] chardev-frontends: Explicitly check, inc and dec avail_connections v2 Anthony Liguori

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=1364412581-3672-2-git-send-email-hdegoede@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=pbonzini@redhat.com \
    --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).