diff options
author | rpj <rpj> | 1999-02-02 02:01:56 +0000 |
---|---|---|
committer | rpj <rpj> | 1999-02-02 02:01:56 +0000 |
commit | cc29ad943903e9b8dba96cd978cb126f79f73e38 (patch) | |
tree | 17a310f2fb26c829acbecf498453531637bf863c /semaphore.c | |
parent | 6237335fec273ac2b00c32609a8eef91c6ec7473 (diff) |
Mods to compile under Mingw32 egcs.
Tue Feb 2 18:07:43 1999 Ross Johnson <rpj@swan.canberra.edu.au>
* implement.h: Add #include <pthread.h>.
Change sem_t to _pthread_sem_t.
Various patches by Kevin Ruland <Kevin.Ruland@anheuser-busch.com>
* signal.c (pthread_sigmask): Add and modify casts.
Reverse LHS/RHS bitwise assignments.
* pthread.h: Remove #include <semaphore.h>.
(_PTHREAD_ATTR_VALID): Add cast.
(struct pthread_t_): Add sigmask element.
* dll.c: Add "extern C" for DLLMain.
(DllMain): Add cast.
* create.c (pthread_create): Set sigmask in thread.
* condvar.c: Remove #include. Change sem_* to _pthread_sem_*.
* attr.c: Changed #include.
* Makefile.in: Additional targets and changes to build the library
as a DLL.
Fri Jan 29 11:56:28 1999 Ross Johnson <rpj@swan.canberra.edu.au>
* Makefile.in (OBJS): Add semaphore.o to list.
* semaphore.c (_pthread_sem_timedwait): Move from private.c.
Rename sem_* to _pthread_sem_*.
* pthread.h (pthread_cond_t): Change type of sem_t.
_POSIX_SEMAPHORES no longer defined.
* semaphore.h: Contents moved to implement.h.
Removed from source tree.
* implement.h: Add semaphore function prototypes and ename all
functions to prepend '_pthread_'. They are
now private to the pthreads-win32 implementation.
* private.c: Change #warning.
Move _pthread_sem_timedwait() to semaphore.c.
* cleanup.c: Change #warning.
* misc.c: Remove #include <errno.h>
* pthread.def: Cleanup CVS merge conflicts.
* global.c: Ditto.
* ChangeLog: Ditto.
* cleanup.c: Ditto.
Diffstat (limited to 'semaphore.c')
-rw-r--r-- | semaphore.c | 106 |
1 files changed, 90 insertions, 16 deletions
diff --git a/semaphore.c b/semaphore.c index 9645379..e682201 100644 --- a/semaphore.c +++ b/semaphore.c @@ -34,14 +34,16 @@ * * ------------------------------------------------------------- */ -#include <pthread.h> -#include <string.h> -#include "semaphore.h" +#include <sys/timeb.h> +#include <string.h> + +#include <pthread.h> +#include "implement.h" int -sem_init (sem_t * sem, int pshared, unsigned int value) +_pthread_sem_init (_pthread_sem_t * sem, int pshared, unsigned int value) /* * ------------------------------------------------------ * DOCPUBLIC @@ -50,7 +52,7 @@ sem_init (sem_t * sem, int pshared, unsigned int value) * * PARAMETERS * sem - * pointer to an instance of sem_t + * pointer to an instance of _pthread_sem_t * * pshared * if zero, this semaphore may only be shared between @@ -90,7 +92,7 @@ sem_init (sem_t * sem, int pshared, unsigned int value) { /* * NOTE: Taking advantage of the fact that - * sem_t is a simple structure with one entry; + * _pthread_sem_t is a simple structure with one entry; * We don't have to allocate it... */ *sem = CreateSemaphore ( @@ -111,7 +113,7 @@ sem_init (sem_t * sem, int pshared, unsigned int value) int -sem_destroy (sem_t * sem) +_pthread_sem_destroy (_pthread_sem_t * sem) /* * ------------------------------------------------------ * DOCPUBLIC @@ -119,7 +121,7 @@ sem_destroy (sem_t * sem) * * PARAMETERS * sem - * pointer to an instance of sem_t + * pointer to an instance of _pthread_sem_t * * DESCRIPTION * This function destroys an unnamed semaphore. @@ -144,7 +146,7 @@ sem_destroy (sem_t * sem) int -sem_trywait (sem_t * sem) +_pthread_sem_trywait (_pthread_sem_t * sem) /* * ------------------------------------------------------ * DOCPUBLIC @@ -152,7 +154,7 @@ sem_trywait (sem_t * sem) * * PARAMETERS * sem - * pointer to an instance of sem_t + * pointer to an instance of _pthread_sem_t * * DESCRIPTION * This function tries to wait on a semaphore. If the @@ -181,7 +183,7 @@ sem_trywait (sem_t * sem) int -sem_wait (sem_t * sem) +_pthread_sem_wait (_pthread_sem_t * sem) /* * ------------------------------------------------------ * DOCPUBLIC @@ -189,7 +191,7 @@ sem_wait (sem_t * sem) * * PARAMETERS * sem - * pointer to an instance of sem_t + * pointer to an instance of _pthread_sem_t * * DESCRIPTION * This function waits on a semaphore. If the @@ -219,7 +221,81 @@ sem_wait (sem_t * sem) int -sem_post (sem_t * sem) +_pthread_sem_timedwait (_pthread_sem_t * sem, const struct timespec * abstime) + /* + * ------------------------------------------------------ + * DOCPUBLIC + * This function waits on a semaphore possibly until + * 'abstime' time. + * + * PARAMETERS + * sem + * pointer to an instance of _pthread_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. + * + * ------------------------------------------------------ + */ +{ +#if defined(__MINGW32__) + struct timeb currSysTime; +#else + struct _timeb currSysTime; +#endif + const DWORD NANOSEC_PER_MILLISEC = 1000000; + const DWORD MILLISEC_PER_SEC = 1000; + DWORD milliseconds; + + if (abstime == NULL) + { + milliseconds = INFINITE; + } + else + { + /* + * Calculate timeout as milliseconds from current system time. + */ + + /* get current system time */ + _ftime(&currSysTime); + + /* subtract current system time from abstime */ + milliseconds = (abstime->tv_sec - currSysTime.time) * MILLISEC_PER_SEC; + milliseconds += (abstime->tv_nsec / NANOSEC_PER_MILLISEC) - + currSysTime.millitm; + } + + return ((sem == NULL) + ? EINVAL + : pthreadCancelableTimedWait (*sem, milliseconds) + ); + +} /* _pthread_sem_timedwait */ + + +int +_pthread_sem_post (_pthread_sem_t * sem) /* * ------------------------------------------------------ * DOCPUBLIC @@ -227,7 +303,7 @@ sem_post (sem_t * sem) * * PARAMETERS * sem - * pointer to an instance of sem_t + * pointer to an instance of _pthread_sem_t * * DESCRIPTION * This function posts a wakeup to a semaphore. If there @@ -249,5 +325,3 @@ sem_post (sem_t * sem) : EINVAL)); } /* sem_post */ - - |