]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_cancelable.c
Libc-583.tar.gz
[apple/libc.git] / pthreads / pthread_cancelable.c
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 *
43 */
44 /*
45 * MkLinux
46 */
47
48 /*
49 * POSIX Pthread Library
50 */
51
52 #include "pthread_internals.h"
53
54 #include <assert.h>
55 #include <stdio.h> /* For printf(). */
56 #include <stdlib.h>
57 #include <errno.h> /* For __mach_errno_addr() prototype. */
58 #include <signal.h>
59 #include <sys/time.h>
60 #include <sys/resource.h>
61 #include <sys/sysctl.h>
62 #include <sys/queue.h>
63 #include <machine/vmparam.h>
64 #include <mach/vm_statistics.h>
65
66 extern int __unix_conforming;
67 extern void __posix_join_cleanup(void *arg);
68 extern pthread_lock_t _pthread_list_lock;
69 extern void _pthread_testcancel(pthread_t thread, int isconforming);
70 extern int _pthread_reap_thread(pthread_t th, mach_port_t kernel_thread, void **value_ptr, int conforming);
71 extern int _pthread_cond_wait(pthread_cond_t *cond,
72 pthread_mutex_t *mutex,
73 const struct timespec *abstime,
74 int isRelative,
75 int isconforming);
76 extern int __semwait_signal(int cond_sem, int mutex_sem, int timeout, int relative, __int64_t tv_sec, __int32_t tv_nsec);
77 extern int __sigwait(const sigset_t *set, int *sig);
78
79 /*
80 * Wait for a thread to terminate and obtain its exit value.
81 */
82 int
83 pthread_join(pthread_t thread,
84 void **value_ptr)
85 {
86 int res = 0;
87 pthread_t self = pthread_self();
88 mach_port_t kthport;
89 int conforming = 0;
90 #if !__DARWIN_UNIX03
91 kern_return_t kern_res;
92 #endif
93
94 #if __DARWIN_UNIX03
95 if (__unix_conforming == 0)
96 __unix_conforming = 1;
97
98 #ifdef VARIANT_CANCELABLE
99 _pthread_testcancel(self, 1);
100 #endif /* VARIANT_CANCELABLE */
101 #endif /* __DARWIN_UNIX03 */
102
103 if ((res = _pthread_lookup_thread(thread, &kthport, 1)) != 0)
104 return(res);
105
106 if (thread->sig == _PTHREAD_SIG)
107 {
108 if (thread->newstyle == 0) {
109 semaphore_t death = new_sem_from_pool(); /* in case we need it */
110
111 LOCK(thread->lock);
112 if ((thread->detached & PTHREAD_CREATE_JOINABLE) &&
113 thread->death == SEMAPHORE_NULL)
114 {
115
116 assert(thread->joiner == NULL);
117 if (thread != self && (self == NULL || self->joiner != thread))
118 {
119 int already_exited = (thread->detached & _PTHREAD_EXITED);
120
121 thread->death = death;
122 thread->joiner = self;
123 UNLOCK(thread->lock);
124
125 if (!already_exited)
126 {
127 #if __DARWIN_UNIX03
128 /* Wait for it to signal... */
129 pthread_cleanup_push(__posix_join_cleanup, (void *)thread);
130 do {
131 res = __semwait_signal(death, 0, 0, 0, (int64_t)0, (int32_t)0);
132 } while ((res < 0) && (errno == EINTR));
133 pthread_cleanup_pop(0);
134
135 #else /* __DARWIN_UNIX03 */
136 /* Wait for it to signal... */
137 do {
138 PTHREAD_MACH_CALL(semaphore_wait(death), kern_res);
139 } while (kern_res != KERN_SUCCESS);
140 #endif /* __DARWIN_UNIX03 */
141 }
142
143 LOCK(_pthread_list_lock);
144 TAILQ_REMOVE(&__pthread_head, thread, plist);
145 #if WQ_TRACE
146 __kdebug_trace(0x9000010, thread, 0, 0, 16, 0);
147 #endif
148 UNLOCK(_pthread_list_lock);
149 /* ... and wait for it to really be dead */
150 while ((res = _pthread_reap_thread(thread,
151 thread->kernel_thread,
152 value_ptr, __unix_conforming)) == EAGAIN)
153 {
154 sched_yield();
155 }
156
157 } else {
158 UNLOCK(thread->lock);
159 res = EDEADLK;
160 }
161 } else {
162 UNLOCK(thread->lock);
163 res = EINVAL;
164 }
165 restore_sem_to_pool(death);
166 return res;
167 } else {
168 /* new style */
169
170 semaphore_t death = SEMAPHORE_NULL; /* in case we need it */
171 semaphore_t joinsem = SEMAPHORE_NULL;
172
173 if (thread->joiner_notify == MACH_PORT_NULL)
174 death = new_sem_from_pool();
175
176 LOCK(thread->lock);
177 if ((thread->detached & PTHREAD_CREATE_JOINABLE) &&
178 (thread->joiner == NULL))
179 {
180 assert(thread->kernel_thread == kthport);
181 if (thread != self && (self == NULL || self->joiner != thread))
182 {
183 if (thread->joiner_notify == MACH_PORT_NULL) {
184 if (death == SEMAPHORE_NULL)
185 LIBC_ABORT("thread %p: death == SEMAPHORE_NULL", thread);
186 thread->joiner_notify = death;
187 death = SEMAPHORE_NULL;
188 }
189 joinsem = thread->joiner_notify;
190 thread->joiner = self;
191 UNLOCK(thread->lock);
192
193 if (death != SEMAPHORE_NULL) {
194 restore_sem_to_pool(death);
195 death = SEMAPHORE_NULL;
196 }
197 #if __DARWIN_UNIX03
198 /* Wait for it to signal... */
199 pthread_cleanup_push(__posix_join_cleanup, (void *)thread);
200 do {
201 res = __semwait_signal(joinsem, 0, 0, 0, (int64_t)0, (int32_t)0);
202 } while ((res < 0) && (errno == EINTR));
203 pthread_cleanup_pop(0);
204 #else /* __DARWIN_UNIX03 */
205 /* Wait for it to signal... */
206 do {
207 PTHREAD_MACH_CALL(semaphore_wait(joinsem), kern_res);
208 } while (kern_res != KERN_SUCCESS);
209 #endif /* __DARWIN_UNIX03 */
210
211 restore_sem_to_pool(joinsem);
212 res = _pthread_join_cleanup(thread, value_ptr, conforming);
213 } else {
214 UNLOCK(thread->lock);
215 res = EDEADLK;
216 }
217 } else {
218 UNLOCK(thread->lock);
219 res = EINVAL;
220 }
221 if (death != SEMAPHORE_NULL)
222 restore_sem_to_pool(death);
223 return res;
224 }/* end of new style */
225 }
226 return ESRCH;
227 }
228
229 int
230 pthread_cond_wait(pthread_cond_t *cond,
231 pthread_mutex_t *mutex)
232 {
233 int conforming;
234 #if __DARWIN_UNIX03
235
236 if (__unix_conforming == 0)
237 __unix_conforming = 1;
238
239 #ifdef VARIANT_CANCELABLE
240 conforming = 1;
241 #else /* !VARIANT_CANCELABLE */
242 conforming = -1;
243 #endif /* VARIANT_CANCELABLE */
244 #else /* __DARWIN_UNIX03 */
245 conforming = 0;
246 #endif /* __DARWIN_UNIX03 */
247 return (_pthread_cond_wait(cond, mutex, (struct timespec *)NULL, 0, conforming));
248 }
249
250 int
251 pthread_cond_timedwait(pthread_cond_t *cond,
252 pthread_mutex_t *mutex,
253 const struct timespec *abstime)
254 {
255 int conforming;
256 #if __DARWIN_UNIX03
257 if (__unix_conforming == 0)
258 __unix_conforming = 1;
259
260 #ifdef VARIANT_CANCELABLE
261 conforming = 1;
262 #else /* !VARIANT_CANCELABLE */
263 conforming = -1;
264 #endif /* VARIANT_CANCELABLE */
265 #else /* __DARWIN_UNIX03 */
266 conforming = 0;
267 #endif /* __DARWIN_UNIX03 */
268
269 return (_pthread_cond_wait(cond, mutex, abstime, 0, conforming));
270 }
271
272 int
273 sigwait(const sigset_t * set, int * sig)
274 {
275 #if __DARWIN_UNIX03
276 int err = 0;
277
278 if (__unix_conforming == 0)
279 __unix_conforming = 1;
280
281 #ifdef VARIANT_CANCELABLE
282 _pthread_testcancel(pthread_self(), 1);
283 #endif /* VARIANT_CANCELABLE */
284
285 if (__sigwait(set, sig) == -1) {
286 err = errno;
287
288 /*
289 * EINTR that isn't a result of pthread_cancel()
290 * is translated to 0.
291 */
292 if (err == EINTR) {
293 err = 0;
294 }
295 }
296 return(err);
297 #else /* __DARWIN_UNIX03 */
298 if (__sigwait(set, sig) == -1) {
299 /*
300 * EINTR that isn't a result of pthread_cancel()
301 * is translated to 0.
302 */
303 if (errno != EINTR) {
304 return -1;
305 }
306 }
307
308 return 0;
309 #endif /* __DARWIN_UNIX03 */
310 }