lib: Initialize libvirt and libxml2 once when the library loads.

This commit is contained in:
Richard W.M. Jones
2012-07-23 21:26:18 +01:00
parent bf00be0e17
commit 941ec968b8
3 changed files with 36 additions and 15 deletions

View File

@@ -21,7 +21,6 @@
#include <pthread.h>
#include <guestfs.h>
#include <libvirt/libvirt.h>
struct threaddata {
const char *src;
@@ -66,9 +65,6 @@ main (int argc, char *argv[])
struct timeval start_t, end_t;
int64_t ms;
/* This is required when using libvirt from multiple threads. */
virInitialize ();
if (argc != 5) {
usage ();
exit (EXIT_FAILURE);

View File

@@ -61,6 +61,15 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#ifdef HAVE_LIBVIRT
#include <libvirt/libvirt.h>
#endif
#ifdef HAVE_LIBXML2
#include <libxml/parser.h>
#include <libxml/xmlversion.h>
#endif
#include "c-ctype.h"
#include "glthread/lock.h"
#include "hash.h"
@@ -80,6 +89,33 @@ gl_lock_define_initialized (static, handles_lock);
static guestfs_h *handles = NULL;
static int atexit_handler_set = 0;
gl_lock_define_initialized (static, init_lock);
/* No initialization is required by libguestfs, but libvirt and
* libxml2 require initialization if they might be called from
* multiple threads. Hence this constructor function which is called
* when libguestfs is first loaded.
*/
static void init_libguestfs (void) __attribute__((constructor));
static void
init_libguestfs (void)
{
#if defined(HAVE_LIBVIRT) || defined(HAVE_LIBXML2)
gl_lock_lock (init_lock);
#endif
#ifdef HAVE_LIBVIRT
virInitialize ();
#endif
#ifdef HAVE_LIBXML2
xmlInitParser ();
LIBXML_TEST_VERSION;
#endif
#if defined(HAVE_LIBVIRT) || defined(HAVE_LIBXML2)
gl_lock_unlock (init_lock);
#endif
}
guestfs_h *
guestfs_create (void)
{

View File

@@ -44,17 +44,6 @@
#if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2)
static void init_libxml2 (void) __attribute__((constructor));
static void
init_libxml2 (void)
{
/* I am told that you don't really need to call virInitialize ... */
xmlInitParser ();
LIBXML_TEST_VERSION;
}
static void
ignore_errors (void *ignore, virErrorPtr ignore2)
{