Files
NazaraEngine/src/Nazara/Core/Posix/ConditionVariableImpl.cpp
Youri Hubaut 37586e7283 Switch from Nz prefix to namespace Nz for linux
Former-commit-id: 64eeaf3c633254b04910ebd4576fd9e910002be0
2015-09-27 15:58:49 +02:00

51 lines
1.2 KiB
C++

// Copyright (C) 2015 Alexandre Janniaux
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/ConditionVariableImpl.hpp>
#include <Nazara/Core/Posix/MutexImpl.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
ConditionVariableImpl::ConditionVariableImpl()
{
pthread_cond_init(&m_cv, nullptr);
}
ConditionVariableImpl::~ConditionVariableImpl()
{
pthread_cond_destroy(&m_cv);
}
void ConditionVariableImpl::Signal()
{
pthread_cond_signal(&m_cv);
}
void ConditionVariableImpl::SignalAll()
{
pthread_cond_broadcast(&m_cv);
}
void ConditionVariableImpl::Wait(MutexImpl* mutex)
{
pthread_cond_wait(&m_cv, &mutex->m_handle);
}
bool ConditionVariableImpl::Wait(MutexImpl* mutex, UInt32 timeout)
{
// get the current time
timeval tv;
gettimeofday(&tv, nullptr);
// construct the time limit (current time + time to wait)
timespec ti;
ti.tv_nsec = (tv.tv_usec + (timeout % 1000)) * 1000000;
ti.tv_sec = tv.tv_sec + (timeout / 1000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec %= 1000000000;
return pthread_cond_timedwait(&m_cv,&mutex->m_handle, &ti) != 0;
}
}