2 * -------------------------------------------------------------
4 * Module: sem_timedwait.c
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"
52 } sem_timedwait_cleanup_args_t
;
55 static void PTW32_CDECL
56 ptw32_sem_timedwait_cleanup (void * args
)
58 sem_timedwait_cleanup_args_t
* a
= (sem_timedwait_cleanup_args_t
*)args
;
61 if (pthread_mutex_lock (&s
->lock
) == 0)
64 * We either timed out or were cancelled.
65 * If someone has posted between then and now we try to take the semaphore.
66 * Otherwise the semaphore count may be wrong after we
67 * return. In the case of a cancellation, it is as if we
68 * were cancelled just before we return (after taking the semaphore)
71 if (WaitForSingleObject(s
->sem
, 0) == WAIT_OBJECT_0
)
73 /* We got the semaphore on the second attempt */
78 /* Indicate we're no longer waiting */
87 * Don't release the W32 sema, it doesn't need adjustment
88 * because it doesn't record the number of waiters.
92 (void) pthread_mutex_unlock (&s
->lock
);
98 sem_timedwait (sem_t
* sem
, const struct timespec
*abstime
)
100 * ------------------------------------------------------
102 * This function waits on a semaphore possibly until
107 * pointer to an instance of sem_t
110 * pointer to an instance of struct timespec
113 * This function waits on a semaphore. If the
114 * semaphore value is greater than zero, it decreases
115 * its value by one. If the semaphore value is zero, then
116 * the calling thread (or process) is blocked until it can
117 * successfully decrease the value or until interrupted by
120 * If 'abstime' is a NULL pointer then this function will
121 * block until it can successfully decrease the value or
122 * until interrupted by a signal.
125 * 0 successfully decreased semaphore,
126 * -1 failed, error in errno
128 * EINVAL 'sem' is not a valid semaphore,
129 * ENOSYS semaphores are not supported,
130 * EINTR the function was interrupted by a signal,
131 * EDEADLK a deadlock condition was detected.
132 * ETIMEDOUT abstime elapsed before success.
134 * ------------------------------------------------------
140 pthread_testcancel();
152 milliseconds
= INFINITE
;
157 * Calculate timeout as milliseconds from current system time.
159 milliseconds
= ptw32_relmillisecs (abstime
);
162 if ((result
= pthread_mutex_lock (&s
->lock
)) == 0)
170 (void) pthread_mutex_unlock (&s
->lock
);
176 (void) pthread_mutex_unlock (&s
->lock
);
180 #if defined(NEED_SEM)
183 sem_timedwait_cleanup_args_t cleanup_args
;
185 cleanup_args
.sem
= s
;
186 cleanup_args
.resultPtr
= &result
;
188 #if defined(_MSC_VER) && _MSC_VER < 1400
189 #pragma inline_depth(0)
192 pthread_cleanup_push(ptw32_sem_timedwait_cleanup
, (void *) &cleanup_args
);
193 #if defined(NEED_SEM)
196 result
= pthreadCancelableTimedWait (s
->sem
, milliseconds
);
197 pthread_cleanup_pop(result
);
198 #if defined(_MSC_VER) && _MSC_VER < 1400
199 #pragma inline_depth()
202 #if defined(NEED_SEM)
204 if (!timedout
&& pthread_mutex_lock (&s
->lock
) == 0)
208 (void) pthread_mutex_unlock (&s
->lock
);
213 if (s
->leftToUnblock
> 0)
218 (void) pthread_mutex_unlock (&s
->lock
);
221 #endif /* NEED_SEM */
238 } /* sem_timedwait */