[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

 

 

 

 

你可能感兴趣的:([android JB audioflinger] fastmixer-1: StateQueue)