2 * -------------------------------------------------------------
7 * Semaphores aren't actually part of the PThreads standard.
8 * They are defined by the POSIX Standard:
10 * POSIX 1003.1b-1993 (POSIX.1b)
12 * -------------------------------------------------------------
14 * --------------------------------------------------------------------------
16 * Pthreads-win32 - POSIX Threads Library for Win32
17 * Copyright(C) 1998 John E. Bossom
18 * Copyright(C) 1999,2005 Pthreads-win32 contributors
20 * Contact Email: rpj@callisto.canberra.edu.au
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
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.
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.
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
45 #include "semaphore.h"
46 #include "implement.h"
49 static void PTW32_CDECL
50 ptw32_sem_wait_cleanup(void * sem
)
52 sem_t s
= (sem_t
) sem
;
54 if (pthread_mutex_lock (&s
->lock
) == 0)
57 * If sema is destroyed do nothing, otherwise:-
58 * If the sema is posted between us being cancelled and us locking
59 * the sema again above then we need to consume that post but cancel
60 * anyway. If we don't get the semaphore we indicate that we're no
63 if (*((sem_t
*)sem
) != NULL
&& !(WaitForSingleObject(s
->sem
, 0) == WAIT_OBJECT_0
))
73 * Don't release the W32 sema, it doesn't need adjustment
74 * because it doesn't record the number of waiters.
78 (void) pthread_mutex_unlock (&s
->lock
);
83 sem_wait (sem_t
* sem
)
85 * ------------------------------------------------------
87 * This function waits on a semaphore.
91 * pointer to an instance of sem_t
94 * This function waits on a semaphore. If the
95 * semaphore value is greater than zero, it decreases
96 * its value by one. If the semaphore value is zero, then
97 * the calling thread (or process) is blocked until it can
98 * successfully decrease the value or until interrupted by
102 * 0 successfully decreased semaphore,
103 * -1 failed, error in errno
105 * EINVAL 'sem' is not a valid semaphore,
106 * ENOSYS semaphores are not supported,
107 * EINTR the function was interrupted by a signal,
108 * EDEADLK a deadlock condition was detected.
110 * ------------------------------------------------------
116 pthread_testcancel();
124 if ((result
= pthread_mutex_lock (&s
->lock
)) == 0)
132 (void) pthread_mutex_unlock (&s
->lock
);
138 (void) pthread_mutex_unlock (&s
->lock
);
142 #if defined(_MSC_VER) && _MSC_VER < 1400
143 #pragma inline_depth(0)
146 pthread_cleanup_push(ptw32_sem_wait_cleanup
, (void *) s
);
147 result
= pthreadCancelableWait (s
->sem
);
148 /* Cleanup if we're canceled or on any other error */
149 pthread_cleanup_pop(result
);
150 #if defined(_MSC_VER) && _MSC_VER < 1400
151 #pragma inline_depth()
154 #if defined(NEED_SEM)
156 if (!result
&& pthread_mutex_lock (&s
->lock
) == 0)
160 (void) pthread_mutex_unlock (&s
->lock
);
165 if (s
->leftToUnblock
> 0)
170 (void) pthread_mutex_unlock (&s
->lock
);
173 #endif /* NEED_SEM */