Get current thread stack base

IRIX/Nekoware development, porting and related topics.
Forum rules
Any posts concerning pirated software or offering to buy/sell/trade commercial software are subject to removal.
pip
Posts: 139
Joined: Thu Sep 28, 2006 6:50 pm
Location: Long Beach, CA

Get current thread stack base

Unread postby pip » Sat Mar 31, 2012 9:14 pm

It seems like a recurring problem for anyone trying to compile a modern web browser/JS engine (whether it's KJS/WebKit or Firefox) on IRIX is the lack of a function to get the stack base for the current thread. Well, here you go:

Code: Select all

#if (_MIPS_SIM == _MIPS_SIM_NABI32)
#define STK_BASE_OFFSET 0x74    // N32
#elif (_MIPS_SIM == _MIPS_SIM_ABI64)
#define STK_BASE_OFFSET 0xB8    // 64
#else
#error N32 and 64 only
#endif 

void* IRIX_currentThreadStackBase()
{   
        void **pstk = (void **)((char *)(PRDALIB->pthread_data[1]) + STK_BASE_OFFSET);
        return *pstk;
}


irix_currentThreadStackBase.c
currentThreadStackBase() implemented for IRIX
(430 Bytes) Downloaded 21 times


I figured this out when trying to compile WebKit a few years ago--KJS implements a function called currentThreadStackBase() (in kjs/Collector.cpp) which returns the base address for the current thread's stack (with #ifdefs for a bunch of different OSes, and none of the paths worked on IRIX).

For example (and as search keywords), here's how it's done in other OSes (from qt 4.4.3's WebKit):

Code: Select all

Darwin uses:
    return pthread_get_stackaddr_np(pthread_self())

Solaris:
    stack_t s;
    thr_stksegment(&s);
    return s.ss_sp;

Linux/FreeBSD:
    static void *stackBase = 0;
    static size_t stackSize = 0;
    static pthread_t stackThread;
    pthread_t thread = pthread_self();
    if (stackBase == 0 || thread != stackThread) {
        pthread_attr_t sattr;
        pthread_attr_init(&sattr);
#if HAVE(PTHREAD_NP_H)
        // e.g. on FreeBSD 5.4, neundorf@kde.org
        pthread_attr_get_np(thread, &sattr);
#else   
        // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
        pthread_getattr_np(thread, &sattr);
#endif 
        int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
        (void)rc; // FIXME: deal with error code somehow?  seems fatal...
        ASSERT(stackBase);
        pthread_attr_destroy(&sattr);
        stackThread = thread;
    }
    return (void*)(size_t(stackBase) + stackSize);


I never looked into getting the stack size (since WebKit didn't need it), but if something else (Firefox?) does, we can try to figure that out also.
:Octane: R12K/300x2, MXE
:1600SW: :ChallengeL:

telackey
Posts: 48
Joined: Tue Aug 03, 2010 9:08 am

Re: Get current thread stack base

Unread postby telackey » Sun Jul 22, 2012 11:30 am

Thanks for posting this! This is no doubt going to save me a chunk of time.
:1600SW: :O2: :O2+: :O2: :1600SW:


Return to “SGI: Development”

Who is online

Users browsing this forum: dmm and 2 guests