2012-11-15 00:39:56 +01:00
|
|
|
#pragma once
|
|
|
|
#include "Array.h"
|
2013-07-12 14:42:17 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
class ThreadExec;
|
|
|
|
|
|
|
|
class ThreadBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
wxString m_name;
|
|
|
|
bool m_detached;
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
public:
|
|
|
|
wxMutex m_main_mutex;
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
protected:
|
2013-06-30 10:46:29 +02:00
|
|
|
ThreadBase(bool detached = true, const wxString& name = "Unknown ThreadBase");
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadExec* m_executor;
|
|
|
|
|
|
|
|
virtual void Task()=0;
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
virtual void Start();
|
|
|
|
virtual void Resume();
|
|
|
|
virtual void Pause();
|
|
|
|
virtual void Stop(bool wait = true);
|
|
|
|
|
|
|
|
virtual bool Wait() const;
|
|
|
|
virtual bool IsRunning() const;
|
|
|
|
virtual bool IsPaused() const;
|
|
|
|
virtual bool IsAlive() const;
|
|
|
|
virtual bool TestDestroy() const;
|
|
|
|
virtual wxString GetThreadName() const;
|
|
|
|
virtual void SetThreadName(const wxString& name);
|
2012-11-15 00:39:56 +01:00
|
|
|
};
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
ThreadBase* GetCurrentNamedThread();
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
class ThreadExec : public wxThread
|
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
wxCriticalSection m_wait_for_exit;
|
2012-11-15 00:39:56 +01:00
|
|
|
volatile bool m_alive;
|
|
|
|
|
|
|
|
public:
|
2013-06-30 10:46:29 +02:00
|
|
|
ThreadBase* m_parent;
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
ThreadExec(bool detached, ThreadBase* parent)
|
|
|
|
: wxThread(detached ? wxTHREAD_DETACHED : wxTHREAD_JOINABLE)
|
|
|
|
, m_parent(parent)
|
|
|
|
, m_alive(true)
|
|
|
|
{
|
|
|
|
Create();
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stop(bool wait = true)
|
|
|
|
{
|
|
|
|
if(!m_alive) return;
|
|
|
|
|
|
|
|
m_parent = nullptr;
|
2013-06-30 10:46:29 +02:00
|
|
|
|
|
|
|
//if(wait)
|
|
|
|
{
|
|
|
|
Delete();
|
|
|
|
//wxCriticalSectionLocker lock(m_wait_for_exit);
|
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ExitCode Entry()
|
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
//wxCriticalSectionLocker lock(m_wait_for_exit);
|
2012-11-15 00:39:56 +01:00
|
|
|
m_parent->Task();
|
|
|
|
m_alive = false;
|
|
|
|
if(m_parent) m_parent->m_executor = nullptr;
|
|
|
|
return (ExitCode)0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
//ThreadBase* GetCurrentThread();
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
template<typename T> class MTPacketBuffer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
volatile bool m_busy;
|
|
|
|
volatile u32 m_put, m_get;
|
|
|
|
Array<u8> m_buffer;
|
|
|
|
u32 m_max_buffer_size;
|
2013-06-30 10:46:29 +02:00
|
|
|
mutable wxCriticalSection m_cs_main;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
void CheckBusy()
|
|
|
|
{
|
|
|
|
m_busy = m_put >= m_max_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
MTPacketBuffer(u32 max_buffer_size)
|
|
|
|
: m_max_buffer_size(max_buffer_size)
|
|
|
|
{
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
~MTPacketBuffer()
|
|
|
|
{
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flush()
|
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
wxCriticalSectionLocker lock(m_cs_main);
|
2012-11-15 00:39:56 +01:00
|
|
|
m_put = m_get = 0;
|
|
|
|
m_buffer.Clear();
|
|
|
|
m_busy = false;
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
private:
|
|
|
|
virtual void _push(const T& v) = 0;
|
|
|
|
virtual T _pop() = 0;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
public:
|
|
|
|
void Push(const T& v)
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_cs_main);
|
|
|
|
_push(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
T Pop()
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker lock(m_cs_main);
|
|
|
|
return _pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNewPacket() const { wxCriticalSectionLocker lock(m_cs_main); return m_put != m_get; }
|
2012-11-15 00:39:56 +01:00
|
|
|
bool IsBusy() const { return m_busy; }
|
|
|
|
};
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
static __forceinline bool SemaphorePostAndWait(wxSemaphore& sem)
|
|
|
|
{
|
|
|
|
if(sem.TryWait() != wxSEMA_BUSY) return false;
|
|
|
|
|
|
|
|
sem.Post();
|
|
|
|
sem.Wait();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
/*
|
|
|
|
class StepThread : public ThreadBase
|
|
|
|
{
|
|
|
|
wxSemaphore m_main_sem;
|
|
|
|
wxSemaphore m_destroy_sem;
|
|
|
|
volatile bool m_exit;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StepThread(const wxString& name = "Unknown StepThread")
|
|
|
|
: ThreadBase(true, name)
|
|
|
|
, m_exit(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~StepThread() throw()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void Task()
|
|
|
|
{
|
|
|
|
m_exit = false;
|
|
|
|
|
|
|
|
while(!TestDestroy())
|
|
|
|
{
|
|
|
|
m_main_sem.Wait();
|
|
|
|
|
|
|
|
if(TestDestroy() || m_exit) break;
|
|
|
|
|
|
|
|
Step();
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!TestDestroy()) Sleep(0);
|
|
|
|
if(m_destroy_sem.TryWait() != wxSEMA_NO_ERROR) m_destroy_sem.Post();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Step()=0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void DoStep()
|
|
|
|
{
|
|
|
|
if(IsRunning()) m_main_sem.Post();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForExit()
|
|
|
|
{
|
|
|
|
if(TestDestroy()) m_destroy_sem.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForNextStep()
|
|
|
|
{
|
|
|
|
if(!IsRunning()) return;
|
|
|
|
|
|
|
|
while(m_main_sem.TryWait() != wxSEMA_NO_ERROR) Sleep(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Exit(bool wait = false)
|
|
|
|
{
|
|
|
|
if(!IsAlive()) return;
|
|
|
|
|
|
|
|
if(m_main_sem.TryWait() != wxSEMA_NO_ERROR)
|
|
|
|
{
|
|
|
|
m_exit = true;
|
|
|
|
m_main_sem.Post();
|
|
|
|
}
|
|
|
|
|
|
|
|
Delete();
|
|
|
|
|
|
|
|
if(wait) WaitForExit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|