diff options
Diffstat (limited to 'ptw32_reuse.c')
-rw-r--r-- | ptw32_reuse.c | 84 |
1 files changed, 69 insertions, 15 deletions
diff --git a/ptw32_reuse.c b/ptw32_reuse.c index a21737a..7056395 100644 --- a/ptw32_reuse.c +++ b/ptw32_reuse.c @@ -39,10 +39,34 @@ /* + * How it works: + * A pthread_t is a struct (2x32 bit scalar types on IA-32, 2x64 bit on IA-64) + * which is normally passed/returned by value to/from pthreads routines. + * Applications are therefore storing a copy of the struct as it is at that + * time. + * + * The original pthread_t struct plus all copies of it contain the address of + * the thread state struct ptw32_thread_t_ (p), plus a reuse counter (x). Each + * ptw32_thread_t contains the original copy of it's pthread_t. + * Once malloced, a ptw2_thread_t_ struct is never freed. + * * The thread reuse stack is a simple LIFO stack managed through a singly - * linked list element in the pthread_t_ struct. + * linked list element in the ptw32_thread_t. + * + * Each time a thread is destroyed, the ptw32_thread_t address is pushed onto the + * reuse stack after it's ptHandle's reuse counter has been incremented. + * + * The following can now be said from this: + * - two pthread_t's are identical if their ptw32_thread_t reference pointers + * are equal and their reuse couters are equal. That is, + * + * equal = (a.p == b.p && a.x == b.x) + * + * - a pthread_t copy refers to a destroyed thread if the reuse counter in + * the copy is not equal to the reuse counter in the original. + * + * threadDestroyed = (copy.x != ((ptw32_thread_t *)copy.p)->ptHandle.x) * - * All thread structs on the stack are clean and ready for reuse. */ /* @@ -51,20 +75,26 @@ pthread_t ptw32_threadReusePop (void) { - pthread_t t; + pthread_t t = {NULL, 0}; EnterCriticalSection (&ptw32_thread_reuse_lock); - t = ptw32_threadReuseTop; - - if (PTW32_THREAD_REUSE_BOTTOM != t) - { - ptw32_threadReuseTop = t->prevReuse; - t->prevReuse = NULL; - } - else + if (PTW32_THREAD_REUSE_EMPTY != ptw32_threadReuseTop) { - t = NULL; + ptw32_thread_t * tp; + + tp = ptw32_threadReuseTop; + + ptw32_threadReuseTop = tp->prevReuse; + + if (PTW32_THREAD_REUSE_EMPTY == ptw32_threadReuseTop) + { + ptw32_threadReuseBottom = PTW32_THREAD_REUSE_EMPTY; + } + + tp->prevReuse = NULL; + + t = tp->ptHandle; } LeaveCriticalSection (&ptw32_thread_reuse_lock); @@ -75,15 +105,39 @@ ptw32_threadReusePop (void) /* * Push a clean pthread_t struct onto the reuse stack. + * Must be re-initialised when reused. + * All object elements (mutexes, events etc) must have been either + * detroyed before this, or never initialised. */ void ptw32_threadReusePush (pthread_t thread) { + ptw32_thread_t * tp = (ptw32_thread_t *) thread.p; + pthread_t t; + EnterCriticalSection (&ptw32_thread_reuse_lock); - memset (thread, 0, sizeof (*thread)); - thread->prevReuse = ptw32_threadReuseTop; - ptw32_threadReuseTop = thread; + t = tp->ptHandle; + memset(tp, 0, sizeof(ptw32_thread_t)); + + /* Must restore the original POSIX handle that we just wiped. */ + tp->ptHandle = t; + + /* Bump the reuse counter now */ + tp->ptHandle.x++; + + tp->prevReuse = PTW32_THREAD_REUSE_EMPTY; + + if (PTW32_THREAD_REUSE_EMPTY != ptw32_threadReuseBottom) + { + ptw32_threadReuseBottom->prevReuse = tp; + } + else + { + ptw32_threadReuseTop = tp; + } + + ptw32_threadReuseBottom = tp; LeaveCriticalSection (&ptw32_thread_reuse_lock); } |