diff options
Diffstat (limited to 'nsprpub/pr/src/pthreads/ptthread.c')
-rw-r--r-- | nsprpub/pr/src/pthreads/ptthread.c | 562 |
1 files changed, 303 insertions, 259 deletions
diff --git a/nsprpub/pr/src/pthreads/ptthread.c b/nsprpub/pr/src/pthreads/ptthread.c index e61a4cbde..292c89ebc 100644 --- a/nsprpub/pr/src/pthreads/ptthread.c +++ b/nsprpub/pr/src/pthreads/ptthread.c @@ -25,11 +25,8 @@ #include <pthread_np.h> #endif -#ifdef SYMBIAN -/* In Open C sched_get_priority_min/max do not work properly, so we undefine - * _POSIX_THREAD_PRIORITY_SCHEDULING here. - */ -#undef _POSIX_THREAD_PRIORITY_SCHEDULING +#if defined(ANDROID) +#include <sys/prctl.h> #endif #ifdef _PR_NICE_PRIORITY_SCHEDULING @@ -79,7 +76,7 @@ static PRIntn pt_PriorityMap(PRThreadPriority pri) return 10; #else return pt_book.minPrio + - pri * (pt_book.maxPrio - pt_book.minPrio) / PR_PRIORITY_LAST; + pri * (pt_book.maxPrio - pt_book.minPrio) / PR_PRIORITY_LAST; #endif } #elif defined(_PR_NICE_PRIORITY_SCHEDULING) @@ -185,15 +182,18 @@ static void *_pt_root(void *arg) #endif /* If this is a GCABLE thread, set its state appropriately */ - if (thred->suspend & PT_THREAD_SETGCABLE) - thred->state |= PT_THREAD_GCABLE; + if (thred->suspend & PT_THREAD_SETGCABLE) { + thred->state |= PT_THREAD_GCABLE; + } thred->suspend = 0; thred->prev = pt_book.last; - if (pt_book.last) + if (pt_book.last) { pt_book.last->next = thred; - else + } + else { pt_book.first = thred; + } thred->next = NULL; pt_book.last = thred; PR_Unlock(pt_book.ml); @@ -209,22 +209,29 @@ static void *_pt_root(void *arg) */ if (detached) { - while (!thred->okToDelete) + while (!thred->okToDelete) { PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); + } } - if (thred->state & PT_THREAD_SYSTEM) + if (thred->state & PT_THREAD_SYSTEM) { pt_book.system -= 1; - else if (--pt_book.user == pt_book.this_many) + } + else if (--pt_book.user == pt_book.this_many) { PR_NotifyAllCondVar(pt_book.cv); - if (NULL == thred->prev) + } + if (NULL == thred->prev) { pt_book.first = thred->next; - else + } + else { thred->prev->next = thred->next; - if (NULL == thred->next) + } + if (NULL == thred->next) { pt_book.last = thred->prev; - else + } + else { thred->next->prev = thred->prev; + } PR_Unlock(pt_book.ml); /* @@ -259,7 +266,9 @@ static PRThread* pt_AttachThread(void) * either (e.g., PR_SetError()) as that will result in infinite * recursion. */ - if (!_pr_initialized) return NULL; + if (!_pr_initialized) { + return NULL; + } /* PR_NEWZAP must not call PR_GetCurrentThread() */ thred = PR_NEWZAP(PRThread); @@ -281,10 +290,12 @@ static PRThread* pt_AttachThread(void) /* then put it into the list */ thred->prev = pt_book.last; - if (pt_book.last) + if (pt_book.last) { pt_book.last->next = thred; - else + } + else { pt_book.first = thred; + } thred->next = NULL; pt_book.last = thred; PR_Unlock(pt_book.ml); @@ -302,12 +313,16 @@ static PRThread* _PR_CreateThread( PRThread *thred; pthread_attr_t tattr; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } - if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)priority) + if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)priority) { priority = PR_PRIORITY_FIRST; - else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)priority) + } + else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)priority) { priority = PR_PRIORITY_LAST; + } rv = _PT_PTHREAD_ATTR_INIT(&tattr); PR_ASSERT(0 == rv); @@ -339,8 +354,8 @@ static PRThread* _PR_CreateThread( } rv = pthread_attr_setdetachstate(&tattr, - ((PR_JOINABLE_THREAD == state) ? - PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)); + ((PR_JOINABLE_THREAD == state) ? + PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)); PR_ASSERT(0 == rv); /* @@ -349,8 +364,9 @@ static PRThread* _PR_CreateThread( if (stackSize) { #ifdef _MD_MINIMUM_STACK_SIZE - if (stackSize < _MD_MINIMUM_STACK_SIZE) + if (stackSize < _MD_MINIMUM_STACK_SIZE) { stackSize = _MD_MINIMUM_STACK_SIZE; + } #endif rv = pthread_attr_setstacksize(&tattr, stackSize); PR_ASSERT(0 == rv); @@ -369,36 +385,42 @@ static PRThread* _PR_CreateThread( thred->arg = arg; thred->startFunc = start; thred->priority = priority; - if (PR_UNJOINABLE_THREAD == state) + if (PR_UNJOINABLE_THREAD == state) { thred->state |= PT_THREAD_DETACHED; + } + + if (PR_LOCAL_THREAD == scope) { + scope = PR_GLOBAL_THREAD; + } - if (PR_LOCAL_THREAD == scope) - scope = PR_GLOBAL_THREAD; - if (PR_GLOBAL_BOUND_THREAD == scope) { #if _POSIX_THREAD_PRIORITY_SCHEDULING > 0 - rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); - if (rv) { - /* - * system scope not supported - */ - scope = PR_GLOBAL_THREAD; - /* - * reset scope - */ - rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_PROCESS); - PR_ASSERT(0 == rv); - } + rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); + if (rv) { + /* + * system scope not supported + */ + scope = PR_GLOBAL_THREAD; + /* + * reset scope + */ + rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_PROCESS); + PR_ASSERT(0 == rv); + } #endif - } - if (PR_GLOBAL_THREAD == scope) + } + if (PR_GLOBAL_THREAD == scope) { thred->state |= PT_THREAD_GLOBAL; - else if (PR_GLOBAL_BOUND_THREAD == scope) + } + else if (PR_GLOBAL_BOUND_THREAD == scope) { thred->state |= (PT_THREAD_GLOBAL | PT_THREAD_BOUND); - else /* force it global */ + } + else { /* force it global */ thred->state |= PT_THREAD_GLOBAL; - if (PR_SYSTEM_THREAD == type) + } + if (PR_SYSTEM_THREAD == type) { thred->state |= PT_THREAD_SYSTEM; + } thred->suspend =(isGCAble) ? PT_THREAD_SETGCABLE : 0; @@ -420,9 +442,12 @@ static PRThread* _PR_CreateThread( /* make the thread counted to the rest of the runtime */ PR_Lock(pt_book.ml); - if (PR_SYSTEM_THREAD == type) + if (PR_SYSTEM_THREAD == type) { pt_book.system += 1; - else pt_book.user += 1; + } + else { + pt_book.user += 1; + } PR_Unlock(pt_book.ml); /* @@ -434,27 +459,15 @@ static PRThread* _PR_CreateThread( if (EPERM == rv) { -#if defined(IRIX) - if (PR_GLOBAL_BOUND_THREAD == scope) { - /* - * SCOPE_SYSTEM requires appropriate privilege - * reset to process scope and try again - */ - rv = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_PROCESS); - PR_ASSERT(0 == rv); - thred->state &= ~PT_THREAD_BOUND; - } -#else /* Remember that we don't have thread scheduling privilege. */ pt_schedpriv = EPERM; PR_LOG(_pr_thread_lm, PR_LOG_MIN, - ("_PR_CreateThread: no thread scheduling privilege")); + ("_PR_CreateThread: no thread scheduling privilege")); /* Try creating the thread again without setting priority. */ #if _POSIX_THREAD_PRIORITY_SCHEDULING > 0 rv = pthread_attr_setinheritsched(&tattr, PTHREAD_INHERIT_SCHED); PR_ASSERT(0 == rv); #endif -#endif /* IRIX */ rv = _PT_PTHREAD_CREATE(&id, tattr, _pt_root, thred); } @@ -462,10 +475,12 @@ static PRThread* _PR_CreateThread( { PRIntn oserr = rv; PR_Lock(pt_book.ml); - if (thred->state & PT_THREAD_SYSTEM) + if (thred->state & PT_THREAD_SYSTEM) { pt_book.system -= 1; - else if (--pt_book.user == pt_book.this_many) + } + else if (--pt_book.user == pt_book.this_many) { PR_NotifyAllCondVar(pt_book.cv); + } PR_Unlock(pt_book.ml); PR_Free(thred->stack); @@ -516,23 +531,23 @@ PR_IMPLEMENT(PRThread*) PR_CreateThread( PRThreadState state, PRUint32 stackSize) { return _PR_CreateThread( - type, start, arg, priority, scope, state, stackSize, PR_FALSE); + type, start, arg, priority, scope, state, stackSize, PR_FALSE); } /* PR_CreateThread */ PR_IMPLEMENT(PRThread*) PR_CreateThreadGCAble( - PRThreadType type, void (*start)(void *arg), void *arg, + PRThreadType type, void (*start)(void *arg), void *arg, PRThreadPriority priority, PRThreadScope scope, PRThreadState state, PRUint32 stackSize) { return _PR_CreateThread( - type, start, arg, priority, scope, state, stackSize, PR_TRUE); + type, start, arg, priority, scope, state, stackSize, PR_TRUE); } /* PR_CreateThreadGCAble */ PR_IMPLEMENT(void*) GetExecutionEnvironment(PRThread *thred) { return thred->environment; } /* GetExecutionEnvironment */ - + PR_IMPLEMENT(void) SetExecutionEnvironment(PRThread *thred, void *env) { thred->environment = env; @@ -552,8 +567,8 @@ PR_IMPLEMENT(PRStatus) PR_JoinThread(PRThread *thred) PR_ASSERT(thred != NULL); if ((0xafafafaf == thred->state) - || (PT_THREAD_DETACHED == (PT_THREAD_DETACHED & thred->state)) - || (PT_THREAD_FOREIGN == (PT_THREAD_FOREIGN & thred->state))) + || (PT_THREAD_DETACHED == (PT_THREAD_DETACHED & thred->state)) + || (PT_THREAD_FOREIGN == (PT_THREAD_FOREIGN & thred->state))) { /* * This might be a bad address, but if it isn't, the state should @@ -606,7 +621,9 @@ PR_IMPLEMENT(void) PR_DetachThread(void) int rv; _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); - if (NULL == thred) return; + if (NULL == thred) { + return; + } _pt_thread_death(thred); rv = pthread_setspecific(pt_book.key, NULL); PR_ASSERT(0 == rv); @@ -616,10 +633,14 @@ PR_IMPLEMENT(PRThread*) PR_GetCurrentThread(void) { void *thred; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } _PT_PTHREAD_GETSPECIFIC(pt_book.key, thred); - if (NULL == thred) thred = pt_AttachThread(); + if (NULL == thred) { + thred = pt_AttachThread(); + } PR_ASSERT(NULL != thred); return (PRThread*)thred; } /* PR_GetCurrentThread */ @@ -627,19 +648,19 @@ PR_IMPLEMENT(PRThread*) PR_GetCurrentThread(void) PR_IMPLEMENT(PRThreadScope) PR_GetThreadScope(const PRThread *thred) { return (thred->state & PT_THREAD_BOUND) ? - PR_GLOBAL_BOUND_THREAD : PR_GLOBAL_THREAD; + PR_GLOBAL_BOUND_THREAD : PR_GLOBAL_THREAD; } /* PR_GetThreadScope() */ PR_IMPLEMENT(PRThreadType) PR_GetThreadType(const PRThread *thred) { return (thred->state & PT_THREAD_SYSTEM) ? - PR_SYSTEM_THREAD : PR_USER_THREAD; + PR_SYSTEM_THREAD : PR_USER_THREAD; } PR_IMPLEMENT(PRThreadState) PR_GetThreadState(const PRThread *thred) { return (thred->state & PT_THREAD_DETACHED) ? - PR_UNJOINABLE_THREAD : PR_JOINABLE_THREAD; + PR_UNJOINABLE_THREAD : PR_JOINABLE_THREAD; } /* PR_GetThreadState */ PR_IMPLEMENT(PRThreadPriority) PR_GetThreadPriority(const PRThread *thred) @@ -654,10 +675,12 @@ PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thred, PRThreadPriority newPri PR_ASSERT(NULL != thred); - if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)newPri) + if ((PRIntn)PR_PRIORITY_FIRST > (PRIntn)newPri) { newPri = PR_PRIORITY_FIRST; - else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)newPri) + } + else if ((PRIntn)PR_PRIORITY_LAST < (PRIntn)newPri) { newPri = PR_PRIORITY_LAST; + } #if _POSIX_THREAD_PRIORITY_SCHEDULING > 0 if (EPERM != pt_schedpriv) @@ -667,22 +690,24 @@ PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thred, PRThreadPriority newPri rv = pthread_getschedparam(thred->id, &policy, &schedule); if(0 == rv) { - schedule.sched_priority = pt_PriorityMap(newPri); - rv = pthread_setschedparam(thred->id, policy, &schedule); - if (EPERM == rv) - { - pt_schedpriv = EPERM; - PR_LOG(_pr_thread_lm, PR_LOG_MIN, - ("PR_SetThreadPriority: no thread scheduling privilege")); - } - } - if (rv != 0) - rv = -1; + schedule.sched_priority = pt_PriorityMap(newPri); + rv = pthread_setschedparam(thred->id, policy, &schedule); + if (EPERM == rv) + { + pt_schedpriv = EPERM; + PR_LOG(_pr_thread_lm, PR_LOG_MIN, + ("PR_SetThreadPriority: no thread scheduling privilege")); + } + } + if (rv != 0) { + rv = -1; + } } #elif defined(_PR_NICE_PRIORITY_SCHEDULING) PR_Lock(pt_book.ml); - while (thred->tid == 0) + while (thred->tid == 0) { PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); + } PR_Unlock(pt_book.ml); errno = 0; @@ -699,8 +724,8 @@ PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thred, PRThreadPriority newPri * because adjusting the nice value might be permitted for certain * ranges but not for others. */ PR_LOG(_pr_thread_lm, PR_LOG_MIN, - ("PR_SetThreadPriority: setpriority failed with error %d", - errno)); + ("PR_SetThreadPriority: setpriority failed with error %d", + errno)); } } #else @@ -733,7 +758,9 @@ PR_IMPLEMENT(PRStatus) PR_Interrupt(PRThread *thred) */ PRCondVar *cv; PR_ASSERT(NULL != thred); - if (NULL == thred) return PR_FAILURE; + if (NULL == thred) { + return PR_FAILURE; + } thred->state |= PT_THREAD_ABORTED; @@ -744,8 +771,9 @@ PR_IMPLEMENT(PRStatus) PR_Interrupt(PRThread *thred) (void)PR_ATOMIC_INCREMENT(&cv->notify_pending); rv = pthread_cond_broadcast(&cv->cv); PR_ASSERT(0 == rv); - if (0 > PR_ATOMIC_DECREMENT(&cv->notify_pending)) + if (0 > PR_ATOMIC_DECREMENT(&cv->notify_pending)) { PR_DestroyCondVar(cv); + } } return PR_SUCCESS; } /* PR_Interrupt */ @@ -772,7 +800,7 @@ PR_IMPLEMENT(PRStatus) PR_Yield(void) { static PRBool warning = PR_TRUE; if (warning) warning = _PR_Obsolete( - "PR_Yield()", "PR_Sleep(PR_INTERVAL_NO_WAIT)"); + "PR_Yield()", "PR_Sleep(PR_INTERVAL_NO_WAIT)"); return PR_Sleep(PR_INTERVAL_NO_WAIT); } @@ -780,7 +808,9 @@ PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { PRStatus rv = PR_SUCCESS; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if (PR_INTERVAL_NO_WAIT == ticks) { @@ -799,7 +829,9 @@ PR_IMPLEMENT(PRStatus) PR_Sleep(PRIntervalTime ticks) { PRIntervalTime now = PR_IntervalNow(); PRIntervalTime delta = now - timein; - if (delta > ticks) break; + if (delta > ticks) { + break; + } rv = PR_WaitCondVar(cv, ticks - delta); } while (PR_SUCCESS == rv); PR_Unlock(_pr_sleeplock); @@ -824,7 +856,7 @@ static void _pt_thread_death(void *arg) PR_ASSERT(0 == rv); } - /* PR_TRUE for: call destructors */ + /* PR_TRUE for: call destructors */ _pt_thread_death_internal(arg, PR_TRUE); if (NULL == thred) @@ -841,29 +873,38 @@ static void _pt_thread_death_internal(void *arg, PRBool callDestructors) if (thred->state & (PT_THREAD_FOREIGN|PT_THREAD_PRIMORD)) { PR_Lock(pt_book.ml); - if (NULL == thred->prev) + if (NULL == thred->prev) { pt_book.first = thred->next; - else + } + else { thred->prev->next = thred->next; - if (NULL == thred->next) + } + if (NULL == thred->next) { pt_book.last = thred->prev; - else + } + else { thred->next->prev = thred->prev; + } PR_Unlock(pt_book.ml); } - if (callDestructors) + if (callDestructors) { _PR_DestroyThreadPrivate(thred); + } PR_Free(thred->privateData); - if (NULL != thred->errorString) + if (NULL != thred->errorString) { PR_Free(thred->errorString); - if (NULL != thred->name) + } + if (NULL != thred->name) { PR_Free(thred->name); + } PR_Free(thred->stack); - if (NULL != thred->syspoll_list) + if (NULL != thred->syspoll_list) { PR_Free(thred->syspoll_list); + } #if defined(_PR_POLL_WITH_SELECT) - if (NULL != thred->selectfd_list) + if (NULL != thred->selectfd_list) { PR_Free(thred->selectfd_list); + } #endif #if defined(DEBUG) memset(thred, 0xaf, sizeof(PRThread)); @@ -893,17 +934,17 @@ void _PR_InitThreads( #if _POSIX_THREAD_PRIORITY_SCHEDULING > 0 #if defined(FREEBSD) { - pthread_attr_t attr; - int policy; - /* get the min and max priorities of the default policy */ - pthread_attr_init(&attr); - pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); - pthread_attr_getschedpolicy(&attr, &policy); - pt_book.minPrio = sched_get_priority_min(policy); - PR_ASSERT(-1 != pt_book.minPrio); - pt_book.maxPrio = sched_get_priority_max(policy); - PR_ASSERT(-1 != pt_book.maxPrio); - pthread_attr_destroy(&attr); + pthread_attr_t attr; + int policy; + /* get the min and max priorities of the default policy */ + pthread_attr_init(&attr); + pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + pthread_attr_getschedpolicy(&attr, &policy); + pt_book.minPrio = sched_get_priority_min(policy); + PR_ASSERT(-1 != pt_book.minPrio); + pt_book.maxPrio = sched_get_priority_max(policy); + PR_ASSERT(-1 != pt_book.maxPrio); + pthread_attr_destroy(&attr); } #else /* @@ -913,7 +954,7 @@ void _PR_InitThreads( pt_book.maxPrio = PT_PRIO_MAX; #endif #endif - + PR_ASSERT(NULL == pt_book.ml); pt_book.ml = PR_NewLock(); PR_ASSERT(NULL != pt_book.ml); @@ -935,12 +976,12 @@ void _PR_InitThreads( { thred->state |= PT_THREAD_SYSTEM; pt_book.system += 1; - pt_book.this_many = 0; + pt_book.this_many = 0; } else { - pt_book.user += 1; - pt_book.this_many = 1; + pt_book.user += 1; + pt_book.this_many = 1; } thred->next = thred->prev = NULL; pt_book.first = pt_book.last = thred; @@ -949,7 +990,7 @@ void _PR_InitThreads( PR_ASSERT(thred->stack != NULL); thred->stack->stackSize = 0; thred->stack->thr = thred; - _PR_InitializeStack(thred->stack); + _PR_InitializeStack(thred->stack); /* * Create a key for our use to store a backpointer in the pthread @@ -966,8 +1007,9 @@ void _PR_InitThreads( * nothing. */ rv = _PT_PTHREAD_KEY_CREATE(&pt_book.key, _pt_thread_death); - if (0 != rv) + if (0 != rv) { PR_Assert("0 == rv", __FILE__, __LINE__); + } pt_book.keyCreated = PR_TRUE; rv = pthread_setspecific(pt_book.key, thred); PR_ASSERT(0 == rv); @@ -989,7 +1031,7 @@ static void _PR_Fini(void); __attribute__ ((constructor)) static void _register_PR_Fini() { - atexit(_PR_Fini); + atexit(_PR_Fini); } #else static void _PR_Fini(void) __attribute__ ((destructor)); @@ -1031,9 +1073,9 @@ void PR_HPUX10xInit(shl_t handle, int loading) * loading is non-zero at startup and zero at termination. */ if (loading) { - /* ... do some initializations ... */ + /* ... do some initializations ... */ } else { - _PR_Fini(); + _PR_Fini(); } } #endif @@ -1047,7 +1089,7 @@ void _PR_Fini(void) int rv; if (!_pr_initialized) { - /* Either NSPR was never successfully initialized or + /* Either NSPR was never successfully initialized or * PR_Cleanup has been called already. */ if (pt_book.keyCreated) { @@ -1062,7 +1104,7 @@ void _PR_Fini(void) if (NULL != thred) { /* - * PR_FALSE, because it is unsafe to call back to the + * PR_FALSE, because it is unsafe to call back to the * thread private data destructors at final cleanup. */ _pt_thread_death_internal(thred, PR_FALSE); @@ -1085,12 +1127,15 @@ PR_IMPLEMENT(PRStatus) PR_Cleanup(void) if (me->state & PT_THREAD_PRIMORD) { PR_Lock(pt_book.ml); - while (pt_book.user > pt_book.this_many) + while (pt_book.user > pt_book.this_many) { PR_WaitCondVar(pt_book.cv, PR_INTERVAL_NO_TIMEOUT); - if (me->state & PT_THREAD_SYSTEM) + } + if (me->state & PT_THREAD_SYSTEM) { pt_book.system -= 1; - else + } + else { pt_book.user -= 1; + } PR_Unlock(pt_book.ml); _PR_MD_EARLY_CLEANUP(); @@ -1168,7 +1213,7 @@ PR_SetThreadDumpProc(PRThread* thread, PRThreadDumpProc dump, void *arg) thread->dumpArg = arg; } -/* +/* * Garbage collection support follows. */ @@ -1194,15 +1239,14 @@ static void null_signal_handler(PRIntn sig); */ static void init_pthread_gc_support(void) { -#ifndef SYMBIAN PRIntn rv; - { - struct sigaction sigact_usr2; + { + struct sigaction sigact_usr2; - sigact_usr2.sa_handler = suspend_signal_handler; - sigact_usr2.sa_flags = SA_RESTART; - sigemptyset (&sigact_usr2.sa_mask); + sigact_usr2.sa_handler = suspend_signal_handler; + sigact_usr2.sa_flags = SA_RESTART; + sigemptyset (&sigact_usr2.sa_mask); rv = sigaction (SIGUSR2, &sigact_usr2, NULL); PR_ASSERT(0 == rv); @@ -1213,31 +1257,30 @@ static void init_pthread_gc_support(void) #else sigaddset (&sigwait_set, SIGUSR2); #endif /* defined(PT_NO_SIGTIMEDWAIT) */ - } + } #if defined(PT_NO_SIGTIMEDWAIT) - { - struct sigaction sigact_null; - sigact_null.sa_handler = null_signal_handler; - sigact_null.sa_flags = SA_RESTART; - sigemptyset (&sigact_null.sa_mask); + { + struct sigaction sigact_null; + sigact_null.sa_handler = null_signal_handler; + sigact_null.sa_flags = SA_RESTART; + sigemptyset (&sigact_null.sa_mask); rv = sigaction (SIGUSR1, &sigact_null, NULL); - PR_ASSERT(0 ==rv); + PR_ASSERT(0 ==rv); } #endif /* defined(PT_NO_SIGTIMEDWAIT) */ -#endif /* SYMBIAN */ } PR_IMPLEMENT(void) PR_SetThreadGCAble(void) { PR_Lock(pt_book.ml); - PR_GetCurrentThread()->state |= PT_THREAD_GCABLE; + PR_GetCurrentThread()->state |= PT_THREAD_GCABLE; PR_Unlock(pt_book.ml); } PR_IMPLEMENT(void) PR_ClearThreadGCAble(void) { PR_Lock(pt_book.ml); - PR_GetCurrentThread()->state &= (~PT_THREAD_GCABLE); + PR_GetCurrentThread()->state &= (~PT_THREAD_GCABLE); PR_Unlock(pt_book.ml); } @@ -1284,23 +1327,24 @@ PR_IMPLEMENT(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg) if (_PT_IS_GCABLE_THREAD(thred)) { PR_ASSERT((thred == me) || (thred->suspend & PT_THREAD_SUSPENDED)); - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("In PR_EnumerateThreads callback thread %p thid = %X\n", + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("In PR_EnumerateThreads callback thread %p thid = %X\n", thred, thred->id)); rv = func(thred, count++, arg); - if (rv != PR_SUCCESS) + if (rv != PR_SUCCESS) { return rv; + } } thred = next; } - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("End PR_EnumerateThreads count = %d \n", count)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("End PR_EnumerateThreads count = %d \n", count)); return rv; } /* PR_EnumerateThreads */ /* - * PR_SuspendAll and PR_ResumeAll are called during garbage collection. The strategy + * PR_SuspendAll and PR_ResumeAll are called during garbage collection. The strategy * we use is to send a SIGUSR2 signal to every gc able thread that we intend to suspend. * The signal handler will record the stack pointer and will block until resumed by * the resume call. Since the signal handler is the last routine called for the @@ -1313,97 +1357,96 @@ PR_IMPLEMENT(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg) /* * In the signal handler, we can not use condition variable notify or wait. - * This does not work consistently across all pthread platforms. We also can not + * This does not work consistently across all pthread platforms. We also can not * use locking since that does not seem to work reliably across platforms. * Only thing we can do is yielding while testing for a global condition * to change. This does work on pthread supported platforms. We may have * to play with priortities if there are any problems detected. */ - /* - * In AIX, you cannot use ANY pthread calls in the signal handler except perhaps - * pthread_yield. But that is horribly inefficient. Hence we use only sigwait, no - * sigtimedwait is available. We need to use another user signal, SIGUSR1. Actually - * SIGUSR1 is also used by exec in Java. So our usage here breaks the exec in Java, - * for AIX. You cannot use pthread_cond_wait or pthread_delay_np in the signal - * handler as all synchronization mechanisms just break down. - */ +/* + * In AIX, you cannot use ANY pthread calls in the signal handler except perhaps + * pthread_yield. But that is horribly inefficient. Hence we use only sigwait, no + * sigtimedwait is available. We need to use another user signal, SIGUSR1. Actually + * SIGUSR1 is also used by exec in Java. So our usage here breaks the exec in Java, + * for AIX. You cannot use pthread_cond_wait or pthread_delay_np in the signal + * handler as all synchronization mechanisms just break down. + */ #if defined(PT_NO_SIGTIMEDWAIT) static void null_signal_handler(PRIntn sig) { - return; + return; } #endif static void suspend_signal_handler(PRIntn sig) { - PRThread *me = PR_GetCurrentThread(); + PRThread *me = PR_GetCurrentThread(); - PR_ASSERT(me != NULL); - PR_ASSERT(_PT_IS_GCABLE_THREAD(me)); - PR_ASSERT((me->suspend & PT_THREAD_SUSPENDED) == 0); + PR_ASSERT(me != NULL); + PR_ASSERT(_PT_IS_GCABLE_THREAD(me)); + PR_ASSERT((me->suspend & PT_THREAD_SUSPENDED) == 0); - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("Begin suspend_signal_handler thred %p thread id = %X\n", - me, me->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("Begin suspend_signal_handler thred %p thread id = %X\n", + me, me->id)); - /* - * save stack pointer - */ - me->sp = &me; + /* + * save stack pointer + */ + me->sp = &me; - /* - At this point, the thread's stack pointer has been saved, - And it is going to enter a wait loop until it is resumed. - So it is _really_ suspended - */ + /* + At this point, the thread's stack pointer has been saved, + And it is going to enter a wait loop until it is resumed. + So it is _really_ suspended + */ - me->suspend |= PT_THREAD_SUSPENDED; + me->suspend |= PT_THREAD_SUSPENDED; - /* - * now, block current thread - */ + /* + * now, block current thread + */ #if defined(PT_NO_SIGTIMEDWAIT) - pthread_cond_signal(&me->suspendResumeCV); - while (me->suspend & PT_THREAD_SUSPENDED) - { + pthread_cond_signal(&me->suspendResumeCV); + while (me->suspend & PT_THREAD_SUSPENDED) + { #if !defined(FREEBSD) && !defined(NETBSD) && !defined(OPENBSD) \ && !defined(BSDI) && !defined(UNIXWARE) \ - && !defined(DARWIN) && !defined(RISCOS) \ - && !defined(SYMBIAN) /*XXX*/ + && !defined(DARWIN) && !defined(RISCOS) PRIntn rv; - sigwait(&sigwait_set, &rv); + sigwait(&sigwait_set, &rv); #endif - } - me->suspend |= PT_THREAD_RESUMED; - pthread_cond_signal(&me->suspendResumeCV); + } + me->suspend |= PT_THREAD_RESUMED; + pthread_cond_signal(&me->suspendResumeCV); #else /* defined(PT_NO_SIGTIMEDWAIT) */ - while (me->suspend & PT_THREAD_SUSPENDED) - { - PRIntn rv = sigtimedwait(&sigwait_set, NULL, &hundredmillisec); - PR_ASSERT(-1 == rv); - } - me->suspend |= PT_THREAD_RESUMED; + while (me->suspend & PT_THREAD_SUSPENDED) + { + PRIntn rv = sigtimedwait(&sigwait_set, NULL, &hundredmillisec); + PR_ASSERT(-1 == rv); + } + me->suspend |= PT_THREAD_RESUMED; #endif /* * At this point, thread has been resumed, so set a global condition. - * The ResumeAll needs to know that this has really been resumed. - * So the signal handler sets a flag which PR_ResumeAll will reset. + * The ResumeAll needs to know that this has really been resumed. + * So the signal handler sets a flag which PR_ResumeAll will reset. * The PR_ResumeAll must reset this flag ... */ - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("End suspend_signal_handler thred = %p tid = %X\n", me, me->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("End suspend_signal_handler thred = %p tid = %X\n", me, me->id)); } /* suspend_signal_handler */ static void pt_SuspendSet(PRThread *thred) { PRIntn rv; - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("pt_SuspendSet thred %p thread id = %X\n", thred, thred->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("pt_SuspendSet thred %p thread id = %X\n", thred, thred->id)); /* @@ -1412,54 +1455,49 @@ static void pt_SuspendSet(PRThread *thred) PR_ASSERT((thred->suspend & PT_THREAD_SUSPENDED) == 0); - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("doing pthread_kill in pt_SuspendSet thred %p tid = %X\n", - thred, thred->id)); -#if defined(SYMBIAN) - /* All signal group functions are not implemented in Symbian OS */ - rv = 0; -#else + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("doing pthread_kill in pt_SuspendSet thred %p tid = %X\n", + thred, thred->id)); rv = pthread_kill (thred->id, SIGUSR2); -#endif PR_ASSERT(0 == rv); } static void pt_SuspendTest(PRThread *thred) { - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("Begin pt_SuspendTest thred %p thread id = %X\n", thred, thred->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("Begin pt_SuspendTest thred %p thread id = %X\n", thred, thred->id)); /* * Wait for the thread to be really suspended. This happens when the * suspend signal handler stores the stack pointer and sets the state - * to suspended. + * to suspended. */ #if defined(PT_NO_SIGTIMEDWAIT) pthread_mutex_lock(&thred->suspendResumeMutex); while ((thred->suspend & PT_THREAD_SUSPENDED) == 0) { - pthread_cond_timedwait( - &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); - } - pthread_mutex_unlock(&thred->suspendResumeMutex); + pthread_cond_timedwait( + &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); + } + pthread_mutex_unlock(&thred->suspendResumeMutex); #else while ((thred->suspend & PT_THREAD_SUSPENDED) == 0) { - PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); - PR_ASSERT(-1 == rv); - } + PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); + PR_ASSERT(-1 == rv); + } #endif PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("End pt_SuspendTest thred %p tid %X\n", thred, thred->id)); + ("End pt_SuspendTest thred %p tid %X\n", thred, thred->id)); } /* pt_SuspendTest */ static void pt_ResumeSet(PRThread *thred) { - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("pt_ResumeSet thred %p thread id = %X\n", thred, thred->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("pt_ResumeSet thred %p thread id = %X\n", thred, thred->id)); /* * Clear the global state and set the thread state so that it will @@ -1472,43 +1510,39 @@ static void pt_ResumeSet(PRThread *thred) thred->suspend &= ~PT_THREAD_SUSPENDED; #if defined(PT_NO_SIGTIMEDWAIT) -#if defined(SYMBIAN) - /* All signal group functions are not implemented in Symbian OS */ -#else - pthread_kill(thred->id, SIGUSR1); -#endif + pthread_kill(thred->id, SIGUSR1); #endif } /* pt_ResumeSet */ static void pt_ResumeTest(PRThread *thred) { - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("Begin pt_ResumeTest thred %p thread id = %X\n", thred, thred->id)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("Begin pt_ResumeTest thred %p thread id = %X\n", thred, thred->id)); /* * Wait for the threads resume state to change - * to indicate it is really resumed + * to indicate it is really resumed */ #if defined(PT_NO_SIGTIMEDWAIT) pthread_mutex_lock(&thred->suspendResumeMutex); while ((thred->suspend & PT_THREAD_RESUMED) == 0) { - pthread_cond_timedwait( - &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); + pthread_cond_timedwait( + &thred->suspendResumeCV, &thred->suspendResumeMutex, &onemillisec); } pthread_mutex_unlock(&thred->suspendResumeMutex); #else while ((thred->suspend & PT_THREAD_RESUMED) == 0) { - PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); - PR_ASSERT(-1 == rv); - } + PRIntn rv = sigtimedwait(&sigwait_set, NULL, &onemillisec); + PR_ASSERT(-1 == rv); + } #endif thred->suspend &= ~PT_THREAD_RESUMED; PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, ( - "End pt_ResumeTest thred %p tid %X\n", thred, thred->id)); + "End pt_ResumeTest thred %p tid %X\n", thred, thred->id)); } /* pt_ResumeTest */ static pthread_once_t pt_gc_support_control = PTHREAD_ONCE_INIT; @@ -1535,8 +1569,9 @@ PR_IMPLEMENT(void) PR_SuspendAll(void) #endif while (thred != NULL) { - if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) - pt_SuspendSet(thred); + if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) { + pt_SuspendSet(thred); + } thred = thred->next; } @@ -1544,8 +1579,9 @@ PR_IMPLEMENT(void) PR_SuspendAll(void) thred = pt_book.first; while (thred != NULL) { - if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) + if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) { pt_SuspendTest(thred); + } thred = thred->next; } @@ -1554,8 +1590,8 @@ PR_IMPLEMENT(void) PR_SuspendAll(void) #ifdef DEBUG etime = PR_IntervalNow(); PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS,\ - ("End PR_SuspendAll (time %dms)\n", - PR_IntervalToMilliseconds(etime - stime))); + ("End PR_SuspendAll (time %dms)\n", + PR_IntervalToMilliseconds(etime - stime))); #endif } /* PR_SuspendAll */ @@ -1577,16 +1613,18 @@ PR_IMPLEMENT(void) PR_ResumeAll(void) while (thred != NULL) { - if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) - pt_ResumeSet(thred); + if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) { + pt_ResumeSet(thred); + } thred = thred->next; } thred = pt_book.first; while (thred != NULL) { - if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) - pt_ResumeTest(thred); + if ((thred != me) && _PT_IS_GCABLE_THREAD(thred)) { + pt_ResumeTest(thred); + } thred = thred->next; } @@ -1595,17 +1633,17 @@ PR_IMPLEMENT(void) PR_ResumeAll(void) suspendAllOn = PR_FALSE; etime = PR_IntervalNow(); PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("End PR_ResumeAll (time %dms)\n", - PR_IntervalToMilliseconds(etime - stime))); + ("End PR_ResumeAll (time %dms)\n", + PR_IntervalToMilliseconds(etime - stime))); #endif } /* PR_ResumeAll */ /* Return the stack pointer for the given thread- used by the GC */ PR_IMPLEMENT(void *)PR_GetSP(PRThread *thred) { - PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, - ("in PR_GetSP thred %p thid = %X, sp = %p\n", - thred, thred->id, thred->sp)); + PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, + ("in PR_GetSP thred %p thid = %X, sp = %p\n", + thred, thred->id, thred->sp)); return thred->sp; } /* PR_GetSP */ @@ -1621,18 +1659,22 @@ 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); #if defined(OPENBSD) || defined(FREEBSD) || defined(DRAGONFLY) pthread_set_name_np(thread->id, name); +#elif defined(ANDROID) + prctl(PR_SET_NAME, (unsigned long)(name)); #elif defined(NETBSD) result = pthread_setname_np(thread->id, "%s", (void *)name); #else /* not BSD */ @@ -1648,8 +1690,9 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name) *(void**)(&dynamic_pthread_setname_np) = dlsym(RTLD_DEFAULT, "pthread_setname_np"); - if (!dynamic_pthread_setname_np) + if (!dynamic_pthread_setname_np) { return PR_SUCCESS; + } /* * The 15-character name length limit is an experimentally determined @@ -1688,8 +1731,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; } |