mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-09-23 23:00:42 +00:00
Move some C++11 types from Android to AerofoilSDL.
This commit is contained in:
@@ -21,6 +21,7 @@ LOCAL_SRC_FILES := \
|
||||
GpDisplayDriver_SDL_GL2.cpp \
|
||||
GpFiber_SDL.cpp \
|
||||
GpFiberStarter_SDL.cpp \
|
||||
GpThreadEvent_Cpp11.cpp \
|
||||
ShaderCode/CopyQuadP.cpp \
|
||||
ShaderCode/DrawQuadPaletteP.cpp \
|
||||
ShaderCode/DrawQuad32P.cpp \
|
||||
|
66
AerofoilSDL/GpMutex_Cpp11.h
Normal file
66
AerofoilSDL/GpMutex_Cpp11.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "IGpMutex.h"
|
||||
#include <mutex>
|
||||
|
||||
template<class TMutex>
|
||||
class GpMutex_Cpp11 final : public IGpMutex
|
||||
{
|
||||
public:
|
||||
~GpMutex_Cpp11();
|
||||
|
||||
void Destroy() override;
|
||||
static GpMutex_Cpp11<TMutex> *Create();
|
||||
|
||||
void Lock() override;
|
||||
void Unlock() override;
|
||||
|
||||
private:
|
||||
GpMutex_Cpp11();
|
||||
|
||||
TMutex m_mutex;
|
||||
};
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
template<class TMutex>
|
||||
GpMutex_Cpp11<TMutex>::GpMutex_Cpp11()
|
||||
{
|
||||
}
|
||||
|
||||
template<class TMutex>
|
||||
GpMutex_Cpp11<TMutex>::~GpMutex_Cpp11()
|
||||
{
|
||||
}
|
||||
|
||||
template<class TMutex>
|
||||
void GpMutex_Cpp11<TMutex>::Destroy()
|
||||
{
|
||||
this->~GpMutex_Cpp11();
|
||||
free(this);
|
||||
}
|
||||
|
||||
template<class TMutex>
|
||||
GpMutex_Cpp11<TMutex> *GpMutex_Cpp11<TMutex>::Create()
|
||||
{
|
||||
GpMutex_Cpp11<TMutex> *mutex = static_cast<GpMutex_Cpp11<TMutex>*>(malloc(sizeof(GpMutex_Cpp11<TMutex>)));
|
||||
if (!mutex)
|
||||
return nullptr;
|
||||
|
||||
return new (mutex) GpMutex_Cpp11<TMutex>();
|
||||
}
|
||||
|
||||
template<class TMutex>
|
||||
void GpMutex_Cpp11<TMutex>::Lock()
|
||||
{
|
||||
m_mutex.lock();
|
||||
}
|
||||
|
||||
template<class TMutex>
|
||||
void GpMutex_Cpp11<TMutex>::Unlock()
|
||||
{
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
typedef GpMutex_Cpp11<std::mutex> GpMutex_Cpp11_NonRecursive;
|
||||
typedef GpMutex_Cpp11<std::recursive_mutex> GpMutex_Cpp11_Recursive;
|
82
AerofoilSDL/GpThreadEvent_Cpp11.cpp
Normal file
82
AerofoilSDL/GpThreadEvent_Cpp11.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "GpThreadEvent_Cpp11.h"
|
||||
|
||||
GpThreadEvent_Cpp11::GpThreadEvent_Cpp11(bool autoReset, bool startSignaled)
|
||||
: m_flag(startSignaled)
|
||||
, m_autoReset(autoReset)
|
||||
{
|
||||
}
|
||||
|
||||
GpThreadEvent_Cpp11::~GpThreadEvent_Cpp11()
|
||||
{
|
||||
}
|
||||
|
||||
void GpThreadEvent_Cpp11::Wait()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
if (m_autoReset)
|
||||
{
|
||||
m_cvar.wait(lock,[&]()->bool{
|
||||
if (m_flag)
|
||||
{
|
||||
m_flag = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
});
|
||||
}
|
||||
else
|
||||
m_cvar.wait(lock,[&]()->bool{ return m_flag; });
|
||||
}
|
||||
|
||||
bool GpThreadEvent_Cpp11::WaitTimed(uint32_t msec)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
if (m_autoReset)
|
||||
{
|
||||
if (!m_cvar.wait_for(lock, std::chrono::milliseconds(msec), [&]()->bool{
|
||||
if (m_flag)
|
||||
{
|
||||
m_flag = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_cvar.wait_for(lock, std::chrono::milliseconds(msec), [&]()->bool{ return m_flag; }))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void GpThreadEvent_Cpp11::Signal()
|
||||
{
|
||||
m_mutex.lock();
|
||||
m_flag = true;
|
||||
m_mutex.unlock();
|
||||
if (m_autoReset)
|
||||
m_cvar.notify_one();
|
||||
else
|
||||
m_cvar.notify_all();
|
||||
}
|
||||
|
||||
void GpThreadEvent_Cpp11::Destroy()
|
||||
{
|
||||
this->~GpThreadEvent_Cpp11();
|
||||
free(this);
|
||||
}
|
||||
|
||||
|
||||
GpThreadEvent_Cpp11 *GpThreadEvent_Cpp11::Create(bool autoReset, bool startSignaled)
|
||||
{
|
||||
GpThreadEvent_Cpp11 *evt = static_cast<GpThreadEvent_Cpp11*>(malloc(sizeof(GpThreadEvent_Cpp11)));
|
||||
if (!evt)
|
||||
return nullptr;
|
||||
|
||||
return new (evt) GpThreadEvent_Cpp11(autoReset, startSignaled);
|
||||
}
|
||||
|
28
AerofoilSDL/GpThreadEvent_Cpp11.h
Normal file
28
AerofoilSDL/GpThreadEvent_Cpp11.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "IGpThreadEvent.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
class GpThreadEvent_Cpp11 final : public IGpThreadEvent
|
||||
{
|
||||
public:
|
||||
~GpThreadEvent_Cpp11();
|
||||
|
||||
void Wait() override;
|
||||
bool WaitTimed(uint32_t msec) override;
|
||||
void Signal() override;
|
||||
void Destroy() override;
|
||||
|
||||
static GpThreadEvent_Cpp11 *Create(bool autoReset, bool startSignaled);
|
||||
|
||||
private:
|
||||
GpThreadEvent_Cpp11(bool autoReset, bool startSignaled);
|
||||
GpThreadEvent_Cpp11() = delete;
|
||||
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_cvar;
|
||||
bool m_flag;
|
||||
bool m_autoReset;
|
||||
};
|
Reference in New Issue
Block a user