summaryrefslogtreecommitdiff
path: root/pr/src/threads/combined/pruthr.c
diff options
context:
space:
mode:
Diffstat (limited to 'pr/src/threads/combined/pruthr.c')
-rw-r--r--pr/src/threads/combined/pruthr.c676
1 files changed, 366 insertions, 310 deletions
diff --git a/pr/src/threads/combined/pruthr.c b/pr/src/threads/combined/pruthr.c
index fe0c4293..44a08200 100644
--- a/pr/src/threads/combined/pruthr.c
+++ b/pr/src/threads/combined/pruthr.c
@@ -60,7 +60,7 @@ static void _PR_InitializeRecycledThread(PRThread *thread);
static void _PR_UserRunThread(void);
void _PR_InitThreads(PRThreadType type, PRThreadPriority priority,
- PRUintn maxPTDs)
+ PRUintn maxPTDs)
{
PRThread *thread;
PRThreadStack *stack;
@@ -74,13 +74,13 @@ void _PR_InitThreads(PRThreadType type, PRThreadPriority priority,
stack = PR_NEWZAP(PRThreadStack);
#ifdef HAVE_STACK_GROWING_UP
stack->stackTop = (char*) ((((PRWord)&type) >> _pr_pageShift)
- << _pr_pageShift);
+ << _pr_pageShift);
#else
#if defined(SOLARIS) || defined (UNIXWARE) && defined (USR_SVR4_THREADS)
stack->stackTop = (char*) &thread;
#else
stack->stackTop = (char*) ((((PRWord)&type + _pr_pageSize - 1)
- >> _pr_pageShift) << _pr_pageShift);
+ >> _pr_pageShift) << _pr_pageShift);
#endif
#endif
#else
@@ -104,11 +104,13 @@ void _PR_InitThreads(PRThreadType type, PRThreadPriority priority,
_pr_userActive++;
_pr_primordialExitCount = 1;
}
- thread->no_sched = 1;
- _pr_primordialExitCVar = PR_NewCondVar(_pr_activeLock);
+ thread->no_sched = 1;
+ _pr_primordialExitCVar = PR_NewCondVar(_pr_activeLock);
}
- if (!thread) PR_Abort();
+ if (!thread) {
+ PR_Abort();
+ }
#ifdef _PR_LOCAL_THREADS_ONLY
thread->flags |= _PR_PRIMORDIAL;
#else
@@ -174,13 +176,13 @@ static void _PR_InitializeNativeStack(PRThreadStack *ts)
** Setup stackTop and stackBottom values.
*/
#ifdef HAVE_STACK_GROWING_UP
- ts->allocBase = (char*) ((((PRWord)&ts) >> _pr_pageShift)
- << _pr_pageShift);
+ ts->allocBase = (char*) ((((PRWord)&ts) >> _pr_pageShift)
+ << _pr_pageShift);
ts->stackBottom = ts->allocBase + ts->stackSize;
ts->stackTop = ts->allocBase;
#else
ts->allocBase = (char*) ((((PRWord)&ts + _pr_pageSize - 1)
- >> _pr_pageShift) << _pr_pageShift);
+ >> _pr_pageShift) << _pr_pageShift);
ts->stackTop = ts->allocBase;
ts->stackBottom = ts->allocBase - ts->stackSize;
#endif
@@ -251,19 +253,19 @@ static void _PR_InitializeRecycledThread(PRThread *thread)
PRStatus _PR_RecycleThread(PRThread *thread)
{
if ( _PR_IS_NATIVE_THREAD(thread) &&
- _PR_NUM_DEADNATIVE < _pr_recycleThreads) {
+ _PR_NUM_DEADNATIVE < _pr_recycleThreads) {
_PR_DEADQ_LOCK;
PR_APPEND_LINK(&thread->links, &_PR_DEADNATIVEQ);
_PR_INC_DEADNATIVE;
_PR_DEADQ_UNLOCK;
- return (PR_SUCCESS);
+ return (PR_SUCCESS);
} else if ( !_PR_IS_NATIVE_THREAD(thread) &&
_PR_NUM_DEADUSER < _pr_recycleThreads) {
_PR_DEADQ_LOCK;
PR_APPEND_LINK(&thread->links, &_PR_DEADUSERQ);
_PR_INC_DEADUSER;
_PR_DEADQ_UNLOCK;
- return (PR_SUCCESS);
+ return (PR_SUCCESS);
}
return (PR_FAILURE);
}
@@ -433,7 +435,7 @@ void _PR_NativeRunThread(void *arg)
thread->state = _PR_DEAD_STATE;
if (!_pr_recycleThreads || (_PR_RecycleThread(thread) ==
- PR_FAILURE)) {
+ PR_FAILURE)) {
/*
* thread not recycled
* platform-specific thread exit processing
@@ -460,8 +462,9 @@ static void _PR_UserRunThread(void)
PRThread *thread = _PR_MD_CURRENT_THREAD();
PRIntn is;
- if (_MD_LAST_THREAD())
- _MD_LAST_THREAD()->no_sched = 0;
+ if (_MD_LAST_THREAD()) {
+ _MD_LAST_THREAD()->no_sched = 0;
+ }
#ifdef HAVE_CUSTOM_USER_THREADS
if (thread->stack == NULL) {
@@ -472,17 +475,19 @@ static void _PR_UserRunThread(void)
while(1) {
/* Run thread main */
- if ( !_PR_IS_NATIVE_THREAD(thread)) _PR_MD_SET_INTSOFF(0);
+ if ( !_PR_IS_NATIVE_THREAD(thread)) {
+ _PR_MD_SET_INTSOFF(0);
+ }
- /*
- * Add to list of active threads
- */
- if (!(thread->flags & _PR_IDLE_THREAD)) {
- PR_Lock(_pr_activeLock);
- PR_APPEND_LINK(&thread->active, &_PR_ACTIVE_LOCAL_THREADQ());
- _pr_local_threads++;
- PR_Unlock(_pr_activeLock);
- }
+ /*
+ * Add to list of active threads
+ */
+ if (!(thread->flags & _PR_IDLE_THREAD)) {
+ PR_Lock(_pr_activeLock);
+ PR_APPEND_LINK(&thread->active, &_PR_ACTIVE_LOCAL_THREADQ());
+ _pr_local_threads++;
+ PR_Unlock(_pr_activeLock);
+ }
(*thread->startFunc)(thread->arg);
@@ -504,14 +509,14 @@ static void _PR_UserRunThread(void)
PR_ASSERT(thread->io_suspended == PR_FALSE);
PR_Lock(_pr_activeLock);
- /*
- * remove thread from list of active threads
- */
- if (!(thread->flags & _PR_IDLE_THREAD)) {
- PR_REMOVE_LINK(&thread->active);
- _pr_local_threads--;
- }
- PR_Unlock(_pr_activeLock);
+ /*
+ * remove thread from list of active threads
+ */
+ if (!(thread->flags & _PR_IDLE_THREAD)) {
+ PR_REMOVE_LINK(&thread->active);
+ _pr_local_threads--;
+ }
+ PR_Unlock(_pr_activeLock);
PR_LOG(_pr_thread_lm, PR_LOG_MIN, ("thread exiting"));
/* All done, time to go away */
@@ -521,16 +526,16 @@ static void _PR_UserRunThread(void)
_PR_NotifyJoinWaiters(thread);
- _PR_DecrActiveThreadCount(thread);
+ _PR_DecrActiveThreadCount(thread);
thread->state = _PR_DEAD_STATE;
if (!_pr_recycleThreads || (_PR_RecycleThread(thread) ==
- PR_FAILURE)) {
+ PR_FAILURE)) {
/*
** Destroy the thread resources
*/
- _PR_UserDestroyThread(thread);
+ _PR_UserDestroyThread(thread);
}
/*
@@ -557,54 +562,58 @@ void _PR_SetThreadPriority(PRThread *thread, PRThreadPriority newPri)
return;
}
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_INTSOFF(is);
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_INTSOFF(is);
+ }
_PR_THREAD_LOCK(thread);
if (newPri != thread->priority) {
- _PRCPU *cpu = thread->cpu;
+ _PRCPU *cpu = thread->cpu;
- switch (thread->state) {
- case _PR_RUNNING:
- /* Change my priority */
+ switch (thread->state) {
+ case _PR_RUNNING:
+ /* Change my priority */
- _PR_RUNQ_LOCK(cpu);
- thread->priority = newPri;
- if (_PR_RUNQREADYMASK(cpu) >> (newPri + 1)) {
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_SET_RESCHED_FLAG();
- }
- _PR_RUNQ_UNLOCK(cpu);
- break;
+ _PR_RUNQ_LOCK(cpu);
+ thread->priority = newPri;
+ if (_PR_RUNQREADYMASK(cpu) >> (newPri + 1)) {
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_SET_RESCHED_FLAG();
+ }
+ }
+ _PR_RUNQ_UNLOCK(cpu);
+ break;
- case _PR_RUNNABLE:
+ case _PR_RUNNABLE:
- _PR_RUNQ_LOCK(cpu);
- /* Move to different runQ */
- _PR_DEL_RUNQ(thread);
- thread->priority = newPri;
- PR_ASSERT(!(thread->flags & _PR_IDLE_THREAD));
- _PR_ADD_RUNQ(thread, cpu, newPri);
- _PR_RUNQ_UNLOCK(cpu);
+ _PR_RUNQ_LOCK(cpu);
+ /* Move to different runQ */
+ _PR_DEL_RUNQ(thread);
+ thread->priority = newPri;
+ PR_ASSERT(!(thread->flags & _PR_IDLE_THREAD));
+ _PR_ADD_RUNQ(thread, cpu, newPri);
+ _PR_RUNQ_UNLOCK(cpu);
- if (newPri > me->priority) {
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_SET_RESCHED_FLAG();
- }
+ if (newPri > me->priority) {
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_SET_RESCHED_FLAG();
+ }
+ }
- break;
+ break;
- case _PR_LOCK_WAIT:
- case _PR_COND_WAIT:
- case _PR_IO_WAIT:
- case _PR_SUSPENDED:
+ case _PR_LOCK_WAIT:
+ case _PR_COND_WAIT:
+ case _PR_IO_WAIT:
+ case _PR_SUSPENDED:
- thread->priority = newPri;
- break;
- }
+ thread->priority = newPri;
+ break;
+ }
}
_PR_THREAD_UNLOCK(thread);
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_INTSON(is);
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_INTSON(is);
+ }
}
/*
@@ -618,51 +627,53 @@ static void _PR_Suspend(PRThread *thread)
PR_ASSERT(thread != me);
PR_ASSERT(!_PR_IS_NATIVE_THREAD(thread) || (!thread->cpu));
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSOFF(is);
+ }
_PR_THREAD_LOCK(thread);
switch (thread->state) {
- case _PR_RUNNABLE:
- if (!_PR_IS_NATIVE_THREAD(thread)) {
- _PR_RUNQ_LOCK(thread->cpu);
- _PR_DEL_RUNQ(thread);
- _PR_RUNQ_UNLOCK(thread->cpu);
+ case _PR_RUNNABLE:
+ if (!_PR_IS_NATIVE_THREAD(thread)) {
+ _PR_RUNQ_LOCK(thread->cpu);
+ _PR_DEL_RUNQ(thread);
+ _PR_RUNQ_UNLOCK(thread->cpu);
+
+ _PR_MISCQ_LOCK(thread->cpu);
+ _PR_ADD_SUSPENDQ(thread, thread->cpu);
+ _PR_MISCQ_UNLOCK(thread->cpu);
+ } else {
+ /*
+ * Only LOCAL threads are suspended by _PR_Suspend
+ */
+ PR_ASSERT(0);
+ }
+ thread->state = _PR_SUSPENDED;
+ break;
- _PR_MISCQ_LOCK(thread->cpu);
- _PR_ADD_SUSPENDQ(thread, thread->cpu);
- _PR_MISCQ_UNLOCK(thread->cpu);
- } else {
+ case _PR_RUNNING:
/*
- * Only LOCAL threads are suspended by _PR_Suspend
+ * The thread being suspended should be a LOCAL thread with
+ * _pr_numCPUs == 1. Hence, the thread cannot be in RUNNING state
*/
- PR_ASSERT(0);
- }
- thread->state = _PR_SUSPENDED;
- break;
-
- case _PR_RUNNING:
- /*
- * The thread being suspended should be a LOCAL thread with
- * _pr_numCPUs == 1. Hence, the thread cannot be in RUNNING state
- */
- PR_ASSERT(0);
- break;
+ PR_ASSERT(0);
+ break;
- case _PR_LOCK_WAIT:
- case _PR_IO_WAIT:
- case _PR_COND_WAIT:
- if (_PR_IS_NATIVE_THREAD(thread)) {
- _PR_MD_SUSPEND_THREAD(thread);
- }
- thread->flags |= _PR_SUSPENDING;
- break;
+ case _PR_LOCK_WAIT:
+ case _PR_IO_WAIT:
+ case _PR_COND_WAIT:
+ if (_PR_IS_NATIVE_THREAD(thread)) {
+ _PR_MD_SUSPEND_THREAD(thread);
+ }
+ thread->flags |= _PR_SUSPENDING;
+ break;
- default:
- PR_Abort();
+ default:
+ PR_Abort();
}
_PR_THREAD_UNLOCK(thread);
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_INTSON(is);
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_INTSON(is);
+ }
}
static void _PR_Resume(PRThread *thread)
@@ -671,72 +682,75 @@ static void _PR_Resume(PRThread *thread)
PRIntn is;
PRThread *me = _PR_MD_CURRENT_THREAD();
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_INTSOFF(is);
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_INTSOFF(is);
+ }
_PR_THREAD_LOCK(thread);
switch (thread->state) {
- case _PR_SUSPENDED:
- thread->state = _PR_RUNNABLE;
- thread->flags &= ~_PR_SUSPENDING;
- if (!_PR_IS_NATIVE_THREAD(thread)) {
- _PR_MISCQ_LOCK(thread->cpu);
- _PR_DEL_SUSPENDQ(thread);
- _PR_MISCQ_UNLOCK(thread->cpu);
-
- pri = thread->priority;
-
- _PR_RUNQ_LOCK(thread->cpu);
- _PR_ADD_RUNQ(thread, thread->cpu, pri);
- _PR_RUNQ_UNLOCK(thread->cpu);
-
- if (pri > _PR_MD_CURRENT_THREAD()->priority) {
- if (!_PR_IS_NATIVE_THREAD(me))
- _PR_SET_RESCHED_FLAG();
+ case _PR_SUSPENDED:
+ thread->state = _PR_RUNNABLE;
+ thread->flags &= ~_PR_SUSPENDING;
+ if (!_PR_IS_NATIVE_THREAD(thread)) {
+ _PR_MISCQ_LOCK(thread->cpu);
+ _PR_DEL_SUSPENDQ(thread);
+ _PR_MISCQ_UNLOCK(thread->cpu);
+
+ pri = thread->priority;
+
+ _PR_RUNQ_LOCK(thread->cpu);
+ _PR_ADD_RUNQ(thread, thread->cpu, pri);
+ _PR_RUNQ_UNLOCK(thread->cpu);
+
+ if (pri > _PR_MD_CURRENT_THREAD()->priority) {
+ if (!_PR_IS_NATIVE_THREAD(me)) {
+ _PR_SET_RESCHED_FLAG();
+ }
+ }
+ } else {
+ PR_ASSERT(0);
}
- } else {
- PR_ASSERT(0);
- }
- break;
+ break;
- case _PR_IO_WAIT:
- case _PR_COND_WAIT:
- thread->flags &= ~_PR_SUSPENDING;
-/* PR_ASSERT(thread->wait.monitor->stickyCount == 0); */
- break;
+ case _PR_IO_WAIT:
+ case _PR_COND_WAIT:
+ thread->flags &= ~_PR_SUSPENDING;
+ /* PR_ASSERT(thread->wait.monitor->stickyCount == 0); */
+ break;
- case _PR_LOCK_WAIT:
- {
- PRLock *wLock = thread->wait.lock;
+ case _PR_LOCK_WAIT:
+ {
+ PRLock *wLock = thread->wait.lock;
- thread->flags &= ~_PR_SUSPENDING;
+ thread->flags &= ~_PR_SUSPENDING;
- _PR_LOCK_LOCK(wLock);
- if (thread->wait.lock->owner == 0) {
- _PR_UnblockLockWaiter(thread->wait.lock);
+ _PR_LOCK_LOCK(wLock);
+ if (thread->wait.lock->owner == 0) {
+ _PR_UnblockLockWaiter(thread->wait.lock);
+ }
+ _PR_LOCK_UNLOCK(wLock);
+ break;
}
- _PR_LOCK_UNLOCK(wLock);
- break;
- }
- case _PR_RUNNABLE:
- break;
- case _PR_RUNNING:
- /*
- * The thread being suspended should be a LOCAL thread with
- * _pr_numCPUs == 1. Hence, the thread cannot be in RUNNING state
- */
- PR_ASSERT(0);
- break;
+ case _PR_RUNNABLE:
+ break;
+ case _PR_RUNNING:
+ /*
+ * The thread being suspended should be a LOCAL thread with
+ * _pr_numCPUs == 1. Hence, the thread cannot be in RUNNING state
+ */
+ PR_ASSERT(0);
+ break;
- default:
- /*
- * thread should have been in one of the above-listed blocked states
- * (_PR_JOIN_WAIT, _PR_IO_WAIT, _PR_UNBORN, _PR_DEAD_STATE)
- */
- PR_Abort();
+ default:
+ /*
+ * thread should have been in one of the above-listed blocked states
+ * (_PR_JOIN_WAIT, _PR_IO_WAIT, _PR_UNBORN, _PR_DEAD_STATE)
+ */
+ PR_Abort();
}
_PR_THREAD_UNLOCK(thread);
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSON(is);
+ }
}
@@ -761,7 +775,7 @@ static PRThread *get_thread(_PRCPU *cpu, PRBool *wakeup_cpus)
}
thread = NULL;
for (pri = priMax; pri >= priMin ; pri-- ) {
- if (r & (1 << pri)) {
+ if (r & (1 << pri)) {
for (qp = _PR_RUNQ(cpu)[pri].next;
qp != &_PR_RUNQ(cpu)[pri];
qp = qp->next) {
@@ -851,16 +865,16 @@ void _PR_Schedule(void)
* operation, for example
*/
if ((thread = suspendAllThread) != 0) {
- if ((!(thread->no_sched)) && (thread->state == _PR_RUNNABLE)) {
+ if ((!(thread->no_sched)) && (thread->state == _PR_RUNNABLE)) {
/* Pull thread off of its run queue */
_PR_DEL_RUNQ(thread);
_PR_RUNQ_UNLOCK(cpu);
goto found_thread;
- } else {
+ } else {
thread = NULL;
_PR_RUNQ_UNLOCK(cpu);
goto idle_thread;
- }
+ }
}
r = _PR_RUNQREADYMASK(cpu);
if (r==0) {
@@ -873,7 +887,7 @@ void _PR_Schedule(void)
}
thread = NULL;
for (pri = priMax; pri >= priMin ; pri-- ) {
- if (r & (1 << pri)) {
+ if (r & (1 << pri)) {
for (qp = _PR_RUNQ(cpu)[pri].next;
qp != &_PR_RUNQ(cpu)[pri];
qp = qp->next) {
@@ -882,7 +896,7 @@ void _PR_Schedule(void)
* skip non-schedulable threads
*/
PR_ASSERT(!(thread->flags & _PR_IDLE_THREAD));
- if ((thread->no_sched) && (me != thread)){
+ if ((thread->no_sched) && (me != thread)) {
thread = NULL;
continue;
} else {
@@ -904,35 +918,37 @@ void _PR_Schedule(void)
for (qp = _PR_CPUQ().next; qp != &_PR_CPUQ(); qp = qp->next) {
if (cpu != _PR_CPU_PTR(qp)) {
if ((thread = get_thread(_PR_CPU_PTR(qp), &wakeup_cpus))
- != NULL) {
+ != NULL) {
thread->cpu = cpu;
_PR_CPU_LIST_UNLOCK();
- if (wakeup_cpus == PR_TRUE)
+ if (wakeup_cpus == PR_TRUE) {
_PR_MD_WAKEUP_CPUS();
+ }
goto found_thread;
}
}
}
_PR_CPU_LIST_UNLOCK();
- if (wakeup_cpus == PR_TRUE)
+ if (wakeup_cpus == PR_TRUE) {
_PR_MD_WAKEUP_CPUS();
+ }
#endif /* _PR_LOCAL_THREADS_ONLY */
idle_thread:
- /*
- ** There are no threads to run. Switch to the idle thread
- */
+ /*
+ ** There are no threads to run. Switch to the idle thread
+ */
PR_LOG(_pr_sched_lm, PR_LOG_MAX, ("pausing"));
thread = _PR_MD_CURRENT_CPU()->idle_thread;
found_thread:
PR_ASSERT((me == thread) || ((thread->state == _PR_RUNNABLE) &&
- (!(thread->no_sched))));
+ (!(thread->no_sched))));
/* Resume the thread */
PR_LOG(_pr_sched_lm, PR_LOG_MAX,
- ("switching to %d[%p]", thread->id, thread));
+ ("switching to %d[%p]", thread->id, thread));
PR_ASSERT(thread->state != _PR_RUNNING);
thread->state = _PR_RUNNING;
@@ -940,9 +956,10 @@ found_thread:
* resource, and by the time we got here another real native thread had
* already given us the resource and put us back on the runqueue
*/
- PR_ASSERT(thread->cpu == _PR_MD_CURRENT_CPU());
- if (thread != me)
+ PR_ASSERT(thread->cpu == _PR_MD_CURRENT_CPU());
+ if (thread != me) {
_PR_MD_RESTORE_CONTEXT(thread);
+ }
#if 0
/* XXXMB; with setjmp/longjmp it is impossible to land here, but
* it is not with fibers... Is this a bad thing? I believe it is
@@ -959,7 +976,7 @@ found_thread:
*/
static PRThread *
_PR_AttachThread(PRThreadType type, PRThreadPriority priority,
- PRThreadStack *stack)
+ PRThreadStack *stack)
{
PRThread *thread;
char *mem;
@@ -978,9 +995,9 @@ _PR_AttachThread(PRThreadType type, PRThreadPriority priority,
thread->state = _PR_RUNNING;
PR_INIT_CLIST(&thread->lockList);
if (_PR_MD_NEW_LOCK(&thread->threadLock) == PR_FAILURE) {
- PR_DELETE(thread);
- return 0;
- }
+ PR_DELETE(thread);
+ return 0;
+ }
return thread;
}
@@ -991,13 +1008,13 @@ _PR_AttachThread(PRThreadType type, PRThreadPriority priority,
PR_IMPLEMENT(PRThread*)
_PR_NativeCreateThread(PRThreadType type,
- void (*start)(void *arg),
- void *arg,
- PRThreadPriority priority,
- PRThreadScope scope,
- PRThreadState state,
- PRUint32 stackSize,
- PRUint32 flags)
+ void (*start)(void *arg),
+ void *arg,
+ PRThreadPriority priority,
+ PRThreadScope scope,
+ PRThreadState state,
+ PRUint32 stackSize,
+ PRUint32 flags)
{
PRThread *thread;
@@ -1031,27 +1048,27 @@ _PR_NativeCreateThread(PRThreadType type,
*/
if (state == PR_JOINABLE_THREAD) {
thread->term = PR_NewCondVar(_pr_terminationCVLock);
- if (thread->term == NULL) {
- PR_DELETE(thread->stack);
- goto done;
- }
+ if (thread->term == NULL) {
+ PR_DELETE(thread->stack);
+ goto done;
+ }
}
- thread->state = _PR_RUNNING;
+ thread->state = _PR_RUNNING;
if (_PR_MD_CREATE_THREAD(thread, _PR_NativeRunThread, priority,
- scope,state,stackSize) == PR_SUCCESS) {
+ scope,state,stackSize) == PR_SUCCESS) {
return thread;
}
if (thread->term) {
PR_DestroyCondVar(thread->term);
thread->term = NULL;
}
- PR_DELETE(thread->stack);
+ PR_DELETE(thread->stack);
}
done:
if (thread) {
- _PR_DecrActiveThreadCount(thread);
+ _PR_DecrActiveThreadCount(thread);
_PR_DestroyThread(thread);
}
return NULL;
@@ -1060,13 +1077,13 @@ done:
/************************************************************************/
PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
- void (*start)(void *arg),
- void *arg,
- PRThreadPriority priority,
- PRThreadScope scope,
- PRThreadState state,
- PRUint32 stackSize,
- PRUint32 flags)
+ void (*start)(void *arg),
+ void *arg,
+ PRThreadPriority priority,
+ PRThreadScope scope,
+ PRThreadState state,
+ PRUint32 stackSize,
+ PRUint32 flags)
{
PRThread *me;
PRThread *thread = NULL;
@@ -1087,33 +1104,38 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
priority = PR_PRIORITY_FIRST;
}
- if (!_pr_initialized) _PR_ImplicitInitialization();
+ if (!_pr_initialized) {
+ _PR_ImplicitInitialization();
+ }
- if (! (flags & _PR_IDLE_THREAD))
+ if (! (flags & _PR_IDLE_THREAD)) {
me = _PR_MD_CURRENT_THREAD();
+ }
#if defined(_PR_GLOBAL_THREADS_ONLY)
- /*
- * can create global threads only
- */
- if (scope == PR_LOCAL_THREAD)
- scope = PR_GLOBAL_THREAD;
+ /*
+ * can create global threads only
+ */
+ if (scope == PR_LOCAL_THREAD) {
+ scope = PR_GLOBAL_THREAD;
+ }
#endif
- if (_native_threads_only)
- scope = PR_GLOBAL_THREAD;
+ if (_native_threads_only) {
+ scope = PR_GLOBAL_THREAD;
+ }
native = (((scope == PR_GLOBAL_THREAD)|| (scope == PR_GLOBAL_BOUND_THREAD))
- && _PR_IS_NATIVE_THREAD_SUPPORTED());
+ && _PR_IS_NATIVE_THREAD_SUPPORTED());
_PR_ADJUST_STACKSIZE(stackSize);
if (native) {
- /*
- * clear the IDLE_THREAD flag which applies to LOCAL
- * threads only
- */
- flags &= ~_PR_IDLE_THREAD;
+ /*
+ * clear the IDLE_THREAD flag which applies to LOCAL
+ * threads only
+ */
+ flags &= ~_PR_IDLE_THREAD;
flags |= _PR_GLOBAL_SCOPE;
if (_PR_NUM_DEADNATIVE > 0) {
_PR_DEADQ_LOCK;
@@ -1129,35 +1151,38 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
_PR_InitializeRecycledThread(thread);
thread->startFunc = start;
thread->arg = arg;
- thread->flags = (flags | _PR_GLOBAL_SCOPE);
- if (type == PR_SYSTEM_THREAD)
- {
- thread->flags |= _PR_SYSTEM;
- PR_ATOMIC_INCREMENT(&_pr_systemActive);
- }
- else PR_ATOMIC_INCREMENT(&_pr_userActive);
+ thread->flags = (flags | _PR_GLOBAL_SCOPE);
+ if (type == PR_SYSTEM_THREAD)
+ {
+ thread->flags |= _PR_SYSTEM;
+ PR_ATOMIC_INCREMENT(&_pr_systemActive);
+ }
+ else {
+ PR_ATOMIC_INCREMENT(&_pr_userActive);
+ }
- if (state == PR_JOINABLE_THREAD) {
- if (!thread->term)
- thread->term = PR_NewCondVar(_pr_terminationCVLock);
- }
- else {
- if(thread->term) {
- PR_DestroyCondVar(thread->term);
+ if (state == PR_JOINABLE_THREAD) {
+ if (!thread->term) {
+ thread->term = PR_NewCondVar(_pr_terminationCVLock);
+ }
+ }
+ else {
+ if(thread->term) {
+ PR_DestroyCondVar(thread->term);
thread->term = 0;
- }
- }
+ }
+ }
thread->priority = priority;
- _PR_MD_SET_PRIORITY(&(thread->md), priority);
- /* XXX what about stackSize? */
- thread->state = _PR_RUNNING;
+ _PR_MD_SET_PRIORITY(&(thread->md), priority);
+ /* XXX what about stackSize? */
+ thread->state = _PR_RUNNING;
_PR_MD_WAKEUP_WAITER(thread);
- return thread;
+ return thread;
}
}
thread = _PR_NativeCreateThread(type, start, arg, priority,
- scope, state, stackSize, flags);
+ scope, state, stackSize, flags);
} else {
if (_PR_NUM_DEADUSER > 0) {
_PR_DEADQ_LOCK;
@@ -1174,7 +1199,7 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
while( ptr != &_PR_DEADUSERQ ) {
thread = _PR_THREAD_PTR(ptr);
if ((thread->stack->stackSize >= stackSize) &&
- (!thread->no_sched)) {
+ (!thread->no_sched)) {
PR_REMOVE_LINK(&thread->links);
_PR_DEC_DEADUSER;
break;
@@ -1186,20 +1211,21 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
_PR_DEADQ_UNLOCK;
- if (thread) {
+ if (thread) {
_PR_InitializeRecycledThread(thread);
thread->startFunc = start;
thread->arg = arg;
thread->priority = priority;
- if (state == PR_JOINABLE_THREAD) {
- if (!thread->term)
- thread->term = PR_NewCondVar(_pr_terminationCVLock);
- } else {
- if(thread->term) {
- PR_DestroyCondVar(thread->term);
- thread->term = 0;
- }
- }
+ if (state == PR_JOINABLE_THREAD) {
+ if (!thread->term) {
+ thread->term = PR_NewCondVar(_pr_terminationCVLock);
+ }
+ } else {
+ if(thread->term) {
+ PR_DestroyCondVar(thread->term);
+ thread->term = 0;
+ }
+ }
useRecycled++;
}
}
@@ -1257,8 +1283,9 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
PR_INIT_CLIST(&thread->lockList);
if (_PR_MD_INIT_THREAD(thread) == PR_FAILURE) {
- if (thread->threadAllocatedOnStack == 1)
+ if (thread->threadAllocatedOnStack == 1) {
_PR_FreeStack(thread->stack);
+ }
else {
PR_DELETE(thread);
}
@@ -1267,8 +1294,9 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
}
if (_PR_MD_NEW_LOCK(&thread->threadLock) == PR_FAILURE) {
- if (thread->threadAllocatedOnStack == 1)
+ if (thread->threadAllocatedOnStack == 1) {
_PR_FreeStack(thread->stack);
+ }
else {
PR_DELETE(thread->privateData);
PR_DELETE(thread);
@@ -1281,8 +1309,9 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
if (status == PR_FALSE) {
_PR_MD_FREE_LOCK(&thread->threadLock);
- if (thread->threadAllocatedOnStack == 1)
+ if (thread->threadAllocatedOnStack == 1) {
_PR_FreeStack(thread->stack);
+ }
else {
PR_DELETE(thread->privateData);
PR_DELETE(thread);
@@ -1298,8 +1327,9 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
thread->term = PR_NewCondVar(_pr_terminationCVLock);
if (thread->term == NULL) {
_PR_MD_FREE_LOCK(&thread->threadLock);
- if (thread->threadAllocatedOnStack == 1)
+ if (thread->threadAllocatedOnStack == 1) {
_PR_FreeStack(thread->stack);
+ }
else {
PR_DELETE(thread->privateData);
PR_DELETE(thread);
@@ -1323,15 +1353,17 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
/* Make thread runnable */
thread->state = _PR_RUNNABLE;
- /*
- * Add to list of active threads
- */
+ /*
+ * Add to list of active threads
+ */
PR_Unlock(_pr_activeLock);
- if ((! (thread->flags & _PR_IDLE_THREAD)) && _PR_IS_NATIVE_THREAD(me) )
+ if ((! (thread->flags & _PR_IDLE_THREAD)) && _PR_IS_NATIVE_THREAD(me) ) {
thread->cpu = _PR_GetPrimordialCPU();
- else
+ }
+ else {
thread->cpu = _PR_MD_CURRENT_CPU();
+ }
PR_ASSERT(!_PR_IS_NATIVE_THREAD(thread));
@@ -1353,20 +1385,21 @@ PR_IMPLEMENT(PRThread*) _PR_CreateThread(PRThreadType type,
} else if (_PR_IS_NATIVE_THREAD(me)) {
_PR_MD_WAKEUP_WAITER(thread);
}
- if ((! (thread->flags & _PR_IDLE_THREAD)) && !_PR_IS_NATIVE_THREAD(me) )
+ if ((! (thread->flags & _PR_IDLE_THREAD)) && !_PR_IS_NATIVE_THREAD(me) ) {
_PR_INTSON(is);
+ }
}
return thread;
}
PR_IMPLEMENT(PRThread*) PR_CreateThread(PRThreadType type,
- void (*start)(void *arg),
- void *arg,
- PRThreadPriority priority,
- PRThreadScope scope,
- PRThreadState state,
- PRUint32 stackSize)
+ void (*start)(void *arg),
+ void *arg,
+ PRThreadPriority priority,
+ PRThreadScope scope,
+ PRThreadState state,
+ PRUint32 stackSize)
{
return _PR_CreateThread(type, start, arg, priority, scope, state,
stackSize, 0);
@@ -1385,7 +1418,7 @@ PR_IMPLEMENT(PRThread*) PR_CreateThread(PRThreadType type,
** thread. PR_Init does this automatically for the primordial thread.
*/
PRThread* _PRI_AttachThread(PRThreadType type,
- PRThreadPriority priority, PRThreadStack *stack, PRUint32 flags)
+ PRThreadPriority priority, PRThreadStack *stack, PRUint32 flags)
{
PRThread *thread;
@@ -1416,9 +1449,9 @@ PRThread* _PRI_AttachThread(PRThreadType type,
PR_INIT_CLIST(&thread->links);
if (_PR_MD_INIT_ATTACHED_THREAD(thread) == PR_FAILURE) {
- PR_DELETE(thread->stack);
- _PR_DestroyThread(thread);
- return NULL;
+ PR_DELETE(thread->stack);
+ _PR_DestroyThread(thread);
+ return NULL;
}
_PR_MD_SET_CURRENT_CPU(NULL);
@@ -1442,7 +1475,7 @@ PRThread* _PRI_AttachThread(PRThreadType type,
}
PR_IMPLEMENT(PRThread*) PR_AttachThread(PRThreadType type,
- PRThreadPriority priority, PRThreadStack *stack)
+ PRThreadPriority priority, PRThreadStack *stack)
{
return PR_GetCurrentThread();
}
@@ -1458,8 +1491,9 @@ PR_IMPLEMENT(void) PR_DetachThread(void)
PRThread *me;
if (_pr_initialized) {
me = _PR_MD_GET_ATTACHED_THREAD();
- if ((me != NULL) && (me->flags & _PR_ATTACHED))
+ if ((me != NULL) && (me->flags & _PR_ATTACHED)) {
_PRI_DetachThread();
+ }
}
#endif
}
@@ -1468,12 +1502,12 @@ void _PRI_DetachThread(void)
{
PRThread *me = _PR_MD_CURRENT_THREAD();
- if (me->flags & _PR_PRIMORDIAL) {
- /*
- * ignore, if primordial thread
- */
- return;
- }
+ if (me->flags & _PR_PRIMORDIAL) {
+ /*
+ * ignore, if primordial thread
+ */
+ return;
+ }
PR_ASSERT(me->flags & _PR_ATTACHED);
PR_ASSERT(_PR_IS_NATIVE_THREAD(me));
_PR_CleanupThread(me);
@@ -1483,8 +1517,9 @@ void _PRI_DetachThread(void)
_PR_MD_CLEAN_THREAD(me);
_PR_MD_SET_CURRENT_THREAD(NULL);
- if (!me->threadAllocatedOnStack)
+ if (!me->threadAllocatedOnStack) {
PR_DELETE(me->stack);
+ }
_PR_MD_FREE_LOCK(&me->threadLock);
PR_DELETE(me);
}
@@ -1508,8 +1543,9 @@ PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thread)
PRCondVar *term;
PRThread *me = _PR_MD_CURRENT_THREAD();
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSOFF(is);
+ }
term = thread->term;
/* can't join a non-joinable thread */
if (term == NULL) {
@@ -1521,8 +1557,9 @@ PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thread)
if (term->condQ.next != &term->condQ) {
goto ErrorExit;
}
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSON(is);
+ }
/* wait for the target thread's termination cv invariant */
PR_Lock (_pr_terminationCVLock);
@@ -1536,8 +1573,9 @@ PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thread)
again and put it back on its run Q. When it gets scheduled later in
_PR_RunThread code, it will clean up its stack.
*/
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSOFF(is);
+ }
thread->state = _PR_RUNNABLE;
if ( !_PR_IS_NATIVE_THREAD(thread) ) {
_PR_THREAD_LOCK(thread);
@@ -1549,20 +1587,23 @@ PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thread)
_PR_AddThreadToRunQ(me, thread);
_PR_THREAD_UNLOCK(thread);
}
- if (!_PR_IS_NATIVE_THREAD(me))
+ if (!_PR_IS_NATIVE_THREAD(me)) {
_PR_INTSON(is);
+ }
_PR_MD_WAKEUP_WAITER(thread);
return PR_SUCCESS;
ErrorExit:
- if ( !_PR_IS_NATIVE_THREAD(me)) _PR_INTSON(is);
+ if ( !_PR_IS_NATIVE_THREAD(me)) {
+ _PR_INTSON(is);
+ }
return PR_FAILURE;
}
PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thread,
- PRThreadPriority newPri)
+ PRThreadPriority newPri)
{
/*
@@ -1578,7 +1619,9 @@ PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thread,
if ( _PR_IS_NATIVE_THREAD(thread) ) {
thread->priority = newPri;
_PR_MD_SET_PRIORITY(&(thread->md), newPri);
- } else _PR_SetThreadPriority(thread, newPri);
+ } else {
+ _PR_SetThreadPriority(thread, newPri);
+ }
}
PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name)
@@ -1592,14 +1635,16 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name)
}
thread = PR_GetCurrentThread();
- if (!thread)
+ if (!thread) {
return PR_FAILURE;
+ }
PR_Free(thread->name);
nameLen = strlen(name);
thread->name = (char *)PR_Malloc(nameLen + 1);
- if (!thread->name)
+ if (!thread->name) {
return PR_FAILURE;
+ }
memcpy(thread->name, name, nameLen + 1);
_PR_MD_SET_CURRENT_THREAD_NAME(thread->name);
return PR_SUCCESS;
@@ -1607,8 +1652,9 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name)
PR_IMPLEMENT(const char *) PR_GetThreadName(const PRThread *thread)
{
- if (!thread)
+ if (!thread) {
return NULL;
+ }
return thread->name;
}
@@ -1630,19 +1676,21 @@ PR_IMPLEMENT(void) PR_SuspendAll(void)
suspendAllThread = _PR_MD_CURRENT_THREAD();
_PR_MD_BEGIN_SUSPEND_ALL();
for (qp = _PR_ACTIVE_LOCAL_THREADQ().next;
- qp != &_PR_ACTIVE_LOCAL_THREADQ(); qp = qp->next) {
+ qp != &_PR_ACTIVE_LOCAL_THREADQ(); qp = qp->next) {
if ((me != _PR_ACTIVE_THREAD_PTR(qp)) &&
_PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp))) {
_PR_Suspend(_PR_ACTIVE_THREAD_PTR(qp));
- PR_ASSERT((_PR_ACTIVE_THREAD_PTR(qp))->state != _PR_RUNNING);
- }
+ PR_ASSERT((_PR_ACTIVE_THREAD_PTR(qp))->state != _PR_RUNNING);
+ }
}
for (qp = _PR_ACTIVE_GLOBAL_THREADQ().next;
- qp != &_PR_ACTIVE_GLOBAL_THREADQ(); qp = qp->next) {
+ qp != &_PR_ACTIVE_GLOBAL_THREADQ(); qp = qp->next) {
if ((me != _PR_ACTIVE_THREAD_PTR(qp)) &&
_PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp)))
/* PR_Suspend(_PR_ACTIVE_THREAD_PTR(qp)); */
- _PR_MD_SUSPEND_THREAD(_PR_ACTIVE_THREAD_PTR(qp));
+ {
+ _PR_MD_SUSPEND_THREAD(_PR_ACTIVE_THREAD_PTR(qp));
+ }
}
_PR_MD_END_SUSPEND_ALL();
}
@@ -1661,16 +1709,18 @@ PR_IMPLEMENT(void) PR_ResumeAll(void)
*/
_PR_MD_BEGIN_RESUME_ALL();
for (qp = _PR_ACTIVE_LOCAL_THREADQ().next;
- qp != &_PR_ACTIVE_LOCAL_THREADQ(); qp = qp->next) {
+ qp != &_PR_ACTIVE_LOCAL_THREADQ(); qp = qp->next) {
if ((me != _PR_ACTIVE_THREAD_PTR(qp)) &&
- _PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp)))
+ _PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp))) {
_PR_Resume(_PR_ACTIVE_THREAD_PTR(qp));
+ }
}
for (qp = _PR_ACTIVE_GLOBAL_THREADQ().next;
- qp != &_PR_ACTIVE_GLOBAL_THREADQ(); qp = qp->next) {
+ qp != &_PR_ACTIVE_GLOBAL_THREADQ(); qp = qp->next) {
if ((me != _PR_ACTIVE_THREAD_PTR(qp)) &&
- _PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp)))
- _PR_MD_RESUME_THREAD(_PR_ACTIVE_THREAD_PTR(qp));
+ _PR_IS_GCABLE_THREAD(_PR_ACTIVE_THREAD_PTR(qp))) {
+ _PR_MD_RESUME_THREAD(_PR_ACTIVE_THREAD_PTR(qp));
+ }
}
_PR_MD_END_RESUME_ALL();
suspendAllThread = NULL;
@@ -1712,8 +1762,9 @@ PR_IMPLEMENT(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg)
if (_PR_IS_GCABLE_THREAD(t))
{
rv = (*func)(t, i, arg);
- if (rv != PR_SUCCESS)
+ if (rv != PR_SUCCESS) {
return rv;
+ }
i++;
}
}
@@ -1725,8 +1776,9 @@ PR_IMPLEMENT(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg)
if (_PR_IS_GCABLE_THREAD(t))
{
rv = (*func)(t, i, arg);
- if (rv != PR_SUCCESS)
+ if (rv != PR_SUCCESS) {
return rv;
+ }
i++;
}
}
@@ -1818,24 +1870,28 @@ _PR_DelSleepQ(PRThread *thread, PRBool propogate_time)
if (propogate_time == PR_TRUE) {
PRThread *after = _PR_THREAD_PTR(q);
after->sleep += thread->sleep;
- } else
+ } else {
_PR_SLEEPQMAX(cpu) -= thread->sleep;
+ }
} else {
/* Check if prev is the beggining of the list; if so,
* we are the only element on the list.
*/
- if (thread->links.prev != &_PR_SLEEPQ(cpu))
+ if (thread->links.prev != &_PR_SLEEPQ(cpu)) {
_PR_SLEEPQMAX(cpu) -= thread->sleep;
- else
+ }
+ else {
_PR_SLEEPQMAX(cpu) = 0;
+ }
}
thread->flags &= ~_PR_ON_SLEEPQ;
} else {
thread->flags &= ~_PR_ON_PAUSEQ;
}
PR_REMOVE_LINK(&thread->links);
- } else
+ } else {
PR_ASSERT(0);
+ }
}
void
@@ -1858,17 +1914,17 @@ _PR_AddThreadToRunQ(
* "post" the awakened thread to the IO completion port
* for the next idle CPU to execute (this is done in
* _PR_MD_WAKEUP_WAITER).
- * Threads with a suspended I/O operation remain bound to
- * the same cpu until I/O is cancelled
+ * Threads with a suspended I/O operation remain bound to
+ * the same cpu until I/O is cancelled
*
* NOTE: the boolean expression below must be the exact
* opposite of the corresponding boolean expression in
* _PR_MD_WAKEUP_WAITER.
*/
if ((!_PR_IS_NATIVE_THREAD(me) && (cpu == me->cpu)) ||
- (thread->md.thr_bound_cpu)) {
- PR_ASSERT(!thread->md.thr_bound_cpu ||
- (thread->md.thr_bound_cpu == cpu));
+ (thread->md.thr_bound_cpu)) {
+ PR_ASSERT(!thread->md.thr_bound_cpu ||
+ (thread->md.thr_bound_cpu == cpu));
_PR_RUNQ_LOCK(cpu);
_PR_ADD_RUNQ(thread, cpu, pri);
_PR_RUNQ_UNLOCK(cpu);