xref: /relibc/pthreads-emb/pte_reuse.c (revision a12978ccca4c576f02971f5cfc2cbefe23e4daba)
1 /*
2  * pte_threadReuse.c
3  *
4  * Description:
5  * This translation unit implements miscellaneous thread functions.
6  *
7  * --------------------------------------------------------------------------
8  *
9  *      Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
10  *      Copyright(C) 2008 Jason Schmidlapp
11  *
12  *      Contact Email: jschmidlapp@users.sourceforge.net
13  *
14  *
15  *      Based upon Pthreads-win32 - POSIX Threads Library for Win32
16  *      Copyright(C) 1998 John E. Bossom
17  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
18  *
19  *      Contact Email: rpj@callisto.canberra.edu.au
20  *
21  *      The original list of contributors to the Pthreads-win32 project
22  *      is contained in the file CONTRIBUTORS.ptw32 included with the
23  *      source code distribution. The list can also be seen at the
24  *      following World Wide Web location:
25  *      http://sources.redhat.com/pthreads-win32/contributors.html
26  *
27  *      This library is free software; you can redistribute it and/or
28  *      modify it under the terms of the GNU Lesser General Public
29  *      License as published by the Free Software Foundation; either
30  *      version 2 of the License, or (at your option) any later version.
31  *
32  *      This library is distributed in the hope that it will be useful,
33  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
34  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35  *      Lesser General Public License for more details.
36  *
37  *      You should have received a copy of the GNU Lesser General Public
38  *      License along with this library in the file COPYING.LIB;
39  *      if not, write to the Free Software Foundation, Inc.,
40  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
41  */
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "pthread.h"
48 #include "implement.h"
49 
50 
51 /*
52  * How it works:
53  * A pthread_t is a struct which is normally passed/returned by
54  * value to/from pthreads routines.  Applications are therefore storing
55  * a copy of the struct as it is at that time.
56  *
57  * The original pthread_t struct plus all copies of it contain the address of
58  * the thread state struct pte_thread_t_ (p), plus a reuse counter (x). Each
59  * pte_thread_t contains the original copy of it's pthread_t.
60  * Once malloced, a pte_thread_t_ struct is not freed until the process exits.
61  *
62  * The thread reuse stack is a simple LILO stack managed through a singly
63  * linked list element in the pte_thread_t.
64  *
65  * Each time a thread is destroyed, the pte_thread_t address is pushed onto the
66  * reuse stack after it's ptHandle's reuse counter has been incremented.
67  *
68  * The following can now be said from this:
69  * - two pthread_t's are identical if their pte_thread_t reference pointers
70  * are equal and their reuse counters are equal. That is,
71  *
72  *   equal = (a.p == b.p && a.x == b.x)
73  *
74  * - a pthread_t copy refers to a destroyed thread if the reuse counter in
75  * the copy is not equal to the reuse counter in the original.
76  *
77  *   threadDestroyed = (copy.x != ((pte_thread_t *)copy.p)->ptHandle.x)
78  *
79  */
80 
81 /*
82  * Pop a clean pthread_t struct off the reuse stack.
83  */
84 pthread_t
pte_threadReusePop(void)85 pte_threadReusePop (void)
86 {
87   pthread_t t = NULL;
88 
89 
90   pte_osMutexLock (pte_thread_reuse_lock);
91 
92   if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseTop)
93     {
94       pte_thread_t * tp;
95 
96       tp = pte_threadReuseTop;
97 
98       pte_threadReuseTop = tp->prevReuse;
99 
100       if (PTE_THREAD_REUSE_EMPTY == pte_threadReuseTop)
101         {
102           pte_threadReuseBottom = PTE_THREAD_REUSE_EMPTY;
103         }
104 
105       tp->prevReuse = NULL;
106 
107       t = tp->ptHandle;
108     }
109 
110   pte_osMutexUnlock(pte_thread_reuse_lock);
111 
112   return t;
113 
114 }
115 
116 /*
117  * Push a clean pthread_t struct onto the reuse stack.
118  * Must be re-initialised when reused.
119  * All object elements (mutexes, events etc) must have been either
120  * detroyed before this, or never initialised.
121  */
122 void
pte_threadReusePush(pthread_t thread)123 pte_threadReusePush (pthread_t thread)
124 {
125   pte_thread_t * tp = (pte_thread_t *) thread;
126   pthread_t t;
127 
128 
129   pte_osMutexLock (pte_thread_reuse_lock);
130 
131   t = tp->ptHandle;
132   memset(tp, 0, sizeof(pte_thread_t));
133 
134   /* Must restore the original POSIX handle that we just wiped. */
135   tp->ptHandle = t;
136 
137   tp->prevReuse = PTE_THREAD_REUSE_EMPTY;
138 
139   if (PTE_THREAD_REUSE_EMPTY != pte_threadReuseBottom)
140     {
141       pte_threadReuseBottom->prevReuse = tp;
142     }
143   else
144     {
145       pte_threadReuseTop = tp;
146     }
147 
148   pte_threadReuseBottom = tp;
149 
150   pte_osMutexUnlock(pte_thread_reuse_lock);
151 }
152