📄 evtmgr.cpp
字号:
#include "evtmgr.h"
#include "event.h"
typedef struct EventMessage
{
int event_count;
MSG_HANDLER event_handler;
}EVT_MSG;
static EVT_MSG evt_table[MAX_EVENT_NUMBER] = { };
ERR sig_event(unsigned event_id)
{
ASSERT(event_id < MAX_EVENT_NUMBER);
if(evt_table[event_id].event_count >= MAX_READY_COUNT)
return INDEX_OUTOF_RANGE;
evt_table[event_id].event_count++;
return VALID_SUCCESS;
}
ERR digest_event(unsigned event_id)
{
ASSERT(event_id < MAX_EVENT_NUMBER);
if(evt_table[event_id].event_count <= 0)
return INDEX_OUTOF_RANGE;
evt_table[event_id].event_count--;
return VALID_SUCCESS;
}
void reg_event(unsigned event_id, MSG_HANDLER event_handler)
{
ASSERT(event_id < MAX_EVENT_NUMBER);
evt_table[event_id].event_count = 0;
evt_table[event_id].event_handler = event_handler;
}
void unreg_event(unsigned event_id)
{
ASSERT(event_id < MAX_EVENT_NUMBER);
evt_table[event_id].event_count = 0;
evt_table[event_id].event_handler = NULL;
}
typedef struct QueueMessage
{
int message_id;
void* param;
}QUEUE_MSG;
static QUEUE_MSG active_table[MAX_READY_COUNT] = { };
static int queue_head = 0;
static int queue_tail = 0;
#define IS_QUEUE_EMPTY() ((queue_head == queue_tail) && (active_table[queue_head].message_id == -1))
#define IS_QUEUE_FULL() ((queue_head == queue_tail) && (active_table[queue_head].message_id != -1))
static inline void queue_insert(unsigned message_id, void* param)
{
active_table[queue_head].message_id = message_id;
active_table[queue_head].param = param;
queue_head = (queue_head + 1) & (MAX_READY_COUNT - 1);
}
static inline QUEUE_MSG queue_delete(void)
{
QUEUE_MSG msg = active_table[queue_tail];
active_table[queue_tail].message_id = -1;
queue_tail = (queue_tail + 1) & (MAX_READY_COUNT - 1);
return msg;
}
void queue_init(void)
{
for(int i=0; i<MAX_READY_COUNT; i++)
active_table[i].message_id = -1;
}
ERR post_message(unsigned message_id, void* param)
{
ASSERT(message_id < MAX_EVENT_NUMBER);
if(IS_QUEUE_FULL())
return QUEUE_FULL_WARNING;
queue_insert(message_id, param);
return VALID_SUCCESS;
}
ERR receive_message(MSG_HANDLER* pMsgHandler, void** pParam)
{
QUEUE_MSG msg;
ASSERT(pMsgHandler != NULL && pParam != NULL);
if(IS_QUEUE_EMPTY())
return QUEUE_EMPTY_WARNIG;
msg = queue_delete();
*pMsgHandler = evt_table[msg.message_id].event_handler;
if(*pMsgHandler == NULL)
return INVALID_RESULT;
*pParam = msg.param;
return VALID_SUCCESS;
}
ERR execute_message(void)
{
QUEUE_MSG msg;
ERR result = VALID_SUCCESS;
while(!IS_QUEUE_EMPTY())
{
msg = queue_delete();
result = digest_event(msg.message_id);
if(result != VALID_SUCCESS)
return result;
result = (*evt_table[msg.message_id].event_handler)(msg.param);
if(result != VALID_SUCCESS)
return result;
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -