]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-once2.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / tst-once2.c
CommitLineData
f7a9f785 1/* Copyright (C) 2002-2016 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
76a50749
UD
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <time.h>
23
24
25#define N 100
26
27static pthread_once_t once = PTHREAD_ONCE_INIT;
28
29static int global;
30
31static void
32once_handler (void)
33{
34 struct timespec ts;
35
36 ++global;
37
38 ts.tv_sec = 2;
39 ts.tv_nsec = 0;
40 nanosleep (&ts, NULL);
41}
42
43
44static void *
45tf (void *arg)
46{
47 pthread_once (&once, once_handler);
48
49 if (global != 1)
50 {
51 printf ("thread %ld: global == %d\n", (long int) arg, global);
52 exit (1);
53 }
54
55 return NULL;
56}
57
58
59static int
60do_test (void)
61{
4d1a02ef 62 pthread_attr_t at;
76a50749
UD
63 pthread_t th[N];
64 int cnt;
65
4d1a02ef
UD
66 if (pthread_attr_init (&at) != 0)
67 {
68 puts ("attr_init failed");
69 return 1;
70 }
71
72 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
73 {
74 puts ("attr_setstacksize failed");
75 return 1;
76 }
77
76a50749 78 for (cnt = 0; cnt < N; ++cnt)
4d1a02ef 79 if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0)
76a50749
UD
80 {
81 printf ("creation of thread %d failed\n", cnt);
82 return 1;
83 }
84
4d1a02ef
UD
85 if (pthread_attr_destroy (&at) != 0)
86 {
87 puts ("attr_destroy failed");
88 return 1;
89 }
90
76a50749
UD
91 for (cnt = 0; cnt < N; ++cnt)
92 if (pthread_join (th[cnt], NULL) != 0)
93 {
94 printf ("join of thread %d failed\n", cnt);
95 return 1;
96 }
97
98 return 0;
99}
100
101#define TEST_FUNCTION do_test ()
102#define TIMEOUT 4
103#include "../test-skeleton.c"