From 677bfb0881c56dad767a07b31ac543db284e16c6 Mon Sep 17 00:00:00 2001 From: rpj Date: Thu, 14 Jan 1999 22:33:54 +0000 Subject: Fri Jan 15 15:41:28 1999 Ross Johnson * pthread.h: Add new 'abstime' arg to pthreadCancelableWait() prototype. * condvar.c (cond_timedwait): New generalised function called by both pthread_cond_wait() and pthread_cond_timedwait(). This is essentially pthread_cond_wait() renamed and modified to add the 'abstime' arg and call the new _pthread_sem_timedwait() instead of sem_wait(). (pthread_cond_wait): Now just calls the internal static function cond_timedwait() with an INFINITE wait. (pthread_cond_timedwait): Now implemented. Calls the internal static function cond_timedwait(). * implement.h (_pthread_sem_timedwait): New internal function prototype. * misc.c (pthreadCancelableWait): Added new 'abstime' argument to allow shorter than INFINITE wait. * semaphore.c (_pthread_sem_timedwait): New function for internal use. This is essentially sem_wait() modified to add the 'abstime' arg and call the modified (see above) pthreadCancelableWait(). --- semaphore.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'semaphore.c') diff --git a/semaphore.c b/semaphore.c index 8ab326b..fc24b1a 100644 --- a/semaphore.c +++ b/semaphore.c @@ -203,7 +203,7 @@ sem_wait (sem_t * sem) * a signal. * * RESULTS - * 0 successfully destroyed semaphore, + * 0 successfully decreased semaphore, * EINVAL 'sem' is not a valid semaphore, * ENOSYS semaphores are not supported, * EINTR the function was interrupted by a signal, @@ -221,6 +221,65 @@ sem_wait (sem_t * sem) } /* sem_wait */ +int +_pthread_sem_timedwait (sem_t * sem, const struct timespec * abstime) + /* + * ------------------------------------------------------ + * DOCPUBLIC + * This function waits on a semaphore for at most + * 'abstime'. + * + * PARAMETERS + * sem + * pointer to an instance of sem_t + * + * abstime + * pointer to an instance of struct timespec + * + * DESCRIPTION + * This function waits on a semaphore. If the + * semaphore value is greater than zero, it decreases + * its value by one. If the semaphore value is zero, then + * the calling thread (or process) is blocked until it can + * successfully decrease the value or until interrupted by + * a signal. + * + * If 'abstime' is a NULL pointer then this function will + * block until it can successfully decrease the value or + * until interrupted by a signal. + * + * RESULTS + * 0 successfully decreased semaphore, + * EINVAL 'sem' is not a valid semaphore, + * ENOSYS semaphores are not supported, + * EINTR the function was interrupted by a signal, + * EDEADLK a deadlock condition was detected. + * ETIMEDOUT abstime elapsed before success. + * + * ------------------------------------------------------ + */ +{ + DWORD msecs; + + if (abstime == NULL) + { + msecs = INFINITE; + } + else + { + /* Calculate the number of milliseconds in abstime. */ + msecs = abstime->tv_sec * 1000; + msecs += abstime->tv_nsec / 1000000; + } + + return ((sem == NULL) + ? EINVAL + : pthreadCancelableWait (*sem, msecs) + ); + +} /* sem_wait */ + + int sem_post (sem_t * sem) /* -- cgit v1.2.3