Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 0/5] Add support for thread-safe handle.
Previous discussion here: https://www.redhat.com/archives/libguestfs/2015-June/thread.html#00048 v2: - Use a cleanup handler to release the lock. - Rebase to upstream. Note I have not fixed the problem(s) with error handling (patch 3).
Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 1/5] threads: Add a lock (a recursive mutex) to the handle.
Add a g->lock field. This commit simply initializes and destroys the lock on handle creation/free, and does nothing else. --- src/guestfs-internal.h | 6 ++++++ src/handle.c | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index bbd7fb4..b68942f 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -33,6 +33,7 @@ #include <libvirt/libvirt.h> #endif +#include "glthread/lock.h" #include "hash.h" #include "guestfs-internal-frontend.h" @@ -365,6 +366,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 + * protects the handle. + */ + gl_recursive_lock_define (, lock); + /**** Configuration of the handle. ****/ bool verbose; /* Debugging. */ bool trace; /* Trace calls. */ diff --git a/src/handle.c b/src/handle.c index 51b9572..a057475 100644 --- a/src/handle.c +++ b/src/handle.c @@ -84,6 +84,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; @@ -167,6 +169,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; } @@ -389,6 +392,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.3.1
Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 2/5] threads: Acquire and release the lock around each public guestfs_* API.
Acquire the per-handle lock on entering each public API function. The lock is released by a cleanup handler, so we only need to use the ACQUIRE_LOCK macro at the top of each function. Although this looks a bit odd, it makes it easy to write reliable code. Note this means we require __attribute__((cleanup)). On platforms where this is not supported, the code will probably hang whenever a libguestfs function is called. The only definitive list of public APIs is found indirectly in the generator (in generator/c.ml : globals). --- generator/c.ml | 4 ++++ src/cleanup.c | 10 +++++++++- src/errors.c | 8 ++++++++ src/events.c | 8 ++++++++ src/guestfs-internal-frontend.h | 4 ++++ src/guestfs-internal.h | 8 ++++++++ src/handle.c | 17 ++++++++++++++++- src/private-data.c | 7 +++++++ 8 files changed, 64 insertions(+), 2 deletions(-) diff --git a/generator/c.ml b/generator/c.ml index a2b9c94..d19e2b4 100644 --- a/generator/c.ml +++ b/generator/c.ml @@ -1565,6 +1565,7 @@ and generate_client_actions hash () ~dll_public:true c_name style; pr "{\n"; + pr " ACQUIRE_LOCK (g);\n"; handle_null_optargs optargs c_name; @@ -1651,6 +1652,7 @@ and generate_client_actions hash () c_name style; pr "{\n"; + pr " ACQUIRE_LOCK (g);\n"; handle_null_optargs optargs c_name; @@ -1998,6 +2000,7 @@ and generate_client_actions_variants () ~handle:"g" ~prefix:"guestfs_" ~suffix:"_va" ~optarg_proto:VA c_name style; pr "{\n"; + pr " ACQUIRE_LOCK (g);\n"; pr " struct guestfs_%s_argv optargs_s;\n" c_name; pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" c_name; pr " int i;\n"; @@ -2055,6 +2058,7 @@ and generate_client_actions_variants () ~handle:"g" ~prefix:"guestfs_" name (ret, args, []); pr "{\n"; + pr " ACQUIRE_LOCK (g);\n"; pr " struct guestfs_%s_opts_argv optargs_s = { .bitmask = 0 };\n" name; pr " struct guestfs_%s_opts_argv *optargs = &optargs_s;\n" name; pr "\n"; diff --git a/src/cleanup.c b/src/cleanup.c index 71c26ec..3230563 100644 --- a/src/cleanup.c +++ b/src/cleanup.c @@ -1,5 +1,5 @@ /* libguestfs - * Copyright (C) 2013 Red Hat Inc. + * Copyright (C) 2013-2015 Red Hat Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -28,6 +28,7 @@ #include <libxml/xpath.h> #include <libxml/xmlwriter.h> +#include "glthread/lock.h" #include "hash.h" #include "guestfs.h" @@ -140,3 +141,10 @@ guestfs_int_cleanup_pclose (void *ptr) if (f) pclose (f); } + +void +guestfs_int_cleanup_gl_recursive_lock_unlock (void *ptr) +{ + gl_recursive_lock_t *lockp = * (gl_recursive_lock_t **) ptr; + gl_recursive_lock_unlock (*lockp); +} diff --git a/src/errors.c b/src/errors.c index 2d3ae84..629fb7c 100644 --- a/src/errors.c +++ b/src/errors.c @@ -32,12 +32,14 @@ const char * guestfs_last_error (guestfs_h *g) { + ACQUIRE_LOCK (g); return g->last_error; } int guestfs_last_errno (guestfs_h *g) { + ACQUIRE_LOCK (g); return g->last_errnum; } @@ -164,12 +166,14 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...) void guestfs_set_out_of_memory_handler (guestfs_h *g, guestfs_abort_cb cb) { + ACQUIRE_LOCK (g); g->abort_cb = cb; } guestfs_abort_cb guestfs_get_out_of_memory_handler (guestfs_h *g) { + ACQUIRE_LOCK (g); return g->abort_cb; } @@ -177,6 +181,7 @@ void guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data) { + ACQUIRE_LOCK (g); g->error_cb = cb; g->error_cb_data = data; } @@ -184,6 +189,7 @@ guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **data_rtn) { + ACQUIRE_LOCK (g); if (data_rtn) *data_rtn = g->error_cb_data; return g->error_cb; } @@ -192,6 +198,7 @@ void guestfs_push_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data) { + ACQUIRE_LOCK (g); struct error_cb_stack *old_stack; old_stack = g->error_cb_stack; @@ -206,6 +213,7 @@ guestfs_push_error_handler (guestfs_h *g, void guestfs_pop_error_handler (guestfs_h *g) { + ACQUIRE_LOCK (g); struct error_cb_stack *next_stack; if (g->error_cb_stack) { diff --git a/src/events.c b/src/events.c index 51b9948..ba16bad 100644 --- a/src/events.c +++ b/src/events.c @@ -39,6 +39,7 @@ guestfs_set_event_callback (guestfs_h *g, int flags, void *opaque) { + ACQUIRE_LOCK (g); int event_handle; if (flags != 0) { @@ -73,6 +74,8 @@ guestfs_set_event_callback (guestfs_h *g, void guestfs_delete_event_callback (guestfs_h *g, int event_handle) { + ACQUIRE_LOCK (g); + if (event_handle < 0 || event_handle >= (int) g->nr_events) return; @@ -295,6 +298,7 @@ void guestfs_set_log_message_callback (guestfs_h *g, guestfs_log_message_cb cb, void *opaque) { + ACQUIRE_LOCK (g); replace_old_style_event_callback (g, log_message_callback_wrapper, GUESTFS_EVENT_APPLIANCE, opaque, cb); @@ -317,6 +321,7 @@ void guestfs_set_subprocess_quit_callback (guestfs_h *g, guestfs_subprocess_quit_cb cb, void *opaque) { + ACQUIRE_LOCK (g); replace_old_style_event_callback (g, subprocess_quit_callback_wrapper, GUESTFS_EVENT_SUBPROCESS_QUIT, opaque, cb); @@ -339,6 +344,7 @@ void guestfs_set_launch_done_callback (guestfs_h *g, guestfs_launch_done_cb cb, void *opaque) { + ACQUIRE_LOCK (g); replace_old_style_event_callback (g, launch_done_callback_wrapper, GUESTFS_EVENT_LAUNCH_DONE, opaque, cb); @@ -361,6 +367,7 @@ void guestfs_set_close_callback (guestfs_h *g, guestfs_close_cb cb, void *opaque) { + ACQUIRE_LOCK (g); replace_old_style_event_callback (g, close_callback_wrapper, GUESTFS_EVENT_CLOSE, opaque, cb); @@ -384,6 +391,7 @@ void guestfs_set_progress_callback (guestfs_h *g, guestfs_progress_cb cb, void *opaque) { + ACQUIRE_LOCK (g); replace_old_style_event_callback (g, progress_callback_wrapper, GUESTFS_EVENT_PROGRESS, opaque, cb); diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h index 9322201..295ccbe 100644 --- a/src/guestfs-internal-frontend.h +++ b/src/guestfs-internal-frontend.h @@ -57,6 +57,8 @@ __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeObject))) #define CLEANUP_FCLOSE __attribute__((cleanup(guestfs_int_cleanup_fclose))) #define CLEANUP_PCLOSE __attribute__((cleanup(guestfs_int_cleanup_pclose))) +#define CLEANUP_GL_RECURSIVE_LOCK_UNLOCK \ + __attribute__((cleanup(guestfs_int_cleanup_gl_recursive_lock_unlock))) #else #define CLEANUP_FREE #define CLEANUP_FREE_STRING_LIST @@ -70,6 +72,7 @@ #define CLEANUP_XMLXPATHFREEOBJECT #define CLEANUP_FCLOSE #define CLEANUP_PCLOSE +/* XXX no safe equivalent to CLEANUP_GL_RECURSIVE_LOCK_UNLOCK */ #endif /* NB: At some point we will stop exporting these safe_* allocation @@ -122,6 +125,7 @@ extern void guestfs_int_cleanup_xmlXPathFreeContext (void *ptr); extern void guestfs_int_cleanup_xmlXPathFreeObject (void *ptr); extern void guestfs_int_cleanup_fclose (void *ptr); extern void guestfs_int_cleanup_pclose (void *ptr); +extern void guestfs_int_cleanup_gl_recursive_lock_unlock (void *ptr); /* These are in a separate header so the header can be generated. * Don't include the following file directly: diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index b68942f..be77459 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -58,6 +58,14 @@ #define TRACE4(name, arg1, arg2, arg3, arg4) #endif +/* Acquire and release the per-handle lock. Note the release happens + * in an __attribute__((cleanup)) handler, making it simple to write + * bug-free code. + */ +#define ACQUIRE_LOCK(g) \ + CLEANUP_GL_RECURSIVE_LOCK_UNLOCK gl_recursive_lock_t *_lock = &(g)->lock; \ + gl_recursive_lock_lock (*_lock) + /* Default and minimum appliance memory size. */ /* Needs to be larger on ppc64 because of the larger page size (64K). diff --git a/src/handle.c b/src/handle.c index a057475..424b130 100644 --- a/src/handle.c +++ b/src/handle.c @@ -316,6 +316,7 @@ guestfs_close (guestfs_h *g) { struct hv_param *hp, *hp_next; guestfs_h **gg; + int r; if (g->state == NO_HANDLE) { /* Not safe to call ANY callbacks here, so ... */ @@ -392,7 +393,21 @@ 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); + r = glthread_recursive_lock_destroy (&g->lock); + if (r != 0) { + /* If pthread_mutex_destroy returns 16 (EBUSY), this indicates + * that the lock is held somewhere. That means a programming + * error if the main program is using threads. + */ + errno = r; + perror ("guestfs_close: g->lock"); + /* While we're debugging locks in libguestfs I want this to fail + * noisily. Remove this later since there are valid times when + * this might fail such as if the program exits during a + * libguestfs operation. + */ + abort (); + } free (g); } diff --git a/src/private-data.c b/src/private-data.c index 725b74b..c23dd86 100644 --- a/src/private-data.c +++ b/src/private-data.c @@ -66,6 +66,7 @@ freer (void *x) void guestfs_set_private (guestfs_h *g, const char *key, void *data) { + ACQUIRE_LOCK (g); struct pda_entry *new_entry, *old_entry, *entry; if (g->pda == NULL) { @@ -90,6 +91,8 @@ guestfs_set_private (guestfs_h *g, const char *key, void *data) void * guestfs_get_private (guestfs_h *g, const char *key) { + ACQUIRE_LOCK (g); + if (g->pda == NULL) return NULL; /* no keys have been set */ @@ -105,6 +108,8 @@ guestfs_get_private (guestfs_h *g, const char *key) void * guestfs_first_private (guestfs_h *g, const char **key_rtn) { + ACQUIRE_LOCK (g); + if (g->pda == NULL) return NULL; @@ -124,6 +129,8 @@ guestfs_first_private (guestfs_h *g, const char **key_rtn) void * guestfs_next_private (guestfs_h *g, const char **key_rtn) { + ACQUIRE_LOCK (g); + if (g->pda == NULL) return NULL; -- 2.3.1
Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 3/5] threads: Use thread-local storage for errors.
We permit the following constructs in libguestfs code: if (guestfs_some_call (g) == -1) { fprintf (stderr, "failed: error is %s\n", guestfs_last_error (g)); } and: guestfs_push_error_handler (g, NULL, NULL); guestfs_some_call (g); guestfs_pop_error_handler (g); Neither of these would be safe if we allowed the handle to be used from threads concurrently, since the error string or error handler could be changed by another thread. Solve this in approximately the same way that libvirt does: by making the error, current error handler, and stack of error handlers use thread-local storage (TLS). The implementation is not entirely straightforward, mainly because POSIX doesn't give us useful destructor behaviour, so effectively we end up creating our own destructor using a linked list. Note that you have to set the error handler in each thread separately, which is an API change (eg: if you set the error handler in one thread, then pass the handle 'g' to another thread, the error handler in the second thread appears to have reset itself back to the default error handler). I haven't yet worked out a better way to solve this. --- bootstrap | 1 + m4/.gitignore | 1 + src/errors.c | 196 ++++++++++++++++++++++++++++++++++++++++--------- src/guestfs-internal.h | 23 +++--- src/handle.c | 11 +-- 5 files changed, 180 insertions(+), 52 deletions(-) diff --git a/bootstrap b/bootstrap index 5df6f0f..7733f8f 100755 --- a/bootstrap +++ b/bootstrap @@ -91,6 +91,7 @@ strndup symlinkat sys_select sys_wait +tls vasprintf vc-list-files warnings diff --git a/m4/.gitignore b/m4/.gitignore index eff909a..9a26217 100644 --- a/m4/.gitignore +++ b/m4/.gitignore @@ -241,6 +241,7 @@ /thread.m4 /time_h.m4 /timespec.m4 +/tls.m4 /ttyname_r.m4 /uintmax_t.m4 /ulonglong.m4 diff --git a/src/errors.c b/src/errors.c index 629fb7c..49921ed 100644 --- a/src/errors.c +++ b/src/errors.c @@ -29,26 +29,150 @@ #include "guestfs.h" #include "guestfs-internal.h" +/* How errors and error handlers works in the handle: + * + * The handle has a g->error_data field which is a thread-local + * storage (TLS) key. + * + * We use TLS because we want to support the common idioms of: + * if (guestfs_foo (g) == -1) + * printf ("%s\n", guestfs_last_error (g)); + * and: + * guestfs_push_error_handler (g, ...); + * guestfs_foo (g); + * guestfs_pop_error_handler (g); + * neither of which would ordinarily be safe when using the same + * handle from multiple threads. + * + * In each thread, the TLS data is either NULL or contains a pointer + * to a 'struct error_data'. + * + * When it is NULL, it means the stack is empty (in that thread) and + * the default handler (default_error_cb) is installed. + * + * As soon as the current thread calls guestfs_set_error_handler, + * guestfs_push_error_handler, or an error is set in the handle (calls + * like guestfs_int_perrorf and so on), the key is created and + * initialized with a pointer to a real 'struct error_data'. + * + * All the 'struct error_data' structures associated with one handle + * are linked together in a linked list, so that we are able to free + * them when the handle is closed. (The pthread_key* API doesn't give + * us any other way to do this, in particular pthread_key_destroy + * doesn't call the destructor associated with the key). + */ + +static void default_error_cb (guestfs_h *g, void *data, const char *msg); + +/* Stack of old error handlers. */ +struct error_cb_stack { + struct error_cb_stack *next; + guestfs_error_handler_cb error_cb; + void * error_cb_data; +}; + +/* Error data, stored in thread-local storage in g->error_data key. */ +struct error_data { + /* Linked list of error_data structs allocated for this handle. */ + struct error_data *next; + + char *last_error; /* Last error on handle. */ + int last_errnum; /* errno, or 0 if there was no errno */ + + /* Error handler and stack of old error handlers. */ + guestfs_error_handler_cb error_cb; + void * error_cb_data; + struct error_cb_stack *error_cb_stack; +}; + +static void +free_error_data (struct error_data *error_data) +{ + struct error_cb_stack *p, *next_p; + + free (error_data->last_error); + for (p = error_data->error_cb_stack; p != NULL; p = next_p) { + next_p = p->next; + free (p); + } + free (error_data); +} + +/* Free all the error_data structs created for a particular handle. */ +void +guestfs_int_free_error_data_list (guestfs_h *g) +{ + struct error_data *p, *next_p; + + gl_lock_lock (g->error_data_list_lock); + + for (p = g->error_data_list; p != NULL; p = next_p) { + next_p = p->next; + free_error_data (p); + } + + g->error_data_list = NULL; + + gl_lock_unlock (g->error_data_list_lock); +} + +/* Get thread-specific error_data struct. Create it if necessary. */ +static struct error_data * +get_error_data (guestfs_h *g) +{ + struct error_data *ret; + + ret = gl_tls_get (g->error_data); + + /* Not allocated yet for this thread, so allocate one. */ + if (ret == NULL) { + ret = safe_malloc (g, sizeof *ret); + ret->last_error = NULL; + ret->last_errnum = 0; + ret->error_cb = default_error_cb; + ret->error_cb_data = NULL; + ret->error_cb_stack = NULL; + + /* Add it to the linked list of struct error_data that are + * associated with this handle, so we can free them when the + * handle is closed. + */ + gl_lock_lock (g->error_data_list_lock); + ret->next = g->error_data_list; + g->error_data_list = ret; + gl_lock_unlock (g->error_data_list_lock); + + /* Set the TLS to point to the struct. This is safe because we + * should have acquired the handle lock. + */ + gl_tls_set (g->error_data, ret); + } + + return ret; +} + const char * guestfs_last_error (guestfs_h *g) { ACQUIRE_LOCK (g); - return g->last_error; + return get_error_data (g)->last_error; } int guestfs_last_errno (guestfs_h *g) { ACQUIRE_LOCK (g); - return g->last_errnum; + return get_error_data (g)->last_errnum; } static void set_last_error (guestfs_h *g, int errnum, const char *msg) { - free (g->last_error); - g->last_error = strdup (msg); - g->last_errnum = errnum; + struct error_data *error_data = get_error_data (g); + + free (error_data->last_error); + error_data->last_error = strdup (msg); + error_data->last_errnum = errnum; } /* Warning are printed unconditionally. We try to make these rare. @@ -121,6 +245,7 @@ guestfs_int_error_errno (guestfs_h *g, int errnum, const char *fs, ...) va_list args; CLEANUP_FREE char *msg = NULL; int err; + struct error_data *error_data = get_error_data (g); va_start (args, fs); err = vasprintf (&msg, fs, args); @@ -132,7 +257,8 @@ guestfs_int_error_errno (guestfs_h *g, int errnum, const char *fs, ...) * message and errno through the handle if it wishes. */ set_last_error (g, errnum, msg); - if (g->error_cb) g->error_cb (g, g->error_cb_data, msg); + if (error_data->error_cb) + error_data->error_cb (g, error_data->error_cb_data, msg); } void @@ -143,6 +269,7 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...) int errnum = errno; int err; char buf[256]; + struct error_data *error_data = get_error_data (g); va_start (args, fs); err = vasprintf (&msg, fs, args); @@ -160,7 +287,8 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...) * message and errno through the handle if it wishes. */ set_last_error (g, errnum, msg); - if (g->error_cb) g->error_cb (g, g->error_cb_data, msg); + if (error_data->error_cb) + error_data->error_cb (g, error_data->error_cb_data, msg); } void @@ -182,16 +310,21 @@ guestfs_set_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data) { ACQUIRE_LOCK (g); - g->error_cb = cb; - g->error_cb_data = data; + struct error_data *error_data; + + error_data = get_error_data (g); + error_data->error_cb = cb; + error_data->error_cb_data = data; } guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *g, void **data_rtn) { ACQUIRE_LOCK (g); - if (data_rtn) *data_rtn = g->error_cb_data; - return g->error_cb; + struct error_data *error_data = get_error_data (g); + + if (data_rtn) *data_rtn = error_data->error_cb_data; + return error_data->error_cb; } void @@ -199,13 +332,15 @@ guestfs_push_error_handler (guestfs_h *g, guestfs_error_handler_cb cb, void *data) { ACQUIRE_LOCK (g); + struct error_data *error_data; struct error_cb_stack *old_stack; - old_stack = g->error_cb_stack; - g->error_cb_stack = safe_malloc (g, sizeof (struct error_cb_stack)); - g->error_cb_stack->next = old_stack; - g->error_cb_stack->error_cb = g->error_cb; - g->error_cb_stack->error_cb_data = g->error_cb_data; + error_data = get_error_data (g); + old_stack = error_data->error_cb_stack; + error_data->error_cb_stack = safe_malloc (g, sizeof (struct error_cb_stack)); + error_data->error_cb_stack->next = old_stack; + error_data->error_cb_stack->error_cb = error_data->error_cb; + error_data->error_cb_stack->error_cb_data = error_data->error_cb_data; guestfs_set_error_handler (g, cb, data); } @@ -214,26 +349,21 @@ void guestfs_pop_error_handler (guestfs_h *g) { ACQUIRE_LOCK (g); + struct error_data *error_data; struct error_cb_stack *next_stack; - if (g->error_cb_stack) { - next_stack = g->error_cb_stack->next; - guestfs_set_error_handler (g, g->error_cb_stack->error_cb, - g->error_cb_stack->error_cb_data); - free (g->error_cb_stack); - g->error_cb_stack = next_stack; + error_data = get_error_data (g); + if (error_data->error_cb_stack) { + next_stack = error_data->error_cb_stack->next; + guestfs_set_error_handler (g, error_data->error_cb_stack->error_cb, + error_data->error_cb_stack->error_cb_data); + free (error_data->error_cb_stack); + error_data->error_cb_stack = next_stack; + } + else { + error_data->error_cb = default_error_cb; + error_data->error_cb_data = NULL; } - else - guestfs_int_init_error_handler (g); -} - -static void default_error_cb (guestfs_h *g, void *data, const char *msg); - -void -guestfs_int_init_error_handler (guestfs_h *g) -{ - g->error_cb = default_error_cb; - g->error_cb_data = NULL; } static void diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index be77459..51cc02a 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -34,6 +34,7 @@ #endif #include "glthread/lock.h" +#include "glthread/tls.h" #include "hash.h" #include "guestfs-internal-frontend.h" @@ -361,13 +362,6 @@ struct connection_ops { int (*can_read_data) (guestfs_h *g, struct connection *); }; -/* Stack of old error handlers. */ -struct error_cb_stack { - struct error_cb_stack *next; - guestfs_error_handler_cb error_cb; - void * error_cb_data; -}; - /* The libguestfs handle. */ struct guestfs_h { @@ -433,9 +427,6 @@ struct guestfs_h char **backend_settings; /* Backend settings (can be NULL). */ /**** Runtime information. ****/ - char *last_error; /* Last error on handle. */ - int last_errnum; /* errno, or 0 if there was no errno */ - /* Temporary and cache directories. */ /* The actual temporary directory - this is not created with the * handle, you have to call guestfs_int_lazy_make_tmpdir. @@ -447,9 +438,13 @@ struct guestfs_h char *int_cachedir; /* $LIBGUESTFS_CACHEDIR or guestfs_set_cachedir or NULL */ /* Error handler, plus stack of old error handlers. */ - guestfs_error_handler_cb error_cb; - void * error_cb_data; - struct error_cb_stack *error_cb_stack; + gl_tls_key_t error_data; + + /* Linked list of error_data structures allocated for this handle, + * plus a mutex to protect the linked list. + */ + gl_lock_define (, error_data_list_lock); + struct error_data *error_data_list; /* Out of memory error handler. */ guestfs_abort_cb abort_cb; @@ -628,7 +623,7 @@ struct guestfs_progress; extern int guestfs_int_get_backend_setting_bool (guestfs_h *g, const char *name); /* errors.c */ -extern void guestfs_int_init_error_handler (guestfs_h *g); +extern void guestfs_int_free_error_data_list (guestfs_h *g); extern void guestfs_int_error_errno (guestfs_h *g, int errnum, const char *fs, ...) __attribute__((format (printf,3,4))); diff --git a/src/handle.c b/src/handle.c index 424b130..d61ade3 100644 --- a/src/handle.c +++ b/src/handle.c @@ -32,6 +32,7 @@ #include <libxml/xmlversion.h> #include "glthread/lock.h" +#include "glthread/tls.h" #include "ignore-value.h" #include "guestfs.h" @@ -90,7 +91,7 @@ guestfs_create_flags (unsigned flags, ...) g->conn = NULL; - guestfs_int_init_error_handler (g); + gl_tls_key_init (g->error_data, NULL); g->abort_cb = abort; g->recovery_proc = 1; @@ -169,6 +170,8 @@ guestfs_create_flags (unsigned flags, ...) free (g->path); free (g->hv); free (g->append); + guestfs_int_free_error_data_list (g); + gl_tls_key_destroy (g->error_data); gl_recursive_lock_destroy (g->lock); free (g); return NULL; @@ -376,16 +379,12 @@ guestfs_close (guestfs_h *g) free (hp); } - while (g->error_cb_stack) - guestfs_pop_error_handler (g); - if (g->pda) hash_free (g->pda); free (g->tmpdir); free (g->env_tmpdir); free (g->int_tmpdir); free (g->int_cachedir); - free (g->last_error); free (g->program); free (g->path); free (g->hv); @@ -393,6 +392,8 @@ guestfs_close (guestfs_h *g) free (g->backend_data); guestfs_int_free_string_list (g->backend_settings); free (g->append); + guestfs_int_free_error_data_list (g); + gl_tls_key_destroy (g->error_data); r = glthread_recursive_lock_destroy (&g->lock); if (r != 0) { /* If pthread_mutex_destroy returns 16 (EBUSY), this indicates -- 2.3.1
Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 4/5] threads: Update documentation in guestfs(3) to describe the new behaviour.
--- src/guestfs.pod | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/guestfs.pod b/src/guestfs.pod index 5ceef92..2384a1d 100644 --- a/src/guestfs.pod +++ b/src/guestfs.pod @@ -1319,14 +1319,31 @@ encrypted devices. All high-level libguestfs actions are synchronous. If you want to use libguestfs asynchronously then you must create a thread. -Only use the handle from a single thread. Either use the handle -exclusively from one thread, or provide your own mutex so that two -threads cannot issue calls on the same handle at the same time. Even -apparently innocent functions like L</guestfs_get_trace> are I<not> -safe to be called from multiple threads without a mutex. - -See the graphical program guestfs-browser for one possible -architecture for multithreaded programs using libvirt and libguestfs. +=head3 Threads in libguestfs E<ge> 1.30 + +In libguestfs E<ge> 1.30, each handle (C<guestfs_h>) contains a lock +which is acquired automatically when you call a libguestfs function. +The practical effect of this is you can call libguestfs functions with +the same handle from multiple threads without needing to do any +locking. + +Also in libguestfs E<ge> 1.30, the last error on the handle +(L</guestfs_last_error>, L</guestfs_last_errno>) is stored in +thread-local storage, so it is safe to write code like: + + if (guestfs_add_drive_ro (g, drive) == -1) + fprintf (stderr, "error was: %s\n", guestfs_last_error (g)); + +even when other threads may be concurrently using the same handle C<g>. + +=head3 Threads in libguestfs E<lt> 1.30 + +In libguestfs E<lt> 1.30, you must use the handle only from a single +thread. Either use the handle exclusively from one thread, or provide +your own mutex so that two threads cannot issue calls on the same +handle at the same time. Even apparently innocent functions like +L</guestfs_get_trace> are I<not> safe to be called from multiple +threads without a mutex in libguestfs E<lt> 1.30. =head2 PATH -- 2.3.1
Richard W.M. Jones
2015-Jun-16 16:02 UTC
[Libguestfs] [PATCH threads v2 5/5] threads: Add a test.
--- .gitignore | 1 + tests/c-api/Makefile.am | 20 ++++++- tests/c-api/test-threads.c | 133 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 tests/c-api/test-threads.c diff --git a/.gitignore b/.gitignore index 6f14915..22ab687 100644 --- a/.gitignore +++ b/.gitignore @@ -509,6 +509,7 @@ Makefile.in /tests/c-api/test-pwd /tests/c-api/tests /tests/c-api/tests.c +/tests/c-api/test-threads /tests/c-api/test*.tmp /tests/c-api/test-user-cancel /tests/charsets/test-charset-fidelity diff --git a/tests/c-api/Makefile.am b/tests/c-api/Makefile.am index 7bfffe5..55e82b2 100644 --- a/tests/c-api/Makefile.am +++ b/tests/c-api/Makefile.am @@ -37,7 +37,8 @@ check_PROGRAMS = \ test-debug-to-file \ test-environment \ test-pwd \ - test-event-string + test-event-string \ + test-threads if HAVE_LIBDL check_PROGRAMS += \ test-dlopen @@ -55,7 +56,8 @@ TESTS = \ test-user-cancel \ test-debug-to-file \ test-environment \ - test-event-string + test-event-string \ + test-threads if HAVE_LIBDL TESTS += \ test-dlopen @@ -238,6 +240,20 @@ test_event_string_LDADD = \ $(LTLIBINTL) \ $(top_builddir)/gnulib/lib/libgnu.la +test_threads_SOURCES = test-threads.c +test_threads_CPPFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + -I$(top_srcdir)/gnulib/lib \ + -I$(top_builddir)/gnulib/lib +test_threads_CFLAGS = \ + -pthread \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +test_threads_LDADD = \ + $(top_builddir)/src/libguestfs.la \ + $(LTLIBTHREAD) \ + $(LTLIBINTL) \ + $(top_builddir)/gnulib/lib/libgnu.la + if HAVE_LIBVIRT test_add_libvirt_dom_SOURCES = test-add-libvirt-dom.c test_add_libvirt_dom_CPPFLAGS = \ diff --git a/tests/c-api/test-threads.c b/tests/c-api/test-threads.c new file mode 100644 index 0000000..218080f --- /dev/null +++ b/tests/c-api/test-threads.c @@ -0,0 +1,133 @@ +/* libguestfs + * Copyright (C) 2015 Red Hat Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* Test that we can make API calls safely from multiple threads. */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <assert.h> + +#include <pthread.h> + +#include "guestfs.h" +#include "guestfs-internal-frontend.h" + +static guestfs_h *g; + +#define RUN_TIME 60 /* seconds */ +#define NR_CONCURRENT_THREADS 4 + +static void *start_thread (void *nullv); + +int +main (int argc, char *argv[]) +{ + time_t start_t, t; + pthread_t threads[NR_CONCURRENT_THREADS]; + void *ret; + int i, r; + + /* Because we rely on error message content below, force LC_ALL=C. */ + setenv ("LC_ALL", "C", 1); + + g = guestfs_create (); + if (!g) { + perror ("guestfs_create"); + exit (EXIT_FAILURE); + } + + time (&start_t); + + while (time (&t), t - start_t < RUN_TIME) { + for (i = 0; i < NR_CONCURRENT_THREADS; ++i) { + r = pthread_create (&threads[i], NULL, start_thread, NULL); + if (r != 0) { + fprintf (stderr, "pthread_create: %s\n", strerror (r)); + exit (EXIT_FAILURE); + } + } + + for (i = 0; i < NR_CONCURRENT_THREADS; ++i) { + r = pthread_join (threads[i], &ret); + if (r != 0) { + fprintf (stderr, "pthread_join: %s\n", strerror (r)); + exit (EXIT_FAILURE); + } + if (ret != NULL) { + fprintf (stderr, "thread[%d] failed\n", i); + exit (EXIT_FAILURE); + } + } + } + + guestfs_close (g); + + exit (EXIT_SUCCESS); +} + +static void * +start_thread (void *nullv) +{ + char *p; + const char *err; + int iterations; + + for (iterations = 0; iterations < 1000; ++iterations) { + guestfs_set_hv (g, "test"); + p = guestfs_get_hv (g); + if (!p || STRNEQ (p, "test")) { + fprintf (stderr, "invalid return from guestfs_get_hv\n"); + pthread_exit ((void *)-1); + } + free (p); + + guestfs_push_error_handler (g, NULL, NULL); + guestfs_set_hv (g, "test"); + p = guestfs_get_hv (g); + guestfs_pop_error_handler (g); + if (!p || STRNEQ (p, "test")) { + fprintf (stderr, "invalid return from guestfs_get_hv\n"); + pthread_exit ((void *)-1); + } + free (p); + + guestfs_push_error_handler (g, NULL, NULL); + guestfs_set_program (g, NULL); /* deliberately cause an error */ + guestfs_pop_error_handler (g); + err = guestfs_last_error (g); + if (!err || !STRPREFIX (err, "set_program: program: ")) { + fprintf (stderr, "invalid error message: %s\n", err ? err : "NULL"); + pthread_exit ((void *)-1); + } + + guestfs_push_error_handler (g, NULL, NULL); + guestfs_set_memsize (g, 1); /* deliberately cause an error */ + guestfs_pop_error_handler (g); + err = guestfs_last_error (g); + if (!err || strstr (err, "memsize") == NULL) { + fprintf (stderr, "invalid error message: %s\n", err ? err : "NULL"); + pthread_exit ((void *)-1); + } + } + + pthread_exit (NULL); +} -- 2.3.1
Reasonably Related Threads
- [PATCH 0/5] Add support for thread-safe handle.
- [PATCH v3 0/5] threads: Add support for thread-safe handle.
- [PATCH v3 REPOST 0/5] threads: Add support for thread-safe handle.
- [PATCH 3/5] threads: Use thread-local storage for errors.
- [PATCH v3 3/5] threads: Use thread-local storage for errors.