Merge remote-tracking branch 'remotes/vivier/tags/m68k-for-6.0-pull-request' into...
[qemu/ar7.git] / tests / tcg / multiarch / threadcount.c
blob545a1c81466f4a9c6598b638793edcf2840b3d4b
1 /*
2 * Thread Exerciser
4 * Unlike testthread which is mainly concerned about testing thread
5 * semantics this test is used to exercise the thread creation and
6 * accounting. A version of this test found a problem with clashing
7 * cpu_indexes which caused a break in plugin handling.
9 * Based on the original test case by Nikolay Igotti.
11 * Copyright (c) 2020 Linaro Ltd
13 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <pthread.h>
22 int max_threads = 10;
24 typedef struct {
25 int delay;
26 } ThreadArg;
28 static void *thread_fn(void* varg)
30 ThreadArg *arg = varg;
31 usleep(arg->delay);
32 free(arg);
33 return NULL;
36 int main(int argc, char **argv)
38 int i;
39 pthread_t *threads;
41 if (argc > 1) {
42 max_threads = atoi(argv[1]);
44 threads = calloc(sizeof(pthread_t), max_threads);
46 for (i = 0; i < max_threads; i++) {
47 ThreadArg *arg = calloc(sizeof(ThreadArg), 1);
48 arg->delay = i * 100;
49 pthread_create(threads + i, NULL, thread_fn, arg);
52 printf("Created %d threads\n", max_threads);
54 /* sleep until roughly half the threads have "finished" */
55 usleep(max_threads * 50);
57 for (i = 0; i < max_threads; i++) {
58 pthread_join(threads[i], NULL);
61 printf("Done\n");
63 return 0;