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

Contents of /trunk/3rdparty/w32pthreads/pthread_detach.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: 4153 byte(s)
committing r3113 initial commit again...
1 /*
2 * pthread_detach.c
3 *
4 * Description:
5 * This translation unit implements functions related to thread
6 * synchronisation.
7 *
8 * --------------------------------------------------------------------------
9 *
10 * Pthreads-win32 - POSIX Threads Library for Win32
11 * Copyright(C) 1998 John E. Bossom
12 * Copyright(C) 1999,2005 Pthreads-win32 contributors
13 *
14 * Contact Email: rpj@callisto.canberra.edu.au
15 *
16 * The current list of contributors is contained
17 * in the file CONTRIBUTORS included with the source
18 * code distribution. The list can also be seen at the
19 * following World Wide Web location:
20 * http://sources.redhat.com/pthreads-win32/contributors.html
21 *
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public
33 * License along with this library in the file COPYING.LIB;
34 * if not, write to the Free Software Foundation, Inc.,
35 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
36 */
37
38 #include "ptw32pch.h"
39
40 /*
41 * Not needed yet, but defining it should indicate clashes with build target
42 * environment that should be fixed.
43 */
44 #include <signal.h>
45
46
47 int
48 pthread_detach (pthread_t thread)
49 /*
50 * ------------------------------------------------------
51 * DOCPUBLIC
52 * This function detaches the given thread.
53 *
54 * PARAMETERS
55 * thread
56 * an instance of a pthread_t
57 *
58 *
59 * DESCRIPTION
60 * This function detaches the given thread. You may use it to
61 * detach the main thread or to detach a joinable thread.
62 * NOTE: detached threads cannot be joined;
63 * storage is freed immediately on termination.
64 *
65 * RESULTS
66 * 0 successfully detached the thread,
67 * EINVAL thread is not a joinable thread,
68 * ENOSPC a required resource has been exhausted,
69 * ESRCH no thread could be found for 'thread',
70 *
71 * ------------------------------------------------------
72 */
73 {
74 int result;
75 BOOL destroyIt = PTW32_FALSE;
76 ptw32_thread_t * tp = (ptw32_thread_t *) thread.p;
77
78 EnterCriticalSection (&ptw32_thread_reuse_lock);
79
80 if (NULL == tp
81 || thread.x != tp->ptHandle.x)
82 {
83 result = ESRCH;
84 }
85 else if (PTHREAD_CREATE_DETACHED == tp->detachState)
86 {
87 result = EINVAL;
88 }
89 else
90 {
91 /*
92 * Joinable ptw32_thread_t structs are not scavenged until
93 * a join or detach is done. The thread may have exited already,
94 * but all of the state and locks etc are still there.
95 */
96 result = 0;
97
98 if (pthread_mutex_lock (&tp->cancelLock) == 0)
99 {
100 if (tp->state != PThreadStateLast)
101 {
102 tp->detachState = PTHREAD_CREATE_DETACHED;
103 }
104 else if (tp->detachState != PTHREAD_CREATE_DETACHED)
105 {
106 /*
107 * Thread is joinable and has exited or is exiting.
108 */
109 destroyIt = PTW32_TRUE;
110 }
111 (void) pthread_mutex_unlock (&tp->cancelLock);
112 }
113 else
114 {
115 /* cancelLock shouldn't fail, but if it does ... */
116 result = ESRCH;
117 }
118 }
119
120 LeaveCriticalSection (&ptw32_thread_reuse_lock);
121
122 if (result == 0)
123 {
124 /* Thread is joinable */
125
126 if (destroyIt)
127 {
128 /* The thread has exited or is exiting but has not been joined or
129 * detached. Need to wait in case it's still exiting.
130 */
131 (void) WaitForSingleObject(tp->threadH, INFINITE);
132 ptw32_threadDestroy (thread);
133 }
134 }
135
136 return (result);
137
138 } /* pthread_detach */

  ViewVC Help
Powered by ViewVC 1.1.22