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

Contents of /trunk/3rdparty/w32pthreads/pthread_mutex_destroy.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: 3998 byte(s)
committing r3113 initial commit again...
1 /*
2 * pthread_mutex_destroy.c
3 *
4 * Description:
5 * This translation unit implements mutual exclusion (mutex) primitives.
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 int
41 pthread_mutex_destroy (pthread_mutex_t * mutex)
42 {
43 int result = 0;
44 pthread_mutex_t mx;
45
46 /*
47 * Let the system deal with invalid pointers.
48 */
49
50 /*
51 * Check to see if we have something to delete.
52 */
53 if (!ptw32_static_mutex_enable || (*mutex < PTHREAD_ERRORCHECK_MUTEX_INITIALIZER))
54 {
55 mx = *mutex;
56
57 result = pthread_mutex_trylock (&mx);
58
59 /*
60 * If trylock succeeded and the mutex is not recursively locked it
61 * can be destroyed.
62 */
63 if (result == 0)
64 {
65 if (mx->kind != PTHREAD_MUTEX_RECURSIVE || 1 == mx->recursive_count)
66 {
67 /*
68 * FIXME!!!
69 * The mutex isn't held by another thread but we could still
70 * be too late invalidating the mutex below since another thread
71 * may already have entered mutex_lock and the check for a valid
72 * *mutex != NULL.
73 *
74 * Note that this would be an unusual situation because it is not
75 * common that mutexes are destroyed while they are still in
76 * use by other threads.
77 */
78 *mutex = NULL;
79
80 result = pthread_mutex_unlock (&mx);
81
82 if (result == 0)
83 {
84 if (!CloseHandle (mx->event))
85 {
86 *mutex = mx;
87 result = EINVAL;
88 }
89 else
90 {
91 free (mx);
92 }
93 }
94 else
95 {
96 /*
97 * Restore the mutex before we return the error.
98 */
99 *mutex = mx;
100 }
101 }
102 else /* mx->recursive_count > 1 */
103 {
104 /*
105 * The mutex must be recursive and already locked by us (this thread).
106 */
107 mx->recursive_count--; /* Undo effect of pthread_mutex_trylock() above */
108 result = EBUSY;
109 }
110 }
111 }
112 else
113 {
114 /*
115 * See notes in ptw32_mutex_check_need_init() above also.
116 */
117 EnterCriticalSection (&ptw32_mutex_test_init_lock);
118
119 /*
120 * Check again.
121 */
122 if (ptw32_static_mutex_enable && (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER))
123 {
124 /*
125 * This is all we need to do to destroy a statically
126 * initialised mutex that has not yet been used (initialised).
127 * If we get to here, another thread
128 * waiting to initialise this mutex will get an EINVAL.
129 */
130 *mutex = NULL;
131 }
132 else
133 {
134 /*
135 * The mutex has been initialised while we were waiting
136 * so assume it's in use.
137 */
138 result = EBUSY;
139 }
140
141 LeaveCriticalSection (&ptw32_mutex_test_init_lock);
142 }
143
144 return (result);
145 }

  ViewVC Help
Powered by ViewVC 1.1.22