[android JB audioflinger] fastmixer-1: StateQueue

StateQueue :

1. mantain pointers: mMutating,mNext,mExpecting,mAck,mAck;

2. defined:   mStates[N=4]; //  ring sequence.

 

StateQueue<T>::begin()  {

get mMutating and set status of mMutating = true, which means we're currently in the middle of a mutation

}

对begin()获得的对象进行操作,操作完后调用end();

StateQueue<T>::end(){

set the status of mMutating = false and mark the dirty ,when mMutating=true, stateQueue.push can not be called.

}

理解各变量的用途:

/*

    volatile const T* mNext; // written by mutator (push)to advance next, read by observer
    volatile const T* mAck;  // written by observer (poll) to acknowledge advance of next, read by mutator

    // only used by observe
    const T*          mCurrent;         // most recent value returned by poll()

    // only used by mutator
    T*                mMutating;        // where updates by mutator are done in place
    const T*          mExpecting;       // what the mutator expects mAck to be set to
    bool              mInMutation;      // whether we're currently in the middle of a mutation
    bool              mIsDirty;         // whether mutating state has been modified since last push
    bool              mIsInitialized;   // whether mutating state has been initialized yet

*/

 

StateQueue<T>::push()的工作流程:

a. 第一次调用state队列之前,各指针变量状态:mMutating=&mStates[0], mNext=mExpecting=mAck=mAck=NULL

b. 第一次调用:mNext=mExpecting=&mStates[0]; mMutating指向mStates[1], 并赋值mStates[1]=mStates[0], 标识当前mMuatating 所指对象 mIsDirty=false;

   如果当前block标识为BLOCK_UNTIL_ACKED,则调用进程进入等待睡眠状态(具体等待时间和平台的时钟中断有关);反之直接返回。

c. 如果另个线程又调用了push.则它将有两种结果:要么直接返回,要么进入睡眠状态。

d. poll 被调用。

 

StateQueue<T>::poll(){

a. 获得mNext 的值

b. mAck和mCurrent指向mNext

返回mNext 的值.

}

接着前面的push:

e. poll 被调用完,前面调用push 的进程被唤醒,继续执行, 这是 mAck=mNext=mExpecting,  mExpecting 被赋值NULL,调用线程返回。

 

总结: StateQueue 的作用是为线程间提供一个共享循环队列, 一个线程mutator 向队列中写入请求,另一个线程oberser响应写入的请求。当前的请求如果没有被响应,则再次写请求时不能成功。

 

mixthread is mutator , fast mixer thread is observer

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值