source: vendor/glibc-tests/2005-06-14/nptl/tst-key4.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1/* Copyright (C) 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <limits.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25
26
27#ifdef PTHREAD_KEYS_MAX
28const int max = PTHREAD_KEYS_MAX;
29#else
30const int max = _POSIX_THREAD_KEYS_MAX;
31#endif
32static pthread_key_t *keys;
33
34
35static void *
36tf1 (void *arg)
37{
38 int i;
39 for (i = 0; i < max; ++i)
40 if (pthread_setspecific (keys[i], (void *) (long int) (i + 1)) != 0)
41 {
42 puts ("setspecific failed");
43 exit (1);
44 }
45
46 return NULL;
47}
48
49
50static void *
51tf2 (void *arg)
52{
53 int i;
54 for (i = 0; i < max; ++i)
55 if (pthread_getspecific (keys[i]) != NULL)
56 {
57 printf ("getspecific for key %d not NULL\n", i);
58 exit (1);
59 }
60
61 return NULL;
62}
63
64
65static int
66do_test (void)
67{
68 keys = alloca (max * sizeof (pthread_key_t));
69
70 int i;
71 for (i = 0; i < max; ++i)
72 if (pthread_key_create (&keys[i], NULL) != 0)
73 {
74 puts ("key_create failed");
75 exit (1);
76 }
77
78 pthread_attr_t a;
79
80 if (pthread_attr_init (&a) != 0)
81 {
82 puts ("attr_init failed");
83 exit (1);
84 }
85
86 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
87 {
88 puts ("attr_setstacksize failed");
89 return 1;
90 }
91
92 for (i = 0; i < 10; ++i)
93 {
94 int j;
95#define N 2
96 pthread_t th[N];
97 for (j = 0; j < N; ++j)
98 if (pthread_create (&th[j], NULL, tf1, NULL) != 0)
99 {
100 puts ("1st create failed");
101 exit (1);
102 }
103
104 for (j = 0; j < N; ++j)
105 if (pthread_join (th[j], NULL) != 0)
106 {
107 puts ("1st join failed");
108 exit (1);
109 }
110
111 for (j = 0; j < N; ++j)
112 if (pthread_create (&th[j], NULL, tf2, NULL) != 0)
113 {
114 puts ("2nd create failed");
115 exit (1);
116 }
117
118 for (j = 0; j < N; ++j)
119 if (pthread_join (th[j], NULL) != 0)
120 {
121 puts ("2nd join failed");
122 exit (1);
123 }
124 }
125
126 if (pthread_attr_destroy (&a) != 0)
127 {
128 puts ("attr_destroy failed");
129 exit (1);
130 }
131
132 return 0;
133}
134
135
136#define TEST_FUNCTION do_test ()
137#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.