Add Posix condition variable support.

Former-commit-id: 2af214aa206f2f9486c96750ccd412ac672ef62d
This commit is contained in:
Alexandre Janniaux 2013-01-04 18:20:42 +01:00
parent 3677401319
commit 50870b9a2d
2 changed files with 89 additions and 0 deletions

View File

@ -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 <Nazara/Core/Posix/ConditionVariableImpl.hpp>
#include <Nazara/Core/Posix/MutexImpl.hpp>
#include <Nazara/Core/Debug.hpp>
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);
}

View File

@ -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 <Nazara/Prerequesites.hpp>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
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