1062c5bc4SJason Schmidlapp /*
2062c5bc4SJason Schmidlapp * pte_threadReuse.c
3062c5bc4SJason Schmidlapp *
4062c5bc4SJason Schmidlapp * Description:
5062c5bc4SJason Schmidlapp * This translation unit implements miscellaneous thread functions.
6062c5bc4SJason Schmidlapp *
7062c5bc4SJason Schmidlapp * --------------------------------------------------------------------------
8062c5bc4SJason Schmidlapp *
9062c5bc4SJason Schmidlapp * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
10062c5bc4SJason Schmidlapp * Copyright(C) 2008 Jason Schmidlapp
11062c5bc4SJason Schmidlapp *
12062c5bc4SJason Schmidlapp * Contact Email: jschmidlapp@users.sourceforge.net
13062c5bc4SJason Schmidlapp *
14062c5bc4SJason Schmidlapp *
15062c5bc4SJason Schmidlapp * Based upon Pthreads-win32 - POSIX Threads Library for Win32
16062c5bc4SJason Schmidlapp * Copyright(C) 1998 John E. Bossom
17062c5bc4SJason Schmidlapp * Copyright(C) 1999,2005 Pthreads-win32 contributors
18062c5bc4SJason Schmidlapp *
19062c5bc4SJason Schmidlapp * Contact Email: rpj@callisto.canberra.edu.au
20062c5bc4SJason Schmidlapp *
21062c5bc4SJason Schmidlapp * The original list of contributors to the Pthreads-win32 project
22062c5bc4SJason Schmidlapp * is contained in the file CONTRIBUTORS.ptw32 included with the
23062c5bc4SJason Schmidlapp * source code distribution. The list can also be seen at the
24062c5bc4SJason Schmidlapp * following World Wide Web location:
25062c5bc4SJason Schmidlapp * http://sources.redhat.com/pthreads-win32/contributors.html
26062c5bc4SJason Schmidlapp *
27062c5bc4SJason Schmidlapp * This library is free software; you can redistribute it and/or
28062c5bc4SJason Schmidlapp * modify it under the terms of the GNU Lesser General Public
29062c5bc4SJason Schmidlapp * License as published by the Free Software Foundation; either
30062c5bc4SJason Schmidlapp * version 2 of the License, or (at your option) any later version.
31062c5bc4SJason Schmidlapp *
32062c5bc4SJason Schmidlapp * This library is distributed in the hope that it will be useful,
33062c5bc4SJason Schmidlapp * but WITHOUT ANY WARRANTY; without even the implied warranty of
34062c5bc4SJason Schmidlapp * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35062c5bc4SJason Schmidlapp * Lesser General Public License for more details.
36062c5bc4SJason Schmidlapp *
37062c5bc4SJason Schmidlapp * You should have received a copy of the GNU Lesser General Public
38062c5bc4SJason Schmidlapp * License along with this library in the file COPYING.LIB;
39062c5bc4SJason Schmidlapp * if not, write to the Free Software Foundation, Inc.,
40062c5bc4SJason Schmidlapp * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
41062c5bc4SJason Schmidlapp */
42062c5bc4SJason Schmidlapp
43062c5bc4SJason Schmidlapp #include <stdio.h>
44062c5bc4SJason Schmidlapp #include <stdlib.h>
45062c5bc4SJason Schmidlapp #include <string.h>
46062c5bc4SJason Schmidlapp
47062c5bc4SJason Schmidlapp #include "pthread.h"
48062c5bc4SJason Schmidlapp #include "implement.h"
49062c5bc4SJason Schmidlapp
50062c5bc4SJason Schmidlapp
51062c5bc4SJason Schmidlapp /*
52062c5bc4SJason Schmidlapp * How it works:
53062c5bc4SJason Schmidlapp * A pthread_t is a struct which is normally passed/returned by
54062c5bc4SJason Schmidlapp * value to/from pthreads routines. Applications are therefore storing
55062c5bc4SJason Schmidlapp * a copy of the struct as it is at that time.
56062c5bc4SJason Schmidlapp *
57062c5bc4SJason Schmidlapp * The original pthread_t struct plus all copies of it contain the address of
58062c5bc4SJason Schmidlapp * the thread state struct pte_thread_t_ (p), plus a reuse counter (x). Each
59062c5bc4SJason Schmidlapp * pte_thread_t contains the original copy of it's pthread_t.
60062c5bc4SJason Schmidlapp * Once malloced, a pte_thread_t_ struct is not freed until the process exits.
61062c5bc4SJason Schmidlapp *
62062c5bc4SJason Schmidlapp * The thread reuse stack is a simple LILO stack managed through a singly
63062c5bc4SJason Schmidlapp * linked list element in the pte_thread_t.
64062c5bc4SJason Schmidlapp *
65062c5bc4SJason Schmidlapp * Each time a thread is destroyed, the pte_thread_t address is pushed onto the
66062c5bc4SJason Schmidlapp * reuse stack after it's ptHandle's reuse counter has been incremented.
67062c5bc4SJason Schmidlapp *
68062c5bc4SJason Schmidlapp * The following can now be said from this:
69062c5bc4SJason Schmidlapp * - two pthread_t's are identical if their pte_thread_t reference pointers
70062c5bc4SJason Schmidlapp * are equal and their reuse counters are equal. That is,
71062c5bc4SJason Schmidlapp *
72062c5bc4SJason Schmidlapp * equal = (a.p == b.p && a.x == b.x)
73062c5bc4SJason Schmidlapp *
74062c5bc4SJason Schmidlapp * - a pthread_t copy refers to a destroyed thread if the reuse counter in
75062c5bc4SJason Schmidlapp * the copy is not equal to the reuse counter in the original.
76062c5bc4SJason Schmidlapp *
77062c5bc4SJason Schmidlapp * threadDestroyed = (copy.x != ((pte_thread_t *)copy.p)->ptHandle.x)
78062c5bc4SJason Schmidlapp *
79062c5bc4SJason Schmidlapp */
80062c5bc4SJason Schmidlapp
81062c5bc4SJason Schmidlapp /*
82062c5bc4SJason Schmidlapp * Pop a clean pthread_t struct off the reuse stack.
83062c5bc4SJason Schmidlapp */
84062c5bc4SJason Schmidlapp pthread_t
pte_threadReusePop(void)85062c5bc4SJason Schmidlapp pte_threadReusePop (void)
86062c5bc4SJason Schmidlapp {
87*a12978ccSJeremy Soller pthread_t t = NULL;
88062c5bc4SJason Schmidlapp
89062c5bc4SJason Schmidlapp
90062c5bc4SJason Schmidlapp pte_osMutexLock (pte_thread_reuse_lock);
91062c5bc4SJason Schmidlapp
92062c5bc4SJason Schmidlapp if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseTop)
93062c5bc4SJason Schmidlapp {
94062c5bc4SJason Schmidlapp pte_thread_t * tp;
95062c5bc4SJason Schmidlapp
96062c5bc4SJason Schmidlapp tp = pte_threadReuseTop;
97062c5bc4SJason Schmidlapp
98062c5bc4SJason Schmidlapp pte_threadReuseTop = tp->prevReuse;
99062c5bc4SJason Schmidlapp
100062c5bc4SJason Schmidlapp if (PTE_THREAD_REUSE_EMPTY == pte_threadReuseTop)
101062c5bc4SJason Schmidlapp {
102062c5bc4SJason Schmidlapp pte_threadReuseBottom = PTE_THREAD_REUSE_EMPTY;
103062c5bc4SJason Schmidlapp }
104062c5bc4SJason Schmidlapp
105062c5bc4SJason Schmidlapp tp->prevReuse = NULL;
106062c5bc4SJason Schmidlapp
107062c5bc4SJason Schmidlapp t = tp->ptHandle;
108062c5bc4SJason Schmidlapp }
109062c5bc4SJason Schmidlapp
110062c5bc4SJason Schmidlapp pte_osMutexUnlock(pte_thread_reuse_lock);
111062c5bc4SJason Schmidlapp
112062c5bc4SJason Schmidlapp return t;
113062c5bc4SJason Schmidlapp
114062c5bc4SJason Schmidlapp }
115062c5bc4SJason Schmidlapp
116062c5bc4SJason Schmidlapp /*
117062c5bc4SJason Schmidlapp * Push a clean pthread_t struct onto the reuse stack.
118062c5bc4SJason Schmidlapp * Must be re-initialised when reused.
119062c5bc4SJason Schmidlapp * All object elements (mutexes, events etc) must have been either
120062c5bc4SJason Schmidlapp * detroyed before this, or never initialised.
121062c5bc4SJason Schmidlapp */
122062c5bc4SJason Schmidlapp void
pte_threadReusePush(pthread_t thread)123062c5bc4SJason Schmidlapp pte_threadReusePush (pthread_t thread)
124062c5bc4SJason Schmidlapp {
125*a12978ccSJeremy Soller pte_thread_t * tp = (pte_thread_t *) thread;
126062c5bc4SJason Schmidlapp pthread_t t;
127062c5bc4SJason Schmidlapp
128062c5bc4SJason Schmidlapp
129062c5bc4SJason Schmidlapp pte_osMutexLock (pte_thread_reuse_lock);
130062c5bc4SJason Schmidlapp
131062c5bc4SJason Schmidlapp t = tp->ptHandle;
132062c5bc4SJason Schmidlapp memset(tp, 0, sizeof(pte_thread_t));
133062c5bc4SJason Schmidlapp
134062c5bc4SJason Schmidlapp /* Must restore the original POSIX handle that we just wiped. */
135062c5bc4SJason Schmidlapp tp->ptHandle = t;
136062c5bc4SJason Schmidlapp
137062c5bc4SJason Schmidlapp tp->prevReuse = PTE_THREAD_REUSE_EMPTY;
138062c5bc4SJason Schmidlapp
139062c5bc4SJason Schmidlapp if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseBottom)
140062c5bc4SJason Schmidlapp {
141062c5bc4SJason Schmidlapp pte_threadReuseBottom->prevReuse = tp;
142062c5bc4SJason Schmidlapp }
143062c5bc4SJason Schmidlapp else
144062c5bc4SJason Schmidlapp {
145062c5bc4SJason Schmidlapp pte_threadReuseTop = tp;
146062c5bc4SJason Schmidlapp }
147062c5bc4SJason Schmidlapp
148062c5bc4SJason Schmidlapp pte_threadReuseBottom = tp;
149062c5bc4SJason Schmidlapp
150062c5bc4SJason Schmidlapp pte_osMutexUnlock(pte_thread_reuse_lock);
151062c5bc4SJason Schmidlapp }
152