]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-tsd4.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-tsd4.c
1 /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <limits.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25
26 static pthread_key_t key;
27
28
29 static int rounds;
30
31
32 static void
33 destr (void *arg)
34 {
35 ++rounds;
36
37 if (pthread_setspecific (key, (void *) 1l) != 0)
38 {
39 puts ("destr: setspecific failed");
40 exit (1);
41 }
42 }
43
44
45 static void *
46 tf (void *arg)
47 {
48 if (pthread_setspecific (key, (void *) 1l) != 0)
49 {
50 puts ("tf: setspecific failed");
51 exit (1);
52 }
53
54 return NULL;
55 }
56
57
58 /* This test check non-standard behavior. The standard does not
59 require that the implementation has to stop calling TSD destructors
60 when they are set over and over again. But NPTL does. */
61 static int
62 do_test (void)
63 {
64 /* Allocate two keys, both with destructors. */
65 if (pthread_key_create (&key, destr) != 0)
66 {
67 puts ("key_create failed");
68 return 1;
69 }
70
71 pthread_t th;
72 if (pthread_create (&th, NULL, tf, NULL) != 0)
73 {
74 puts ("create failed");
75 return 1;
76 }
77
78 if (pthread_join (th, NULL) != 0)
79 {
80 puts ("join failed");
81 return 1;
82 }
83
84 if (rounds < PTHREAD_DESTRUCTOR_ITERATIONS)
85 {
86 printf ("rounds == %d, PTHREAD_DESTRUCTOR_ITERATIONS = %d\n",
87 rounds, PTHREAD_DESTRUCTOR_ITERATIONS);
88 return 1;
89 }
90
91 if (pthread_getspecific (key) != NULL)
92 {
93 puts ("key data != NULL");
94 return 1;
95 }
96
97 return 0;
98 }
99
100
101 #define TEST_FUNCTION do_test ()
102 #include "../test-skeleton.c"