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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 5 months ago) by william
File MIME type: text/plain
File size: 5185 byte(s)
exported r3113 from ./upstream/trunk
1 /*
2 * -------------------------------------------------------------
3 *
4 * Module: sem_wait.c
5 *
6 * Purpose:
7 * Semaphores aren't actually part of the PThreads standard.
8 * They are defined by the POSIX Standard:
9 *
10 * POSIX 1003.1b-1993 (POSIX.1b)
11 *
12 * -------------------------------------------------------------
13 *
14 * --------------------------------------------------------------------------
15 *
16 * Pthreads-win32 - POSIX Threads Library for Win32
17 * Copyright(C) 1998 John E. Bossom
18 * Copyright(C) 1999,2005 Pthreads-win32 contributors
19 *
20 * Contact Email: rpj@callisto.canberra.edu.au
21 *
22 * The current list of contributors is contained
23 * in the file CONTRIBUTORS included with the source
24 * code distribution. The list can also be seen at the
25 * following World Wide Web location:
26 * http://sources.redhat.com/pthreads-win32/contributors.html
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
37 *
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library in the file COPYING.LIB;
40 * if not, write to the Free Software Foundation, Inc.,
41 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42 */
43
44 #include "ptw32pch.h"
45 #include "semaphore.h"
46
47
48 static void PTW32_CDECL
49 ptw32_sem_wait_cleanup(void * sem)
50 {
51 sem_t s = (sem_t) sem;
52
53 if (pthread_mutex_lock (&s->lock) == 0)
54 {
55 /*
56 * If sema is destroyed do nothing, otherwise:-
57 * If the sema is posted between us being cancelled and us locking
58 * the sema again above then we need to consume that post but cancel
59 * anyway. If we don't get the semaphore we indicate that we're no
60 * longer waiting.
61 */
62 if (*((sem_t *)sem) != NULL && !(WaitForSingleObject(s->sem, 0) == WAIT_OBJECT_0))
63 {
64 ++s->value;
65 #ifdef NEED_SEM
66 if (s->value > 0)
67 {
68 s->leftToUnblock = 0;
69 }
70 #else
71 /*
72 * Don't release the W32 sema, it doesn't need adjustment
73 * because it doesn't record the number of waiters.
74 */
75 #endif /* NEED_SEM */
76 }
77 (void) pthread_mutex_unlock (&s->lock);
78 }
79 }
80
81 INLINE int
82 sem_wait (sem_t * sem)
83 /*
84 * ------------------------------------------------------
85 * DOCPUBLIC
86 * This function waits on a semaphore.
87 *
88 * PARAMETERS
89 * sem
90 * pointer to an instance of sem_t
91 *
92 * DESCRIPTION
93 * This function waits on a semaphore. If the
94 * semaphore value is greater than zero, it decreases
95 * its value by one. If the semaphore value is zero, then
96 * the calling thread (or process) is blocked until it can
97 * successfully decrease the value or until interrupted by
98 * a signal.
99 *
100 * RESULTS
101 * 0 successfully decreased semaphore,
102 * -1 failed, error in errno
103 * ERRNO
104 * EINVAL 'sem' is not a valid semaphore,
105 * ENOSYS semaphores are not supported,
106 * EINTR the function was interrupted by a signal,
107 * EDEADLK a deadlock condition was detected.
108 *
109 * ------------------------------------------------------
110 */
111 {
112 int result = 0;
113 sem_t s = *sem;
114
115 pthread_testcancel();
116
117 if (s == NULL)
118 {
119 result = EINVAL;
120 }
121 else
122 {
123 if ((result = pthread_mutex_lock (&s->lock)) == 0)
124 {
125 int v;
126
127 /* See sem_destroy.c
128 */
129 if (*sem == NULL)
130 {
131 (void) pthread_mutex_unlock (&s->lock);
132 errno = EINVAL;
133 return -1;
134 }
135
136 v = --s->value;
137 (void) pthread_mutex_unlock (&s->lock);
138
139 if (v < 0)
140 {
141 /* Must wait */
142 pthread_cleanup_push(ptw32_sem_wait_cleanup, (void *) s);
143 result = pthreadCancelableWait (s->sem);
144 /* Cleanup if we're canceled or on any other error */
145 pthread_cleanup_pop(result);
146 }
147 #ifdef NEED_SEM
148
149 if (!result && pthread_mutex_lock (&s->lock) == 0)
150 {
151 if (*sem == NULL)
152 {
153 (void) pthread_mutex_unlock (&s->lock);
154 errno = EINVAL;
155 return -1;
156 }
157
158 if (s->leftToUnblock > 0)
159 {
160 --s->leftToUnblock;
161 SetEvent(s->sem);
162 }
163 (void) pthread_mutex_unlock (&s->lock);
164 }
165
166 #endif /* NEED_SEM */
167
168 }
169
170 }
171
172 if (result != 0)
173 {
174 errno = result;
175 return -1;
176 }
177
178 return 0;
179
180 } /* sem_wait */

  ViewVC Help
Powered by ViewVC 1.1.22