2 * Copyright (c) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #define _XOPEN_SOURCE 600L
35 extern size_t pthread_get_stacksize_np(pthread_t
);
36 extern void *pthread_get_stackaddr_np(pthread_t
);
38 extern int __in_sigtramp
;
39 #endif /* __DYNAMIC_ */
41 __private_extern__ mcontext_t
42 getmcontext(ucontext_t
*uctx
, void *sp
)
44 pthread_t self
= pthread_self();
45 mcontext_t mctx
= (mcontext_t
)&uctx
->__mcontext_data
;
46 size_t stacksize
= pthread_get_stacksize_np(self
);
49 uctx
->uc_stack
.ss_sp
= sp
;
50 uctx
->uc_stack
.ss_flags
= 0;
52 if (0 == sigaltstack(NULL
, &stack
)) {
53 if (stack
.ss_flags
& SS_ONSTACK
) {
54 uctx
->uc_stack
= stack
;
55 stacksize
= stack
.ss_size
;
59 if (stacksize
== 0) { /* main thread doesn't have pthread stack size */
61 if (0 == getrlimit(RLIMIT_STACK
, &rlim
))
62 stacksize
= rlim
.rlim_cur
;
65 uctx
->uc_stack
.ss_size
= stacksize
;
67 if (uctx
->uc_mcontext
!= mctx
) {
68 uctx
->uc_mcontext
= mctx
;
71 uctx
->uc_link
= (ucontext_t
*)__in_sigtramp
; /* non-zero if in signal handler */
72 #else /* !__DYNAMIC__ */
74 #endif /* __DYNAMIC__ */
78 sigprocmask(0, NULL
, &uctx
->uc_sigmask
);