13
0

Add mechanism to reset/lock semaphores

This commit is contained in:
Robin Gareus 2017-04-22 23:00:53 +02:00
parent 629e6cbefb
commit 9c0ba3b3d5
2 changed files with 14 additions and 0 deletions

View File

@ -55,10 +55,12 @@ class LIBPBD_API Semaphore {
int signal ();
int wait ();
int reset ();
#else
int signal () { return sem_post (ptr_to_sem()); }
int wait () { return sem_wait (ptr_to_sem()); }
int reset () { int rv = 0 ; while (sem_trywait (ptr_to_sem()) == 0) ++rv; return rv; }
#endif
};

View File

@ -74,4 +74,16 @@ Semaphore::wait ()
return (result == WAIT_OBJECT_0);
}
int
Semaphore::reset ()
{
int rv = -1;
DWORD result;
do {
++rv;
result = WaitForSingleObject(_sem, 0);
} while (result == WAIT_OBJECT_0);
return rv;
}
#endif