1/* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <pthread.h>
19#include <stdio.h>
20#include <unistd.h>
21
22#define N 2
23
24
25static int do_test (void);
26
27#define TEST_FUNCTION do_test ()
28#include "../test-skeleton.c"
29
30static int cnt0;
31static void
32f0 (void *p)
33{
34 ++cnt0;
35}
36
37
38static int cnt1;
39static void
40f1 (void *p)
41{
42 ++cnt1;
43}
44
45
46static void (*fcts[N]) (void *) =
47{
48 f0,
49 f1
50};
51
52
53static void *
54tf (void *arg)
55{
56 pthread_key_t *key = (pthread_key_t *) arg;
57
58 /* Use an arbitrary but valid pointer to avoid GCC warnings. */
59 if (pthread_setspecific (key: *key, pointer: arg) != 0)
60 {
61 write_message (message: "setspecific failed\n");
62 _exit (1);
63 }
64
65 return NULL;
66}
67
68
69int
70do_test (void)
71{
72 pthread_key_t keys[N];
73
74 int i;
75 for (i = 0; i < N; ++i)
76 if (pthread_key_create (key: &keys[i], destr_function: fcts[i]) != 0)
77 {
78 write_message (message: "key_create failed\n");
79 _exit (1);
80 }
81
82 pthread_t th;
83 if (pthread_create (newthread: &th, NULL, start_routine: tf, arg: &keys[1]) != 0)
84 {
85 write_message (message: "create failed\n");
86 _exit (1);
87 }
88
89 if (pthread_join (th: th, NULL) != 0)
90 {
91 write_message (message: "join failed\n");
92 _exit (1);
93 }
94
95 if (cnt0 != 0)
96 {
97 write_message (message: "cnt0 != 0\n");
98 _exit (1);
99 }
100
101 if (cnt1 != 1)
102 {
103 write_message (message: "cnt1 != 1\n");
104 _exit (1);
105 }
106
107 for (i = 0; i < N; ++i)
108 if (pthread_key_delete (key: keys[i]) != 0)
109 {
110 write_message (message: "key_delete failed\n");
111 _exit (1);
112 }
113
114 return 0;
115}
116

source code of glibc/sysdeps/pthread/tst-key2.c