/[pcsx2_0.9.7]/trunk/3rdparty/w32pthreads/ptw32_reuse.c
ViewVC logotype

Contents of /trunk/3rdparty/w32pthreads/ptw32_reuse.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22