[Libguestfs] [PATCH v3 1/5] threads: Add a lock (a recursive mutex) to the handle.

Richard W.M. Jones
Tue, 27 Jun 2017 11:55:55 -0000

Add a g->lock field.  This commit simply initializes and destroys the
lock on handle creation/free, and does nothing else.
---
 lib/guestfs-internal.h | 6 ++++++
 lib/handle.c           | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
index ec70336e2..c4a14e962 100644
--- a/lib/guestfs-internal.h
+++ b/lib/guestfs-internal.h
@@ -53,6 +53,7 @@
 #endif
 #endif

+#include "glthread/lock.h"
 #include "hash.h"

 #include "guestfs-internal-frontend.h"
@@ -389,6 +390,11 @@ struct guestfs_h {
   struct guestfs_h *next;	/* Linked list of open handles. */
   enum state state;             /* See the state machine diagram in
guestfs(3)*/

+  /* Lock acquired when entering any public guestfs_* function to
+   * protect the handle.
+   */
+  gl_recursive_lock_define (, lock);
+
   /**** Configuration of the handle. ****/
   bool verbose;                 /* Debugging. */
   bool trace;                   /* Trace calls. */
diff --git a/lib/handle.c b/lib/handle.c
index 91f5f755d..09c29ed84 100644
--- a/lib/handle.c
+++ b/lib/handle.c
@@ -86,6 +86,8 @@ guestfs_create_flags (unsigned flags, ...)
   g = calloc (1, sizeof (*g));
   if (!g) return NULL;

+  gl_recursive_lock_init (g->lock);
+
   g->state = CONFIG;

   g->conn = NULL;
@@ -169,6 +171,7 @@ guestfs_create_flags (unsigned flags, ...)
   free (g->path);
   free (g->hv);
   free (g->append);
+  gl_recursive_lock_destroy (g->lock);
   free (g);
   return NULL;
 }
@@ -399,6 +402,7 @@ guestfs_close (guestfs_h *g)
   free (g->backend_data);
   guestfs_int_free_string_list (g->backend_settings);
   free (g->append);
+  gl_recursive_lock_destroy (g->lock);
   free (g);
 }

--
2.13.0