1 /* 2 * Thread Exerciser 3 * 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. 8 * 9 * Based on the original test case by Nikolay Igotti. 10 * 11 * Copyright (c) 2020 Linaro Ltd 12 * 13 * SPDX-License-Identifier: GPL-2.0-or-later 14 */ 15 16 #include <stdint.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <unistd.h> 20 #include <pthread.h> 21 22 int max_threads = 10; 23 24 typedef struct { 25 int delay; 26 } ThreadArg; 27 thread_fn(void * varg)28static void *thread_fn(void* varg) 29 { 30 ThreadArg *arg = varg; 31 usleep(arg->delay); 32 free(arg); 33 return NULL; 34 } 35 main(int argc,char ** argv)36int main(int argc, char **argv) 37 { 38 int i; 39 pthread_t *threads; 40 41 if (argc > 1) { 42 max_threads = atoi(argv[1]); 43 } 44 threads = calloc(sizeof(pthread_t), max_threads); 45 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); 50 } 51 52 printf("Created %d threads\n", max_threads); 53 54 /* sleep until roughly half the threads have "finished" */ 55 usleep(max_threads * 50); 56 57 for (i = 0; i < max_threads; i++) { 58 pthread_join(threads[i], NULL); 59 } 60 61 printf("Done\n"); 62 63 return 0; 64 } 65