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 "primpl.h" andre@0: andre@0: #if defined(WIN95) andre@0: /* andre@0: ** Some local variables report warnings on Win95 because the code paths andre@0: ** using them are conditioned on HAVE_CUSTOME_USER_THREADS. andre@0: ** The pragma suppresses the warning. andre@0: ** andre@0: */ andre@0: #pragma warning(disable : 4101) andre@0: #endif andre@0: andre@0: andre@0: extern PRLock *_pr_sleeplock; /* allocated and initialized in prinit */ andre@0: /* andre@0: ** Routines common to both native and user threads. andre@0: ** andre@0: ** andre@0: ** Clean up a thread object, releasing all of the attached data. Do not andre@0: ** free the object itself (it may not have been malloc'd) andre@0: */ andre@0: void _PR_CleanupThread(PRThread *thread) andre@0: { andre@0: /* Free up per-thread-data */ andre@0: _PR_DestroyThreadPrivate(thread); andre@0: andre@0: /* Free any thread dump procs */ andre@0: if (thread->dumpArg) { andre@0: PR_DELETE(thread->dumpArg); andre@0: } andre@0: thread->dump = 0; andre@0: andre@0: PR_DELETE(thread->name); andre@0: PR_DELETE(thread->errorString); andre@0: thread->errorStringSize = 0; andre@0: thread->errorStringLength = 0; andre@0: thread->environment = NULL; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_Yield() andre@0: { andre@0: static PRBool warning = PR_TRUE; andre@0: if (warning) warning = _PR_Obsolete( andre@0: "PR_Yield()", "PR_Sleep(PR_INTERVAL_NO_WAIT)"); andre@0: return (PR_Sleep(PR_INTERVAL_NO_WAIT)); andre@0: } andre@0: andre@0: /* andre@0: ** Make the current thread sleep until "timeout" ticks amount of time andre@0: ** has expired. If "timeout" is PR_INTERVAL_NO_WAIT then the call is andre@0: ** equivalent to a yield. Waiting for an infinite amount of time is andre@0: ** allowed in the expectation that another thread will interrupt(). andre@0: ** andre@0: ** A single lock is used for all threads calling sleep. Each caller andre@0: ** does get its own condition variable since each is expected to have andre@0: ** a unique 'timeout'. andre@0: */ andre@0: PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime timeout) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (PR_INTERVAL_NO_WAIT == timeout) andre@0: { andre@0: /* andre@0: ** This is a simple yield, nothing more, nothing less. andre@0: */ andre@0: PRIntn is; andre@0: PRThread *me = PR_GetCurrentThread(); andre@0: PRUintn pri = me->priority; andre@0: _PRCPU *cpu = _PR_MD_CURRENT_CPU(); andre@0: andre@0: if ( _PR_IS_NATIVE_THREAD(me) ) _PR_MD_YIELD(); andre@0: else andre@0: { andre@0: _PR_INTSOFF(is); andre@0: _PR_RUNQ_LOCK(cpu); andre@0: if (_PR_RUNQREADYMASK(cpu) >> pri) { andre@0: me->cpu = cpu; andre@0: me->state = _PR_RUNNABLE; andre@0: _PR_ADD_RUNQ(me, cpu, pri); andre@0: _PR_RUNQ_UNLOCK(cpu); andre@0: andre@0: PR_LOG(_pr_sched_lm, PR_LOG_MIN, ("PR_Yield: yielding")); andre@0: _PR_MD_SWITCH_CONTEXT(me); andre@0: PR_LOG(_pr_sched_lm, PR_LOG_MIN, ("PR_Yield: done")); andre@0: andre@0: _PR_FAST_INTSON(is); andre@0: } andre@0: else andre@0: { andre@0: _PR_RUNQ_UNLOCK(cpu); andre@0: _PR_INTSON(is); andre@0: } andre@0: } andre@0: } andre@0: else andre@0: { andre@0: /* andre@0: ** This is waiting for some finite period of time. andre@0: ** A thread in this state is interruptible (PR_Interrupt()), andre@0: ** but the lock and cvar used are local to the implementation andre@0: ** and not visible to the caller, therefore not notifiable. andre@0: */ andre@0: PRCondVar *cv; andre@0: PRIntervalTime timein; andre@0: andre@0: timein = PR_IntervalNow(); andre@0: cv = PR_NewCondVar(_pr_sleeplock); andre@0: PR_ASSERT(cv != NULL); andre@0: PR_Lock(_pr_sleeplock); andre@0: do andre@0: { andre@0: PRIntervalTime delta = PR_IntervalNow() - timein; andre@0: if (delta > timeout) break; andre@0: rv = PR_WaitCondVar(cv, timeout - delta); andre@0: } while (rv == PR_SUCCESS); andre@0: PR_Unlock(_pr_sleeplock); andre@0: PR_DestroyCondVar(cv); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_GetThreadID(PRThread *thread) andre@0: { andre@0: return thread->id; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThreadPriority) PR_GetThreadPriority(const PRThread *thread) andre@0: { andre@0: return (PRThreadPriority) thread->priority; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThread *) PR_GetCurrentThread() andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: return _PR_MD_CURRENT_THREAD(); andre@0: } andre@0: andre@0: /* andre@0: ** Set the interrupt flag for a thread. The thread will be unable to andre@0: ** block in i/o functions when this happens. Also, any PR_Wait's in andre@0: ** progress will be undone. The interrupt remains in force until andre@0: ** PR_ClearInterrupt is called. andre@0: */ andre@0: PR_IMPLEMENT(PRStatus) PR_Interrupt(PRThread *thread) andre@0: { andre@0: #ifdef _PR_GLOBAL_THREADS_ONLY andre@0: PRCondVar *victim; andre@0: andre@0: _PR_THREAD_LOCK(thread); andre@0: thread->flags |= _PR_INTERRUPT; andre@0: victim = thread->wait.cvar; andre@0: _PR_THREAD_UNLOCK(thread); andre@0: if ((NULL != victim) && (!(thread->flags & _PR_INTERRUPT_BLOCKED))) { andre@0: int haveLock = (victim->lock->owner == _PR_MD_CURRENT_THREAD()); andre@0: andre@0: if (!haveLock) PR_Lock(victim->lock); andre@0: PR_NotifyAllCondVar(victim); andre@0: if (!haveLock) PR_Unlock(victim->lock); andre@0: } andre@0: return PR_SUCCESS; andre@0: #else /* ! _PR_GLOBAL_THREADS_ONLY */ andre@0: PRIntn is; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (!_PR_IS_NATIVE_THREAD(me)) andre@0: _PR_INTSOFF(is); andre@0: andre@0: _PR_THREAD_LOCK(thread); andre@0: thread->flags |= _PR_INTERRUPT; andre@0: switch (thread->state) { andre@0: case _PR_COND_WAIT: andre@0: /* andre@0: * call is made with thread locked; andre@0: * on return lock is released andre@0: */ andre@0: if (!(thread->flags & _PR_INTERRUPT_BLOCKED)) andre@0: _PR_NotifyLockedThread(thread); andre@0: break; andre@0: case _PR_IO_WAIT: andre@0: /* andre@0: * Need to hold the thread lock when calling andre@0: * _PR_Unblock_IO_Wait(). On return lock is andre@0: * released. andre@0: */ andre@0: #if defined(XP_UNIX) || defined(WINNT) || defined(WIN16) andre@0: if (!(thread->flags & _PR_INTERRUPT_BLOCKED)) andre@0: _PR_Unblock_IO_Wait(thread); andre@0: #else andre@0: _PR_THREAD_UNLOCK(thread); andre@0: #endif andre@0: break; andre@0: case _PR_RUNNING: andre@0: case _PR_RUNNABLE: andre@0: case _PR_LOCK_WAIT: andre@0: default: andre@0: _PR_THREAD_UNLOCK(thread); andre@0: break; andre@0: } andre@0: if (!_PR_IS_NATIVE_THREAD(me)) andre@0: _PR_INTSON(is); andre@0: return PR_SUCCESS; andre@0: #endif /* _PR_GLOBAL_THREADS_ONLY */ andre@0: } andre@0: andre@0: /* andre@0: ** Clear the interrupt flag for self. andre@0: */ andre@0: PR_IMPLEMENT(void) PR_ClearInterrupt() andre@0: { andre@0: PRIntn is; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSOFF(is); andre@0: _PR_THREAD_LOCK(me); andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: _PR_THREAD_UNLOCK(me); andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSON(is); andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_BlockInterrupt() andre@0: { andre@0: PRIntn is; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSOFF(is); andre@0: _PR_THREAD_LOCK(me); andre@0: _PR_THREAD_BLOCK_INTERRUPT(me); andre@0: _PR_THREAD_UNLOCK(me); andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSON(is); andre@0: } /* PR_BlockInterrupt */ andre@0: andre@0: PR_IMPLEMENT(void) PR_UnblockInterrupt() andre@0: { andre@0: PRIntn is; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSOFF(is); andre@0: _PR_THREAD_LOCK(me); andre@0: _PR_THREAD_UNBLOCK_INTERRUPT(me); andre@0: _PR_THREAD_UNLOCK(me); andre@0: if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSON(is); andre@0: } /* PR_UnblockInterrupt */ andre@0: andre@0: /* andre@0: ** Return the thread stack pointer of the given thread. andre@0: */ andre@0: PR_IMPLEMENT(void *) PR_GetSP(PRThread *thread) andre@0: { andre@0: return (void *)_PR_MD_GET_SP(thread); andre@0: } andre@0: andre@0: PR_IMPLEMENT(void*) GetExecutionEnvironment(PRThread *thread) andre@0: { andre@0: return thread->environment; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) SetExecutionEnvironment(PRThread *thread, void *env) andre@0: { andre@0: thread->environment = env; andre@0: } andre@0: andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask) andre@0: { andre@0: #ifdef HAVE_THREAD_AFFINITY andre@0: return _PR_MD_GETTHREADAFFINITYMASK(thread, mask); andre@0: #else andre@0: return 0; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask ) andre@0: { andre@0: #ifdef HAVE_THREAD_AFFINITY andre@0: #ifndef IRIX andre@0: return _PR_MD_SETTHREADAFFINITYMASK(thread, mask); andre@0: #else andre@0: return 0; andre@0: #endif andre@0: #else andre@0: return 0; andre@0: #endif andre@0: } andre@0: andre@0: /* This call is thread unsafe if another thread is calling SetConcurrency() andre@0: */ andre@0: PR_IMPLEMENT(PRInt32) PR_SetCPUAffinityMask(PRUint32 mask) andre@0: { andre@0: #ifdef HAVE_THREAD_AFFINITY andre@0: PRCList *qp; andre@0: extern PRUint32 _pr_cpu_affinity_mask; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: _pr_cpu_affinity_mask = mask; andre@0: andre@0: qp = _PR_CPUQ().next; andre@0: while(qp != &_PR_CPUQ()) { andre@0: _PRCPU *cpu; andre@0: andre@0: cpu = _PR_CPU_PTR(qp); andre@0: PR_SetThreadAffinityMask(cpu->thread, mask); andre@0: andre@0: qp = qp->next; andre@0: } andre@0: #endif andre@0: andre@0: return 0; andre@0: } andre@0: andre@0: PRUint32 _pr_recycleThreads = 0; andre@0: PR_IMPLEMENT(void) PR_SetThreadRecycleMode(PRUint32 count) andre@0: { andre@0: _pr_recycleThreads = count; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThread*) PR_CreateThreadGCAble(PRThreadType type, andre@0: void (*start)(void *arg), andre@0: void *arg, andre@0: PRThreadPriority priority, andre@0: PRThreadScope scope, andre@0: PRThreadState state, andre@0: PRUint32 stackSize) andre@0: { andre@0: return _PR_CreateThread(type, start, arg, priority, scope, state, andre@0: stackSize, _PR_GCABLE_THREAD); andre@0: } andre@0: andre@0: #ifdef SOLARIS andre@0: PR_IMPLEMENT(PRThread*) PR_CreateThreadBound(PRThreadType type, andre@0: void (*start)(void *arg), andre@0: void *arg, andre@0: PRUintn priority, andre@0: PRThreadScope scope, andre@0: PRThreadState state, andre@0: PRUint32 stackSize) andre@0: { andre@0: return _PR_CreateThread(type, start, arg, priority, scope, state, andre@0: stackSize, _PR_BOUND_THREAD); andre@0: } andre@0: #endif andre@0: andre@0: andre@0: PR_IMPLEMENT(PRThread*) PR_AttachThreadGCAble( andre@0: PRThreadType type, PRThreadPriority priority, PRThreadStack *stack) andre@0: { andre@0: /* $$$$ not sure how to finese this one */ andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_SetThreadGCAble() andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: PR_Lock(_pr_activeLock); andre@0: _PR_MD_CURRENT_THREAD()->flags |= _PR_GCABLE_THREAD; andre@0: PR_Unlock(_pr_activeLock); andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_ClearThreadGCAble() andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: PR_Lock(_pr_activeLock); andre@0: _PR_MD_CURRENT_THREAD()->flags &= (~_PR_GCABLE_THREAD); andre@0: PR_Unlock(_pr_activeLock); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThreadScope) PR_GetThreadScope(const PRThread *thread) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (_PR_IS_NATIVE_THREAD(thread)) { andre@0: return (thread->flags & _PR_BOUND_THREAD) ? PR_GLOBAL_BOUND_THREAD : andre@0: PR_GLOBAL_THREAD; andre@0: } else andre@0: return PR_LOCAL_THREAD; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThreadType) PR_GetThreadType(const PRThread *thread) andre@0: { andre@0: return (thread->flags & _PR_SYSTEM) ? PR_SYSTEM_THREAD : PR_USER_THREAD; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRThreadState) PR_GetThreadState(const PRThread *thread) andre@0: { andre@0: return (NULL == thread->term) ? PR_UNJOINABLE_THREAD : PR_JOINABLE_THREAD; andre@0: } /* PR_GetThreadState */