binder核心知识补充

//这两货是进程中的全局变量,一个是当前进程锁,锁的对象是gProcess,即当前进程状态。另一个是当前进程的状态.

Mutex gProcessMutex; //状态锁
sp<ProcessState> gProcess; //状态

static bool gHaveTLS = false;
static pthread_key_t gTLS = 0;

线程可以有自己的存储空间,android通过Thread Local storage机制来实现。即IPCThreadState对象。
进程通信本质上是进程A中某个线程A(x)通信与另外一个进程B中的某个线程B(y)通信。
进程中保存了(handle, BpBinder), A(x)通信时通过进程中BpBinder::transact()=>IPCThreadState::self()->transact()来写数据。
也就是进程中某个线程A(x)跟其它进程中的线程是可以同时进行的,它们互不影响。因为它们通信使用的是线程存储变量IPCThreadState::self()。 

class ProcessState : public virtual RefBase
{
public:
    static  sp<ProcessState>    self();
            void                setContextObject(const sp<IBinder>& object);
            sp<IBinder>         getContextObject(const sp<IBinder>& caller);
        
            void                setContextObject(const sp<IBinder>& object, const String16& name);
            sp<IBinder>         getContextObject(const String16& name, const sp<IBinder>& caller);

            void                startThreadPool();
            bool                isContextManager(void) const;
            bool                becomeContextManager(context_check_func checkFunc, void* userData);

            sp<IBinder>         getStrongProxyForHandle(int32_t handle);
            wp<IBinder>         getWeakProxyForHandle(int32_t handle);
            void                expungeHandle(int32_t handle, IBinder* binder);

            void                spawnPooledThread(bool isMain);
            
            status_t            setThreadPoolMaxThreadCount(size_t maxThreads);
            void                giveThreadPoolName();

private:
    friend class IPCThreadState;            
                                ProcessState();
                                ~ProcessState();

                                ProcessState(const ProcessState& o);
            ProcessState&       operator=(const ProcessState& o);
            String8             makeBinderThreadName();
            
            struct handle_entry {
                IBinder* binder;
                RefBase::weakref_type* refs;
            };
            
            handle_entry*       lookupHandleLocked(int32_t handle);

            int                 mDriverFD; //文件描述符mamp()用的 Binder对应的设备
            void*               mVMStart;
            
    mutable Mutex               mLock;  // protects everything below.
            
            Vector<handle_entry> mHandleToObject;  

            bool                mManagesContexts;
            context_check_func  mBinderContextCheckFunc;
            void*               mBinderContextUserData;
            
            KeyedVector<String16, sp<IBinder> > mContexts;

            String8             mRootDir;
            bool                mThreadPoolStarted;
    volatile int32_t            mThreadPoolSeq;
};

 

class IPCThreadState
{
public:
    static  IPCThreadState*     self(); //配合构造函数,用来实现线程存储的。很巧妙的方式。
    static  IPCThreadState*     selfOrNull();  
    
            sp<ProcessState>    process();            
            status_t            clearLastError();
            void                joinThreadPool(bool isMain = true);

            void                stopProcess(bool immediate = true);
            
            status_t            transact(int32_t handle, uint32_t code, const Parcel& data,
                                         Parcel* reply, uint32_t flags);
            status_t            clearDeathNotification( int32_t handle,
                                                        BpBinder* proxy); 

    static  void                shutdown();
    static  void                disableBackgroundScheduling(bool disable);
    
private:
                                IPCThreadState();
                                ~IPCThreadState();

            status_t            sendReply(const Parcel& reply, uint32_t flags);
            status_t            waitForResponse(Parcel *reply,
                                                status_t *acquireResult=NULL);
            status_t            talkWithDriver(bool doReceive=true);
            status_t            writeTransactionData(int32_t cmd,
                                                     uint32_t binderFlags,
                                                     int32_t handle,
                                                     uint32_t code,
                                                     const Parcel& data,
                                                     status_t* statusBuffer);
            status_t            getAndExecuteCommand();
            status_t            executeCommand(int32_t command);
            void                processPendingDerefs();
            
            void                clearCaller();
    
    const   sp<ProcessState>    mProcess;
    const   pid_t               mMyThreadId;
            Vector<BBinder*>    mPendingStrongDerefs;
            Vector<RefBase::weakref_type*> mPendingWeakDerefs;
            
            Parcel              mIn;  // 读的数据的包装
            Parcel              mOut; // 要写的数据的包装
            status_t            mLastError;
            pid_t               mCallingPid;
            uid_t               mCallingUid;
            int32_t             mStrictModePolicy;
            int32_t             mLastTransactionBinderFlags;
};

 binder:粘合剂,android里面意思就是将上层和设备直接联系起来。linux里面通常和设备打交道都是要通过内核才行的。通过Binder机制可以直接和设备打交道。binder机制本质上就是内存映射机制。

BpBinder p是proxy      BnBinder  n是native的意思 BnBinder通常在本地层实现。当然也有上层实现的。

posted @ 2015-04-18 17:35  牧 天  阅读(445)  评论(0)    收藏  举报