Home | History | Annotate | Download | only in newlib_tests
      1 /*
      2  * Copyright (c) 2016 Cyril Hrubis <chrubis (at) 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 
     27 static pthread_barrier_t barrier;
     28 
     29 static void setup(void)
     30 {
     31 	pthread_barrier_init(&barrier, NULL, THREADS);
     32 
     33 	tst_res(TINFO, "setup() executed");
     34 }
     35 
     36 static 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 
     49 static 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 
     56 static void do_test(void)
     57 {
     58 	long i;
     59 	pthread_t threads[THREADS];
     60 
     61 	for (i = 0; i < THREADS; i++)
     62 		pthread_create(threads+i, NULL, worker, (void*)i);
     63 
     64 	for (i = 0; i < THREADS; i++) {
     65 		tst_res(TINFO, "Joining thread %li", i);
     66 		pthread_join(threads[i], NULL);
     67 	}
     68 }
     69 
     70 static struct tst_test test = {
     71 	.tid = "test08",
     72 	.test_all = do_test,
     73 	.setup = setup,
     74 	.cleanup = cleanup,
     75 };
     76