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

Contents of /trunk/3rdparty/w32pthreads/w32_CancelableWait.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: 4479 byte(s)
committing r3113 initial commit again...
1 /*
2 * w32_CancelableWait.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 static INLINE int
41 ptw32_cancelable_wait (HANDLE waitHandle, DWORD timeout)
42 /*
43 * -------------------------------------------------------------------
44 * This provides an extra hook into the pthread_cancel
45 * mechanism that will allow you to wait on a Windows handle and make it a
46 * cancellation point. This function blocks until the given WIN32 handle is
47 * signaled or pthread_cancel has been called. It is implemented using
48 * WaitForMultipleObjects on 'waitHandle' and a manually reset WIN32
49 * event used to implement pthread_cancel.
50 *
51 * Given this hook it would be possible to implement more of the cancellation
52 * points.
53 * -------------------------------------------------------------------
54 */
55 {
56 int result;
57 pthread_t self;
58 ptw32_thread_t * sp;
59 HANDLE handles[2];
60 DWORD nHandles = 1;
61 DWORD status;
62
63 handles[0] = waitHandle;
64
65 self = pthread_self();
66 sp = (ptw32_thread_t *) self.p;
67
68 if (sp != NULL)
69 {
70 /*
71 * Get cancelEvent handle
72 */
73 if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
74 {
75
76 if ((handles[1] = sp->cancelEvent) != NULL)
77 {
78 nHandles++;
79 }
80 }
81 }
82 else
83 {
84 handles[1] = NULL;
85 }
86
87 status = WaitForMultipleObjects (nHandles, handles, PTW32_FALSE, timeout);
88
89 switch (status - WAIT_OBJECT_0)
90 {
91 case 0:
92 /*
93 * Got the handle.
94 * In the event that both handles are signalled, the smallest index
95 * value (us) is returned. As it has been arranged, this ensures that
96 * we don't drop a signal that we should act on (i.e. semaphore,
97 * mutex, or condition variable etc).
98 */
99 result = 0;
100 break;
101
102 case 1:
103 /*
104 * Got cancel request.
105 * In the event that both handles are signaled, the cancel will
106 * be ignored (see case 0 comment).
107 */
108 ResetEvent (handles[1]);
109
110 if (sp != NULL)
111 {
112 /*
113 * Should handle POSIX and implicit POSIX threads..
114 * Make sure we haven't been async-canceled in the meantime.
115 */
116 (void) pthread_mutex_lock (&sp->cancelLock);
117 if (sp->state < PThreadStateCanceling)
118 {
119 sp->state = PThreadStateCanceling;
120 sp->cancelState = PTHREAD_CANCEL_DISABLE;
121 (void) pthread_mutex_unlock (&sp->cancelLock);
122 ptw32_throw (PTW32_EPS_CANCEL);
123
124 /* Never reached */
125 }
126 (void) pthread_mutex_unlock (&sp->cancelLock);
127 }
128
129 /* Should never get to here. */
130 result = EINVAL;
131 break;
132
133 default:
134 if (status == WAIT_TIMEOUT)
135 {
136 result = ETIMEDOUT;
137 }
138 else
139 {
140 result = EINVAL;
141 }
142 break;
143 }
144
145 return (result);
146
147 } /* CancelableWait */
148
149 int
150 pthreadCancelableWait (HANDLE waitHandle)
151 {
152 return (ptw32_cancelable_wait (waitHandle, INFINITE));
153 }
154
155 int
156 pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout)
157 {
158 return (ptw32_cancelable_wait (waitHandle, timeout));
159 }

  ViewVC Help
Powered by ViewVC 1.1.22