]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 2002-2015 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 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
76a50749 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <pthread.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/mman.h> | |
26 | #include <sys/wait.h> | |
27 | ||
28 | ||
29 | int *condition; | |
30 | ||
b910f788 RM |
31 | static int |
32 | do_test (void) | |
76a50749 UD |
33 | { |
34 | size_t ps = sysconf (_SC_PAGESIZE); | |
35 | char tmpfname[] = "/tmp/tst-cond4.XXXXXX"; | |
36 | char data[ps]; | |
37 | void *mem; | |
38 | int fd; | |
39 | pthread_mutexattr_t ma; | |
40 | pthread_mutex_t *mut1; | |
41 | pthread_mutex_t *mut2; | |
42 | pthread_condattr_t ca; | |
43 | pthread_cond_t *cond; | |
44 | pid_t pid; | |
45 | int result = 0; | |
46 | int p; | |
47 | ||
48 | fd = mkstemp (tmpfname); | |
49 | if (fd == -1) | |
50 | { | |
51 | printf ("cannot open temporary file: %m\n"); | |
b910f788 | 52 | return 1; |
76a50749 UD |
53 | } |
54 | ||
55 | /* Make sure it is always removed. */ | |
56 | unlink (tmpfname); | |
57 | ||
58 | /* Create one page of data. */ | |
59 | memset (data, '\0', ps); | |
60 | ||
61 | /* Write the data to the file. */ | |
f9657e88 | 62 | if (write (fd, data, ps) != (ssize_t) ps) |
76a50749 UD |
63 | { |
64 | puts ("short write"); | |
b910f788 | 65 | return 1; |
76a50749 UD |
66 | } |
67 | ||
68 | mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
69 | if (mem == MAP_FAILED) | |
70 | { | |
71 | printf ("mmap failed: %m\n"); | |
b910f788 | 72 | return 1; |
76a50749 UD |
73 | } |
74 | ||
75 | mut1 = (pthread_mutex_t *) (((uintptr_t) mem | |
76 | + __alignof (pthread_mutex_t)) | |
77 | & ~(__alignof (pthread_mutex_t) - 1)); | |
78 | mut2 = mut1 + 1; | |
79 | ||
80 | cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1) | |
81 | + __alignof (pthread_cond_t)) | |
82 | & ~(__alignof (pthread_cond_t) - 1)); | |
83 | ||
84 | condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int)) | |
85 | & ~(__alignof (int) - 1)); | |
86 | ||
87 | if (pthread_mutexattr_init (&ma) != 0) | |
88 | { | |
89 | puts ("mutexattr_init failed"); | |
b910f788 | 90 | return 1; |
76a50749 UD |
91 | } |
92 | ||
93 | if (pthread_mutexattr_getpshared (&ma, &p) != 0) | |
94 | { | |
95 | puts ("1st mutexattr_getpshared failed"); | |
b910f788 | 96 | return 1; |
76a50749 UD |
97 | } |
98 | ||
99 | if (p != PTHREAD_PROCESS_PRIVATE) | |
100 | { | |
101 | puts ("default pshared value wrong"); | |
b910f788 | 102 | return 1; |
76a50749 UD |
103 | } |
104 | ||
105 | if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0) | |
106 | { | |
107 | puts ("mutexattr_setpshared failed"); | |
b910f788 | 108 | return 1; |
76a50749 UD |
109 | } |
110 | ||
111 | if (pthread_mutexattr_getpshared (&ma, &p) != 0) | |
112 | { | |
113 | puts ("2nd mutexattr_getpshared failed"); | |
b910f788 | 114 | return 1; |
76a50749 UD |
115 | } |
116 | ||
117 | if (p != PTHREAD_PROCESS_SHARED) | |
118 | { | |
119 | puts ("pshared value after setpshared call wrong"); | |
b910f788 | 120 | return 1; |
76a50749 UD |
121 | } |
122 | ||
123 | if (pthread_mutex_init (mut1, &ma) != 0) | |
124 | { | |
125 | puts ("1st mutex_init failed"); | |
b910f788 | 126 | return 1; |
76a50749 UD |
127 | } |
128 | ||
129 | if (pthread_mutex_init (mut2, &ma) != 0) | |
130 | { | |
131 | puts ("2nd mutex_init failed"); | |
b910f788 | 132 | return 1; |
76a50749 UD |
133 | } |
134 | ||
135 | if (pthread_condattr_init (&ca) != 0) | |
136 | { | |
137 | puts ("condattr_init failed"); | |
b910f788 | 138 | return 1; |
76a50749 UD |
139 | } |
140 | ||
700bf7af UD |
141 | if (pthread_condattr_getpshared (&ca, &p) != 0) |
142 | { | |
143 | puts ("1st condattr_getpshared failed"); | |
b910f788 | 144 | return 1; |
700bf7af UD |
145 | } |
146 | ||
147 | if (p != PTHREAD_PROCESS_PRIVATE) | |
148 | { | |
149 | puts ("default value for pshared in condattr wrong"); | |
b910f788 | 150 | return 1; |
700bf7af UD |
151 | } |
152 | ||
76a50749 UD |
153 | if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0) |
154 | { | |
155 | puts ("condattr_setpshared failed"); | |
b910f788 | 156 | return 1; |
76a50749 UD |
157 | } |
158 | ||
700bf7af UD |
159 | if (pthread_condattr_getpshared (&ca, &p) != 0) |
160 | { | |
161 | puts ("2nd condattr_getpshared failed"); | |
b910f788 | 162 | return 1; |
700bf7af UD |
163 | } |
164 | ||
165 | if (p != PTHREAD_PROCESS_SHARED) | |
166 | { | |
167 | puts ("pshared condattr still not set"); | |
b910f788 | 168 | return 1; |
700bf7af UD |
169 | } |
170 | ||
76a50749 UD |
171 | if (pthread_cond_init (cond, &ca) != 0) |
172 | { | |
173 | puts ("cond_init failed"); | |
b910f788 | 174 | return 1; |
76a50749 UD |
175 | } |
176 | ||
177 | if (pthread_mutex_lock (mut1) != 0) | |
178 | { | |
179 | puts ("parent: 1st mutex_lock failed"); | |
b910f788 | 180 | return 1; |
76a50749 UD |
181 | } |
182 | ||
183 | puts ("going to fork now"); | |
184 | pid = fork (); | |
185 | if (pid == -1) | |
186 | { | |
187 | puts ("fork failed"); | |
b910f788 | 188 | return 1; |
76a50749 UD |
189 | } |
190 | else if (pid == 0) | |
191 | { | |
192 | if (pthread_mutex_lock (mut2) != 0) | |
193 | { | |
194 | puts ("child: mutex_lock failed"); | |
b910f788 | 195 | return 1; |
76a50749 UD |
196 | } |
197 | ||
198 | if (pthread_mutex_unlock (mut1) != 0) | |
199 | { | |
200 | puts ("child: 1st mutex_unlock failed"); | |
b910f788 | 201 | return 1; |
76a50749 UD |
202 | } |
203 | ||
204 | do | |
205 | if (pthread_cond_wait (cond, mut2) != 0) | |
206 | { | |
207 | puts ("child: cond_wait failed"); | |
b910f788 | 208 | return 1; |
76a50749 UD |
209 | } |
210 | while (*condition == 0); | |
211 | ||
212 | if (pthread_mutex_unlock (mut2) != 0) | |
213 | { | |
214 | puts ("child: 2nd mutex_unlock failed"); | |
b910f788 | 215 | return 1; |
76a50749 UD |
216 | } |
217 | ||
218 | puts ("child done"); | |
219 | } | |
220 | else | |
221 | { | |
222 | int status; | |
223 | ||
224 | if (pthread_mutex_lock (mut1) != 0) | |
225 | { | |
226 | puts ("parent: 2nd mutex_lock failed"); | |
b910f788 | 227 | return 1; |
76a50749 UD |
228 | } |
229 | ||
230 | if (pthread_mutex_lock (mut2) != 0) | |
231 | { | |
232 | puts ("parent: 3rd mutex_lock failed"); | |
b910f788 | 233 | return 1; |
76a50749 UD |
234 | } |
235 | ||
236 | if (pthread_cond_signal (cond) != 0) | |
237 | { | |
238 | puts ("parent: cond_signal failed"); | |
b910f788 | 239 | return 1; |
76a50749 UD |
240 | } |
241 | ||
242 | *condition = 1; | |
243 | ||
244 | if (pthread_mutex_unlock (mut2) != 0) | |
245 | { | |
246 | puts ("parent: mutex_unlock failed"); | |
b910f788 | 247 | return 1; |
76a50749 UD |
248 | } |
249 | ||
250 | puts ("waiting for child"); | |
251 | ||
252 | waitpid (pid, &status, 0); | |
253 | result |= status; | |
254 | ||
255 | puts ("parent done"); | |
256 | } | |
257 | ||
258 | return result; | |
259 | } | |
b910f788 RM |
260 | |
261 | #define TEST_FUNCTION do_test () | |
262 | #include "../test-skeleton.c" |