相关源码位置:
frameworks/native/libs/binder/ProcessState.cpp
frameworks/native/libs/binder/include/binder/ProcessState.h

继承关系

class ProcessState : public virtual RefBase

方法

实例化方法

  • 构造方法

    424 ProcessState::ProcessState(const char *driver)
    425     : mDriverName(String8(driver))
    426     , mDriverFD(open_driver(driver))
    427     , mVMStart(MAP_FAILED)
    428     , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
    429     , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
    430     , mExecutingThreadsCount(0)
    431     , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
    432     , mStarvationStartTimeMs(0)
    433     , mManagesContexts(false)
    434     , mBinderContextCheckFunc(nullptr)
    435     , mBinderContextUserData(nullptr)
    436     , mThreadPoolStarted(false)
    437     , mThreadPoolSeq(1)
    438     , mCallRestriction(CallRestriction::NONE)
    439 {
    440     if (mDriverFD >= 0) {
    441         // mmap the binder, providing a chunk of virtual address space to receive transactions.
    442         mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
    443         if (mVMStart == MAP_FAILED) {
    444             // *sigh*
    445             ALOGE("Using %s failed: unable to mmap transaction memory.\n", mDriverName.c_str());
    446             close(mDriverFD);
    447             mDriverFD = -1;
    448             mDriverName.clear();
    449         }
    450     }
    451 
    452     LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened.  Terminating.");
    453 }

    在构造函数初始化open_driver(driver),driver对于servicemanager进程来说即是/dev/binder

    46 #ifdef __ANDROID_VNDK__
    47 const char* kDefaultDriver = "/dev/vndbinder";
    48 #else
    49 const char* kDefaultDriver = "/dev/binder";
    50 #endif
  • self

    74 sp<ProcessState> ProcessState::self()
    75 {
    76     Mutex::Autolock _l(gProcessMutex);
    77     if (gProcess != nullptr) {
    78         return gProcess;
    79     }
    80     gProcess = new ProcessState(kDefaultDriver);
    81     return gProcess;
    82 }

    gProcess定义在
    frameworks/native/libs/binder/include/private/binder/Static.h:36:extern sp<ProcessState> gProcess;

其它方法

  • getContextObject

    116 sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
    117 {
    118     return getStrongProxyForHandle(0);
    119 }
  • getStrongProxyForHandle

259 sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
260 {
261     sp<IBinder> result;
262 
263     AutoMutex _l(mLock);
264 
265     handle_entry* e = lookupHandleLocked(handle);
266 
267     if (e != nullptr) {
268         // We need to create a new BpBinder if there isn't currently one, OR we
269         // are unable to acquire a weak reference on this current one.  See comment
270         // in getWeakProxyForHandle() for more info about this.
271         IBinder* b = e->binder;
272         if (b == nullptr || !e->refs->attemptIncWeak(this)) {
273             if (handle == 0) {
274                 // Special case for context manager...
275                 // The context manager is the only object for which we create
276                 // a BpBinder proxy without already holding a reference.
277                 // Perform a dummy transaction to ensure the context manager
278                 // is registered before we create the first local reference
279                 // to it (which will occur when creating the BpBinder).
280                 // If a local reference is created for the BpBinder when the
281                 // context manager is not present, the driver will fail to
282                 // provide a reference to the context manager, but the
283                 // driver API does not return status.
284                 //
285                 // Note that this is not race-free if the context manager
286                 // dies while this code runs.
287                 //
288                 // TODO: add a driver API to wait for context manager, or
289                 // stop special casing handle 0 for context manager and add
290                 // a driver API to get a handle to the context manager with
291                 // proper reference counting.
292 
293                 Parcel data;
294                 status_t status = IPCThreadState::self()->transact(
295                         0, IBinder::PING_TRANSACTION, data, nullptr, 0);
296                 if (status == DEAD_OBJECT)
297                    return nullptr;
298             }
299 
300             b = BpBinder::create(handle);
301             e->binder = b;
302             if (b) e->refs = b->getWeakRefs();
303             result = b;
304         } else {
305             // This little bit of nastyness is to allow us to add a primary
306             // reference to the remote proxy when this team doesn't have one
307             // but another team is sending the handle to us.
308             result.force_set(b);
309             e->refs->decWeak(this);
310         }
311     }
312 
313     return result;
314 }

→lookupHandleLocked
https://www.jianshu.com/p/944067088827

246 ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
247 {
248     const size_t N=mHandleToObject.size();
249     if (N <= (size_t)handle) {
250         handle_entry e;
251         e.binder = nullptr;
252         e.refs = nullptr;
253         status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
254         if (err < NO_ERROR) return nullptr;
255     }
256     return &mHandleToObject.editItemAt(handle);
257 }

→BpBinder.create
http://xinyiworld.top/wordpress_it/?p=14481


0 条评论

发表回复

您的电子邮箱地址不会被公开。