Orocos ExecutionEngine 对函数Operation调用的实现

15 篇文章 1 订阅
12 篇文章 5 订阅

Orocos 中每一个 ExecutionEngine 可以属于一个线程,其拥有一个成员变量,指向 Activity 类(表示一个线程)。 该 ExecutionEngine 负责函数的调用,数据的传输等等(如下图所示)
这里写图片描述

其中每一个 ExecutionEngine 中都有一个无锁的队列 MWSRQueuesee link

internal::MWSRQueue<base::DisposableInterface*>* mqueue;

MWSRQueue 代表的是 Multi-Writer, Single-Reader queue.

而 DisposableInterface 则表示一个可以调用的对象(LocalOperationCallerImpl,详见 link, 其实就是一个 boost::function 的 wrapper), 但都是以指针形式保存的。

如果该函数不是在调用端执行的 (详见Operation&OperationCaller), 则需要将函数指针 插入到 执行该函数的线程类的队列 中,等到该线程被执行的时候再来执行该函数。

ExecutionEngine 在 Activity(线程) 中运行, Activity 在一个 while 循环中会调用 ExecutionEngine 的 step 函数, 而这个 step 函数的功能就是调用 mqueue 中 每一个指针 的 executeAndDispose 函数,如下:

void ExecutionEngine::step()
{
    // execute all commands from the AtomicQueue.
    // msg_lock may not be held when entering this function !
    DisposableInterface* com(0);
    {
        while ( mqueue->dequeue(com) ) {  // 遍历 dequeue 中的每一个函数指针
            assert( com );
            com->executeAndDispose();  // 调用 LocalOperationCallerImpl 中 BindStorage 的 exec 函数,最终实现 boost::function 的调用
        }
        // there's no need to hold the lock during
        // emptying the queue. But we must hold the
        // lock once between excuteAndDispose and the
        // broadcast to avoid the race condition in
        // waitForMessages().
        // This allows us to recurse into step.
        MutexLock locker( msg_lock );  // 互斥锁
    }
    if ( com )
        msg_cond.broadcast(); // 条件变量 required for waitForMessages() (3rd party thread),用于通知该线程中所有的函数已经被调用。
}

调用的过程很清晰了,但是这个 mqueue 是何时创建的呢?

每次调用ExecutionEngine::process这个函数,就会向 mqueue 中插入一个指针。

bool ExecutionEngine::process( DisposableInterface* c )
{
    // forward message to master ExecutionEngine if available
    if (mmaster) {
        return mmaster->process(c);
    }

    if ( c && this->getActivity() ) {
        // We only reject running functions when we're in the FatalError state.
        if (taskc && taskc->mTaskState == TaskCore::FatalError )
            return false;

        bool result = mqueue->enqueue( c );  // 向 mqueue 插入新的函数指针
        this->getActivity()->trigger();
        msg_cond.broadcast(); // required for waitAndProcessMessages() (EE thread)
        return result;
    }
    return false;
}

到此,Operation 是如何选择在哪个线程中运行的也清晰了。只需要选择对应的 ExecutionEngine,然后调用其 procees 函数,这样被调用对象的指针就插入到了该 ExecutionEngine 中,在 ExecutionEngine的 step 函数中就会调用该函数。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值