1/*
2 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19/*
20 * Test for callback thread safety.
21 */
22#include <pthread.h>
23#include "tst_test.h"
24
25#define THREADS 10
26
27static pthread_barrier_t barrier;
28
29static void setup(void)
30{
31	pthread_barrier_init(&barrier, NULL, THREADS);
32
33	tst_res(TINFO, "setup() executed");
34}
35
36static void cleanup(void)
37{
38	static int flag;
39
40	/* Avoid subsequent threads to enter the cleanup */
41	if (tst_atomic_inc(&flag) != 1)
42		pthread_exit(NULL);
43
44	tst_res(TINFO, "cleanup() started");
45	usleep(10000);
46	tst_res(TINFO, "cleanup() finished");
47}
48
49static void *worker(void *id)
50{
51	tst_res(TINFO, "Thread %ld waiting...", (long)id);
52	pthread_barrier_wait(&barrier);
53	tst_brk(TBROK, "Failure %ld", (long)id);
54
55	return NULL;
56}
57
58static void do_test(void)
59{
60	long i;
61	pthread_t threads[THREADS];
62
63	for (i = 0; i < THREADS; i++)
64		pthread_create(threads+i, NULL, worker, (void*)i);
65
66	for (i = 0; i < THREADS; i++) {
67		tst_res(TINFO, "Joining thread %li", i);
68		pthread_join(threads[i], NULL);
69	}
70}
71
72static struct tst_test test = {
73	.test_all = do_test,
74	.setup = setup,
75	.cleanup = cleanup,
76};
77