Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-sem14.c
blob2b53ef5f7e07111cc1272f5454a1677e974c8e67
1 /* Test for sem_post race: bug 14532.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <pthread.h>
20 #include <semaphore.h>
21 #include <stdio.h>
23 #define NTHREADS 10
24 #define NITER 100000
26 sem_t sem;
27 int c;
28 volatile int thread_fail;
30 static void *
31 tf (void *arg)
33 for (int i = 0; i < NITER; i++)
35 if (sem_wait (&sem) != 0)
37 perror ("sem_wait");
38 thread_fail = 1;
40 ++c;
41 if (sem_post (&sem) != 0)
43 perror ("sem_post");
44 thread_fail = 1;
47 return NULL;
50 static int
51 do_test (void)
53 if (sem_init (&sem, 0, 0) != 0)
55 perror ("sem_init");
56 return 1;
59 pthread_t th[NTHREADS];
60 for (int i = 0; i < NTHREADS; i++)
62 if (pthread_create (&th[i], NULL, tf, NULL) != 0)
64 puts ("pthread_create failed");
65 return 1;
69 if (sem_post (&sem) != 0)
71 perror ("sem_post");
72 return 1;
75 for (int i = 0; i < NTHREADS; i++)
76 if (pthread_join (th[i], NULL) != 0)
78 puts ("pthread_join failed");
79 return 1;
82 if (c != NTHREADS * NITER)
84 printf ("c = %d, should be %d\n", c, NTHREADS * NITER);
85 return 1;
87 return thread_fail;
90 #define TIMEOUT 10
91 #define TEST_FUNCTION do_test ()
92 #include "../test-skeleton.c"