File: test-threads.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (133 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* libguestfs
 * Copyright (C) 2015-2020 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-utils.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);
}