Add the thread identifier to various multi-threaded programs and tests.

This commit is contained in:
Richard W.M. Jones
2015-10-07 18:01:09 +01:00
parent f6e13aeb65
commit f291411f76
5 changed files with 21 additions and 0 deletions

View File

@@ -149,6 +149,7 @@ worker_thread (void *thread_data_vp)
size_t output_len = 0;
guestfs_h *g;
int err;
char id[64];
/* Take the next domain from the list. */
if (thread_data->verbose)
@@ -191,6 +192,11 @@ worker_thread (void *thread_data_vp)
return &thread_data->r;
}
/* Set the handle identifier so we can tell threads apart. */
snprintf (id, sizeof id, "thread_%zu_domain_%zu",
thread_data->thread_num, i);
guestfs_set_identifier (g, id);
/* Copy some settings from the options guestfs handle. */
guestfs_set_trace (g, thread_data->trace);
guestfs_set_verbose (g, thread_data->verbose);

View File

@@ -1328,6 +1328,9 @@ 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.
Use L</guestfs_set_identifier> to make it simpler to identify threads
in trace output.
=head2 PATH
Libguestfs needs a supermin appliance, which it finds by looking along

View File

@@ -177,6 +177,8 @@ start_thread (void *statevp)
pthread_exit (&state->exit_status);
}
guestfs_set_identifier (g, state->mp);
if (guestfs_add_drive_scratch (g, 512*1024*1024, -1) == -1)
goto error;
if (guestfs_launch (g) == -1)

View File

@@ -43,6 +43,7 @@
#define NR_THREADS 5
struct thread_state {
size_t thread_num; /* Thread number. */
pthread_t thread; /* Thread handle. */
int exit_status; /* Thread exit status. */
};
@@ -89,6 +90,7 @@ main (int argc, char *argv[])
sigaction (SIGQUIT, &sa, NULL);
for (i = 0; i < NR_THREADS; ++i) {
threads[i].thread_num = i;
/* Start the thread. */
r = pthread_create (&threads[i].thread, NULL, start_thread,
&threads[i]);
@@ -117,6 +119,7 @@ start_thread (void *statevp)
struct thread_state *state = statevp;
guestfs_h *g;
time_t start_t, t;
char id[64];
time (&start_t);
@@ -133,6 +136,9 @@ start_thread (void *statevp)
pthread_exit (&state->exit_status);
}
snprintf (id, sizeof id, "%zu", state->thread_num);
guestfs_set_identifier (g, id);
if (guestfs_add_drive_opts (g, "/dev/null",
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,

View File

@@ -199,6 +199,7 @@ start_thread (void *thread_data_vp)
size_t i;
guestfs_h *g;
unsigned errors = 0;
char id[64];
for (;;) {
/* Take the next process. */
@@ -237,6 +238,9 @@ start_thread (void *thread_data_vp)
goto error;
}
snprintf (id, sizeof id, "%zu", i);
guestfs_set_identifier (g, id);
guestfs_set_trace (g, trace);
guestfs_set_verbose (g, verbose);