diff --git a/src/Nazara/Core/Posix/ConditionVariableImpl.cpp b/src/Nazara/Core/Posix/ConditionVariableImpl.cpp new file mode 100644 index 000000000..563ff8a1b --- /dev/null +++ b/src/Nazara/Core/Posix/ConditionVariableImpl.cpp @@ -0,0 +1,52 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +// Source: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html + +#include +#include +#include + +NzConditionVariableImpl::NzConditionVariableImpl() +{ + pthread_cond_init(&m_cv, nullptr); +} + +NzConditionVariableImpl::~NzConditionVariableImpl() +{ + pthread_cond_destroy(&m_cv); +} + +void NzConditionVariableImpl::Signal() +{ + pthread_cond_signal(&m_cv); +} + +void NzConditionVariableImpl::SignalAll() +{ + pthread_cond_broadcast(&m_cv); +} + +void NzConditionVariableImpl::Wait(NzMutexImpl* mutex) +{ + pthread_cond_wait(&m_cv, mutex); +} + +bool NzConditionVariableImpl::Wait(NzMutexImpl* mutex, nzUInt32 timeout) +{ + + + // get the current time + timeval tv; + gettimeofday(&tv, NULL); + + // 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; + + pthread_cond_timedwait(&m_cv,mutex, &tv); + +} diff --git a/src/Nazara/Core/Posix/ConditionVariableImpl.hpp b/src/Nazara/Core/Posix/ConditionVariableImpl.hpp new file mode 100644 index 000000000..25eb2bc92 --- /dev/null +++ b/src/Nazara/Core/Posix/ConditionVariableImpl.hpp @@ -0,0 +1,37 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html + +#pragma once + +#ifndef NAZARA_CONDITIONVARIABLEIMPL_HPP +#define NAZARA_CONDITIONVARIABLEIMPL_HPP + +#include +#include +#include +#include + +class NzMutexImpl; + +class NzConditionVariableImpl +{ + public: + NzConditionVariableImpl(); + ~NzConditionVariableImpl(); + + + void Signal(); + void SignalAll(); + + void Wait(NzMutexImpl* mutex); + bool Wait(NzMutexImpl* mutex, nzUInt32 timeout); + + private: + + pthread_cond_t m_cv; +}; + +#endif // NAZARA_CONDITIONVARIABLEIMPL_HPP