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

Contents of /trunk/3rdparty/w32pthreads/pthread_win32_attach_detach_np.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: 5501 byte(s)
committing r3113 initial commit again...
1 /*
2 * pthread_win32_attach_detach_np.c
3 *
4 * Description:
5 * This translation unit implements non-portable 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 * Handle to kernel32.dll
41 */
42 static HINSTANCE ptw32_h_kernel32;
43
44 /*
45 * Handle to quserex.dll
46 */
47 static HINSTANCE ptw32_h_quserex;
48
49 BOOL
50 pthread_win32_process_attach_np ()
51 {
52 BOOL result = TRUE;
53
54 if( ptw32_processInitialized ) return TRUE;
55
56 result = ptw32_processInitialize ();
57
58 #ifdef _UWIN
59 pthread_count++;
60 #endif
61
62 ptw32_features = 0;
63
64
65 /*
66 * Load QUSEREX.DLL and try to get address of QueueUserAPCEx
67 */
68 ptw32_h_quserex = LoadLibrary (TEXT ("QUSEREX.DLL"));
69
70 if (ptw32_h_quserex != NULL)
71 {
72 ptw32_register_cancelation = (DWORD (*)(PAPCFUNC, HANDLE, DWORD))
73 #if defined(NEED_UNICODE_CONSTS)
74 GetProcAddress (ptw32_h_quserex,
75 (const TCHAR *) TEXT ("QueueUserAPCEx"));
76 #else
77 GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx");
78 #endif
79 }
80
81 if (NULL == ptw32_register_cancelation)
82 {
83 ptw32_register_cancelation = ptw32_RegisterCancelation;
84
85 if (ptw32_h_quserex != NULL)
86 {
87 (void) FreeLibrary (ptw32_h_quserex);
88 }
89 ptw32_h_quserex = 0;
90 }
91 else
92 {
93 /* Initialise QueueUserAPCEx */
94 BOOL (*queue_user_apc_ex_init) (VOID);
95
96 queue_user_apc_ex_init = (BOOL (*)(VOID))
97 #if defined(NEED_UNICODE_CONSTS)
98 GetProcAddress (ptw32_h_quserex,
99 (const TCHAR *) TEXT ("QueueUserAPCEx_Init"));
100 #else
101 GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Init");
102 #endif
103
104 if (queue_user_apc_ex_init == NULL || !queue_user_apc_ex_init ())
105 {
106 ptw32_register_cancelation = ptw32_RegisterCancelation;
107
108 (void) FreeLibrary (ptw32_h_quserex);
109 ptw32_h_quserex = 0;
110 }
111 }
112
113 if (ptw32_h_quserex)
114 {
115 ptw32_features |= PTW32_ALERTABLE_ASYNC_CANCEL;
116 }
117
118 return result;
119 }
120
121
122 BOOL
123 pthread_win32_process_detach_np ()
124 {
125 if (ptw32_processInitialized)
126 {
127 ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
128
129 if (sp != NULL)
130 {
131 /*
132 * Detached threads have their resources automatically
133 * cleaned up upon exit (others must be 'joined').
134 */
135 if (sp->detachState == PTHREAD_CREATE_DETACHED)
136 {
137 ptw32_threadDestroy (sp->ptHandle);
138 TlsSetValue (ptw32_selfThreadKey->key, NULL);
139 }
140 }
141
142 /*
143 * The DLL is being unmapped from the process's address space
144 */
145 ptw32_processTerminate ();
146
147 if (ptw32_h_quserex)
148 {
149 /* Close QueueUserAPCEx */
150 BOOL (*queue_user_apc_ex_fini) (VOID);
151
152 queue_user_apc_ex_fini = (BOOL (*)(VOID))
153 #if defined(NEED_UNICODE_CONSTS)
154 GetProcAddress (ptw32_h_quserex,
155 (const TCHAR *) TEXT ("QueueUserAPCEx_Fini"));
156 #else
157 GetProcAddress (ptw32_h_quserex, (LPCSTR) "QueueUserAPCEx_Fini");
158 #endif
159
160 if (queue_user_apc_ex_fini != NULL)
161 {
162 (void) queue_user_apc_ex_fini ();
163 }
164 (void) FreeLibrary (ptw32_h_quserex);
165 }
166
167 if (ptw32_h_kernel32)
168 {
169 (void) FreeLibrary (ptw32_h_kernel32);
170 }
171 }
172
173 return TRUE;
174 }
175
176 BOOL
177 pthread_win32_thread_attach_np ()
178 {
179 return TRUE;
180 }
181
182 BOOL
183 pthread_win32_thread_detach_np ()
184 {
185 if (ptw32_processInitialized)
186 {
187 /*
188 * Don't use pthread_self() - to avoid creating an implicit POSIX thread handle
189 * unnecessarily.
190 */
191 ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
192
193 if (sp != NULL) // otherwise Win32 thread with no implicit POSIX handle.
194 {
195 ptw32_callUserDestroyRoutines (sp->ptHandle);
196
197 (void) pthread_mutex_lock (&sp->cancelLock);
198 sp->state = PThreadStateLast;
199 /*
200 * If the thread is joinable at this point then it MUST be joined
201 * or detached explicitly by the application.
202 */
203 (void) pthread_mutex_unlock (&sp->cancelLock);
204
205 if (sp->detachState == PTHREAD_CREATE_DETACHED)
206 {
207 ptw32_threadDestroy (sp->ptHandle);
208
209 TlsSetValue (ptw32_selfThreadKey->key, NULL);
210 }
211 }
212 }
213
214 return TRUE;
215 }
216
217 BOOL
218 pthread_win32_test_features_np (int feature_mask)
219 {
220 return ((ptw32_features & feature_mask) == feature_mask);
221 }

  ViewVC Help
Powered by ViewVC 1.1.22