Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-once2.c
blob4fc6217995d7cdacd336aba06d7a3820bd82f7a7
1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
25 #define N 100
27 static pthread_once_t once = PTHREAD_ONCE_INIT;
29 static int global;
31 static void
32 once_handler (void)
34 struct timespec ts;
36 ++global;
38 ts.tv_sec = 2;
39 ts.tv_nsec = 0;
40 nanosleep (&ts, NULL);
44 static void *
45 tf (void *arg)
47 pthread_once (&once, once_handler);
49 if (global != 1)
51 printf ("thread %ld: global == %d\n", (long int) arg, global);
52 exit (1);
55 return NULL;
59 static int
60 do_test (void)
62 pthread_attr_t at;
63 pthread_t th[N];
64 int cnt;
66 if (pthread_attr_init (&at) != 0)
68 puts ("attr_init failed");
69 return 1;
72 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
74 puts ("attr_setstacksize failed");
75 return 1;
78 for (cnt = 0; cnt < N; ++cnt)
79 if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0)
81 printf ("creation of thread %d failed\n", cnt);
82 return 1;
85 if (pthread_attr_destroy (&at) != 0)
87 puts ("attr_destroy failed");
88 return 1;
91 for (cnt = 0; cnt < N; ++cnt)
92 if (pthread_join (th[cnt], NULL) != 0)
94 printf ("join of thread %d failed\n", cnt);
95 return 1;
98 return 0;
101 #define TEST_FUNCTION do_test ()
102 #define TIMEOUT 4
103 #include "../test-skeleton.c"