事件相关
在EventQueue.cpp中
Loop,currentCycle增加1
void EventQueue::Loop( )
{
/*
* Loop() is called by NVMain::Cycle() in nvmain.cc, where the whole
* memory system inserts new events into the mainEventQueue. We need to
* guarantee that the event that is inserted in current cycle and must
* be handled in current cycle can be processed.
*/
if( nextEventCycle == currentCycle )
Process( );
currentCycle++;
}
Loop,currentCycle增加steps;若到steps个cycle之前有事件,则需要把这些事件都处理了
void EventQueue::Loop( ncycle_t steps )
{
/* Special case. */
if( steps == 0 && nextEventCycle == currentCycle )
{
Process( );
return;
}
ncycle_t stepCycles = steps;
while( stepCycles > 0 )
{
/* No events in this step amount, just change current cycle. */
if( nextEventCycle > currentCycle + stepCycles )
{
currentCycle += stepCycles;
break;
}
ncycle_t currentSteps = nextEventCycle - currentCycle;
currentCycle += currentSteps;
stepCycles -= currentSteps;
/* Process will update nextEventCycle for the next loop iteration. */
Process( );
}
}
取出在nextEventCycle的事件eventList;并在处理后设置nextEventCycle和lastEventCycle值;事件分多种类型:
EventCycle:则对事件的Recipient(来自的NVMObject)调用该NVMObject的Cycle函数;
EventResponse:则对事件的Recipient(来自的NVMObject)调用该NVMObject的RequestComplete函数
void EventQueue::Process( )
{
/* Process all the events at the next cycle, and figure out the next next cycle. */
assert( eventMap.count( nextEventCycle ) );
EventList& eventList = eventMap[nextEventCycle];
EventList::iterator it;
for( it = eventList.begin( ); it != eventList.end( ); it++ )
{
switch( (*it)->GetType( ) )
{
case EventCycle:
(*it)->GetRecipient( )->Cycle( nextEventCycle - lastEventCycle );
break;
case EventIdle:
// TODO: Add this
break;
case EventRequest:
// TODO: Add this
break;
case EventResponse:
(*it)->GetRecipient( )->RequestComplete( (*it)->GetRequest( ) );
break;
case EventCallback:
{
CallbackPtr cb = (*it)->GetCallback( );
NVMObject *thisPtr = (*it)->GetRecipient( )->GetTrampoline( );
(*thisPtr.*cb)( (*it)->GetData() );
break;
}
case EventUnknown:
// TODO: Add this
default:
break;
}
/* Free event data */
delete (*it);
}
eventMap.erase( nextEventCycle );
/* Figure out the next cycle. */
if( eventMap.empty( ) )
{
lastEventCycle = nextEventCycle;
nextEventCycle = std::numeric_limits<ncycle_t>::max();
}
else
{
lastEventCycle = nextEventCycle;
/* map is sorted by keys, so this works out. */
nextEventCycle = eventMap.begin()->first;
}
}
EventCallback:
在InsertCallback插入Callback事件时,SetCallback( method );设置了该Event的method;所以在执行Process处理callback事件时,函数返回InsertCallBack时设置该Event(不是EventQueue)的method;通过method执行InsertCallback插入事件时设置的data
CallbackPtr GetCallback( ) { return method; }
void EventQueue::InsertCallback( NVMObject *recipient, CallbackPtr method,
ncycle_t when, void *data, int priority )
{
Event *event = new Event( );
event->SetType( EventCallback );
event->SetRecipient( recipient );
event->SetData( data );
event->SetCycle( when );
event->SetPriority( priority );
event->SetCallback( method );
InsertEvent( event, when, priority );
}