Re: [Libguestfs] [PATCH v3 0/5] threads: Add support for thread-safe handle.

Richard W.M. Jones
Tue, 11 Jul 2017 10:31:04 -0000

On Mon, Jul 10, 2017 at 01:52:59PM +0200, Pino Toscano
wrote:
> On Tuesday, 27 June 2017 13:55:54 CEST Richard W.M. Jones wrote:
> > Previously posted in 2015:
> > v1: https://www.redhat.com/archives/libguestfs/2015-June/msg00048.html
> > v2: https://www.redhat.com/archives/libguestfs/2015-June/msg00118.html
> >
> > I have rebased and tidied up the patches, fixing a few spelling
> > mistakes, but they are broadly the same as before.  I also ran all the
> > tests, which pass.
> >
> > As with the previous versions, this makes a change to the API, where
> > you can no longer pass a handle between threads and expect
> > guestfs_last_error() to work.  I'm somewhat more sanguine about
this
> > change, since using the API like that is abstruse and no one should be
> > expecting that to have worked.
>
> A general question on this: does enabling locking for all the handle
> operation cause any noticeable performance issue in single-thread
> handle usages?

Because we're using the gnulib wrapper, if the program is not linked
to pthread then TLS is not used at all.  Instead a simple pointer is
substituted with the key.

However this is no longer a very realistic scenario.  You would have
to disable libvirt and probably other libraries, to be sure that
pthread is not being linked to your libguestfs-using program.

> Would it be worth making it opt-in somehow, so only users of the API
> that require this kind of thread-safety would enable it?

I would be surprised if it was noticable.  The overhead of calling
guestfs_set_verbose just to set an integer flag is already large, see
below.  I think making the API usable from threads without requiring
any thought is better.

Rich.

GUESTFS_DLL_PUBLIC int
guestfs_set_verbose (guestfs_h *g,
                     int verbose)
{
  int trace_flag = g->trace;
  struct trace_buffer trace_buffer;
  int r;

  // This function iterates i = 0 .. nr_events-1.
  guestfs_int_call_callbacks_message (g, GUESTFS_EVENT_ENTER,
                                      "set_verbose", 11);
  if (trace_flag) {
    guestfs_int_trace_open (&trace_buffer);
    fprintf (trace_buffer.fp, "%s", "set_verbose");
    fputs (verbose ? " true" : " false", trace_buffer.fp);
    guestfs_int_trace_send_line (g, &trace_buffer);
  }

  r = guestfs_impl_set_verbose (g, verbose);

  if (r != -1) {
    if (trace_flag) {
      guestfs_int_trace_open (&trace_buffer);
      fprintf (trace_buffer.fp, "%s = ", "set_verbose");
      fprintf (trace_buffer.fp, "%d", r);
      guestfs_int_trace_send_line (g, &trace_buffer);
    }

  } else {
    if (trace_flag)
      guestfs_int_trace (g, "%s = %s (error)",
                         "set_verbose", "-1");
  }

  return r;
}



--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.