michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** File: ptthread.c michael@0: ** Descritpion: Implemenation for threds using pthreds michael@0: ** Exports: ptthread.h michael@0: */ michael@0: michael@0: #if defined(_PR_PTHREADS) || defined(_PR_DCETHREADS) michael@0: michael@0: #include "prlog.h" michael@0: #include "primpl.h" michael@0: #include "prpdce.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef SYMBIAN michael@0: /* In Open C sched_get_priority_min/max do not work properly, so we undefine michael@0: * _POSIX_THREAD_PRIORITY_SCHEDULING here. michael@0: */ michael@0: #undef _POSIX_THREAD_PRIORITY_SCHEDULING michael@0: #endif michael@0: michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: #undef _POSIX_THREAD_PRIORITY_SCHEDULING michael@0: #include michael@0: #ifndef HAVE_GETTID michael@0: #define gettid() (syscall(SYS_gettid)) michael@0: #endif michael@0: #endif michael@0: michael@0: /* michael@0: * Record whether or not we have the privilege to set the scheduling michael@0: * policy and priority of threads. 0 means that privilege is available. michael@0: * EPERM means that privilege is not available. michael@0: */ michael@0: michael@0: static PRIntn pt_schedpriv = 0; michael@0: extern PRLock *_pr_sleeplock; michael@0: michael@0: static struct _PT_Bookeeping michael@0: { michael@0: PRLock *ml; /* a lock to protect ourselves */ michael@0: PRCondVar *cv; /* used to signal global things */ michael@0: PRInt32 system, user; /* a count of the two different types */ michael@0: PRUintn this_many; /* number of threads allowed for exit */ michael@0: pthread_key_t key; /* thread private data key */ michael@0: PRBool keyCreated; /* whether 'key' should be deleted */ michael@0: PRThread *first, *last; /* list of threads we know about */ michael@0: #if defined(_PR_DCETHREADS) || defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: PRInt32 minPrio, maxPrio; /* range of scheduling priorities */ michael@0: #endif michael@0: } pt_book = {0}; michael@0: michael@0: static void _pt_thread_death(void *arg); michael@0: static void _pt_thread_death_internal(void *arg, PRBool callDestructors); michael@0: static void init_pthread_gc_support(void); michael@0: michael@0: #if defined(_PR_DCETHREADS) || defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: static PRIntn pt_PriorityMap(PRThreadPriority pri) michael@0: { michael@0: #ifdef NTO michael@0: /* This priority algorithm causes lots of problems on Neutrino michael@0: * for now I have just hard coded everything to run at priority 10 michael@0: * until I can come up with a new algorithm. michael@0: * Jerry.Kirk@Nexwarecorp.com michael@0: */ michael@0: return 10; michael@0: #else michael@0: return pt_book.minPrio + michael@0: pri * (pt_book.maxPrio - pt_book.minPrio) / PR_PRIORITY_LAST; michael@0: #endif michael@0: } michael@0: #elif defined(_PR_NICE_PRIORITY_SCHEDULING) michael@0: /* michael@0: * This functions maps higher priorities to lower nice values relative to the michael@0: * nice value specified in the |nice| parameter. The corresponding relative michael@0: * adjustments are: michael@0: * michael@0: * PR_PRIORITY_LOW +1 michael@0: * PR_PRIORITY_NORMAL 0 michael@0: * PR_PRIORITY_HIGH -1 michael@0: * PR_PRIORITY_URGENT -2 michael@0: */ michael@0: static int pt_RelativePriority(int nice, PRThreadPriority pri) michael@0: { michael@0: return nice + (1 - pri); michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: ** Initialize a stack for a native pthread thread michael@0: */ michael@0: static void _PR_InitializeStack(PRThreadStack *ts) michael@0: { michael@0: if( ts && (ts->stackTop == 0) ) { michael@0: ts->allocBase = (char *) &ts; michael@0: ts->allocSize = ts->stackSize; michael@0: michael@0: /* michael@0: ** Setup stackTop and stackBottom values. michael@0: */ michael@0: #ifdef HAVE_STACK_GROWING_UP michael@0: ts->stackBottom = ts->allocBase + ts->stackSize; michael@0: ts->stackTop = ts->allocBase; michael@0: #else michael@0: ts->stackTop = ts->allocBase; michael@0: ts->stackBottom = ts->allocBase - ts->stackSize; michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static void *_pt_root(void *arg) michael@0: { michael@0: PRIntn rv; michael@0: PRThread *thred = (PRThread*)arg; michael@0: PRBool detached = (thred->state & PT_THREAD_DETACHED) ? PR_TRUE : PR_FALSE; michael@0: pthread_t id = pthread_self(); michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: pid_t tid; michael@0: #endif michael@0: michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: /* michael@0: * We need to know the kernel thread ID of each thread in order to michael@0: * set its nice value hence we do it here instead of at creation time. michael@0: */ michael@0: tid = gettid(); michael@0: errno = 0; michael@0: rv = getpriority(PRIO_PROCESS, 0); michael@0: michael@0: /* If we cannot read the main thread's nice value don't try to change the michael@0: * new thread's nice value. */ michael@0: if (errno == 0) { michael@0: setpriority(PRIO_PROCESS, tid, michael@0: pt_RelativePriority(rv, thred->priority)); michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: ** DCE Threads can't detach during creation, so do it late. michael@0: ** I would like to do it only here, but that doesn't seem michael@0: ** to work. michael@0: */ michael@0: #if defined(_PR_DCETHREADS) michael@0: if (detached) michael@0: { michael@0: /* pthread_detach() modifies its argument, so we must pass a copy */ michael@0: pthread_t self = id; michael@0: rv = pthread_detach(&self); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: #endif /* defined(_PR_DCETHREADS) */ michael@0: michael@0: /* Set up the thread stack information */ michael@0: _PR_InitializeStack(thred->stack); michael@0: michael@0: /* michael@0: * Set within the current thread the pointer to our object. michael@0: * This object will be deleted when the thread termintates, michael@0: * whether in a join or detached (see _PR_InitThreads()). michael@0: */ michael@0: rv = pthread_setspecific(pt_book.key, thred); michael@0: PR_ASSERT(0 == rv); michael@0: michael@0: /* make the thread visible to the rest of the runtime */ michael@0: PR_Lock(pt_book.ml); michael@0: /* michael@0: * Both the parent thread and this new thread set thred->id. michael@0: * The new thread must ensure that thred->id is set before michael@0: * it executes its startFunc. The parent thread must ensure michael@0: * that thred->id is set before PR_CreateThread() returns. michael@0: * Both threads set thred->id while holding pt_book.ml and michael@0: * use thred->idSet to ensure thred->id is written only once. michael@0: */ michael@0: if (!thred->idSet) michael@0: { michael@0: thred->id = id; michael@0: thred->idSet = PR_TRUE; michael@0: } michael@0: else michael@0: { michael@0: PR_ASSERT(pthread_equal(thred->id, id)); michael@0: } michael@0: michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: thred->tid = tid; michael@0: PR_NotifyAllCondVar(pt_book.cv); michael@0: #endif michael@0: michael@0: /* If this is a GCABLE thread, set its state appropriately */ michael@0: if (thred->suspend & PT_THREAD_SETGCABLE) michael@0: thred->state |= PT_THREAD_GCABLE; michael@0: thred->suspend = 0; michael@0: michael@0: thred->prev = pt_book.last; michael@0: if (pt_book.last) michael@0: pt_book.last->next = thred; michael@0: else michael@0: pt_book.first = thred; michael@0: thred->next = NULL; michael@0: pt_book.last = thred; michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: thred->startFunc(thred->arg); /* make visible to the client */ michael@0: michael@0: /* unhook the thread from the runtime */ michael@0: PR_Lock(pt_book.ml); michael@0: /* michael@0: * At this moment, PR_CreateThread() may not have set thred->id yet. michael@0: * It is safe for a detached thread to free thred only after michael@0: * PR_CreateThread() has accessed thred->id and thred->idSet. michael@0: */ michael@0: if (detached) michael@0: { michael@0: while (!thred->okToDelete) michael@0: PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: michael@0: if (thred->state & PT_THREAD_SYSTEM) michael@0: pt_book.system -= 1; michael@0: else if (--pt_book.user == pt_book.this_many) michael@0: PR_NotifyAllCondVar(pt_book.cv); michael@0: if (NULL == thred->prev) michael@0: pt_book.first = thred->next; michael@0: else michael@0: thred->prev->next = thred->next; michael@0: if (NULL == thred->next) michael@0: pt_book.last = thred->prev; michael@0: else michael@0: thred->next->prev = thred->prev; michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: /* michael@0: * Here we set the pthread's backpointer to the PRThread to NULL. michael@0: * Otherwise the destructor would get called eagerly as the thread michael@0: * returns to the pthread runtime. The joining thread would them be michael@0: * the proud possessor of a dangling reference. However, this is the michael@0: * last chance to delete the object if the thread is detached, so michael@0: * just let the destructor do the work. michael@0: */ michael@0: if (PR_FALSE == detached) michael@0: { michael@0: /* Call TPD destructors on this thread. */ michael@0: _PR_DestroyThreadPrivate(thred); michael@0: rv = pthread_setspecific(pt_book.key, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: michael@0: return NULL; michael@0: } /* _pt_root */ michael@0: michael@0: static PRThread* pt_AttachThread(void) michael@0: { michael@0: PRThread *thred = NULL; michael@0: michael@0: /* michael@0: * NSPR must have been initialized when PR_AttachThread is called. michael@0: * We cannot have PR_AttachThread call implicit initialization michael@0: * because if multiple threads call PR_AttachThread simultaneously, michael@0: * NSPR may be initialized more than once. michael@0: * We can't call any function that calls PR_GetCurrentThread() michael@0: * either (e.g., PR_SetError()) as that will result in infinite michael@0: * recursion. michael@0: */ michael@0: if (!_pr_initialized) return NULL; michael@0: michael@0: /* PR_NEWZAP must not call PR_GetCurrentThread() */ michael@0: thred = PR_NEWZAP(PRThread); michael@0: if (NULL != thred) michael@0: { michael@0: int rv; michael@0: michael@0: thred->priority = PR_PRIORITY_NORMAL; michael@0: thred->id = pthread_self(); michael@0: thred->idSet = PR_TRUE; michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: thred->tid = gettid(); michael@0: #endif michael@0: rv = pthread_setspecific(pt_book.key, thred); michael@0: PR_ASSERT(0 == rv); michael@0: michael@0: thred->state = PT_THREAD_GLOBAL | PT_THREAD_FOREIGN; michael@0: PR_Lock(pt_book.ml); michael@0: michael@0: /* then put it into the list */ michael@0: thred->prev = pt_book.last; michael@0: if (pt_book.last) michael@0: pt_book.last->next = thred; michael@0: else michael@0: pt_book.first = thred; michael@0: thred->next = NULL; michael@0: pt_book.last = thred; michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: } michael@0: return thred; /* may be NULL */ michael@0: } /* pt_AttachThread */ michael@0: michael@0: static PRThread* _PR_CreateThread( michael@0: PRThreadType type, void (*start)(void *arg), michael@0: void *arg, PRThreadPriority priority, PRThreadScope scope, michael@0: PRThreadState state, PRUint32 stackSize, PRBool isGCAble) michael@0: { michael@0: int rv; michael@0: PRThread *thred; michael@0: pthread_attr_t tattr; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)priority) michael@0: priority = PR_PRIORITY_FIRST; michael@0: else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)priority) michael@0: priority = PR_PRIORITY_LAST; michael@0: michael@0: rv = _PT_PTHREAD_ATTR_INIT(&tattr); michael@0: PR_ASSERT(0 == rv); michael@0: michael@0: if (EPERM != pt_schedpriv) michael@0: { michael@0: #if !defined(_PR_DCETHREADS) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: struct sched_param schedule; michael@0: #endif michael@0: michael@0: #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: rv = pthread_attr_setinheritsched(&tattr, PTHREAD_EXPLICIT_SCHED); michael@0: PR_ASSERT(0 == rv); michael@0: #endif michael@0: michael@0: /* Use the default scheduling policy */ michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: rv = pthread_attr_setprio(&tattr, pt_PriorityMap(priority)); michael@0: PR_ASSERT(0 == rv); michael@0: #elif defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: rv = pthread_attr_getschedparam(&tattr, &schedule); michael@0: PR_ASSERT(0 == rv); michael@0: schedule.sched_priority = pt_PriorityMap(priority); michael@0: rv = pthread_attr_setschedparam(&tattr, &schedule); michael@0: PR_ASSERT(0 == rv); michael@0: #ifdef NTO michael@0: rv = pthread_attr_setschedpolicy(&tattr, SCHED_RR); /* Round Robin */ michael@0: PR_ASSERT(0 == rv); michael@0: #endif michael@0: #endif /* !defined(_PR_DCETHREADS) */ michael@0: } michael@0: michael@0: /* michael@0: * DCE threads can't set detach state before creating the thread. michael@0: * AIX can't set detach late. Why can't we all just get along? michael@0: */ michael@0: #if !defined(_PR_DCETHREADS) michael@0: rv = pthread_attr_setdetachstate(&tattr, michael@0: ((PR_JOINABLE_THREAD == state) ? michael@0: PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)); michael@0: PR_ASSERT(0 == rv); michael@0: #endif /* !defined(_PR_DCETHREADS) */ michael@0: michael@0: /* michael@0: * If stackSize is 0, we use the default pthread stack size. michael@0: */ michael@0: if (stackSize) michael@0: { michael@0: #ifdef _MD_MINIMUM_STACK_SIZE michael@0: if (stackSize < _MD_MINIMUM_STACK_SIZE) michael@0: stackSize = _MD_MINIMUM_STACK_SIZE; michael@0: #endif michael@0: rv = pthread_attr_setstacksize(&tattr, stackSize); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: michael@0: thred = PR_NEWZAP(PRThread); michael@0: if (NULL == thred) michael@0: { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, errno); michael@0: goto done; michael@0: } michael@0: else michael@0: { michael@0: pthread_t id; michael@0: michael@0: thred->arg = arg; michael@0: thred->startFunc = start; michael@0: thred->priority = priority; michael@0: if (PR_UNJOINABLE_THREAD == state) michael@0: thred->state |= PT_THREAD_DETACHED; michael@0: michael@0: if (PR_LOCAL_THREAD == scope) michael@0: scope = PR_GLOBAL_THREAD; michael@0: michael@0: if (PR_GLOBAL_BOUND_THREAD == scope) { michael@0: #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); michael@0: if (rv) { michael@0: /* michael@0: * system scope not supported michael@0: */ michael@0: scope = PR_GLOBAL_THREAD; michael@0: /* michael@0: * reset scope michael@0: */ michael@0: rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_PROCESS); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: #endif michael@0: } michael@0: if (PR_GLOBAL_THREAD == scope) michael@0: thred->state |= PT_THREAD_GLOBAL; michael@0: else if (PR_GLOBAL_BOUND_THREAD == scope) michael@0: thred->state |= (PT_THREAD_GLOBAL | PT_THREAD_BOUND); michael@0: else /* force it global */ michael@0: thred->state |= PT_THREAD_GLOBAL; michael@0: if (PR_SYSTEM_THREAD == type) michael@0: thred->state |= PT_THREAD_SYSTEM; michael@0: michael@0: thred->suspend =(isGCAble) ? PT_THREAD_SETGCABLE : 0; michael@0: michael@0: thred->stack = PR_NEWZAP(PRThreadStack); michael@0: if (thred->stack == NULL) { michael@0: PRIntn oserr = errno; michael@0: PR_Free(thred); /* all that work ... poof! */ michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, oserr); michael@0: thred = NULL; /* and for what? */ michael@0: goto done; michael@0: } michael@0: thred->stack->stackSize = stackSize; michael@0: thred->stack->thr = thred; michael@0: michael@0: #ifdef PT_NO_SIGTIMEDWAIT michael@0: pthread_mutex_init(&thred->suspendResumeMutex,NULL); michael@0: pthread_cond_init(&thred->suspendResumeCV,NULL); michael@0: #endif michael@0: michael@0: /* make the thread counted to the rest of the runtime */ michael@0: PR_Lock(pt_book.ml); michael@0: if (PR_SYSTEM_THREAD == type) michael@0: pt_book.system += 1; michael@0: else pt_book.user += 1; michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: /* michael@0: * We pass a pointer to a local copy (instead of thred->id) michael@0: * to pthread_create() because who knows what wacky things michael@0: * pthread_create() may be doing to its argument. michael@0: */ michael@0: rv = _PT_PTHREAD_CREATE(&id, tattr, _pt_root, thred); michael@0: michael@0: #if !defined(_PR_DCETHREADS) michael@0: if (EPERM == rv) michael@0: { michael@0: #if defined(IRIX) michael@0: if (PR_GLOBAL_BOUND_THREAD == scope) { michael@0: /* michael@0: * SCOPE_SYSTEM requires appropriate privilege michael@0: * reset to process scope and try again michael@0: */ michael@0: rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_PROCESS); michael@0: PR_ASSERT(0 == rv); michael@0: thred->state &= ~PT_THREAD_BOUND; michael@0: } michael@0: #else michael@0: /* Remember that we don't have thread scheduling privilege. */ michael@0: pt_schedpriv = EPERM; michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, michael@0: ("_PR_CreateThread: no thread scheduling privilege")); michael@0: /* Try creating the thread again without setting priority. */ michael@0: #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: rv = pthread_attr_setinheritsched(&tattr, PTHREAD_INHERIT_SCHED); michael@0: PR_ASSERT(0 == rv); michael@0: #endif michael@0: #endif /* IRIX */ michael@0: rv = _PT_PTHREAD_CREATE(&id, tattr, _pt_root, thred); michael@0: } michael@0: #endif michael@0: michael@0: if (0 != rv) michael@0: { michael@0: #if defined(_PR_DCETHREADS) michael@0: PRIntn oserr = errno; michael@0: #else michael@0: PRIntn oserr = rv; michael@0: #endif michael@0: PR_Lock(pt_book.ml); michael@0: if (thred->state & PT_THREAD_SYSTEM) michael@0: pt_book.system -= 1; michael@0: else if (--pt_book.user == pt_book.this_many) michael@0: PR_NotifyAllCondVar(pt_book.cv); michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: PR_Free(thred->stack); michael@0: PR_Free(thred); /* all that work ... poof! */ michael@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, oserr); michael@0: thred = NULL; /* and for what? */ michael@0: goto done; michael@0: } michael@0: michael@0: PR_Lock(pt_book.ml); michael@0: /* michael@0: * Both the parent thread and this new thread set thred->id. michael@0: * The parent thread must ensure that thred->id is set before michael@0: * PR_CreateThread() returns. (See comments in _pt_root().) michael@0: */ michael@0: if (!thred->idSet) michael@0: { michael@0: thred->id = id; michael@0: thred->idSet = PR_TRUE; michael@0: } michael@0: else michael@0: { michael@0: PR_ASSERT(pthread_equal(thred->id, id)); michael@0: } michael@0: michael@0: /* michael@0: * If the new thread is detached, tell it that PR_CreateThread() has michael@0: * accessed thred->id and thred->idSet so it's ok to delete thred. michael@0: */ michael@0: if (PR_UNJOINABLE_THREAD == state) michael@0: { michael@0: thred->okToDelete = PR_TRUE; michael@0: PR_NotifyAllCondVar(pt_book.cv); michael@0: } michael@0: PR_Unlock(pt_book.ml); michael@0: } michael@0: michael@0: done: michael@0: rv = _PT_PTHREAD_ATTR_DESTROY(&tattr); michael@0: PR_ASSERT(0 == rv); michael@0: michael@0: return thred; michael@0: } /* _PR_CreateThread */ michael@0: michael@0: PR_IMPLEMENT(PRThread*) PR_CreateThread( michael@0: PRThreadType type, void (*start)(void *arg), void *arg, michael@0: PRThreadPriority priority, PRThreadScope scope, michael@0: PRThreadState state, PRUint32 stackSize) michael@0: { michael@0: return _PR_CreateThread( michael@0: type, start, arg, priority, scope, state, stackSize, PR_FALSE); michael@0: } /* PR_CreateThread */ michael@0: michael@0: PR_IMPLEMENT(PRThread*) PR_CreateThreadGCAble( michael@0: PRThreadType type, void (*start)(void *arg), void *arg, michael@0: PRThreadPriority priority, PRThreadScope scope, michael@0: PRThreadState state, PRUint32 stackSize) michael@0: { michael@0: return _PR_CreateThread( michael@0: type, start, arg, priority, scope, state, stackSize, PR_TRUE); michael@0: } /* PR_CreateThreadGCAble */ michael@0: michael@0: PR_IMPLEMENT(void*) GetExecutionEnvironment(PRThread *thred) michael@0: { michael@0: return thred->environment; michael@0: } /* GetExecutionEnvironment */ michael@0: michael@0: PR_IMPLEMENT(void) SetExecutionEnvironment(PRThread *thred, void *env) michael@0: { michael@0: thred->environment = env; michael@0: } /* SetExecutionEnvironment */ michael@0: michael@0: PR_IMPLEMENT(PRThread*) PR_AttachThread( michael@0: PRThreadType type, PRThreadPriority priority, PRThreadStack *stack) michael@0: { michael@0: return PR_GetCurrentThread(); michael@0: } /* PR_AttachThread */ michael@0: michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thred) michael@0: { michael@0: int rv = -1; michael@0: void *result = NULL; michael@0: PR_ASSERT(thred != NULL); michael@0: michael@0: if ((0xafafafaf == thred->state) michael@0: || (PT_THREAD_DETACHED == (PT_THREAD_DETACHED & thred->state)) michael@0: || (PT_THREAD_FOREIGN == (PT_THREAD_FOREIGN & thred->state))) michael@0: { michael@0: /* michael@0: * This might be a bad address, but if it isn't, the state should michael@0: * either be an unjoinable thread or it's already had the object michael@0: * deleted. However, the client that called join on a detached michael@0: * thread deserves all the rath I can muster.... michael@0: */ michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: PR_LogPrint( michael@0: "PR_JoinThread: %p not joinable | already smashed\n", thred); michael@0: } michael@0: else michael@0: { michael@0: pthread_t id = thred->id; michael@0: rv = pthread_join(id, &result); michael@0: PR_ASSERT(rv == 0 && result == NULL); michael@0: if (0 == rv) michael@0: { michael@0: #ifdef _PR_DCETHREADS michael@0: rv = pthread_detach(&id); michael@0: PR_ASSERT(0 == rv); michael@0: #endif michael@0: /* michael@0: * PR_FALSE, because the thread already called the TPD michael@0: * destructors before exiting _pt_root. michael@0: */ michael@0: _pt_thread_death_internal(thred, PR_FALSE); michael@0: } michael@0: else michael@0: { michael@0: PRErrorCode prerror; michael@0: switch (rv) michael@0: { michael@0: case EINVAL: /* not a joinable thread */ michael@0: case ESRCH: /* no thread with given ID */ michael@0: prerror = PR_INVALID_ARGUMENT_ERROR; michael@0: break; michael@0: case EDEADLK: /* a thread joining with itself */ michael@0: prerror = PR_DEADLOCK_ERROR; michael@0: break; michael@0: default: michael@0: prerror = PR_UNKNOWN_ERROR; michael@0: break; michael@0: } michael@0: PR_SetError(prerror, rv); michael@0: } michael@0: } michael@0: return (0 == rv) ? PR_SUCCESS : PR_FAILURE; michael@0: } /* PR_JoinThread */ michael@0: michael@0: PR_IMPLEMENT(void) PR_DetachThread(void) michael@0: { michael@0: void *thred; michael@0: int rv; michael@0: michael@0: _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); michael@0: if (NULL == thred) return; michael@0: _pt_thread_death(thred); michael@0: rv = pthread_setspecific(pt_book.key, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: } /* PR_DetachThread */ michael@0: michael@0: PR_IMPLEMENT(PRThread*) PR_GetCurrentThread(void) michael@0: { michael@0: void *thred; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); michael@0: if (NULL == thred) thred = pt_AttachThread(); michael@0: PR_ASSERT(NULL != thred); michael@0: return (PRThread*)thred; michael@0: } /* PR_GetCurrentThread */ michael@0: michael@0: PR_IMPLEMENT(PRThreadScope) PR_GetThreadScope(const PRThread *thred) michael@0: { michael@0: return (thred->state & PT_THREAD_BOUND) ? michael@0: PR_GLOBAL_BOUND_THREAD : PR_GLOBAL_THREAD; michael@0: } /* PR_GetThreadScope() */ michael@0: michael@0: PR_IMPLEMENT(PRThreadType) PR_GetThreadType(const PRThread *thred) michael@0: { michael@0: return (thred->state & PT_THREAD_SYSTEM) ? michael@0: PR_SYSTEM_THREAD : PR_USER_THREAD; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRThreadState) PR_GetThreadState(const PRThread *thred) michael@0: { michael@0: return (thred->state & PT_THREAD_DETACHED) ? michael@0: PR_UNJOINABLE_THREAD : PR_JOINABLE_THREAD; michael@0: } /* PR_GetThreadState */ michael@0: michael@0: PR_IMPLEMENT(PRThreadPriority) PR_GetThreadPriority(const PRThread *thred) michael@0: { michael@0: PR_ASSERT(thred != NULL); michael@0: return thred->priority; michael@0: } /* PR_GetThreadPriority */ michael@0: michael@0: PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thred, PRThreadPriority newPri) michael@0: { michael@0: PRIntn rv = -1; michael@0: michael@0: PR_ASSERT(NULL != thred); michael@0: michael@0: if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)newPri) michael@0: newPri = PR_PRIORITY_FIRST; michael@0: else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)newPri) michael@0: newPri = PR_PRIORITY_LAST; michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: rv = pthread_setprio(thred->id, pt_PriorityMap(newPri)); michael@0: /* pthread_setprio returns the old priority */ michael@0: #elif defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: if (EPERM != pt_schedpriv) michael@0: { michael@0: int policy; michael@0: struct sched_param schedule; michael@0: michael@0: rv = pthread_getschedparam(thred->id, &policy, &schedule); michael@0: if(0 == rv) { michael@0: schedule.sched_priority = pt_PriorityMap(newPri); michael@0: rv = pthread_setschedparam(thred->id, policy, &schedule); michael@0: if (EPERM == rv) michael@0: { michael@0: pt_schedpriv = EPERM; michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, michael@0: ("PR_SetThreadPriority: no thread scheduling privilege")); michael@0: } michael@0: } michael@0: if (rv != 0) michael@0: rv = -1; michael@0: } michael@0: #elif defined(_PR_NICE_PRIORITY_SCHEDULING) michael@0: PR_Lock(pt_book.ml); michael@0: while (thred->tid == 0) michael@0: PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: errno = 0; michael@0: rv = getpriority(PRIO_PROCESS, 0); michael@0: michael@0: /* Do not proceed unless we know the main thread's nice value. */ michael@0: if (errno == 0) { michael@0: rv = setpriority(PRIO_PROCESS, thred->tid, michael@0: pt_RelativePriority(rv, newPri)); michael@0: michael@0: if (rv == -1) michael@0: { michael@0: /* We don't set pt_schedpriv to EPERM in case errno == EPERM michael@0: * because adjusting the nice value might be permitted for certain michael@0: * ranges but not for others. */ michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, michael@0: ("PR_SetThreadPriority: setpriority failed with error %d", michael@0: errno)); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: thred->priority = newPri; michael@0: } /* PR_SetThreadPriority */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_Interrupt(PRThread *thred) michael@0: { michael@0: /* michael@0: ** If the target thread indicates that it's waiting, michael@0: ** find the condition and broadcast to it. Broadcast michael@0: ** since we don't know which thread (if there are more michael@0: ** than one). This sounds risky, but clients must michael@0: ** test their invariants when resumed from a wait and michael@0: ** I don't expect very many threads to be waiting on michael@0: ** a single condition and I don't expect interrupt to michael@0: ** be used very often. michael@0: ** michael@0: ** I don't know why I thought this would work. Must have michael@0: ** been one of those weaker momements after I'd been michael@0: ** smelling the vapors. michael@0: ** michael@0: ** Even with the followng changes it is possible that michael@0: ** the pointer to the condition variable is pointing michael@0: ** at a bogus value. Will the unerlying code detect michael@0: ** that? michael@0: */ michael@0: PRCondVar *cv; michael@0: PR_ASSERT(NULL != thred); michael@0: if (NULL == thred) return PR_FAILURE; michael@0: michael@0: thred->state |= PT_THREAD_ABORTED; michael@0: michael@0: cv = thred->waiting; michael@0: if ((NULL != cv) && !thred->interrupt_blocked) michael@0: { michael@0: PRIntn rv; michael@0: (void)PR_ATOMIC_INCREMENT(&cv->notify_pending); michael@0: rv = pthread_cond_broadcast(&cv->cv); michael@0: PR_ASSERT(0 == rv); michael@0: if (0 > PR_ATOMIC_DECREMENT(&cv->notify_pending)) michael@0: PR_DestroyCondVar(cv); michael@0: } michael@0: return PR_SUCCESS; michael@0: } /* PR_Interrupt */ michael@0: michael@0: PR_IMPLEMENT(void) PR_ClearInterrupt(void) michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: me->state &= ~PT_THREAD_ABORTED; michael@0: } /* PR_ClearInterrupt */ michael@0: michael@0: PR_IMPLEMENT(void) PR_BlockInterrupt(void) michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: _PT_THREAD_BLOCK_INTERRUPT(me); michael@0: } /* PR_BlockInterrupt */ michael@0: michael@0: PR_IMPLEMENT(void) PR_UnblockInterrupt(void) michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: _PT_THREAD_UNBLOCK_INTERRUPT(me); michael@0: } /* PR_UnblockInterrupt */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_Yield(void) michael@0: { michael@0: static PRBool warning = PR_TRUE; michael@0: if (warning) warning = _PR_Obsolete( michael@0: "PR_Yield()", "PR_Sleep(PR_INTERVAL_NO_WAIT)"); michael@0: return PR_Sleep(PR_INTERVAL_NO_WAIT); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) michael@0: { michael@0: PRStatus rv = PR_SUCCESS; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if (PR_INTERVAL_NO_WAIT == ticks) michael@0: { michael@0: _PT_PTHREAD_YIELD(); michael@0: } michael@0: else michael@0: { michael@0: PRCondVar *cv; michael@0: PRIntervalTime timein; michael@0: michael@0: timein = PR_IntervalNow(); michael@0: cv = PR_NewCondVar(_pr_sleeplock); michael@0: PR_ASSERT(cv != NULL); michael@0: PR_Lock(_pr_sleeplock); michael@0: do michael@0: { michael@0: PRIntervalTime now = PR_IntervalNow(); michael@0: PRIntervalTime delta = now - timein; michael@0: if (delta > ticks) break; michael@0: rv = PR_WaitCondVar(cv, ticks - delta); michael@0: } while (PR_SUCCESS == rv); michael@0: PR_Unlock(_pr_sleeplock); michael@0: PR_DestroyCondVar(cv); michael@0: } michael@0: return rv; michael@0: } /* PR_Sleep */ michael@0: michael@0: static void _pt_thread_death(void *arg) michael@0: { michael@0: void *thred; michael@0: int rv; michael@0: michael@0: _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); michael@0: if (NULL == thred) michael@0: { michael@0: /* michael@0: * Have PR_GetCurrentThread return the expected value to the michael@0: * destructors. michael@0: */ michael@0: rv = pthread_setspecific(pt_book.key, arg); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: michael@0: /* PR_TRUE for: call destructors */ michael@0: _pt_thread_death_internal(arg, PR_TRUE); michael@0: michael@0: if (NULL == thred) michael@0: { michael@0: rv = pthread_setspecific(pt_book.key, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: } michael@0: michael@0: static void _pt_thread_death_internal(void *arg, PRBool callDestructors) michael@0: { michael@0: PRThread *thred = (PRThread*)arg; michael@0: michael@0: if (thred->state & (PT_THREAD_FOREIGN|PT_THREAD_PRIMORD)) michael@0: { michael@0: PR_Lock(pt_book.ml); michael@0: if (NULL == thred->prev) michael@0: pt_book.first = thred->next; michael@0: else michael@0: thred->prev->next = thred->next; michael@0: if (NULL == thred->next) michael@0: pt_book.last = thred->prev; michael@0: else michael@0: thred->next->prev = thred->prev; michael@0: PR_Unlock(pt_book.ml); michael@0: } michael@0: if (callDestructors) michael@0: _PR_DestroyThreadPrivate(thred); michael@0: PR_Free(thred->privateData); michael@0: if (NULL != thred->errorString) michael@0: PR_Free(thred->errorString); michael@0: if (NULL != thred->name) michael@0: PR_Free(thred->name); michael@0: PR_Free(thred->stack); michael@0: if (NULL != thred->syspoll_list) michael@0: PR_Free(thred->syspoll_list); michael@0: #if defined(_PR_POLL_WITH_SELECT) michael@0: if (NULL != thred->selectfd_list) michael@0: PR_Free(thred->selectfd_list); michael@0: #endif michael@0: #if defined(DEBUG) michael@0: memset(thred, 0xaf, sizeof(PRThread)); michael@0: #endif /* defined(DEBUG) */ michael@0: PR_Free(thred); michael@0: } /* _pt_thread_death */ michael@0: michael@0: void _PR_InitThreads( michael@0: PRThreadType type, PRThreadPriority priority, PRUintn maxPTDs) michael@0: { michael@0: int rv; michael@0: PRThread *thred; michael@0: michael@0: PR_ASSERT(priority == PR_PRIORITY_NORMAL); michael@0: michael@0: #ifdef _PR_NEED_PTHREAD_INIT michael@0: /* michael@0: * On BSD/OS (3.1 and 4.0), the pthread subsystem is lazily michael@0: * initialized, but pthread_self() fails to initialize michael@0: * pthreads and hence returns a null thread ID if invoked michael@0: * by the primordial thread before any other pthread call. michael@0: * So we explicitly initialize pthreads here. michael@0: */ michael@0: pthread_init(); michael@0: #endif michael@0: michael@0: #if defined(_PR_DCETHREADS) || defined(_POSIX_THREAD_PRIORITY_SCHEDULING) michael@0: #if defined(FREEBSD) michael@0: { michael@0: pthread_attr_t attr; michael@0: int policy; michael@0: /* get the min and max priorities of the default policy */ michael@0: pthread_attr_init(&attr); michael@0: pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); michael@0: pthread_attr_getschedpolicy(&attr, &policy); michael@0: pt_book.minPrio = sched_get_priority_min(policy); michael@0: PR_ASSERT(-1 != pt_book.minPrio); michael@0: pt_book.maxPrio = sched_get_priority_max(policy); michael@0: PR_ASSERT(-1 != pt_book.maxPrio); michael@0: pthread_attr_destroy(&attr); michael@0: } michael@0: #else michael@0: /* michael@0: ** These might be function evaluations michael@0: */ michael@0: pt_book.minPrio = PT_PRIO_MIN; michael@0: pt_book.maxPrio = PT_PRIO_MAX; michael@0: #endif michael@0: #endif michael@0: michael@0: PR_ASSERT(NULL == pt_book.ml); michael@0: pt_book.ml = PR_NewLock(); michael@0: PR_ASSERT(NULL != pt_book.ml); michael@0: pt_book.cv = PR_NewCondVar(pt_book.ml); michael@0: PR_ASSERT(NULL != pt_book.cv); michael@0: thred = PR_NEWZAP(PRThread); michael@0: PR_ASSERT(NULL != thred); michael@0: thred->arg = NULL; michael@0: thred->startFunc = NULL; michael@0: thred->priority = priority; michael@0: thred->id = pthread_self(); michael@0: thred->idSet = PR_TRUE; michael@0: #ifdef _PR_NICE_PRIORITY_SCHEDULING michael@0: thred->tid = gettid(); michael@0: #endif michael@0: michael@0: thred->state = (PT_THREAD_DETACHED | PT_THREAD_PRIMORD); michael@0: if (PR_SYSTEM_THREAD == type) michael@0: { michael@0: thred->state |= PT_THREAD_SYSTEM; michael@0: pt_book.system += 1; michael@0: pt_book.this_many = 0; michael@0: } michael@0: else michael@0: { michael@0: pt_book.user += 1; michael@0: pt_book.this_many = 1; michael@0: } michael@0: thred->next = thred->prev = NULL; michael@0: pt_book.first = pt_book.last = thred; michael@0: michael@0: thred->stack = PR_NEWZAP(PRThreadStack); michael@0: PR_ASSERT(thred->stack != NULL); michael@0: thred->stack->stackSize = 0; michael@0: thred->stack->thr = thred; michael@0: _PR_InitializeStack(thred->stack); michael@0: michael@0: /* michael@0: * Create a key for our use to store a backpointer in the pthread michael@0: * to our PRThread object. This object gets deleted when the thread michael@0: * returns from its root in the case of a detached thread. Other michael@0: * threads delete the objects in Join. michael@0: * michael@0: * NB: The destructor logic seems to have a bug so it isn't used. michael@0: * NBB: Oh really? I'm going to give it a spin - AOF 19 June 1998. michael@0: * More info - the problem is that pthreads calls the destructor michael@0: * eagerly as the thread returns from its root, rather than lazily michael@0: * after the thread is joined. Therefore, threads that are joining michael@0: * and holding PRThread references are actually holding pointers to michael@0: * nothing. michael@0: */ michael@0: rv = _PT_PTHREAD_KEY_CREATE(&pt_book.key, _pt_thread_death); michael@0: if (0 != rv) michael@0: PR_Assert("0 == rv", __FILE__, __LINE__); michael@0: pt_book.keyCreated = PR_TRUE; michael@0: rv = pthread_setspecific(pt_book.key, thred); michael@0: PR_ASSERT(0 == rv); michael@0: } /* _PR_InitThreads */ michael@0: michael@0: #ifdef __GNUC__ michael@0: /* michael@0: * GCC supports the constructor and destructor attributes as of michael@0: * version 2.5. michael@0: */ michael@0: static void _PR_Fini(void) __attribute__ ((destructor)); michael@0: #elif defined(__SUNPRO_C) michael@0: /* michael@0: * Sun Studio compiler michael@0: */ michael@0: #pragma fini(_PR_Fini) michael@0: static void _PR_Fini(void); michael@0: #elif defined(HPUX) michael@0: /* michael@0: * Current versions of HP C compiler define __HP_cc. michael@0: * HP C compiler A.11.01.20 doesn't define __HP_cc. michael@0: */ michael@0: #if defined(__ia64) || defined(_LP64) michael@0: #pragma FINI "_PR_Fini" michael@0: static void _PR_Fini(void); michael@0: #else michael@0: /* michael@0: * Only HP-UX 10.x style initializers are supported in 32-bit links. michael@0: * Need to use the +I PR_HPUX10xInit linker option. michael@0: */ michael@0: #include michael@0: michael@0: static void _PR_Fini(void); michael@0: michael@0: void PR_HPUX10xInit(shl_t handle, int loading) michael@0: { michael@0: /* michael@0: * This function is called when a shared library is loaded as well michael@0: * as when the shared library is unloaded. Note that it may not michael@0: * be called when the user's program terminates. michael@0: * michael@0: * handle is the shl_load API handle for the shared library being michael@0: * initialized. michael@0: * michael@0: * loading is non-zero at startup and zero at termination. michael@0: */ michael@0: if (loading) { michael@0: /* ... do some initializations ... */ michael@0: } else { michael@0: _PR_Fini(); michael@0: } michael@0: } michael@0: #endif michael@0: #elif defined(AIX) michael@0: /* Need to use the -binitfini::_PR_Fini linker option. */ michael@0: #endif michael@0: michael@0: void _PR_Fini(void) michael@0: { michael@0: void *thred; michael@0: int rv; michael@0: michael@0: if (!_pr_initialized) { michael@0: /* Either NSPR was never successfully initialized or michael@0: * PR_Cleanup has been called already. */ michael@0: if (pt_book.keyCreated) michael@0: { michael@0: rv = pthread_key_delete(pt_book.key); michael@0: PR_ASSERT(0 == rv); michael@0: pt_book.keyCreated = PR_FALSE; michael@0: } michael@0: return; michael@0: } michael@0: michael@0: _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); michael@0: if (NULL != thred) michael@0: { michael@0: /* michael@0: * PR_FALSE, because it is unsafe to call back to the michael@0: * thread private data destructors at final cleanup. michael@0: */ michael@0: _pt_thread_death_internal(thred, PR_FALSE); michael@0: rv = pthread_setspecific(pt_book.key, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: rv = pthread_key_delete(pt_book.key); michael@0: PR_ASSERT(0 == rv); michael@0: pt_book.keyCreated = PR_FALSE; michael@0: /* TODO: free other resources used by NSPR */ michael@0: /* _pr_initialized = PR_FALSE; */ michael@0: } /* _PR_Fini */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_Cleanup(void) michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: int rv; michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, ("PR_Cleanup: shutting down NSPR")); michael@0: PR_ASSERT(me->state & PT_THREAD_PRIMORD); michael@0: if (me->state & PT_THREAD_PRIMORD) michael@0: { michael@0: PR_Lock(pt_book.ml); michael@0: while (pt_book.user > pt_book.this_many) michael@0: PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: if (me->state & PT_THREAD_SYSTEM) michael@0: pt_book.system -= 1; michael@0: else michael@0: pt_book.user -= 1; michael@0: PR_Unlock(pt_book.ml); michael@0: michael@0: _PR_MD_EARLY_CLEANUP(); michael@0: michael@0: _PR_CleanupMW(); michael@0: _PR_CleanupTime(); michael@0: _PR_CleanupDtoa(); michael@0: _PR_CleanupCallOnce(); michael@0: _PR_ShutdownLinker(); michael@0: _PR_LogCleanup(); michael@0: _PR_CleanupNet(); michael@0: /* Close all the fd's before calling _PR_CleanupIO */ michael@0: _PR_CleanupIO(); michael@0: _PR_CleanupCMon(); michael@0: michael@0: _pt_thread_death(me); michael@0: rv = pthread_setspecific(pt_book.key, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: /* michael@0: * I am not sure if it's safe to delete the cv and lock here, michael@0: * since there may still be "system" threads around. If this michael@0: * call isn't immediately prior to exiting, then there's a michael@0: * problem. michael@0: */ michael@0: if (0 == pt_book.system) michael@0: { michael@0: PR_DestroyCondVar(pt_book.cv); pt_book.cv = NULL; michael@0: PR_DestroyLock(pt_book.ml); pt_book.ml = NULL; michael@0: } michael@0: PR_DestroyLock(_pr_sleeplock); michael@0: _pr_sleeplock = NULL; michael@0: _PR_CleanupLayerCache(); michael@0: _PR_CleanupEnv(); michael@0: #ifdef _PR_ZONE_ALLOCATOR michael@0: _PR_DestroyZones(); michael@0: #endif michael@0: _pr_initialized = PR_FALSE; michael@0: return PR_SUCCESS; michael@0: } michael@0: return PR_FAILURE; michael@0: } /* PR_Cleanup */ michael@0: michael@0: PR_IMPLEMENT(void) PR_ProcessExit(PRIntn status) michael@0: { michael@0: _exit(status); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRUint32) PR_GetThreadID(PRThread *thred) michael@0: { michael@0: #if defined(_PR_DCETHREADS) michael@0: return (PRUint32)&thred->id; /* this is really a sham! */ michael@0: #else michael@0: return (PRUint32)thred->id; /* and I don't know what they will do with it */ michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: * $$$ michael@0: * The following two thread-to-processor affinity functions are not michael@0: * yet implemented for pthreads. By the way, these functions should return michael@0: * PRStatus rather than PRInt32 to indicate the success/failure status. michael@0: * $$$ michael@0: */ michael@0: michael@0: PR_IMPLEMENT(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask) michael@0: { michael@0: return 0; /* not implemented */ michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask ) michael@0: { michael@0: return 0; /* not implemented */ michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_SetThreadDumpProc(PRThread* thread, PRThreadDumpProc dump, void *arg) michael@0: { michael@0: thread->dump = dump; michael@0: thread->dumpArg = arg; michael@0: } michael@0: michael@0: /* michael@0: * Garbage collection support follows. michael@0: */ michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: michael@0: /* michael@0: * statics for Garbage Collection support. We don't need to protect these michael@0: * signal masks since the garbage collector itself is protected by a lock michael@0: * and multiple threads will not be garbage collecting at the same time. michael@0: */ michael@0: static sigset_t javagc_vtalarm_sigmask; michael@0: static sigset_t javagc_intsoff_sigmask; michael@0: michael@0: #else /* defined(_PR_DCETHREADS) */ michael@0: michael@0: /* a bogus signal mask for forcing a timed wait */ michael@0: /* Not so bogus in AIX as we really do a sigwait */ michael@0: static sigset_t sigwait_set; michael@0: michael@0: static struct timespec onemillisec = {0, 1000000L}; michael@0: #ifndef PT_NO_SIGTIMEDWAIT michael@0: static struct timespec hundredmillisec = {0, 100000000L}; michael@0: #endif michael@0: michael@0: static void suspend_signal_handler(PRIntn sig); michael@0: michael@0: #ifdef PT_NO_SIGTIMEDWAIT michael@0: static void null_signal_handler(PRIntn sig); michael@0: #endif michael@0: michael@0: #endif /* defined(_PR_DCETHREADS) */ michael@0: michael@0: /* michael@0: * Linux pthreads use SIGUSR1 and SIGUSR2 internally, which michael@0: * conflict with the use of these two signals in our GC support. michael@0: * So we don't know how to support GC on Linux pthreads. michael@0: */ michael@0: static void init_pthread_gc_support(void) michael@0: { michael@0: #ifndef SYMBIAN michael@0: PRIntn rv; michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: rv = sigemptyset(&javagc_vtalarm_sigmask); michael@0: PR_ASSERT(0 == rv); michael@0: rv = sigaddset(&javagc_vtalarm_sigmask, SIGVTALRM); michael@0: PR_ASSERT(0 == rv); michael@0: #else /* defined(_PR_DCETHREADS) */ michael@0: { michael@0: struct sigaction sigact_usr2; michael@0: michael@0: sigact_usr2.sa_handler = suspend_signal_handler; michael@0: sigact_usr2.sa_flags = SA_RESTART; michael@0: sigemptyset (&sigact_usr2.sa_mask); michael@0: michael@0: rv = sigaction (SIGUSR2, &sigact_usr2, NULL); michael@0: PR_ASSERT(0 == rv); michael@0: michael@0: sigemptyset (&sigwait_set); michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: sigaddset (&sigwait_set, SIGUSR1); michael@0: #else michael@0: sigaddset (&sigwait_set, SIGUSR2); michael@0: #endif /* defined(PT_NO_SIGTIMEDWAIT) */ michael@0: } michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: { michael@0: struct sigaction sigact_null; michael@0: sigact_null.sa_handler = null_signal_handler; michael@0: sigact_null.sa_flags = SA_RESTART; michael@0: sigemptyset (&sigact_null.sa_mask); michael@0: rv = sigaction (SIGUSR1, &sigact_null, NULL); michael@0: PR_ASSERT(0 ==rv); michael@0: } michael@0: #endif /* defined(PT_NO_SIGTIMEDWAIT) */ michael@0: #endif /* defined(_PR_DCETHREADS) */ michael@0: #endif /* SYMBIAN */ michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_SetThreadGCAble(void) michael@0: { michael@0: PR_Lock(pt_book.ml); michael@0: PR_GetCurrentThread()->state |= PT_THREAD_GCABLE; michael@0: PR_Unlock(pt_book.ml); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_ClearThreadGCAble(void) michael@0: { michael@0: PR_Lock(pt_book.ml); michael@0: PR_GetCurrentThread()->state &= (~PT_THREAD_GCABLE); michael@0: PR_Unlock(pt_book.ml); michael@0: } michael@0: michael@0: #if defined(DEBUG) michael@0: static PRBool suspendAllOn = PR_FALSE; michael@0: #endif michael@0: michael@0: static PRBool suspendAllSuspended = PR_FALSE; michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg) michael@0: { michael@0: PRIntn count = 0; michael@0: PRStatus rv = PR_SUCCESS; michael@0: PRThread* thred = pt_book.first; michael@0: michael@0: #if defined(DEBUG) || defined(FORCE_PR_ASSERT) michael@0: #if !defined(_PR_DCETHREADS) michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: #endif michael@0: #endif michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_EnumerateThreads\n")); michael@0: /* michael@0: * $$$ michael@0: * Need to suspend all threads other than me before doing this. michael@0: * This is really a gross and disgusting thing to do. The only michael@0: * good thing is that since all other threads are suspended, holding michael@0: * the lock during a callback seems like child's play. michael@0: * $$$ michael@0: */ michael@0: PR_ASSERT(suspendAllOn); michael@0: michael@0: while (thred != NULL) michael@0: { michael@0: /* Steve Morse, 4-23-97: Note that we can't walk a queue by taking michael@0: * qp->next after applying the function "func". In particular, "func" michael@0: * might remove the thread from the queue and put it into another one in michael@0: * which case qp->next no longer points to the next entry in the original michael@0: * queue. michael@0: * michael@0: * To get around this problem, we save qp->next in qp_next before applying michael@0: * "func" and use that saved value as the next value after applying "func". michael@0: */ michael@0: PRThread* next = thred->next; michael@0: michael@0: if (_PT_IS_GCABLE_THREAD(thred)) michael@0: { michael@0: #if !defined(_PR_DCETHREADS) michael@0: PR_ASSERT((thred == me) || (thred->suspend & PT_THREAD_SUSPENDED)); michael@0: #endif michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("In PR_EnumerateThreads callback thread %p thid = %X\n", michael@0: thred, thred->id)); michael@0: michael@0: rv = func(thred, count++, arg); michael@0: if (rv != PR_SUCCESS) michael@0: return rv; michael@0: } michael@0: thred = next; michael@0: } michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("End PR_EnumerateThreads count = %d \n", count)); michael@0: return rv; michael@0: } /* PR_EnumerateThreads */ michael@0: michael@0: /* michael@0: * PR_SuspendAll and PR_ResumeAll are called during garbage collection. The strategy michael@0: * we use is to send a SIGUSR2 signal to every gc able thread that we intend to suspend. michael@0: * The signal handler will record the stack pointer and will block until resumed by michael@0: * the resume call. Since the signal handler is the last routine called for the michael@0: * suspended thread, the stack pointer will also serve as a place where all the michael@0: * registers have been saved on the stack for the previously executing routines. michael@0: * michael@0: * Through global variables, we also make sure that PR_Suspend and PR_Resume does not michael@0: * proceed until the thread is suspended or resumed. michael@0: */ michael@0: michael@0: #if !defined(_PR_DCETHREADS) michael@0: michael@0: /* michael@0: * In the signal handler, we can not use condition variable notify or wait. michael@0: * This does not work consistently across all pthread platforms. We also can not michael@0: * use locking since that does not seem to work reliably across platforms. michael@0: * Only thing we can do is yielding while testing for a global condition michael@0: * to change. This does work on pthread supported platforms. We may have michael@0: * to play with priortities if there are any problems detected. michael@0: */ michael@0: michael@0: /* michael@0: * In AIX, you cannot use ANY pthread calls in the signal handler except perhaps michael@0: * pthread_yield. But that is horribly inefficient. Hence we use only sigwait, no michael@0: * sigtimedwait is available. We need to use another user signal, SIGUSR1. Actually michael@0: * SIGUSR1 is also used by exec in Java. So our usage here breaks the exec in Java, michael@0: * for AIX. You cannot use pthread_cond_wait or pthread_delay_np in the signal michael@0: * handler as all synchronization mechanisms just break down. michael@0: */ michael@0: michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: static void null_signal_handler(PRIntn sig) michael@0: { michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: static void suspend_signal_handler(PRIntn sig) michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: michael@0: PR_ASSERT(me != NULL); michael@0: PR_ASSERT(_PT_IS_GCABLE_THREAD(me)); michael@0: PR_ASSERT((me->suspend & PT_THREAD_SUSPENDED) == 0); michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("Begin suspend_signal_handler thred %p thread id = %X\n", michael@0: me, me->id)); michael@0: michael@0: /* michael@0: * save stack pointer michael@0: */ michael@0: me->sp = &me; michael@0: michael@0: /* michael@0: At this point, the thread's stack pointer has been saved, michael@0: And it is going to enter a wait loop until it is resumed. michael@0: So it is _really_ suspended michael@0: */ michael@0: michael@0: me->suspend |= PT_THREAD_SUSPENDED; michael@0: michael@0: /* michael@0: * now, block current thread michael@0: */ michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: pthread_cond_signal(&me->suspendResumeCV); michael@0: while (me->suspend & PT_THREAD_SUSPENDED) michael@0: { michael@0: #if !defined(FREEBSD) && !defined(NETBSD) && !defined(OPENBSD) \ michael@0: && !defined(BSDI) && !defined(UNIXWARE) \ michael@0: && !defined(DARWIN) && !defined(RISCOS) \ michael@0: && !defined(SYMBIAN) /*XXX*/ michael@0: PRIntn rv; michael@0: sigwait(&sigwait_set, &rv); michael@0: #endif michael@0: } michael@0: me->suspend |= PT_THREAD_RESUMED; michael@0: pthread_cond_signal(&me->suspendResumeCV); michael@0: #else /* defined(PT_NO_SIGTIMEDWAIT) */ michael@0: while (me->suspend & PT_THREAD_SUSPENDED) michael@0: { michael@0: PRIntn rv = sigtimedwait(&sigwait_set, NULL, &hundredmillisec); michael@0: PR_ASSERT(-1 == rv); michael@0: } michael@0: me->suspend |= PT_THREAD_RESUMED; michael@0: #endif michael@0: michael@0: /* michael@0: * At this point, thread has been resumed, so set a global condition. michael@0: * The ResumeAll needs to know that this has really been resumed. michael@0: * So the signal handler sets a flag which PR_ResumeAll will reset. michael@0: * The PR_ResumeAll must reset this flag ... michael@0: */ michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("End suspend_signal_handler thred = %p tid = %X\n", me, me->id)); michael@0: } /* suspend_signal_handler */ michael@0: michael@0: static void pt_SuspendSet(PRThread *thred) michael@0: { michael@0: PRIntn rv; michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("pt_SuspendSet thred %p thread id = %X\n", thred, thred->id)); michael@0: michael@0: michael@0: /* michael@0: * Check the thread state and signal the thread to suspend michael@0: */ michael@0: michael@0: PR_ASSERT((thred->suspend & PT_THREAD_SUSPENDED) == 0); michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("doing pthread_kill in pt_SuspendSet thred %p tid = %X\n", michael@0: thred, thred->id)); michael@0: #if defined(SYMBIAN) michael@0: /* All signal group functions are not implemented in Symbian OS */ michael@0: rv = 0; michael@0: #else michael@0: rv = pthread_kill (thred->id, SIGUSR2); michael@0: #endif michael@0: PR_ASSERT(0 == rv); michael@0: } michael@0: michael@0: static void pt_SuspendTest(PRThread *thred) michael@0: { michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("Begin pt_SuspendTest thred %p thread id = %X\n", thred, thred->id)); michael@0: michael@0: michael@0: /* michael@0: * Wait for the thread to be really suspended. This happens when the michael@0: * suspend signal handler stores the stack pointer and sets the state michael@0: * to suspended. michael@0: */ michael@0: michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: pthread_mutex_lock(&thred->suspendResumeMutex); michael@0: while ((thred->suspend & PT_THREAD_SUSPENDED) == 0) michael@0: { michael@0: pthread_cond_timedwait( michael@0: &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); michael@0: } michael@0: pthread_mutex_unlock(&thred->suspendResumeMutex); michael@0: #else michael@0: while ((thred->suspend & PT_THREAD_SUSPENDED) == 0) michael@0: { michael@0: PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); michael@0: PR_ASSERT(-1 == rv); michael@0: } michael@0: #endif michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("End pt_SuspendTest thred %p tid %X\n", thred, thred->id)); michael@0: } /* pt_SuspendTest */ michael@0: michael@0: static void pt_ResumeSet(PRThread *thred) michael@0: { michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("pt_ResumeSet thred %p thread id = %X\n", thred, thred->id)); michael@0: michael@0: /* michael@0: * Clear the global state and set the thread state so that it will michael@0: * continue past yield loop in the suspend signal handler michael@0: */ michael@0: michael@0: PR_ASSERT(thred->suspend & PT_THREAD_SUSPENDED); michael@0: michael@0: michael@0: thred->suspend &= ~PT_THREAD_SUSPENDED; michael@0: michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: #if defined(SYMBIAN) michael@0: /* All signal group functions are not implemented in Symbian OS */ michael@0: #else michael@0: pthread_kill(thred->id, SIGUSR1); michael@0: #endif michael@0: #endif michael@0: michael@0: } /* pt_ResumeSet */ michael@0: michael@0: static void pt_ResumeTest(PRThread *thred) michael@0: { michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("Begin pt_ResumeTest thred %p thread id = %X\n", thred, thred->id)); michael@0: michael@0: /* michael@0: * Wait for the threads resume state to change michael@0: * to indicate it is really resumed michael@0: */ michael@0: #if defined(PT_NO_SIGTIMEDWAIT) michael@0: pthread_mutex_lock(&thred->suspendResumeMutex); michael@0: while ((thred->suspend & PT_THREAD_RESUMED) == 0) michael@0: { michael@0: pthread_cond_timedwait( michael@0: &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); michael@0: } michael@0: pthread_mutex_unlock(&thred->suspendResumeMutex); michael@0: #else michael@0: while ((thred->suspend & PT_THREAD_RESUMED) == 0) { michael@0: PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); michael@0: PR_ASSERT(-1 == rv); michael@0: } michael@0: #endif michael@0: michael@0: thred->suspend &= ~PT_THREAD_RESUMED; michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ( michael@0: "End pt_ResumeTest thred %p tid %X\n", thred, thred->id)); michael@0: } /* pt_ResumeTest */ michael@0: michael@0: static pthread_once_t pt_gc_support_control = PTHREAD_ONCE_INIT; michael@0: michael@0: PR_IMPLEMENT(void) PR_SuspendAll(void) michael@0: { michael@0: #ifdef DEBUG michael@0: PRIntervalTime stime, etime; michael@0: #endif michael@0: PRThread* thred = pt_book.first; michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: int rv; michael@0: michael@0: rv = pthread_once(&pt_gc_support_control, init_pthread_gc_support); michael@0: PR_ASSERT(0 == rv); michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_SuspendAll\n")); michael@0: /* michael@0: * Stop all threads which are marked GC able. michael@0: */ michael@0: PR_Lock(pt_book.ml); michael@0: #ifdef DEBUG michael@0: suspendAllOn = PR_TRUE; michael@0: stime = PR_IntervalNow(); michael@0: #endif michael@0: while (thred != NULL) michael@0: { michael@0: if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) michael@0: pt_SuspendSet(thred); michael@0: thred = thred->next; michael@0: } michael@0: michael@0: /* Wait till they are really suspended */ michael@0: thred = pt_book.first; michael@0: while (thred != NULL) michael@0: { michael@0: if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) michael@0: pt_SuspendTest(thred); michael@0: thred = thred->next; michael@0: } michael@0: michael@0: suspendAllSuspended = PR_TRUE; michael@0: michael@0: #ifdef DEBUG michael@0: etime = PR_IntervalNow(); michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS,\ michael@0: ("End PR_SuspendAll (time %dms)\n", michael@0: PR_IntervalToMilliseconds(etime - stime))); michael@0: #endif michael@0: } /* PR_SuspendAll */ michael@0: michael@0: PR_IMPLEMENT(void) PR_ResumeAll(void) michael@0: { michael@0: #ifdef DEBUG michael@0: PRIntervalTime stime, etime; michael@0: #endif michael@0: PRThread* thred = pt_book.first; michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_ResumeAll\n")); michael@0: /* michael@0: * Resume all previously suspended GC able threads. michael@0: */ michael@0: suspendAllSuspended = PR_FALSE; michael@0: #ifdef DEBUG michael@0: stime = PR_IntervalNow(); michael@0: #endif michael@0: michael@0: while (thred != NULL) michael@0: { michael@0: if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) michael@0: pt_ResumeSet(thred); michael@0: thred = thred->next; michael@0: } michael@0: michael@0: thred = pt_book.first; michael@0: while (thred != NULL) michael@0: { michael@0: if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) michael@0: pt_ResumeTest(thred); michael@0: thred = thred->next; michael@0: } michael@0: michael@0: PR_Unlock(pt_book.ml); michael@0: #ifdef DEBUG michael@0: suspendAllOn = PR_FALSE; michael@0: etime = PR_IntervalNow(); michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("End PR_ResumeAll (time %dms)\n", michael@0: PR_IntervalToMilliseconds(etime - stime))); michael@0: #endif michael@0: } /* PR_ResumeAll */ michael@0: michael@0: /* Return the stack pointer for the given thread- used by the GC */ michael@0: PR_IMPLEMENT(void *)PR_GetSP(PRThread *thred) michael@0: { michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, michael@0: ("in PR_GetSP thred %p thid = %X, sp = %p\n", michael@0: thred, thred->id, thred->sp)); michael@0: return thred->sp; michael@0: } /* PR_GetSP */ michael@0: michael@0: #else /* !defined(_PR_DCETHREADS) */ michael@0: michael@0: static pthread_once_t pt_gc_support_control = pthread_once_init; michael@0: michael@0: /* michael@0: * For DCE threads, there is no pthread_kill or a way of suspending or resuming a michael@0: * particular thread. We will just disable the preemption (virtual timer alarm) and michael@0: * let the executing thread finish the garbage collection. This stops all other threads michael@0: * (GC able or not) and is very inefficient but there is no other choice. michael@0: */ michael@0: PR_IMPLEMENT(void) PR_SuspendAll() michael@0: { michael@0: PRIntn rv; michael@0: michael@0: rv = pthread_once(&pt_gc_support_control, init_pthread_gc_support); michael@0: PR_ASSERT(0 == rv); /* returns -1 on failure */ michael@0: #ifdef DEBUG michael@0: suspendAllOn = PR_TRUE; michael@0: #endif michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_SuspendAll\n")); michael@0: /* michael@0: * turn off preemption - i.e add virtual alarm signal to the set of michael@0: * blocking signals michael@0: */ michael@0: rv = sigprocmask( michael@0: SIG_BLOCK, &javagc_vtalarm_sigmask, &javagc_intsoff_sigmask); michael@0: PR_ASSERT(0 == rv); michael@0: suspendAllSuspended = PR_TRUE; michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("End PR_SuspendAll\n")); michael@0: } /* PR_SuspendAll */ michael@0: michael@0: PR_IMPLEMENT(void) PR_ResumeAll() michael@0: { michael@0: PRIntn rv; michael@0: michael@0: suspendAllSuspended = PR_FALSE; michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_ResumeAll\n")); michael@0: /* turn on preemption - i.e re-enable virtual alarm signal */ michael@0: michael@0: rv = sigprocmask(SIG_SETMASK, &javagc_intsoff_sigmask, (sigset_t *)NULL); michael@0: PR_ASSERT(0 == rv); michael@0: #ifdef DEBUG michael@0: suspendAllOn = PR_FALSE; michael@0: #endif michael@0: michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("End PR_ResumeAll\n")); michael@0: } /* PR_ResumeAll */ michael@0: michael@0: /* Return the stack pointer for the given thread- used by the GC */ michael@0: PR_IMPLEMENT(void*)PR_GetSP(PRThread *thred) michael@0: { michael@0: pthread_t tid = thred->id; michael@0: char *thread_tcb, *top_sp; michael@0: michael@0: /* michael@0: * For HPUX DCE threads, pthread_t is a struct with the michael@0: * following three fields (see pthread.h, dce/cma.h): michael@0: * cma_t_address field1; michael@0: * short int field2; michael@0: * short int field3; michael@0: * where cma_t_address is typedef'd to be either void* michael@0: * or char*. michael@0: */ michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("Begin PR_GetSP\n")); michael@0: thread_tcb = (char*)tid.field1; michael@0: top_sp = *(char**)(thread_tcb + 128); michael@0: PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ("End PR_GetSP %p \n", top_sp)); michael@0: return top_sp; michael@0: } /* PR_GetSP */ michael@0: michael@0: #endif /* !defined(_PR_DCETHREADS) */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name) michael@0: { michael@0: PRThread *thread; michael@0: size_t nameLen; michael@0: int result; michael@0: michael@0: if (!name) { michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: thread = PR_GetCurrentThread(); michael@0: if (!thread) michael@0: return PR_FAILURE; michael@0: michael@0: PR_Free(thread->name); michael@0: nameLen = strlen(name); michael@0: thread->name = (char *)PR_Malloc(nameLen + 1); michael@0: if (!thread->name) michael@0: return PR_FAILURE; michael@0: memcpy(thread->name, name, nameLen + 1); michael@0: michael@0: #if defined(OPENBSD) || defined(FREEBSD) michael@0: result = pthread_set_name_np(thread->id, name); michael@0: #else /* not BSD */ michael@0: /* michael@0: * On OSX, pthread_setname_np is only available in 10.6 or later, so test michael@0: * for it at runtime. It also may not be available on all linux distros. michael@0: */ michael@0: #if defined(DARWIN) michael@0: int (*dynamic_pthread_setname_np)(const char*); michael@0: #else michael@0: int (*dynamic_pthread_setname_np)(pthread_t, const char*); michael@0: #endif michael@0: michael@0: *(void**)(&dynamic_pthread_setname_np) = michael@0: dlsym(RTLD_DEFAULT, "pthread_setname_np"); michael@0: if (!dynamic_pthread_setname_np) michael@0: return PR_SUCCESS; michael@0: michael@0: /* michael@0: * The 15-character name length limit is an experimentally determined michael@0: * length of a null-terminated string that most linux distros and OS X michael@0: * accept as an argument to pthread_setname_np. Otherwise the E2BIG michael@0: * error is returned by the function. michael@0: */ michael@0: #define SETNAME_LENGTH_CONSTRAINT 15 michael@0: #define SETNAME_FRAGMENT1_LENGTH (SETNAME_LENGTH_CONSTRAINT >> 1) michael@0: #define SETNAME_FRAGMENT2_LENGTH \ michael@0: (SETNAME_LENGTH_CONSTRAINT - SETNAME_FRAGMENT1_LENGTH - 1) michael@0: char name_dup[SETNAME_LENGTH_CONSTRAINT + 1]; michael@0: if (nameLen > SETNAME_LENGTH_CONSTRAINT) { michael@0: memcpy(name_dup, name, SETNAME_FRAGMENT1_LENGTH); michael@0: name_dup[SETNAME_FRAGMENT1_LENGTH] = '~'; michael@0: /* Note that this also copies the null terminator. */ michael@0: memcpy(name_dup + SETNAME_FRAGMENT1_LENGTH + 1, michael@0: name + nameLen - SETNAME_FRAGMENT2_LENGTH, michael@0: SETNAME_FRAGMENT2_LENGTH + 1); michael@0: name = name_dup; michael@0: } michael@0: michael@0: #if defined(DARWIN) michael@0: result = dynamic_pthread_setname_np(name); michael@0: #else michael@0: result = dynamic_pthread_setname_np(thread->id, name); michael@0: #endif michael@0: #endif /* not BSD */ michael@0: michael@0: if (result) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, result); michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(const char *) PR_GetThreadName(const PRThread *thread) michael@0: { michael@0: if (!thread) michael@0: return NULL; michael@0: return thread->name; michael@0: } michael@0: michael@0: #endif /* defined(_PR_PTHREADS) || defined(_PR_DCETHREADS) */ michael@0: michael@0: /* ptthread.c */