您的位置:首页 > 移动开发 > Android开发

Android FrameWork中的SP、RefBase、weakref_impl,Thread类

2015-09-19 21:31 423 查看
http://blog.csdn.net/gzzaigcnforever/article/details/20649781

在阅读Android的Framework处的代码可以发现,无处不在SP给予了我视觉上的冲击,这个是什么?初级的我,看这个当初就基本当成指针来用,熟不知其的内在美,于是在这里和大家一起学习总结SP类的魅力所在。

1 SP这货是个模板类,让我们看下他的结构。

[cpp] view
plaincopy





template <typename T>  

class sp  

{  

public:  

    inline sp() : m_ptr(0) { }  

  

    sp(T* other);  

    sp(const sp<T>& other);  

    template<typename U> sp(U* other);  

    template<typename U> sp(const sp<U>& other);  

  

    ~sp();  

      

    // Assignment  

  

    sp& operator = (T* other);  

    sp& operator = (const sp<T>& other);  

      

    template<typename U> sp& operator = (const sp<U>& other);  

    template<typename U> sp& operator = (U* other);  

      

    // Reset  

    void clear();  

      

    // Accessors  

  

    inline  T&      operator* () const  { return *m_ptr; }  

    inline  T*      operator-> () const { return m_ptr;  }  

    inline  T*      get() const         { return m_ptr; }  

  

    // Operators  

          

    COMPARE(==)  

    COMPARE(!=)  

    COMPARE(>)  

    COMPARE(<)  

    COMPARE(<=)  

    COMPARE(>=)  

  

private:      

    template<typename Y> friend class sp;  

  

    T*              m_ptr;  

};  

看到了上述的代码结构,瞬间觉得其高大上,作为一个经典的模板类,精懂的人说这个类很好,其实没有过多的去了解他只知道是更好的维护对象。这个SP指针内部有个T* m_ptr成员变量,它是真正指向我们new出来的变量。

我们以sp<A> mA = new A();为例,作为一个模板类的构造函数,调用如下:

[cpp] view
plaincopy





template<typename T>  

sp<T>::sp(T* other)  

    : m_ptr(other)  

{  

    if (other) other->incStrong(this);  

}  

果然,new出来的A对象被存入到sp的成员变量之中,这里看到对mA对象构建时,会调用A的一个incStrong,这个是什么?阅读过代码的都知道一个RefBase类,他是比sp类更为常见的类,我们看她的结构可以发现内容都较多,但都是一些很特别的东西,  我们看下面的UML图来分析我们之间构建的A为何要继承与RefBase。



可以看到继承了RefBase,那么A的对象有东西可以玩了,调用RefBase的incStrong函数,接着看他的实现,先来个构造函数过过瘾:

[cpp] view
plaincopy





RefBase::RefBase()  

    : mRefs(new weakref_impl(this))  

{  

}  

做的东西不多,但是有一个成员变量mRefs,必然他先实现,好吧mRefs被赋值给了一个wekref_impl这个对象,传入的this就是我们的这个new A()对象,好的接着再看看:

[cpp] view
plaincopy





weakref_impl(RefBase* base)  

    : mStrong(INITIAL_STRONG_VALUE)  

    , mWeak(0)  

    , mBase(base)  

    , mFlags(0)  

{  

}  

这里有个mBase,他是一个RefBase类,故按C++的知识,一个从RefBase继承的A类对象被赋值给了mBase,那基类的指针mBase就可以访问类A自带的重载的虚函数了,先留着过会就会用到。

回来我们来看这个刚才other->incStrong(),调用的是基类的incStrong,看他的实现:

[cpp] view
plaincopy





void RefBase::incStrong(const void* id) const  

{  

    weakref_impl* const refs = mRefs;//影子对象的refs  

    refs->incWeak(id);  

      

    refs->addStrongRef(id);  

    const int32_t c = android_atomic_inc(&refs->mStrong);  

    ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);  

#if PRINT_REFS  

    ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);  

#endif  

    if (c != INITIAL_STRONG_VALUE)  {  

        return;  

    }  

  

    android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);  

    refs->mBase->onFirstRef();//mBase维护着继承类实际对象this指针  

}  

这里是增加了对这个指针的引用次数,最重要的一点是关注最后一行代码,在我们写自己类的时候一般都喜欢重载该函数,在这里面对类对象进行必要的初始化。

refs为mRefs,就是我们类A的对象的一个继承成员weakref_impl,该对象有个内部成员mBase,通过上面的分析可知mBase就是指向类A对象的指针,故当访问虚函数时,实际调用的是派生类的重载函数,故最终首次建立一个sp<A> mA 时,就也是所谓的第一次引用吧。

 

2.学一学Android Native的Thread类

上面介绍了很多的类,在onFirstRef里面可以经常会遇到一个run函数,这是启动一个新线程的方法,那就是你的类A 继承了thread类,从上一UML图可以看到,thread有几个核心的函数,最重要的就是一个run函数。来看她的部分代码:

[cpp] view
plaincopy





status_t Thread::run(const char* name, int32_t priority, size_t stack)  

{  

    Mutex::Autolock _l(mLock);  

  

    if (mRunning) {  

        // thread already started  

        return INVALID_OPERATION;  

    }  

  

    // reset status and exitPending to their default value, so we can  

    // try again after an error happened (either below, or in readyToRun())  

    mStatus = NO_ERROR;  

    mExitPending = false;  

    mThread = thread_id_t(-1);  

      

    // hold a strong reference on ourself  

    mHoldSelf = this;  

  

    mRunning = true;  

  

    bool res;  

    if (mCanCallJava) {  

        res = createThreadEtc(_threadLoop,  

                this, name, priority, stack, &mThread);//启动_threadLoop线程  

    } else {  

        res = androidCreateRawThreadEtc(_threadLoop,  

                this, name, priority, stack, &mThread);  

    }  

      

    if (res == false) {  

        mStatus = UNKNOWN_ERROR;   // something happened!  

        mRunning = false;  

        mThread = thread_id_t(-1);  

        mHoldSelf.clear();  // "this" may have gone away after this.  

  

        return UNKNOWN_ERROR;  

    }  

      

    // Do not refer to mStatus here: The thread is already running (may, in fact  

    // already have exited with a valid mStatus result). The NO_ERROR indication  

    // here merely indicates successfully starting the thread and does not  

    // imply successful termination/execution.  

    return NO_ERROR;  

  

    // Exiting scope of mLock is a memory barrier and allows new thread to run  

}  

源码上深入的话会比较复杂,但是一点可以肯定的是调用的线程函数是_thread_Loop,看看他的实现:

[cpp] view
plaincopy





int Thread::_threadLoop(void* user)  

{  

    Thread* const self = static_cast<Thread*>(user);//派生类对象转为基类指针  

do {  

        bool result;  

        if (first) {  

            first = false;  

            self->mStatus = self->readyToRun();//直接调用继承类的readyToRun  

            result = (self->mStatus == NO_ERROR);  

......  

           else  

            result = self->threadLoop();  

        }  

}  

这里的self是什么,实际是我们新建的一个对象,那么首次就是调用类A的操作函数readyToRun(),随后一般是调用threadLoop进入线程的循环,线程的退出主要由threadLoop函数的返回结果来决定。

 

3. 好了,下面再来看看这个这几个比较常见的过程,会和C++的多态符合重载有紧密的关系。

比如mA->fun(),一眼看去感觉和普通的指针调用没有区别,但你是否知道mA只是一个sp类的对象,那么这个mA->的操作符是什么?那就是所谓的符合重载,来看sp的几个运算符的重载函数:

[cpp] view
plaincopy





inline  T&      operator* () const  { return *m_ptr; }  

 inline  T*      operator-> () const { return m_ptr;  }  

 inline  T*      get() const         { return m_ptr; }  

很好,可以看到他的实现,不需要输入参数,返回一个m_ptr,m_ptr是什么,前面说过他是我们new A()出来的一个对象,那么调用的就是类A所属的成员函数.类似的还有:

[cpp] view
plaincopy





template<typename T>  

sp<T>& sp<T>::operator = (T* other)  

{  

    if (other) other->incStrong(this);  

    if (m_ptr) m_ptr->decStrong(this);  

    m_ptr = other;  

    return *this;  

}  

  

template<typename T> template<typename U>  

sp<T>& sp<T>::operator = (const sp<U>& other)  

{  

    T* otherPtr(other.m_ptr);  

    if (otherPtr) otherPtr->incStrong(this);  

    if (m_ptr) m_ptr->decStrong(this);  

    m_ptr = otherPtr;  

    return *this;  

}  

分别是对指针与引用的赋值操作。

 

上述的Android Framework里面会经常遇到这些细节性的问题,了解这些基本类往往可以做到事半功倍,阅读代码更加心领神会!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: