mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-22 17:18:39 +00:00
Make MutexQueue use jsemaphore for signaling
This commit is contained in:
parent
10fdbf7375
commit
8b0b857eaa
13 changed files with 248 additions and 99 deletions
|
@ -36,6 +36,7 @@ public:
|
|||
|
||||
void Post();
|
||||
void Wait();
|
||||
bool Wait(unsigned int time_ms);
|
||||
|
||||
int GetValue();
|
||||
|
||||
|
|
|
@ -17,8 +17,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include "jthread/jsemaphore.h"
|
||||
|
||||
#define UNUSED(expr) do { (void)(expr); } while (0)
|
||||
|
||||
JSemaphore::JSemaphore() {
|
||||
int sem_init_retval = sem_init(&m_semaphore,0,0);
|
||||
assert(sem_init_retval == 0);
|
||||
|
@ -49,6 +53,33 @@ void JSemaphore::Wait() {
|
|||
UNUSED(sem_wait_retval);
|
||||
}
|
||||
|
||||
bool JSemaphore::Wait(unsigned int time_ms) {
|
||||
struct timespec waittime;
|
||||
struct timeval now;
|
||||
|
||||
if (gettimeofday(&now, NULL) == -1) {
|
||||
assert("Unable to get time by clock_gettime!" == 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
|
||||
waittime.tv_sec = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
|
||||
waittime.tv_nsec %= 1000*1000*1000;
|
||||
|
||||
errno = 0;
|
||||
int sem_wait_retval = sem_timedwait(&m_semaphore,&waittime);
|
||||
|
||||
if (sem_wait_retval == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
assert((errno == ETIMEDOUT) || (errno == EINTR));
|
||||
return false;
|
||||
}
|
||||
return sem_wait_retval == 0 ? true : false;
|
||||
}
|
||||
|
||||
int JSemaphore::GetValue() {
|
||||
|
||||
int retval = 0;
|
||||
|
|
|
@ -51,6 +51,21 @@ void JSemaphore::Wait() {
|
|||
INFINITE);
|
||||
}
|
||||
|
||||
bool JSemaphore::Wait(unsigned int time_ms) {
|
||||
unsigned int retval = WaitForSingleObject(
|
||||
m_hSemaphore,
|
||||
time_ms);
|
||||
|
||||
if (retval == WAIT_OBJECT_0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
assert(retval == WAIT_TIMEOUT);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int JSemaphore::GetValue() {
|
||||
|
||||
long int retval = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue