andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "prlog.h" andre@0: #include "prthread.h" andre@0: #include "private/pprthred.h" andre@0: #include "primpl.h" andre@0: andre@0: PR_IMPLEMENT(PRWord *) andre@0: PR_GetGCRegisters(PRThread *t, int isCurrent, int *np) andre@0: { andre@0: return _MD_HomeGCRegisters(t, isCurrent, np); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) andre@0: PR_ThreadScanStackPointers(PRThread* t, andre@0: PRScanStackFun scanFun, void* scanClosure) andre@0: { andre@0: PRThread* current = PR_GetCurrentThread(); andre@0: PRWord *sp, *esp, *p0; andre@0: int n; andre@0: void **ptd; andre@0: PRStatus status; andre@0: PRUint32 index; andre@0: int stack_end; andre@0: andre@0: /* andre@0: ** Store the thread's registers in the thread structure so the GC andre@0: ** can scan them. Then scan them. andre@0: */ andre@0: p0 = _MD_HomeGCRegisters(t, t == current, &n); andre@0: status = scanFun(t, (void**)p0, n, scanClosure); andre@0: if (status != PR_SUCCESS) andre@0: return status; andre@0: andre@0: /* Scan the C stack for pointers into the GC heap */ andre@0: #if defined(XP_PC) && defined(WIN16) andre@0: /* andre@0: ** Under WIN16, the stack of the current thread is always mapped into andre@0: ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan andre@0: ** the "task stack". Otherwise, scan the "cached stack" of the inactive andre@0: ** thread... andre@0: */ andre@0: if (t == current) { andre@0: sp = (PRWord*) &stack_end; andre@0: esp = (PRWord*) _pr_top_of_task_stack; andre@0: andre@0: PR_ASSERT(sp <= esp); andre@0: } else { andre@0: sp = (PRWord*) PR_GetSP(t); andre@0: esp = (PRWord*) t->stack->stackTop; andre@0: andre@0: PR_ASSERT((t->stack->stackSize == 0) || andre@0: ((sp > (PRWord*)t->stack->stackBottom) && andre@0: (sp <= (PRWord*)t->stack->stackTop))); andre@0: } andre@0: #else /* ! WIN16 */ andre@0: #ifdef HAVE_STACK_GROWING_UP andre@0: if (t == current) { andre@0: esp = (PRWord*) &stack_end; andre@0: } else { andre@0: esp = (PRWord*) PR_GetSP(t); andre@0: } andre@0: sp = (PRWord*) t->stack->stackTop; andre@0: if (t->stack->stackSize) { andre@0: PR_ASSERT((esp > (PRWord*)t->stack->stackTop) && andre@0: (esp < (PRWord*)t->stack->stackBottom)); andre@0: } andre@0: #else /* ! HAVE_STACK_GROWING_UP */ andre@0: if (t == current) { andre@0: sp = (PRWord*) &stack_end; andre@0: } else { andre@0: sp = (PRWord*) PR_GetSP(t); andre@0: } andre@0: esp = (PRWord*) t->stack->stackTop; andre@0: if (t->stack->stackSize) { andre@0: PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) && andre@0: (sp < (PRWord*)t->stack->stackTop)); andre@0: } andre@0: #endif /* ! HAVE_STACK_GROWING_UP */ andre@0: #endif /* ! WIN16 */ andre@0: andre@0: #if defined(WIN16) andre@0: { andre@0: prword_t scan; andre@0: prword_t limit; andre@0: andre@0: scan = (prword_t) sp; andre@0: limit = (prword_t) esp; andre@0: while (scan < limit) { andre@0: prword_t *test; andre@0: andre@0: test = *((prword_t **)scan); andre@0: status = scanFun(t, (void**)&test, 1, scanClosure); andre@0: if (status != PR_SUCCESS) andre@0: return status; andre@0: scan += sizeof(char); andre@0: } andre@0: } andre@0: #else andre@0: if (sp < esp) { andre@0: status = scanFun(t, (void**)sp, esp - sp, scanClosure); andre@0: if (status != PR_SUCCESS) andre@0: return status; andre@0: } andre@0: #endif andre@0: andre@0: /* andre@0: ** Mark all of the per-thread-data items attached to this thread andre@0: ** andre@0: ** The execution environment better be accounted for otherwise it andre@0: ** will be collected andre@0: */ andre@0: status = scanFun(t, (void**)&t->environment, 1, scanClosure); andre@0: if (status != PR_SUCCESS) andre@0: return status; andre@0: andre@0: /* if thread is not allocated on stack, this is redundant. */ andre@0: ptd = t->privateData; andre@0: for (index = 0; index < t->tpdLength; index++, ptd++) { andre@0: status = scanFun(t, (void**)ptd, 1, scanClosure); andre@0: if (status != PR_SUCCESS) andre@0: return status; andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* transducer for PR_EnumerateThreads */ andre@0: typedef struct PRScanStackData { andre@0: PRScanStackFun scanFun; andre@0: void* scanClosure; andre@0: } PRScanStackData; andre@0: andre@0: static PRStatus PR_CALLBACK andre@0: pr_ScanStack(PRThread* t, int i, void* arg) andre@0: { andre@0: PRScanStackData* data = (PRScanStackData*)arg; andre@0: return PR_ThreadScanStackPointers(t, data->scanFun, data->scanClosure); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) andre@0: PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure) andre@0: { andre@0: PRScanStackData data; andre@0: data.scanFun = scanFun; andre@0: data.scanClosure = scanClosure; andre@0: return PR_EnumerateThreads(pr_ScanStack, &data); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUword) andre@0: PR_GetStackSpaceLeft(PRThread* t) andre@0: { andre@0: PRThread *current = PR_GetCurrentThread(); andre@0: PRWord *sp, *esp; andre@0: int stack_end; andre@0: andre@0: #if defined(WIN16) andre@0: /* andre@0: ** Under WIN16, the stack of the current thread is always mapped into andre@0: ** the "task stack" (at SS:xxxx). So, if t is the current thread, scan andre@0: ** the "task stack". Otherwise, scan the "cached stack" of the inactive andre@0: ** thread... andre@0: */ andre@0: if (t == current) { andre@0: sp = (PRWord*) &stack_end; andre@0: esp = (PRWord*) _pr_top_of_task_stack; andre@0: andre@0: PR_ASSERT(sp <= esp); andre@0: } else { andre@0: sp = (PRWord*) PR_GetSP(t); andre@0: esp = (PRWord*) t->stack->stackTop; andre@0: andre@0: PR_ASSERT((t->stack->stackSize == 0) || andre@0: ((sp > (PRWord*)t->stack->stackBottom) && andre@0: (sp <= (PRWord*)t->stack->stackTop))); andre@0: } andre@0: #else /* ! WIN16 */ andre@0: #ifdef HAVE_STACK_GROWING_UP andre@0: if (t == current) { andre@0: esp = (PRWord*) &stack_end; andre@0: } else { andre@0: esp = (PRWord*) PR_GetSP(t); andre@0: } andre@0: sp = (PRWord*) t->stack->stackTop; andre@0: if (t->stack->stackSize) { andre@0: PR_ASSERT((esp > (PRWord*)t->stack->stackTop) && andre@0: (esp < (PRWord*)t->stack->stackBottom)); andre@0: } andre@0: #else /* ! HAVE_STACK_GROWING_UP */ andre@0: if (t == current) { andre@0: sp = (PRWord*) &stack_end; andre@0: } else { andre@0: sp = (PRWord*) PR_GetSP(t); andre@0: } andre@0: esp = (PRWord*) t->stack->stackTop; andre@0: if (t->stack->stackSize) { andre@0: PR_ASSERT((sp > (PRWord*)t->stack->stackBottom) && andre@0: (sp < (PRWord*)t->stack->stackTop)); andre@0: } andre@0: #endif /* ! HAVE_STACK_GROWING_UP */ andre@0: #endif /* ! WIN16 */ andre@0: return (PRUword)t->stack->stackSize - ((PRWord)esp - (PRWord)sp); andre@0: }