nvmain源码阅读记录3

事件相关

在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 );
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值