[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2] libxl: fix mutex initialization



# HG changeset patch
# User Roger Pau Monne <roger.pau@xxxxxxxxxxxxx>
# Date 1326564288 -3600
# Node ID 259112aee618753552056e398f940d1fd9fcc6f7
# Parent  f581bb82fecd51e8fbd9c2e4ae9e76b08a695587
libxl: fix mutex initialization

The macro PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is not defined on
NetBSD, so define mutex attributes manually.

Changes since v1:

 * Fix leak of mutex attr.

Signed-off-by: Roger Pau Monne <roger.pau@xxxxxxxxxxxxx>

diff -r f581bb82fecd -r 259112aee618 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Sat Jan 14 19:04:48 2012 +0100
+++ b/tools/libxl/libxl.c       Sat Jan 14 19:04:48 2012 +0100
@@ -26,7 +26,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in
 {
     libxl_ctx *ctx = NULL;
     struct stat stat_buf;
-    const pthread_mutex_t mutex_value = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
     int rc;
 
     if (version != LIBXL_VERSION) { rc = ERROR_VERSION; goto out; }
@@ -40,10 +39,10 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in
     memset(ctx, 0, sizeof(libxl_ctx));
     ctx->lg = lg;
 
-    /* This somewhat convoluted approach is needed because
-     * PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is defined to be valid
-     * only as an initialiser, not as an expression. */
-    memcpy(&ctx->lock, &mutex_value, sizeof(ctx->lock));
+    if (libxl__init_recursive_mutex(ctx, &ctx->lock) < 0) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Failed to initialize mutex");
+        return ERROR_FAIL;
+    }
 
     if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
diff -r f581bb82fecd -r 259112aee618 tools/libxl/libxl_internal.c
--- a/tools/libxl/libxl_internal.c      Sat Jan 14 19:04:48 2012 +0100
+++ b/tools/libxl/libxl_internal.c      Sat Jan 14 19:04:48 2012 +0100
@@ -296,6 +296,33 @@ int libxl__file_reference_unmap(libxl_fi
     return 0;
 }
 
+_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock)
+{
+    pthread_mutexattr_t attr;
+    int rc = 0;
+
+    if (pthread_mutexattr_init(&attr) != 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, 
+                         "Failed to init mutex attributes\n");
+        return ERROR_FAIL;
+    }
+    if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, 
+                         "Failed to set mutex attributes\n");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    if (pthread_mutex_init(lock, &attr) != 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, 
+                         "Failed to init mutex\n");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+out:
+    pthread_mutexattr_destroy(&attr);
+    return rc;
+}
+
 libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
                                                                uint32_t domid)
 {
diff -r f581bb82fecd -r 259112aee618 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h      Sat Jan 14 19:04:48 2012 +0100
+++ b/tools/libxl/libxl_internal.h      Sat Jan 14 19:04:48 2012 +0100
@@ -640,6 +640,8 @@ struct libxl__xen_console_reader {
 _hidden int libxl__parse_mac(const char *s, libxl_mac mac);
 /* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */
 _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b);
+/* init a recursive mutex */
+_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);
 
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.