Home | History | Annotate | Download | only in newlib_tests
      1 /*
      2  * Copyright (c) 2016 Linux Test Project
      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  */
     15 
     16 /*
     17  * Test that tst_atomic_inc works as expected.
     18  */
     19 
     20 #include <pthread.h>
     21 #include "tst_test.h"
     22 
     23 #define THREADS 64
     24 #define ITERATIONS 100000
     25 
     26 static int atomic;
     27 
     28 static void *worker(void *id)
     29 {
     30 	int i;
     31 
     32 	(void) id;
     33 	for (i = 0; i < ITERATIONS; i++)
     34 		tst_atomic_inc(&atomic);
     35 
     36 	return NULL;
     37 }
     38 
     39 static void do_test(void)
     40 {
     41 	long i;
     42 	pthread_t threads[THREADS];
     43 
     44 	for (i = 0; i < THREADS; i++)
     45 		pthread_create(threads+i, NULL, worker, (void *)i);
     46 
     47 	for (i = 0; i < THREADS; i++) {
     48 		tst_res(TINFO, "Joining thread %li", i);
     49 		pthread_join(threads[i], NULL);
     50 	}
     51 
     52 	if (atomic == THREADS * ITERATIONS)
     53 		tst_res(TPASS, "Atomic working as expected");
     54 	else
     55 		tst_res(TFAIL, "Atomic does not have expected value");
     56 }
     57 
     58 static struct tst_test test = {
     59 	.test_all = do_test,
     60 };
     61