1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
43 #include "private/pprthred.h"
47 PR_IMPLEMENT(PRWord
*)
48 PR_GetGCRegisters(PRThread
*t
, int isCurrent
, int *np
)
50 return _MD_HomeGCRegisters(t
, isCurrent
, np
);
53 PR_IMPLEMENT(PRStatus
)
54 PR_ThreadScanStackPointers(PRThread
* t
,
55 PRScanStackFun scanFun
, void* scanClosure
)
57 PRThread
* current
= PR_GetCurrentThread();
58 PRWord
*sp
, *esp
, *p0
;
66 ** Store the thread's registers in the thread structure so the GC
67 ** can scan them. Then scan them.
69 p0
= _MD_HomeGCRegisters(t
, t
== current
, &n
);
70 status
= scanFun(t
, (void**)p0
, n
, scanClosure
);
71 if (status
!= PR_SUCCESS
)
74 /* Scan the C stack for pointers into the GC heap */
75 #if defined(XP_PC) && defined(WIN16)
77 ** Under WIN16, the stack of the current thread is always mapped into
78 ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan
79 ** the "task stack". Otherwise, scan the "cached stack" of the inactive
83 sp
= (PRWord
*) &stack_end
;
84 esp
= (PRWord
*) _pr_top_of_task_stack
;
88 sp
= (PRWord
*) PR_GetSP(t
);
89 esp
= (PRWord
*) t
->stack
->stackTop
;
91 PR_ASSERT((t
->stack
->stackSize
== 0) ||
92 ((sp
> (PRWord
*)t
->stack
->stackBottom
) &&
93 (sp
<= (PRWord
*)t
->stack
->stackTop
)));
96 #ifdef HAVE_STACK_GROWING_UP
98 esp
= (PRWord
*) &stack_end
;
100 esp
= (PRWord
*) PR_GetSP(t
);
102 sp
= (PRWord
*) t
->stack
->stackTop
;
103 if (t
->stack
->stackSize
) {
104 PR_ASSERT((esp
> (PRWord
*)t
->stack
->stackTop
) &&
105 (esp
< (PRWord
*)t
->stack
->stackBottom
));
107 #else /* ! HAVE_STACK_GROWING_UP */
109 sp
= (PRWord
*) &stack_end
;
111 sp
= (PRWord
*) PR_GetSP(t
);
113 esp
= (PRWord
*) t
->stack
->stackTop
;
114 if (t
->stack
->stackSize
) {
115 PR_ASSERT((sp
> (PRWord
*)t
->stack
->stackBottom
) &&
116 (sp
< (PRWord
*)t
->stack
->stackTop
));
118 #endif /* ! HAVE_STACK_GROWING_UP */
126 scan
= (prword_t
) sp
;
127 limit
= (prword_t
) esp
;
128 while (scan
< limit
) {
131 test
= *((prword_t
**)scan
);
132 status
= scanFun(t
, (void**)&test
, 1, scanClosure
);
133 if (status
!= PR_SUCCESS
)
135 scan
+= sizeof(char);
140 status
= scanFun(t
, (void**)sp
, esp
- sp
, scanClosure
);
141 if (status
!= PR_SUCCESS
)
147 ** Mark all of the per-thread-data items attached to this thread
149 ** The execution environment better be accounted for otherwise it
152 status
= scanFun(t
, (void**)&t
->environment
, 1, scanClosure
);
153 if (status
!= PR_SUCCESS
)
156 #ifndef GC_LEAK_DETECTOR
157 /* if thread is not allocated on stack, this is redundant. */
158 ptd
= t
->privateData
;
159 for (index
= 0; index
< t
->tpdLength
; index
++, ptd
++) {
160 status
= scanFun(t
, (void**)ptd
, 1, scanClosure
);
161 if (status
!= PR_SUCCESS
)
169 /* transducer for PR_EnumerateThreads */
170 typedef struct PRScanStackData
{
171 PRScanStackFun scanFun
;
175 static PRStatus PR_CALLBACK
176 pr_ScanStack(PRThread
* t
, int i
, void* arg
)
181 PRScanStackData
* data
= (PRScanStackData
*)arg
;
182 return PR_ThreadScanStackPointers(t
, data
->scanFun
, data
->scanClosure
);
185 PR_IMPLEMENT(PRStatus
)
186 PR_ScanStackPointers(PRScanStackFun scanFun
, void* scanClosure
)
188 PRScanStackData data
;
189 data
.scanFun
= scanFun
;
190 data
.scanClosure
= scanClosure
;
191 return PR_EnumerateThreads(pr_ScanStack
, &data
);
194 PR_IMPLEMENT(PRUword
)
195 PR_GetStackSpaceLeft(PRThread
* t
)
197 PRThread
*current
= PR_GetCurrentThread();
203 ** Under WIN16, the stack of the current thread is always mapped into
204 ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan
205 ** the "task stack". Otherwise, scan the "cached stack" of the inactive
209 sp
= (PRWord
*) &stack_end
;
210 esp
= (PRWord
*) _pr_top_of_task_stack
;
212 PR_ASSERT(sp
<= esp
);
214 sp
= (PRWord
*) PR_GetSP(t
);
215 esp
= (PRWord
*) t
->stack
->stackTop
;
217 PR_ASSERT((t
->stack
->stackSize
== 0) ||
218 ((sp
> (PRWord
*)t
->stack
->stackBottom
) &&
219 (sp
<= (PRWord
*)t
->stack
->stackTop
)));
222 #ifdef HAVE_STACK_GROWING_UP
224 esp
= (PRWord
*) &stack_end
;
226 esp
= (PRWord
*) PR_GetSP(t
);
228 sp
= (PRWord
*) t
->stack
->stackTop
;
229 if (t
->stack
->stackSize
) {
230 PR_ASSERT((esp
> (PRWord
*)t
->stack
->stackTop
) &&
231 (esp
< (PRWord
*)t
->stack
->stackBottom
));
233 #else /* ! HAVE_STACK_GROWING_UP */
235 sp
= (PRWord
*) &stack_end
;
237 sp
= (PRWord
*) PR_GetSP(t
);
239 esp
= (PRWord
*) t
->stack
->stackTop
;
240 if (t
->stack
->stackSize
) {
241 PR_ASSERT((sp
> (PRWord
*)t
->stack
->stackBottom
) &&
242 (sp
< (PRWord
*)t
->stack
->stackTop
));
244 #endif /* ! HAVE_STACK_GROWING_UP */
246 return (PRUword
)t
->stack
->stackSize
- ((PRWord
)esp
- (PRWord
)sp
);