(pthreads) Cleanups

This commit is contained in:
twinaphex 2017-12-27 22:08:59 +01:00
parent 4c0c842451
commit dbaa237c71
1 changed files with 193 additions and 250 deletions

137
deps/pthreads/pte.c vendored
View File

@ -74,9 +74,8 @@ static int pte_thread_detach_common (unsigned char threadShouldExit)
*/ */
pte_thread_t * sp = (pte_thread_t *) pthread_getspecific (pte_selfThreadKey); 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); pte_callUserDestroyRoutines (sp);
(void) pthread_mutex_lock (&sp->cancelLock); (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 (sp->detachState == PTHREAD_CREATE_DETACHED)
{ {
if (threadShouldExit) if (threadShouldExit)
{
pte_threadExitAndDestroy (sp); pte_threadExitAndDestroy (sp);
}
else else
{
pte_threadDestroy (sp); pte_threadDestroy (sp);
}
// pte_osTlsSetValue (pte_selfThreadKey->key, NULL); #if 0
pte_osTlsSetValue (pte_selfThreadKey->key, NULL);
#endif
} }
else else
{ {
if (threadShouldExit) if (threadShouldExit)
{
pte_osThreadExit(); pte_osThreadExit();
} }
} }
} }
}
return 1; return 1;
} }
static void pte_threadDestroyCommon (pthread_t thread, unsigned char shouldThreadExit) 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 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. * 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 (threadCopy.threadId != 0)
{ {
if (shouldThreadExit) if (shouldThreadExit)
{
pte_osThreadExitAndDelete(threadCopy.threadId); pte_osThreadExitAndDelete(threadCopy.threadId);
}
else else
{
pte_osThreadDelete(threadCopy.threadId); pte_osThreadDelete(threadCopy.threadId);
} }
} }
}
} /* pte_threadDestroy */
void void
pte_callUserDestroyRoutines (pthread_t thread) pte_callUserDestroyRoutines (pthread_t thread)
@ -171,13 +159,13 @@ pte_callUserDestroyRoutines (pthread_t thread)
* ------------------------------------------------------------------- * -------------------------------------------------------------------
*/ */
{ {
ThreadKeyAssoc * assoc;
if (thread != NULL)
{
int assocsRemaining; int assocsRemaining;
int iterations = 0; 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 * Run through all Thread<-->Key associations
@ -263,7 +251,7 @@ pte_callUserDestroyRoutines (pthread_t thread)
pte_osTlsSetValue (k->key, NULL); pte_osTlsSetValue (k->key, NULL);
// Every assoc->key exists and has a destructor // 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. * Unlock both locks before the destructor runs.
@ -277,38 +265,11 @@ pte_callUserDestroyRoutines (pthread_t thread)
assocsRemaining++; 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. * Run the caller's cleanup routine.
*/ */
destructor (value); destructor (value);
#endif /* __cplusplus */
} }
else else
{ {
@ -321,9 +282,7 @@ pte_callUserDestroyRoutines (pthread_t thread)
(void) pthread_mutex_unlock(&(k->keyLock)); (void) pthread_mutex_unlock(&(k->keyLock));
} }
} }
} }while (assocsRemaining);
while (assocsRemaining);
}
} }
int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout) 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(); pthread_t self = pthread_self();
pte_thread_t *sp = (pte_thread_t *) self; pte_thread_t *sp = (pte_thread_t *) self;
if (sp != NULL) if (sp)
{ {
/* /*
* Get cancelEvent handle * Get cancelEvent handle
@ -360,7 +319,7 @@ int pte_cancellable_wait (pte_osSemaphoreHandle semHandle, unsigned int* timeout
break; break;
case PTE_OS_INTERRUPTED: case PTE_OS_INTERRUPTED:
if (sp != NULL) if (sp)
{ {
/* /*
* Should handle POSIX and implicit POSIX threads.. * Should handle POSIX and implicit POSIX threads..
@ -519,26 +478,18 @@ int pte_mutex_check_need_init (pthread_mutex_t * mutex)
mtx = *mutex; mtx = *mutex;
if (mtx == PTHREAD_MUTEX_INITIALIZER) if (mtx == PTHREAD_MUTEX_INITIALIZER)
{
result = pthread_mutex_init (mutex, NULL); result = pthread_mutex_init (mutex, NULL);
}
else if (mtx == PTHREAD_RECURSIVE_MUTEX_INITIALIZER) else if (mtx == PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
{
result = pthread_mutex_init (mutex, &pte_recursive_mutexattr); result = pthread_mutex_init (mutex, &pte_recursive_mutexattr);
}
else if (mtx == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER) else if (mtx == PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
{
result = pthread_mutex_init (mutex, &pte_errorcheck_mutexattr); result = pthread_mutex_init (mutex, &pte_errorcheck_mutexattr);
}
else if (mtx == NULL)
{
/* /*
* The mutex has been destroyed while we were waiting to * The mutex has been destroyed while we were waiting to
* initialise it, so the operation that caused the * initialise it, so the operation that caused the
* auto-initialisation should fail. * auto-initialisation should fail.
*/ */
else if (mtx == NULL)
result = EINVAL; result = EINVAL;
}
pte_osMutexUnlock(pte_mutex_test_init_lock); 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 pte_new (void)
{ {
pthread_t nil = NULL; pthread_t nil = NULL;
pte_thread_t * tp; pte_thread_t * tp = NULL;
/* /*
* If there's a reusable pthread_t then use it. * 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 NANOSEC_PER_MILLISEC = 1000000;
const long long MILLISEC_PER_SEC = 1000; const long long MILLISEC_PER_SEC = 1000;
unsigned int milliseconds; unsigned int milliseconds;
long long tmpAbsMilliseconds;
long tmpCurrMilliseconds; long tmpCurrMilliseconds;
struct timeb currSysTime; 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. * 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; tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
/* get current system time */ /* get current system time */
@ -665,7 +615,6 @@ pte_threadReusePop (void)
{ {
pthread_t t = NULL; pthread_t t = NULL;
pte_osMutexLock (pte_thread_reuse_lock); pte_osMutexLock (pte_thread_reuse_lock);
if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseTop) if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseTop)
@ -677,9 +626,7 @@ pte_threadReusePop (void)
pte_threadReuseTop = tp->prevReuse; pte_threadReuseTop = tp->prevReuse;
if (PTE_THREAD_REUSE_EMPTY == pte_threadReuseTop) if (PTE_THREAD_REUSE_EMPTY == pte_threadReuseTop)
{
pte_threadReuseBottom = PTE_THREAD_REUSE_EMPTY; pte_threadReuseBottom = PTE_THREAD_REUSE_EMPTY;
}
tp->prevReuse = NULL; tp->prevReuse = NULL;
@ -859,7 +806,10 @@ int pte_threadStart (void *vthreadParms)
pte_thread_t *sp = (pte_thread_t *) self; pte_thread_t *sp = (pte_thread_t *) self;
start = threadParms->start; start = threadParms->start;
arg = threadParms->arg; arg = threadParms->arg;
// free (threadParms);
#if 0
free (threadParms);
#endif
pthread_setspecific (pte_selfThreadKey, sp); pthread_setspecific (pte_selfThreadKey, sp);
@ -868,14 +818,11 @@ int pte_threadStart (void *vthreadParms)
setjmp_rc = setjmp (sp->start_mark); setjmp_rc = setjmp (sp->start_mark);
if (0 == setjmp_rc)
{
/* /*
* Run the caller's routine; * Run the caller's routine;
*/ */
if (0 == setjmp_rc)
sp->exitStatus = status = (*start) (arg); sp->exitStatus = status = (*start) (arg);
}
else else
{ {
switch (setjmp_rc) switch (setjmp_rc)
@ -934,11 +881,9 @@ void pte_throw (unsigned int exception)
pte_thread_t * sp = (pte_thread_t *) pthread_getspecific (pte_selfThreadKey); pte_thread_t * sp = (pte_thread_t *) pthread_getspecific (pte_selfThreadKey);
if (exception != PTE_EPS_CANCEL && exception != PTE_EPS_EXIT)
{
/* Should never enter here */ /* Should never enter here */
if (exception != PTE_EPS_CANCEL && exception != PTE_EPS_EXIT)
exit (1); exit (1);
}
if (NULL == sp || sp->implicit) if (NULL == sp || sp->implicit)
{ {
@ -962,8 +907,9 @@ void pte_throw (unsigned int exception)
pte_thread_detach_and_exit_np (); pte_thread_detach_and_exit_np ();
// pte_osThreadExit((void*)exitCode); #if 0
pte_osThreadExit((void*)exitCode);
#endif
} }
pte_pop_cleanup_all (1); 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)); assoc = (ThreadKeyAssoc *) calloc (1, sizeof (*assoc));
if (assoc == NULL) if (assoc == NULL)
{
return ENOMEM; return ENOMEM;
}
assoc->thread = sp; assoc->thread = sp;
assoc->key = key; assoc->key = key;
@ -1038,7 +982,7 @@ int pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
*/ */
assoc->prevThread = NULL; assoc->prevThread = NULL;
assoc->nextThread = (ThreadKeyAssoc *) key->threads; assoc->nextThread = (ThreadKeyAssoc *) key->threads;
if (assoc->nextThread != NULL) if (assoc->nextThread)
assoc->nextThread->prevThread = assoc; assoc->nextThread->prevThread = assoc;
key->threads = (void *) assoc; key->threads = (void *) assoc;
@ -1047,7 +991,7 @@ int pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
*/ */
assoc->prevKey = NULL; assoc->prevKey = NULL;
assoc->nextKey = (ThreadKeyAssoc *) sp->keys; assoc->nextKey = (ThreadKeyAssoc *) sp->keys;
if (assoc->nextKey != NULL) if (assoc->nextKey)
assoc->nextKey->prevKey = assoc; assoc->nextKey->prevKey = assoc;
sp->keys = (void *) 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 * Both key->keyLock and thread->threadLock are locked on
* entry to this routine. * entry to this routine.
*/ */
if (assoc != NULL) if (!assoc)
{ return;
ThreadKeyAssoc * prev, * next;
/* Remove assoc from thread's keys chain */ /* Remove assoc from thread's keys chain */
prev = assoc->prevKey; prev = assoc->prevKey;
next = assoc->nextKey; next = assoc->nextKey;
if (prev != NULL) if (prev)
prev->nextKey = next; prev->nextKey = next;
if (next != NULL) if (next)
next->prevKey = prev; next->prevKey = prev;
/* We're at the head of the thread's keys chain */ /* 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 */ /* Remove assoc from key's threads chain */
prev = assoc->prevThread; prev = assoc->prevThread;
next = assoc->nextThread; next = assoc->nextThread;
if (prev != NULL) if (prev)
prev->nextThread = next; prev->nextThread = next;
if (next != NULL) if (next)
next->prevThread = prev; next->prevThread = prev;
/* We're at the head of the key's threads chain */ /* We're at the head of the key's threads chain */
@ -1112,5 +1056,4 @@ void pte_tkAssocDestroy (ThreadKeyAssoc * assoc)
assoc->key->threads = next; assoc->key->threads = next;
free (assoc); free (assoc);
}
} }