* [PATCH] libxl: fix mutex initialization
@ 2012-01-13 13:12 Roger Pau Monne
2012-01-13 14:01 ` Christoph Egger
2012-01-26 17:41 ` Ian Jackson
0 siblings, 2 replies; 3+ messages in thread
From: Roger Pau Monne @ 2012-01-13 13:12 UTC (permalink / raw)
To: xen-devel
# HG changeset patch
# User Roger Pau Monne <roger.pau@entel.upc.edu>
# Date 1326460029 -3600
# Node ID bb6b01995d64f7c2887b7174536861b83ef04364
# Parent 117ad4634ba11c10e45f3d86e6baff35d7d2691c
libxl: fix mutex initialization
The macro PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is not defined on
NetBSD, so define mutex attributes manually.
Signed-off-by: Roger Pau Monne <roger.pau@entel.upc.edu>
diff -r 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c Fri Jan 13 13:55:01 2012 +0100
+++ b/tools/libxl/libxl.c Fri Jan 13 14:07:09 2012 +0100
@@ -41,7 +41,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in
{
libxl_ctx *ctx;
struct stat stat_buf;
- const pthread_mutex_t mutex_value = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
if (version != LIBXL_VERSION)
return ERROR_VERSION;
@@ -55,10 +54,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 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl_internal.c
--- a/tools/libxl/libxl_internal.c Fri Jan 13 13:55:01 2012 +0100
+++ b/tools/libxl/libxl_internal.c Fri Jan 13 14:07:09 2012 +0100
@@ -304,6 +304,28 @@ _hidden int libxl__compare_macs(libxl_ma
return 0;
}
+_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock)
+{
+ pthread_mutexattr_t attr;
+
+ 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");
+ return ERROR_FAIL;
+ }
+ if (pthread_mutex_init(lock, &attr) != 0) {
+ LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
+ "Failed to init mutex\n");
+ return ERROR_FAIL;
+ }
+ return 0;
+}
+
libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
uint32_t domid)
{
diff -r 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h Fri Jan 13 13:55:01 2012 +0100
+++ b/tools/libxl/libxl_internal.h Fri Jan 13 14:07:09 2012 +0100
@@ -606,6 +606,8 @@ _hidden int libxl__e820_alloc(libxl__gc
_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)
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] libxl: fix mutex initialization
2012-01-13 13:12 [PATCH] libxl: fix mutex initialization Roger Pau Monne
@ 2012-01-13 14:01 ` Christoph Egger
2012-01-26 17:41 ` Ian Jackson
1 sibling, 0 replies; 3+ messages in thread
From: Christoph Egger @ 2012-01-13 14:01 UTC (permalink / raw)
To: Roger Pau Monne; +Cc: xen-devel
On 01/13/12 14:12, Roger Pau Monne wrote:
> # HG changeset patch
> # User Roger Pau Monne<roger.pau@entel.upc.edu>
> # Date 1326460029 -3600
> # Node ID bb6b01995d64f7c2887b7174536861b83ef04364
> # Parent 117ad4634ba11c10e45f3d86e6baff35d7d2691c
> libxl: fix mutex initialization
>
> The macro PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is not defined on
> NetBSD, so define mutex attributes manually.
>
> Signed-off-by: Roger Pau Monne<roger.pau@entel.upc.edu>
Acked-by: Christoph Egger <Christoph.Egger@amd.com>
>
> diff -r 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl.c
> --- a/tools/libxl/libxl.c Fri Jan 13 13:55:01 2012 +0100
> +++ b/tools/libxl/libxl.c Fri Jan 13 14:07:09 2012 +0100
> @@ -41,7 +41,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in
> {
> libxl_ctx *ctx;
> struct stat stat_buf;
> - const pthread_mutex_t mutex_value = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
>
> if (version != LIBXL_VERSION)
> return ERROR_VERSION;
> @@ -55,10 +54,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 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl_internal.c
> --- a/tools/libxl/libxl_internal.c Fri Jan 13 13:55:01 2012 +0100
> +++ b/tools/libxl/libxl_internal.c Fri Jan 13 14:07:09 2012 +0100
> @@ -304,6 +304,28 @@ _hidden int libxl__compare_macs(libxl_ma
> return 0;
> }
>
> +_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock)
> +{
> + pthread_mutexattr_t attr;
> +
> + 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");
> + return ERROR_FAIL;
> + }
> + if (pthread_mutex_init(lock,&attr) != 0) {
> + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
> + "Failed to init mutex\n");
> + return ERROR_FAIL;
> + }
> + return 0;
> +}
> +
> libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
> uint32_t domid)
> {
> diff -r 117ad4634ba1 -r bb6b01995d64 tools/libxl/libxl_internal.h
> --- a/tools/libxl/libxl_internal.h Fri Jan 13 13:55:01 2012 +0100
> +++ b/tools/libxl/libxl_internal.h Fri Jan 13 14:07:09 2012 +0100
> @@ -606,6 +606,8 @@ _hidden int libxl__e820_alloc(libxl__gc
> _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@lists.xensource.com
> http://lists.xensource.com/xen-devel
>
--
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85689 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] libxl: fix mutex initialization
2012-01-13 13:12 [PATCH] libxl: fix mutex initialization Roger Pau Monne
2012-01-13 14:01 ` Christoph Egger
@ 2012-01-26 17:41 ` Ian Jackson
1 sibling, 0 replies; 3+ messages in thread
From: Ian Jackson @ 2012-01-26 17:41 UTC (permalink / raw)
To: Roger Pau Monne; +Cc: xen-devel
Roger Pau Monne writes ("[Xen-devel] [PATCH] libxl: fix mutex initialization"):
> + pthread_mutexattr_t attr;
> +
> + 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");
> + return ERROR_FAIL;
> + }
> + if (pthread_mutex_init(lock, &attr) != 0) {
> + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
> + "Failed to init mutex\n");
> + return ERROR_FAIL;
> + }
> + return 0;
This leaks the contents of attr. You need to call
pthread_mutexattr_destroy on all the exit paths (except the one where
_init failed).
What a horrible API for passing in some trivial flags!
Ian.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-01-26 17:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-01-13 13:12 [PATCH] libxl: fix mutex initialization Roger Pau Monne
2012-01-13 14:01 ` Christoph Egger
2012-01-26 17:41 ` Ian Jackson
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.