123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "pthread.h"
- #include "implement.h"
- #include <pte_osal.h>
- #ifdef __cplusplus
- #include <exception>
- using std::terminate;
- #endif
- void
- pte_callUserDestroyRoutines (pthread_t thread)
- {
- ThreadKeyAssoc * assoc;
- if (thread.p != NULL)
- {
- int assocsRemaining;
- int iterations = 0;
- pte_thread_t * sp = (pte_thread_t *) thread.p;
-
- do
- {
- assocsRemaining = 0;
- iterations++;
- (void) pthread_mutex_lock(&(sp->threadLock));
-
- sp->nextAssoc = sp->keys;
- (void) pthread_mutex_unlock(&(sp->threadLock));
- for (;;)
- {
- void * value;
- pthread_key_t k;
- void (*destructor) (void *);
-
- (void) pthread_mutex_lock(&(sp->threadLock));
- if ((assoc = (ThreadKeyAssoc *)sp->nextAssoc) == NULL)
- {
-
- pthread_mutex_unlock(&(sp->threadLock));
- break;
- }
- else
- {
-
- if (pthread_mutex_trylock(&(assoc->key->keyLock)) == EBUSY)
- {
- pthread_mutex_unlock(&(sp->threadLock));
- pte_osThreadSleep(1);
-
- continue;
- }
- }
-
- sp->nextAssoc = assoc->nextKey;
-
- k = assoc->key;
- destructor = k->destructor;
- value = pte_osTlsGetValue(k->key);
- pte_osTlsSetValue (k->key, NULL);
-
- if (value != NULL && iterations <= PTHREAD_DESTRUCTOR_ITERATIONS)
- {
-
- (void) pthread_mutex_unlock(&(sp->threadLock));
- (void) pthread_mutex_unlock(&(k->keyLock));
- assocsRemaining++;
- #ifdef __cplusplus
- try
- {
-
- destructor (value);
- }
- catch (...)
- {
-
- terminate ();
- }
- #else
-
- destructor (value);
- #endif
- }
- else
- {
-
- pte_tkAssocDestroy (assoc);
- (void) pthread_mutex_unlock(&(sp->threadLock));
- (void) pthread_mutex_unlock(&(k->keyLock));
- }
- }
- }
- while (assocsRemaining);
- }
- }
|