]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-key3.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-key3.c
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 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 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749
UD
18
19#include <pthread.h>
20#include <stdio.h>
21#include <unistd.h>
22
23#define N 2
24
25
e0c68519
GG
26static int do_test (void);
27
28#define TEST_FUNCTION do_test ()
29#include "../test-skeleton.c"
30
76a50749
UD
31static int cnt0;
32static void
33f0 (void *p)
34{
35 ++cnt0;
36}
37
38
39static int cnt1;
40static void
41f1 (void *p)
42{
43 ++cnt1;
44}
45
46
47static void (*fcts[N]) (void *) =
48{
49 f0,
50 f1
51};
52
53
54static pthread_barrier_t b;
55
56
57static void *
58tf (void *arg)
59{
60 pthread_key_t *key = (pthread_key_t *) arg;
61
62 if (pthread_setspecific (*key, (void *) -1l) != 0)
63 {
e0c68519 64 write_message ("setspecific failed\n");
76a50749
UD
65 _exit (1);
66 }
67
68 pthread_barrier_wait (&b);
69
70 const struct timespec t = { .tv_sec = 1000, .tv_nsec = 0 };
71 while (1)
72 nanosleep (&t, NULL);
73
74 /* NOTREACHED */
75 return NULL;
76}
77
78
79int
80do_test (void)
81{
82 pthread_key_t keys[N];
83
84 int i;
85 for (i = 0; i < N; ++i)
86 if (pthread_key_create (&keys[i], fcts[i]) != 0)
87 {
e0c68519 88 write_message ("key_create failed\n");
76a50749
UD
89 _exit (1);
90 }
91
92 if (pthread_barrier_init (&b, NULL, 2) != 0)
93 {
e0c68519 94 write_message ("barrier_init failed\n");
76a50749
UD
95 _exit (1);
96 }
97
98 pthread_t th;
99 if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
100 {
e0c68519 101 write_message ("create failed\n");
76a50749
UD
102 _exit (1);
103 }
104
105 pthread_barrier_wait (&b);
106
107 if (pthread_cancel (th) != 0)
108 {
e0c68519 109 write_message ("cancel failed\n");
76a50749
UD
110 _exit (1);
111 }
112
113 void *status;
114 if (pthread_join (th, &status) != 0)
115 {
e0c68519 116 write_message ("join failed\n");
76a50749
UD
117 _exit (1);
118 }
119
120 if (status != PTHREAD_CANCELED)
121 {
e0c68519 122 write_message ("thread not canceled\n");
76a50749
UD
123 _exit (1);
124 }
125
126 /* Note that the TSD destructors not necessarily have to have
127 finished by the time pthread_join returns. At least according to
128 POSIX. We implement the stronger requirement that they indeed
129 have run and therefore these tests succeed. */
130 if (cnt0 != 0)
131 {
e0c68519 132 write_message ("cnt0 != 0\n");
76a50749
UD
133 _exit (1);
134 }
135
136 if (cnt1 != 1)
137 {
e0c68519 138 write_message ("cnt1 != 1\n");
76a50749
UD
139 _exit (1);
140 }
141
142 for (i = 0; i < N; ++i)
143 if (pthread_key_delete (keys[i]) != 0)
144 {
e0c68519 145 write_message ("key_delete failed\n");
76a50749
UD
146 _exit (1);
147 }
148
149 if (pthread_barrier_destroy (&b) != 0)
150 {
e0c68519 151 write_message ("barrier_destroy failed\n");
76a50749
UD
152 _exit (1);
153 }
154
155 return 0;
156}