1/* Copyright (C) 2003-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 <limits.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23
24
25#ifdef PTHREAD_KEYS_MAX
26const int max = PTHREAD_KEYS_MAX;
27#else
28const int max = _POSIX_THREAD_KEYS_MAX;
29#endif
30static pthread_key_t *keys;
31
32
33static void *
34tf1 (void *arg)
35{
36 int i;
37 for (i = 0; i < max; ++i)
38 if (pthread_setspecific (key: keys[i], pointer: (void *) (long int) (i + 1)) != 0)
39 {
40 puts (s: "setspecific failed");
41 exit (1);
42 }
43
44 return NULL;
45}
46
47
48static void *
49tf2 (void *arg)
50{
51 int i;
52 for (i = 0; i < max; ++i)
53 if (pthread_getspecific (key: keys[i]) != NULL)
54 {
55 printf (format: "getspecific for key %d not NULL\n", i);
56 exit (1);
57 }
58
59 return NULL;
60}
61
62
63static int
64do_test (void)
65{
66 keys = alloca (max * sizeof (pthread_key_t));
67
68 int i;
69 for (i = 0; i < max; ++i)
70 if (pthread_key_create (key: &keys[i], NULL) != 0)
71 {
72 puts (s: "key_create failed");
73 exit (1);
74 }
75
76 pthread_attr_t a;
77
78 if (pthread_attr_init (attr: &a) != 0)
79 {
80 puts (s: "attr_init failed");
81 exit (1);
82 }
83
84 if (pthread_attr_setstacksize (attr: &a, stacksize: 1 * 1024 * 1024) != 0)
85 {
86 puts (s: "attr_setstacksize failed");
87 return 1;
88 }
89
90 for (i = 0; i < 10; ++i)
91 {
92 int j;
93#define N 2
94 pthread_t th[N];
95 for (j = 0; j < N; ++j)
96 if (pthread_create (newthread: &th[j], NULL, start_routine: tf1, NULL) != 0)
97 {
98 puts (s: "1st create failed");
99 exit (1);
100 }
101
102 for (j = 0; j < N; ++j)
103 if (pthread_join (th: th[j], NULL) != 0)
104 {
105 puts (s: "1st join failed");
106 exit (1);
107 }
108
109 for (j = 0; j < N; ++j)
110 if (pthread_create (newthread: &th[j], NULL, start_routine: tf2, NULL) != 0)
111 {
112 puts (s: "2nd create failed");
113 exit (1);
114 }
115
116 for (j = 0; j < N; ++j)
117 if (pthread_join (th: th[j], NULL) != 0)
118 {
119 puts (s: "2nd join failed");
120 exit (1);
121 }
122 }
123
124 if (pthread_attr_destroy (attr: &a) != 0)
125 {
126 puts (s: "attr_destroy failed");
127 exit (1);
128 }
129
130 return 0;
131}
132
133
134#define TEST_FUNCTION do_test ()
135#include "../test-skeleton.c"
136

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