splited ASyncEvent
This commit is contained in:
parent
5a88a6072c
commit
d3608bee15
@ -125,6 +125,11 @@ namespace hgl
|
||||
{4,{0x00,0x00,0xFE,0xFF},ByteOrderMask::UTF32BE,&utf32be_charset ,(uint16)CharCodePage::UTF32BE }
|
||||
};
|
||||
|
||||
inline const BOMFileHeader *GetBOM(const ByteOrderMask &bom)
|
||||
{
|
||||
return RangeCheck(bom)?BOMData+uint(bom)-uint(ByteOrderMask::BEGIN_RANGE):nullptr;
|
||||
}
|
||||
|
||||
inline ByteOrderMask CheckBOM(const void *data)
|
||||
{
|
||||
const BOMFileHeader *bom=BOMData;
|
||||
|
98
inc/hgl/event/ASyncEvent.h
Normal file
98
inc/hgl/event/ASyncEvent.h
Normal file
@ -0,0 +1,98 @@
|
||||
#ifndef HGL_EVENT_ASYNC_INCLUDE
|
||||
#define HGL_EVENT_ASYNC_INCLUDE
|
||||
|
||||
#include<hgl/event/EventProc.h>
|
||||
#include<hgl/thread/SwapData.h>
|
||||
#include<hgl/thread/Thread.h>
|
||||
#include<hgl/Time.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using MTEventProcQueue=SemSwapData<EventProcQueue>;
|
||||
using MTEventProcPost=PostToSemSwapData<EventProcQueue>; ///<多线程事件结果投递对象重定义
|
||||
|
||||
/**
|
||||
* 增加事件到异步事件队列
|
||||
* @param queue 事件队列
|
||||
* @param event 事件
|
||||
*/
|
||||
inline void AddToAsyncEventQueue(MTEventProcQueue *queue,EventProc *event)
|
||||
{
|
||||
if(!queue||!event)
|
||||
return;
|
||||
|
||||
//该模板会自动加锁,在释构时自动解锁,并释放信号
|
||||
MTEventProcPost epq(queue);
|
||||
|
||||
epq->Push(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新异步执行事件队列
|
||||
* @param proc_queue 要执行的事件队列
|
||||
* @param wait 是否等待有事件
|
||||
* @return -1 出错
|
||||
* @return >=0 执行的事件数量
|
||||
*/
|
||||
inline int UpdateAsyncEventQueue(MTEventProcQueue *proc_queue,bool wait=false)
|
||||
{
|
||||
if(!proc_queue)
|
||||
return(-1);
|
||||
|
||||
if(wait)
|
||||
{
|
||||
if(!proc_queue->WaitSemSwap())
|
||||
return(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!proc_queue->TrySemSwap())
|
||||
return(0);
|
||||
}
|
||||
|
||||
EventProcQueue &epq=proc_queue->GetReceive();
|
||||
|
||||
return UpdateEventQueue(&epq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步事件异步执行线程
|
||||
*/
|
||||
class EventThread:public Thread
|
||||
{
|
||||
MTEventProcQueue *event_proc_queue;
|
||||
|
||||
public:
|
||||
|
||||
EventThread(MTEventProcQueue *queue):event_proc_queue(queue){}
|
||||
virtual ~EventThread()=default;
|
||||
|
||||
bool Execute() override
|
||||
{
|
||||
if(!event_proc_queue)
|
||||
return(false);
|
||||
|
||||
UpdateAsyncEventQueue(event_proc_queue,true);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class EventThread:public Thread
|
||||
|
||||
/* 使用范例
|
||||
|
||||
// 公用部分
|
||||
|
||||
MTEventProcQueue event_queue; ///<事件队列
|
||||
|
||||
// 其它 thread
|
||||
|
||||
class MyEvent:public EventProc{...}; //自有事件
|
||||
|
||||
AddToEventQueue(&event_queue,new MyEvent); //添加一个事件到事件队列
|
||||
|
||||
// 事件执行线程
|
||||
EventThread *et=new EventThread(&event_queue);
|
||||
et->Start();
|
||||
*/
|
||||
}//namespace hgl
|
||||
#endif//HGL_EVENT_ASYNC_INCLUDE
|
74
inc/hgl/event/EventProc.h
Normal file
74
inc/hgl/event/EventProc.h
Normal file
@ -0,0 +1,74 @@
|
||||
#ifndef HGL_EVENT_PROC_INCLUDE
|
||||
#define HGL_EVENT_PROC_INCLUDE
|
||||
|
||||
#include<hgl/type/Queue.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 事件基类
|
||||
*/
|
||||
class EventProc
|
||||
{
|
||||
public:
|
||||
|
||||
EventProc()=default;
|
||||
virtual ~EventProc()=default;
|
||||
|
||||
virtual bool Proc()=0;
|
||||
};//class EventProc
|
||||
|
||||
using EventProcQueue=Queue<EventProc *>;
|
||||
|
||||
/**
|
||||
* 执行一个事件
|
||||
*/
|
||||
inline bool UpdateEvent(EventProcQueue *epq)
|
||||
{
|
||||
if(!epq||epq->GetCount()<=0)
|
||||
return(false);
|
||||
|
||||
EventProc *e;
|
||||
|
||||
if(epq->Pop(e))
|
||||
{
|
||||
e->Proc();
|
||||
delete e;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
return(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新执行事件队列
|
||||
* @param epq 事件队列
|
||||
* @param max_count 最大刷新个数(-1表示无限制)
|
||||
*/
|
||||
inline int UpdateEventQueue(EventProcQueue *epq,int max_count=-1)
|
||||
{
|
||||
if(!epq||epq->GetCount()<=0||max_count==0)
|
||||
return(0);
|
||||
|
||||
int count=0;
|
||||
EventProc *e;
|
||||
|
||||
while(epq->Pop(e))
|
||||
{
|
||||
if(e)
|
||||
{
|
||||
e->Proc();
|
||||
delete e;
|
||||
}
|
||||
|
||||
++count;
|
||||
|
||||
if(--max_count==0)
|
||||
break;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}//namespace hgl
|
||||
#endif//HGL_EVENT_PROC_INCLUDE
|
@ -1,123 +0,0 @@
|
||||
#ifndef HGL_THREAD_ASYNC_EVENT_INCLUDE
|
||||
#define HGL_THREAD_ASYNC_EVENT_INCLUDE
|
||||
|
||||
#include<hgl/thread/SwapData.h>
|
||||
#include<hgl/type/Queue.h>
|
||||
#include<hgl/thread/Thread.h>
|
||||
#include<hgl/Time.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace async
|
||||
{
|
||||
/**
|
||||
* 事件基类
|
||||
*/
|
||||
class EventProc
|
||||
{
|
||||
public:
|
||||
|
||||
EventProc()=default;
|
||||
virtual ~EventProc()=default;
|
||||
|
||||
virtual bool Proc(const double &cur_time)=0;
|
||||
};//class EventProc
|
||||
|
||||
using EventProcQueue=Queue<EventProc *>;
|
||||
using MTEventProcQueue=SemSwapData<EventProcQueue>;
|
||||
using MTEventProcPost=PostToSemSwapData<EventProcQueue>; ///<多线程事件结果投递对象重定义
|
||||
|
||||
/**
|
||||
* 增加事件到队列
|
||||
* @param queue 事件队列
|
||||
* @param event 事件
|
||||
*/
|
||||
inline void AddToEventQueue(MTEventProcQueue *queue,EventProc *event)
|
||||
{
|
||||
if(!queue||!event)
|
||||
return;
|
||||
|
||||
MTEventProcPost post(queue);
|
||||
|
||||
post->Push(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新执行事件队列
|
||||
* @param proc_queue 要执行的事件队列
|
||||
* @param cur_time 当前时间
|
||||
* @param wait 是否等待有事件
|
||||
*/
|
||||
inline void UpdateEventProcQueue(MTEventProcQueue *proc_queue,const double &cur_time,bool wait=false)
|
||||
{
|
||||
if(!proc_queue)
|
||||
return;
|
||||
|
||||
if(wait)
|
||||
{
|
||||
if(!proc_queue->WaitSemSwap())
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!proc_queue->TrySemSwap())
|
||||
return;
|
||||
}
|
||||
|
||||
EventProcQueue &epq=proc_queue->GetReceive();
|
||||
|
||||
if(epq.GetCount()<=0)
|
||||
return;
|
||||
|
||||
EventProc *event;
|
||||
|
||||
while(epq.Pop(event))
|
||||
{
|
||||
event->Proc(cur_time);
|
||||
delete event;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步事件执行线程
|
||||
*/
|
||||
class EventThread:public Thread
|
||||
{
|
||||
MTEventProcQueue *event_proc_queue;
|
||||
|
||||
public:
|
||||
|
||||
EventThread(MTEventProcQueue *queue):event_proc_queue(queue){}
|
||||
virtual ~EventThread()=default;
|
||||
|
||||
bool Execute() override
|
||||
{
|
||||
if(!event_proc_queue)
|
||||
return(false);
|
||||
|
||||
UpdateEventProcQueue(event_proc_queue,GetDoubleTime(),true);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class EventThread:public Thread
|
||||
|
||||
/**
|
||||
* 使用范例
|
||||
*/
|
||||
/*
|
||||
// 公用部分
|
||||
|
||||
MTEventProcQueue event_queue; ///<事件队列
|
||||
|
||||
// 其它 thread
|
||||
|
||||
class MyEvent:public EventProc{...}; //自有事件
|
||||
|
||||
AddToEventQueue(&event_queue,new MyEvent); //添加一个事件到事件队列
|
||||
|
||||
// 事件执行线程
|
||||
EventThread *et=new EventThread(&event_queue);
|
||||
et->Start();*/
|
||||
}//namespace async
|
||||
}//namespace hgl
|
||||
#endif//HGL_THREAD_ASYNC_EVENT_INCLUDE
|
@ -70,7 +70,7 @@ namespace hgl
|
||||
|
||||
virtual void ProcEndThread(){} ///<结程结束运行函数,在Execute后被调用
|
||||
|
||||
virtual bool IsExitDelete()const{return true;} ///<返回在退出线程时,是否删除本对象(注:此函数不可动态变动值)
|
||||
virtual bool DeletedAfterExit()const{return true;} ///<返回在退出线程时,是否删除本对象(注:此函数不可动态变动值)
|
||||
|
||||
bool IsLive() ///<当前线程是否还活着
|
||||
{
|
||||
@ -114,8 +114,6 @@ namespace hgl
|
||||
|
||||
void WaitThread(Thread **,int,double time=0); ///<等待多个线程中的一个完成
|
||||
|
||||
bool CreateThread(Thread *); ///<创建一个线程
|
||||
|
||||
/**
|
||||
* 简单的多线程管理
|
||||
*/
|
||||
|
@ -163,8 +163,18 @@ SET(BASE_TIME_SOURCE_FILES Time/DateTime.cpp
|
||||
SOURCE_GROUP("Time\\Header Files" FILES ${BASE_TIME_HEADER_FILES})
|
||||
SOURCE_GROUP("Time\\Source Files" FILES ${BASE_TIME_SOURCE_FILES})
|
||||
|
||||
SET(BASE_THREAD_SOURCE Other/ThreadFunc.cpp)
|
||||
SOURCE_GROUP("Thread" FILES ${BASE_THREAD_SOURCE})
|
||||
|
||||
FILE(GLOB BASE_THREAD_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/thread/*.h)
|
||||
SET(BASE_THREAD_SOURCE_FILES Other/ThreadFunc.cpp)
|
||||
|
||||
SOURCE_GROUP("Thread" FILES ${BASE_THREAD_HEADER_FILES}
|
||||
${BASE_THREAD_SOURCE_FILES})
|
||||
|
||||
FILE(GLOB BASE_EVENT_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/event/*.*)
|
||||
FILE(GLOB BASE_EVENT_SOURCE_FILES Event/*.*)
|
||||
|
||||
SOURCE_GROUP("Event" FILES ${BASE_EVENT_HEADER_FILES}
|
||||
${BASE_EVENT_SOURCE_FILES})
|
||||
|
||||
file(GLOB BASE_PLUG_IN_HEADER ${CMCORE_ROOT_INCLUDE_PATH}/hgl/plugin/*.h)
|
||||
file(GLOB BASE_PLUG_IN_SOURCE PlugIn/*.cpp)
|
||||
@ -203,7 +213,12 @@ add_cm_library(CMCore "CM" ${CORE_PLATFORM_HEADER_FILES}
|
||||
${BASE_TIME_SOURCE_FILES}
|
||||
|
||||
${BASE_OTHER_SOURCE}
|
||||
${BASE_THREAD_SOURCE}
|
||||
|
||||
${BASE_THREAD_HEADER_FILES}
|
||||
${BASE_THREAD_SOURCE_FILES}
|
||||
|
||||
${BASE_EVENT_HEADER_FILES}
|
||||
${BASE_EVENT_SOURCE_FILES}
|
||||
|
||||
${BASE_PLUG_IN_HEADER}
|
||||
${BASE_PLUG_IN_SOURCE}
|
||||
|
@ -29,7 +29,7 @@ namespace hgl
|
||||
tc->ProcEndThread();
|
||||
}
|
||||
|
||||
if(tc->IsExitDelete())
|
||||
if(tc->DeletedAfterExit())
|
||||
{
|
||||
tc->live_lock.Unlock();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user