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

Contents of /trunk/3rdparty/w32pthreads/pthread_cancel.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: 4418 byte(s)
committing r3113 initial commit again...
1 /*
2 * pthread_cancel.c
3 *
4 * Description:
5 * POSIX thread functions related to thread cancellation.
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 int
40 pthread_cancel (pthread_t thread)
41 /*
42 * ------------------------------------------------------
43 * DOCPUBLIC
44 * This function requests cancellation of 'thread'.
45 *
46 * PARAMETERS
47 * thread
48 * reference to an instance of pthread_t
49 *
50 *
51 * DESCRIPTION
52 * This function requests cancellation of 'thread'.
53 * NOTE: cancellation is asynchronous; use pthread_join to
54 * wait for termination of 'thread' if necessary.
55 *
56 * RESULTS
57 * 0 successfully requested cancellation,
58 * ESRCH no thread found corresponding to 'thread',
59 * ENOMEM implicit self thread create failed.
60 * ------------------------------------------------------
61 */
62 {
63 int result;
64 int cancel_self;
65 pthread_t self;
66 ptw32_thread_t * tp;
67
68 result = pthread_kill (thread, 0);
69
70 if (0 != result)
71 {
72 return result;
73 }
74
75 if ((self = pthread_self ()).p == NULL)
76 {
77 return ENOMEM;
78 }
79
80 /*
81 * FIXME!!
82 *
83 * Can a thread cancel itself?
84 *
85 * The standard doesn't
86 * specify an error to be returned if the target
87 * thread is itself.
88 *
89 * If it may, then we need to ensure that a thread can't
90 * deadlock itself trying to cancel itself asynchronously
91 * (pthread_cancel is required to be an async-cancel
92 * safe function).
93 */
94 cancel_self = pthread_equal (thread, self);
95 tp = (ptw32_thread_t *) thread.p;
96
97 /*
98 * Lock for async-cancel safety.
99 */
100 (void) pthread_mutex_lock (&tp->cancelLock);
101
102 if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS
103 && tp->cancelState == PTHREAD_CANCEL_ENABLE
104 && tp->state < PThreadStateCanceling)
105 {
106 if (cancel_self)
107 {
108 tp->state = PThreadStateCanceling;
109 tp->cancelState = PTHREAD_CANCEL_DISABLE;
110
111 (void) pthread_mutex_unlock (&tp->cancelLock);
112 ptw32_throw (PTW32_EPS_CANCEL);
113
114 /* Never reached */
115 }
116 else
117 {
118 ptw32_PrepCancel( tp );
119 }
120 }
121 else
122 {
123 /*
124 * Set for deferred cancellation.
125 */
126
127 if (tp->state < PThreadStateCancelPending)
128 {
129 // enables full cancel testing in pthread_testcancel, which is normally disabled because
130 // the full test requires a DLL function invocation and TLS lookup. This provides an
131 // inlinable first-step shortcut for much speedier testing. :)
132
133 // Increment is performed here such that a thread that is already canceling won't get
134 // counted multiple times.
135
136 _InterlockedIncrement( &ptw32_testcancel_enable );
137
138 tp->state = PThreadStateCancelPending;
139 if (!SetEvent (tp->cancelEvent))
140 {
141 result = ESRCH;
142 }
143 }
144 else if (tp->state >= PThreadStateCanceling)
145 {
146 result = ESRCH;
147 }
148
149 (void) pthread_mutex_unlock (&tp->cancelLock);
150 }
151
152 return (result);
153 }

  ViewVC Help
Powered by ViewVC 1.1.22