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

Contents of /trunk/3rdparty/w32pthreads/pthread_mutex_timedlock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 7 months ago) by william
File MIME type: text/plain
File size: 4648 byte(s)
re-commit (had local access denied errors when committing)
1 /*
2 * pthread_mutex_timedlock.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 static INLINE int
41 ptw32_timed_eventwait (HANDLE event, const struct timespec *abstime)
42 /*
43 * ------------------------------------------------------
44 * DESCRIPTION
45 * This function waits on an event until signaled or until
46 * abstime passes.
47 * If abstime has passed when this routine is called then
48 * it returns a result to indicate this.
49 *
50 * If 'abstime' is a NULL pointer then this function will
51 * block until it can successfully decrease the value or
52 * until interrupted by a signal.
53 *
54 * This routine is not a cancelation point.
55 *
56 * RESULTS
57 * 0 successfully signaled,
58 * ETIMEDOUT abstime passed
59 * EINVAL 'event' is not a valid event,
60 *
61 * ------------------------------------------------------
62 */
63 {
64
65 DWORD milliseconds;
66 DWORD status;
67
68 if (event == NULL)
69 {
70 return EINVAL;
71 }
72 else
73 {
74 if (abstime == NULL)
75 {
76 milliseconds = INFINITE;
77 }
78 else
79 {
80 /*
81 * Calculate timeout as milliseconds from current system time.
82 */
83 milliseconds = ptw32_relmillisecs (abstime);
84 }
85
86 status = WaitForSingleObject (event, milliseconds);
87
88 if (status == WAIT_OBJECT_0)
89 {
90 return 0;
91 }
92 else if (status == WAIT_TIMEOUT)
93 {
94 return ETIMEDOUT;
95 }
96 else
97 {
98 return EINVAL;
99 }
100 }
101
102 return 0;
103
104 } /* ptw32_timed_semwait */
105
106
107 int
108 pthread_mutex_timedlock (pthread_mutex_t * mutex,
109 const struct timespec *abstime)
110 {
111 int result;
112 pthread_mutex_t mx;
113
114 /*
115 * Let the system deal with invalid pointers.
116 */
117
118 /*
119 * We do a quick check to see if we need to do more work
120 * to initialise a static mutex. We check
121 * again inside the guarded section of ptw32_mutex_check_need_init()
122 * to avoid race conditions.
123 */
124 if (ptw32_static_mutex_enable && (*mutex >= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER))
125 {
126 if ((result = ptw32_mutex_check_need_init (mutex)) != 0)
127 {
128 return (result);
129 }
130 }
131
132 mx = *mutex;
133
134 if (mx->kind == PTHREAD_MUTEX_NORMAL)
135 {
136 if (_InterlockedExchange(&mx->lock_idx, 1) != 0)
137 {
138 while (_InterlockedExchange(&mx->lock_idx, -1) != 0)
139 {
140 if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
141 {
142 return result;
143 }
144 }
145 }
146 }
147 else
148 {
149 pthread_t self = pthread_self();
150
151 if (_InterlockedCompareExchange(&mx->lock_idx, 1, 0) == 0)
152 {
153 mx->recursive_count = 1;
154 mx->ownerThread = self;
155 }
156 else
157 {
158 if (pthread_equal (mx->ownerThread, self))
159 {
160 if (mx->kind == PTHREAD_MUTEX_RECURSIVE)
161 {
162 mx->recursive_count++;
163 }
164 else
165 {
166 return EDEADLK;
167 }
168 }
169 else
170 {
171 while (_InterlockedExchange(&mx->lock_idx, -1) != 0)
172 {
173 if (0 != (result = ptw32_timed_eventwait (mx->event, abstime)))
174 {
175 return result;
176 }
177 }
178
179 mx->recursive_count = 1;
180 mx->ownerThread = self;
181 }
182 }
183 }
184
185 return 0;
186 }

  ViewVC Help
Powered by ViewVC 1.1.22