On 07/19/2011 07:22 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange"<berra...@redhat.com>

Remove the need for a virNetSocket object to be protected by
locks from the object using it, by introducing its own native
locking and reference counting

* src/rpc/virnetsocket.c: Add locking&  reference counting
---
  src/rpc/virnetsocket.c |  147 +++++++++++++++++++++++++++++++++++++++---------
  1 files changed, 120 insertions(+), 27 deletions(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 7ea1ab7..8dd4d3a 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -40,6 +40,7 @@
  #include "logging.h"
  #include "files.h"
  #include "event.h"
+#include "threads.h"

  #define VIR_FROM_THIS VIR_FROM_RPC

@@ -49,6 +50,9 @@


  struct _virNetSocket {
+    virMutex lock;
+    int refs;

Someday we should revive the virObject patches, for lighter-weight atomic-op reference counting. But that shouldn't hold up this patch.

  int virNetSocketAddIOCallback(virNetSocketPtr sock,
                                int events,
                                virNetSocketIOFunc func,
                                void *opaque)
  {
+    int ret = -1;
+
+    virMutexLock(&sock->lock);
      if (sock->watch>  0) {
          VIR_DEBUG("Watch already registered on socket %p", sock);
-        return -1;
+        goto cleanup;
      }

+    sock->refs++;

This increases the ref-count on registration, but...
  void virNetSocketRemoveIOCallback(virNetSocketPtr sock)
  {
+    virMutexLock(&sock->lock);
+
      if (sock->watch<= 0) {
          VIR_DEBUG("Watch not registered on socket %p", sock);
+        virMutexUnlock(&sock->lock);
          return;
      }

      virEventRemoveHandle(sock->watch);
-    sock->watch = 0;
+
+    virMutexUnlock(&sock->lock);
  }

...this doesn't decrease it.  Am I missing something?

Once that question is answered, then ACK to the rest of the patch.

--
Eric Blake   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to