1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Test for callback thread safety.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci#include <pthread.h>
10f08c3bdfSopenharmony_ci#include "tst_test.h"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define THREADS 10
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic pthread_barrier_t barrier;
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic void setup(void)
17f08c3bdfSopenharmony_ci{
18f08c3bdfSopenharmony_ci	pthread_barrier_init(&barrier, NULL, THREADS);
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci	tst_res(TINFO, "setup() executed");
21f08c3bdfSopenharmony_ci}
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic void cleanup(void)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	static int flag;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	/* Avoid subsequent threads to enter the cleanup */
28f08c3bdfSopenharmony_ci	if (tst_atomic_inc(&flag) != 1)
29f08c3bdfSopenharmony_ci		pthread_exit(NULL);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	tst_res(TINFO, "cleanup() started");
32f08c3bdfSopenharmony_ci	usleep(10000);
33f08c3bdfSopenharmony_ci	tst_res(TINFO, "cleanup() finished");
34f08c3bdfSopenharmony_ci}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void *worker(void *id)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	tst_res(TINFO, "Thread %ld waiting...", (long)id);
39f08c3bdfSopenharmony_ci	pthread_barrier_wait(&barrier);
40f08c3bdfSopenharmony_ci	tst_brk(TBROK, "Failure %ld", (long)id);
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	return NULL;
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void do_test(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	long i;
48f08c3bdfSopenharmony_ci	pthread_t threads[THREADS];
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	for (i = 0; i < THREADS; i++)
51f08c3bdfSopenharmony_ci		pthread_create(threads+i, NULL, worker, (void*)i);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	for (i = 0; i < THREADS; i++) {
54f08c3bdfSopenharmony_ci		tst_res(TINFO, "Joining thread %li", i);
55f08c3bdfSopenharmony_ci		pthread_join(threads[i], NULL);
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.test_all = do_test,
61f08c3bdfSopenharmony_ci	.setup = setup,
62f08c3bdfSopenharmony_ci	.cleanup = cleanup,
63f08c3bdfSopenharmony_ci};
64