1/* Check that __pthread_destroy_specific works correctly if it has to skip
2 unused slots.
3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20#define _GNU_SOURCE
21
22#include <error.h>
23#include <pthread.h>
24#include <stdio.h>
25
26
27#define N_k 42
28
29static volatile int v;
30
31static void
32d (void *x)
33{
34 int *i = (int *) x;
35
36 if (v != *i)
37 error (status: 1, errnum: 0, format: "FAILED %d %d", v, *i);
38 v += 2;
39
40 printf (format: "%s %d\n", __FUNCTION__, *i);
41 fflush (stdout);
42}
43
44static void *
45test (void *x)
46{
47 pthread_key_t k[N_k];
48 static int k_v[N_k];
49
50 int err, i;
51
52 for (i = 0; i < N_k; i += 1)
53 {
54 err = pthread_key_create (key: &k[i], destr_function: &d);
55 if (err != 0)
56 error (status: 1, errnum: err, format: "pthread_key_create %d", i);
57 }
58
59 for (i = 0; i < N_k; i += 1)
60 {
61 k_v[i] = i;
62 err = pthread_setspecific (key: k[i], pointer: &k_v[i]);
63 if (err != 0)
64 error (status: 1, errnum: err, format: "pthread_setspecific %d", i);
65 }
66
67 /* Delete every even key. */
68 for (i = 0; i < N_k; i += 2)
69 {
70 err = pthread_key_delete (key: k[i]);
71 if (err != 0)
72 error (status: 1, errnum: err, format: "pthread_key_delete %d", i);
73 }
74
75 v = 1;
76 pthread_exit (NULL);
77
78 return NULL;
79}
80
81
82int
83main (void)
84{
85 pthread_t tid;
86 int err;
87
88 err = pthread_create (newthread: &tid, attr: 0, start_routine: test, NULL);
89 if (err != 0)
90 error (status: 1, errnum: err, format: "pthread_create");
91
92 err = pthread_join (th: tid, NULL);
93 if (err)
94 error (status: 1, errnum: err, format: "pthread_join");
95
96 if (v != N_k + 1)
97 error (status: 1, errnum: 0, format: "FAILED END %d %d", v, N_k + 1);
98
99 return 0;
100}
101

source code of glibc/htl/tests/test-__pthread_destroy_specific-skip.c