(pthreads) Cleanups
This commit is contained in:
parent
4c0c842451
commit
dbaa237c71
|
@ -74,9 +74,8 @@ static int pte_thread_detach_common (unsigned char threadShouldExit)
|
|||
*/
|
||||
pte_thread_t * sp = (pte_thread_t *) pthread_getspecific (pte_selfThreadKey);
|
||||
|
||||
if (sp != NULL) // otherwise OS thread with no implicit POSIX handle.
|
||||
if (sp) // otherwise OS thread with no implicit POSIX handle.
|
||||
{
|
||||
|
||||
pte_callUserDestroyRoutines (sp);
|
||||
|
||||
(void) pthread_mutex_lock (&sp->cancelLock);
|
||||
|
@ -91,36 +90,33 @@ static int pte_thread_detach_common (unsigned char threadShouldExit)
|
|||
if (sp->detachState == PTHREAD_CREATE_DETACHED)
|
||||
{
|
||||
if (threadShouldExit)
|
||||
{
|
||||
pte_threadExitAndDestroy (sp);
|
||||
}
|
||||
else
|
||||
{
|
||||
pte_threadDestroy (sp);
|
||||
}
|
||||
|
||||
// pte_osTlsSetValue (pte_selfThreadKey->key, NULL);
|
||||
#if 0
|
||||
pte_osTlsSetValue (pte_selfThreadKey->key, NULL);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if (threadShouldExit)
|
||||
{
|
||||
pte_osThreadExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void pte_threadDestroyCommon (pthread_t thread, unsigned char shouldThreadExit)
|
||||
{
|
||||
pte_thread_t * tp = (pte_thread_t *) thread;
|
||||
pte_thread_t threadCopy;
|
||||
pte_thread_t * tp = (pte_thread_t *) thread;
|
||||
|
||||
if (!tp)
|
||||
return;
|
||||
|
||||
if (tp != NULL)
|
||||
{
|
||||
/*
|
||||
* Copy thread state so that the thread can be atomically NULLed.
|
||||
*/
|
||||
|
@ -138,19 +134,11 @@ static void pte_threadDestroyCommon (pthread_t thread, unsigned char shouldThrea
|
|||
if (threadCopy.threadId != 0)
|
||||
{
|
||||
if (shouldThreadExit)
|
||||
{
|
||||
pte_osThreadExitAndDelete(threadCopy.threadId);
|
||||
}
|
||||
else
|
||||
{
|
||||
pte_osThreadDelete(threadCopy.threadId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
} /* pte_threadDestroy */
|
||||
}
|
||||
|
||||
void
|
||||
pte_callUserDestroyRoutines (pthread_t thread)
|
||||
|
@ -171,13 +159,13 @@ pte_callUserDestroyRoutines (pthread_t thread)
|
|||
* -------------------------------------------------------------------
|
||||
*/
|
||||
{
|
||||
ThreadKeyAssoc * assoc;
|
||||
|
||||
if (thread != NULL)
|
||||
{
|
||||
int assocsRemaining;
|
||||
int iterations = 0;
|
||||
pte_thread_t * sp = (pte_thread_t *) thread;
|
||||
ThreadKeyAssoc *assoc = NULL;
|
||||
pte_thread_t *sp = (pte_thread_t *) thread;
|
||||
|
||||
if (!thread)
|
||||
return;
|
||||
|
||||
/*
|
||||
* Run through all Thread<-->Key associations
|
||||
|
@ -263,7 +251,7 @@ pte_callUserDestroyRoutines (pthread_t thread)
|
|||
pte_osTlsSetValue (k->key, NULL);
|
||||
|
||||
// Every assoc->key exists and has a destructor
|
||||
if (value != NULL && iterations <= PTHREAD_DESTRUCTOR_ITERATIONS)
|
||||
if (value && iterations <= PTHREAD_DESTRUCTOR_ITERATIONS)
|
||||
{
|
||||
/*
|
||||
* Unlock both locks before the destructor runs.
|
||||
|
@ -277,38 +265,11 @@ pte_callUserDestroyRoutines (pthread_t thread)
|
|||
|
||||
assocsRemaining++;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
try
|
||||
{
|
||||
/*
|
||||
* Run the caller's cleanup routine.
|
||||
*/
|
||||
destructor (value);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
/*
|
||||
* A system unexpected exception has occurred
|
||||
* running the user's destructor.
|
||||
* We get control back within this block in case
|
||||
* the application has set up it's own terminate
|
||||
* handler. Since we are leaving the thread we
|
||||
* should not get any internal pthreads
|
||||
* exceptions.
|
||||
*/
|
||||
terminate ();
|
||||
}
|
||||
|
||||
#else /* __cplusplus */
|
||||
|
||||
/*
|
||||
* Run the caller's cleanup routine.
|
||||
*/
|
||||
destructor (value);
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -321,9 +282,7 @@ pte_callUserDestroyRoutines (pthread_t thread)
|
|||
(void) pthread_mutex_unlock(&(k->keyLock));
|
||||
}
|
||||
}
|
||||
}
|
||||
while (assocsRemaining);
|
||||
}
|
||||
}while (assocsRemaining);
|
||||
}
|
||||
|
||||
int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout)
|
||||
|
@ -334,7 +293,7 @@ int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout
|
|||
pthread_t self = pthread_self();
|
||||
pte_thread_t *sp = (pte_thread_t *) self;
|
||||
|
||||
if (sp != NULL)
|
||||
if (sp)
|
||||
{
|
||||
/*
|
||||
* Get cancelEvent handle
|
||||
|
@ -360,7 +319,7 @@ int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout
|
|||
break;
|
||||
|
||||
case PTE_OS_INTERRUPTED:
|
||||
if (sp != NULL)
|
||||
if (sp)
|
||||
{
|
||||
/*
|
||||
* Should handle POSIX and implicit POSIX threads..
|
||||
|
@ -519,26 +478,18 @@ int pte_mutex_check_need_init (pthread_mutex_t * mutex)
|
|||
mtx = *mutex;
|
||||
|
||||
if (mtx == PTHREAD_MUTEX_INITIALIZER)
|
||||
{
|
||||
result = pthread_mutex_init (mutex, NULL);
|
||||
}
|
||||
else if (mtx == PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
|
||||
{
|
||||
result = pthread_mutex_init (mutex, &pte_recursive_mutexattr);
|
||||
}
|
||||
else if (mtx == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
|
||||
{
|
||||
result = pthread_mutex_init (mutex, &pte_errorcheck_mutexattr);
|
||||
}
|
||||
else if (mtx == NULL)
|
||||
{
|
||||
/*
|
||||
* The mutex has been destroyed while we were waiting to
|
||||
* initialise it, so the operation that caused the
|
||||
* auto-initialisation should fail.
|
||||
*/
|
||||
else if (mtx == NULL)
|
||||
result = EINVAL;
|
||||
}
|
||||
|
||||
pte_osMutexUnlock(pte_mutex_test_init_lock);
|
||||
|
||||
|
@ -548,7 +499,7 @@ int pte_mutex_check_need_init (pthread_mutex_t * mutex)
|
|||
pthread_t pte_new (void)
|
||||
{
|
||||
pthread_t nil = NULL;
|
||||
pte_thread_t * tp;
|
||||
pte_thread_t * tp = NULL;
|
||||
|
||||
/*
|
||||
* If there's a reusable pthread_t then use it.
|
||||
|
@ -588,7 +539,6 @@ unsigned int pte_relmillisecs (const struct timespec * abstime)
|
|||
const long long NANOSEC_PER_MILLISEC = 1000000;
|
||||
const long long MILLISEC_PER_SEC = 1000;
|
||||
unsigned int milliseconds;
|
||||
long long tmpAbsMilliseconds;
|
||||
long tmpCurrMilliseconds;
|
||||
struct timeb currSysTime;
|
||||
|
||||
|
@ -603,7 +553,7 @@ unsigned int pte_relmillisecs (const struct timespec * abstime)
|
|||
*
|
||||
* Assume all integers are unsigned, i.e. cannot test if less than 0.
|
||||
*/
|
||||
tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
|
||||
long long tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
|
||||
tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
|
||||
|
||||
/* get current system time */
|
||||
|
@ -665,7 +615,6 @@ pte_threadReusePop (void)
|
|||
{
|
||||
pthread_t t = NULL;
|
||||
|
||||
|
||||
pte_osMutexLock (pte_thread_reuse_lock);
|
||||
|
||||
if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseTop)
|
||||
|
@ -677,9 +626,7 @@ pte_threadReusePop (void)
|
|||
pte_threadReuseTop = tp->prevReuse;
|
||||
|
||||
if (PTE_THREAD_REUSE_EMPTY == pte_threadReuseTop)
|
||||
{
|
||||
pte_threadReuseBottom = PTE_THREAD_REUSE_EMPTY;
|
||||
}
|
||||
|
||||
tp->prevReuse = NULL;
|
||||
|
||||
|
@ -859,7 +806,10 @@ int pte_threadStart (void *vthreadParms)
|
|||
pte_thread_t *sp = (pte_thread_t *) self;
|
||||
start = threadParms->start;
|
||||
arg = threadParms->arg;
|
||||
// free (threadParms);
|
||||
|
||||
#if 0
|
||||
free (threadParms);
|
||||
#endif
|
||||
|
||||
pthread_setspecific (pte_selfThreadKey, sp);
|
||||
|
||||
|
@ -868,14 +818,11 @@ int pte_threadStart (void *vthreadParms)
|
|||
setjmp_rc = setjmp (sp->start_mark);
|
||||
|
||||
|
||||
if (0 == setjmp_rc)
|
||||
{
|
||||
|
||||
/*
|
||||
* Run the caller's routine;
|
||||
*/
|
||||
if (0 == setjmp_rc)
|
||||
sp->exitStatus = status = (*start) (arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (setjmp_rc)
|
||||
|
@ -934,11 +881,9 @@ void pte_throw (unsigned int exception)
|
|||
pte_thread_t * sp = (pte_thread_t *) pthread_getspecific (pte_selfThreadKey);
|
||||
|
||||
|
||||
if (exception != PTE_EPS_CANCEL && exception != PTE_EPS_EXIT)
|
||||
{
|
||||
/* Should never enter here */
|
||||
if (exception != PTE_EPS_CANCEL && exception != PTE_EPS_EXIT)
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if (NULL == sp || sp->implicit)
|
||||
{
|
||||
|
@ -962,8 +907,9 @@ void pte_throw (unsigned int exception)
|
|||
|
||||
pte_thread_detach_and_exit_np ();
|
||||
|
||||
// pte_osThreadExit((void*)exitCode);
|
||||
|
||||
#if 0
|
||||
pte_osThreadExit((void*)exitCode);
|
||||
#endif
|
||||
}
|
||||
|
||||
pte_pop_cleanup_all (1);
|
||||
|
@ -1026,9 +972,7 @@ int pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
|
|||
assoc = (ThreadKeyAssoc *) calloc (1, sizeof (*assoc));
|
||||
|
||||
if (assoc == NULL)
|
||||
{
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
assoc->thread = sp;
|
||||
assoc->key = key;
|
||||
|
@ -1038,7 +982,7 @@ int pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
|
|||
*/
|
||||
assoc->prevThread = NULL;
|
||||
assoc->nextThread = (ThreadKeyAssoc *) key->threads;
|
||||
if (assoc->nextThread != NULL)
|
||||
if (assoc->nextThread)
|
||||
assoc->nextThread->prevThread = assoc;
|
||||
key->threads = (void *) assoc;
|
||||
|
||||
|
@ -1047,7 +991,7 @@ int pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
|
|||
*/
|
||||
assoc->prevKey = NULL;
|
||||
assoc->nextKey = (ThreadKeyAssoc *) sp->keys;
|
||||
if (assoc->nextKey != NULL)
|
||||
if (assoc->nextKey)
|
||||
assoc->nextKey->prevKey = assoc;
|
||||
sp->keys = (void *) assoc;
|
||||
|
||||
|
@ -1069,21 +1013,21 @@ void pte_tkAssocDestroy (ThreadKeyAssoc * assoc)
|
|||
* -------------------------------------------------------------------
|
||||
*/
|
||||
{
|
||||
|
||||
ThreadKeyAssoc *prev = NULL;
|
||||
ThreadKeyAssoc *next = NULL;
|
||||
/*
|
||||
* Both key->keyLock and thread->threadLock are locked on
|
||||
* entry to this routine.
|
||||
*/
|
||||
if (assoc != NULL)
|
||||
{
|
||||
ThreadKeyAssoc * prev, * next;
|
||||
if (!assoc)
|
||||
return;
|
||||
|
||||
/* Remove assoc from thread's keys chain */
|
||||
prev = assoc->prevKey;
|
||||
next = assoc->nextKey;
|
||||
if (prev != NULL)
|
||||
if (prev)
|
||||
prev->nextKey = next;
|
||||
if (next != NULL)
|
||||
if (next)
|
||||
next->prevKey = prev;
|
||||
|
||||
/* We're at the head of the thread's keys chain */
|
||||
|
@ -1102,9 +1046,9 @@ void pte_tkAssocDestroy (ThreadKeyAssoc * assoc)
|
|||
/* Remove assoc from key's threads chain */
|
||||
prev = assoc->prevThread;
|
||||
next = assoc->nextThread;
|
||||
if (prev != NULL)
|
||||
if (prev)
|
||||
prev->nextThread = next;
|
||||
if (next != NULL)
|
||||
if (next)
|
||||
next->prevThread = prev;
|
||||
|
||||
/* We're at the head of the key's threads chain */
|
||||
|
@ -1112,5 +1056,4 @@ void pte_tkAssocDestroy (ThreadKeyAssoc * assoc)
|
|||
assoc->key->threads = next;
|
||||
|
||||
free (assoc);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue