Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / native_client_sdk / src / libraries / third_party / pthreads-win32 / ptw32_reuse.c
blob7325857ba2019da7e2cb5bfa04b431cef6a75a9e
1 /*
2 * ptw32_threadReuse.c
4 * Description:
5 * This translation unit implements miscellaneous thread functions.
7 * --------------------------------------------------------------------------
9 * Pthreads-win32 - POSIX Threads Library for Win32
10 * Copyright(C) 1998 John E. Bossom
11 * Copyright(C) 1999,2005 Pthreads-win32 contributors
13 * Contact Email: rpj@callisto.canberra.edu.au
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
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.
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.
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
37 #include "pthread.h"
38 #include "implement.h"
42 * How it works:
43 * A pthread_t is a struct (2x32 bit scalar types on IA-32, 2x64 bit on IA-64)
44 * which is normally passed/returned by value to/from pthreads routines.
45 * Applications are therefore storing a copy of the struct as it is at that
46 * time.
48 * The original pthread_t struct plus all copies of it contain the address of
49 * the thread state struct ptw32_thread_t_ (p), plus a reuse counter (x). Each
50 * ptw32_thread_t contains the original copy of it's pthread_t.
51 * Once malloced, a ptw32_thread_t_ struct is not freed until the process exits.
53 * The thread reuse stack is a simple LILO stack managed through a singly
54 * linked list element in the ptw32_thread_t.
56 * Each time a thread is destroyed, the ptw32_thread_t address is pushed onto the
57 * reuse stack after it's ptHandle's reuse counter has been incremented.
59 * The following can now be said from this:
60 * - two pthread_t's are identical if their ptw32_thread_t reference pointers
61 * are equal and their reuse counters are equal. That is,
63 * equal = (a.p == b.p && a.x == b.x)
65 * - a pthread_t copy refers to a destroyed thread if the reuse counter in
66 * the copy is not equal to the reuse counter in the original.
68 * threadDestroyed = (copy.x != ((ptw32_thread_t *)copy.p)->ptHandle.x)
73 * Pop a clean pthread_t struct off the reuse stack.
75 pthread_t
76 ptw32_threadReusePop (void)
78 pthread_t t = {NULL, 0};
79 ptw32_mcs_local_node_t node;
81 ptw32_mcs_lock_acquire(&ptw32_thread_reuse_lock, &node);
83 if (PTW32_THREAD_REUSE_EMPTY != ptw32_threadReuseTop)
85 ptw32_thread_t * tp;
87 tp = ptw32_threadReuseTop;
89 ptw32_threadReuseTop = tp->prevReuse;
91 if (PTW32_THREAD_REUSE_EMPTY == ptw32_threadReuseTop)
93 ptw32_threadReuseBottom = PTW32_THREAD_REUSE_EMPTY;
96 tp->prevReuse = NULL;
98 t = tp->ptHandle;
101 ptw32_mcs_lock_release(&node);
103 return t;
108 * Push a clean pthread_t struct onto the reuse stack.
109 * Must be re-initialised when reused.
110 * All object elements (mutexes, events etc) must have been either
111 * detroyed before this, or never initialised.
113 void
114 ptw32_threadReusePush (pthread_t thread)
116 ptw32_thread_t * tp = (ptw32_thread_t *) thread.p;
117 pthread_t t;
118 ptw32_mcs_local_node_t node;
120 ptw32_mcs_lock_acquire(&ptw32_thread_reuse_lock, &node);
122 t = tp->ptHandle;
123 memset(tp, 0, sizeof(ptw32_thread_t));
125 /* Must restore the original POSIX handle that we just wiped. */
126 tp->ptHandle = t;
128 /* Bump the reuse counter now */
129 #if defined(PTW32_THREAD_ID_REUSE_INCREMENT)
130 tp->ptHandle.x += PTW32_THREAD_ID_REUSE_INCREMENT;
131 #else
132 tp->ptHandle.x++;
133 #endif
135 tp->state = PThreadStateReuse;
137 tp->prevReuse = PTW32_THREAD_REUSE_EMPTY;
139 if (PTW32_THREAD_REUSE_EMPTY != ptw32_threadReuseBottom)
141 ptw32_threadReuseBottom->prevReuse = tp;
143 else
145 ptw32_threadReuseTop = tp;
148 ptw32_threadReuseBottom = tp;
150 ptw32_mcs_lock_release(&node);