• ProcessState实例化过程讲解


    1.ProcessState实例化过程

    类图
    sp
    +inline sp(): m_ptr(nullptr){ }
    +template static inline sp make(Args&&... args)
    RefBase
    -friend class weakref_type
    #virtual void onFirstRef()
    ProcessState
    - friend class IPCThreadState;
    - struct handle_entry {
    IBinder* binder;
    RefBase::weakref_type* refs;
    }
    +static sp self()
    +static sp initWithMmapSize(size_t mmapSize)
    +void startThreadPool()
    +sp getContextObject(const sp& /*caller*/)
    +void becomeContextManager()
    +sp getStrongProxyForHandle(int32_t handle)
    +wp getWeakProxyForHandle(int32_t handle)
    -static sp init(size_t mmapSize, bool requireMmapSize)
    -explicit ProcessState(size_t mmapSize)
    -String8 makeBinderThreadName()
    IPCThreadState
    weakref_type
    +RefBase* refBase()
    +void incWeak(const void* id)
    wp
    +typedef typename RefBase::weakref_type weakref_type;
    -template friend class sp;
    时序图
    <1>.new ProcessState();
    main.c ProcessState open_driver mmap ioctl new ProcessState(): mDriverFD = open_driver("/dev/binder") mmap(mDriverFD) ioctl(fd, BINDER_VERSION, &vers) ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads) ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable) main.c ProcessState open_driver mmap ioctl ProcessState实例化过程
    <2>.ProcessState::self()->startThreadPool()
    main.c ProcessState.c self() init() make() startThreadPool() spawnPooledThread() PoolThread ProcessState::self()->>startThreadPool() ProcessState::self() init(kDefaultDriver, false ) kDefaultDriver="/dev/vndbinder" kDefaultDriver="/dev/binder" sp gProcess = sp::make(driver) return gProcess spawnPooledThread(true) sp t = sp::make() t->>run(name.string()) main.c ProcessState.c self() init() make() startThreadPool() spawnPooledThread() PoolThread ProcessState实例化过程

    2.程序例子

    1)RefBase基类和智能指针StrongPointer关系
    make()函数用法在StrongPointer.h里.
    system/core/libutils/include/utils/StrongPointer.h
    class sp {
     template <typename T>
     template <typename... Args>
     sp<T> sp<T>::make(Args&&... args) {
        T* t = new T(std::forward<Args>(args)...);
        sp<T> result;
        result.m_ptr = t;
        t->incStrong(t);  // bypass check_not_on_stack for heap allocation
        return result;
     }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    2).其实RefBase.h里引用了utils/StrongPointer.h,即使用了智能指针,如下所示:
    system/core/libutils/include/utils/RefBase.h
    #include **
    
    • 1
    • 2
    <1>.Android.mk
    ```Makefile
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE := sppointer
    LOCAL_SRC_FILES := strong_pointer.cpp
    LOCAL_SHARED_LIBRARIES := libcutils libutils
    include $(BUILD_EXECUTABLE)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    <2>.strong_pointer.cpp
    #include 
    #include 
    #include 
    using namespace android;
    
    class Bigclass : public RefBase{
    public:
      Bigclass(String8 name): mDriverName(name){
        printf("Bigclass::mDriverName = %s\n",mDriverName.c_str());
      }
    
      String8 getDriverName() {
        return mDriverName;
      }
      
    private:
      String8 mDriverName;
    };
    
    int main(){
      const char *driver = "/dev/binder";
      static sp<Bigclass>  bC;
    
      //bC = = new Bigclass(String8(driver));
      //Or  
      //注意:此处make(String8(driver))等价于类实例化new Bigclass(String8(driver));
      bC = sp<Bigclass>::make(String8(driver));
      printf("xxx------> driverName = %s\n",bC->getDriverName().c_str());
      return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    停止Tomcat服务的方式
    ASP.NET Core Web API 接口限流
    java给图片添加自定义文字信息
    WebRTC信令服务器的搭建
    云计算安全:保护你的数据免受黑客侵害
    Typescript中类的使用
    Mit6.006-lecture05-Linear-Sorting
    生命在于学习——MSF初体验(二)
    PHP GC回收机制详解
    链块串的实现(无功能函数实现)
  • 原文地址:https://blog.csdn.net/u010164190/article/details/128195792