Home | History | Annotate | Download | only in binder
      1 /*
      2  * Copyright (C) 2005 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_PROCESS_STATE_H
     18 #define ANDROID_PROCESS_STATE_H
     19 
     20 #include <binder/IBinder.h>
     21 #include <utils/KeyedVector.h>
     22 #include <utils/String8.h>
     23 #include <utils/String16.h>
     24 
     25 #include <utils/threads.h>
     26 
     27 #include <pthread.h>
     28 
     29 // ---------------------------------------------------------------------------
     30 namespace android {
     31 
     32 class IPCThreadState;
     33 
     34 class ProcessState : public virtual RefBase
     35 {
     36 public:
     37     static  sp<ProcessState>    self();
     38     /* initWithDriver() can be used to configure libbinder to use
     39      * a different binder driver dev node. It must be called *before*
     40      * any call to ProcessState::self(). /dev/binder remains the default.
     41      */
     42     static  sp<ProcessState>    initWithDriver(const char *driver);
     43 
     44             void                setContextObject(const sp<IBinder>& object);
     45             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
     46 
     47             void                setContextObject(const sp<IBinder>& object,
     48                                                  const String16& name);
     49             sp<IBinder>         getContextObject(const String16& name,
     50                                                  const sp<IBinder>& caller);
     51 
     52             void                startThreadPool();
     53 
     54     typedef bool (*context_check_func)(const String16& name,
     55                                        const sp<IBinder>& caller,
     56                                        void* userData);
     57 
     58             bool                isContextManager(void) const;
     59             bool                becomeContextManager(
     60                                     context_check_func checkFunc,
     61                                     void* userData);
     62 
     63             sp<IBinder>         getStrongProxyForHandle(int32_t handle);
     64             wp<IBinder>         getWeakProxyForHandle(int32_t handle);
     65             void                expungeHandle(int32_t handle, IBinder* binder);
     66 
     67             void                spawnPooledThread(bool isMain);
     68 
     69             status_t            setThreadPoolMaxThreadCount(size_t maxThreads);
     70             void                giveThreadPoolName();
     71 
     72             String8             getDriverName();
     73 
     74 private:
     75     friend class IPCThreadState;
     76 
     77                                 ProcessState(const char* driver);
     78                                 ~ProcessState();
     79 
     80                                 ProcessState(const ProcessState& o);
     81             ProcessState&       operator=(const ProcessState& o);
     82             String8             makeBinderThreadName();
     83 
     84             struct handle_entry {
     85                 IBinder* binder;
     86                 RefBase::weakref_type* refs;
     87             };
     88 
     89             handle_entry*       lookupHandleLocked(int32_t handle);
     90 
     91             String8             mDriverName;
     92             int                 mDriverFD;
     93             void*               mVMStart;
     94 
     95             // Protects thread count variable below.
     96             pthread_mutex_t     mThreadCountLock;
     97             pthread_cond_t      mThreadCountDecrement;
     98             // Number of binder threads current executing a command.
     99             size_t              mExecutingThreadsCount;
    100             // Maximum number for binder threads allowed for this process.
    101             size_t              mMaxThreads;
    102             // Time when thread pool was emptied
    103             int64_t             mStarvationStartTimeMs;
    104 
    105     mutable Mutex               mLock;  // protects everything below.
    106 
    107             Vector<handle_entry>mHandleToObject;
    108 
    109             bool                mManagesContexts;
    110             context_check_func  mBinderContextCheckFunc;
    111             void*               mBinderContextUserData;
    112 
    113             KeyedVector<String16, sp<IBinder> >
    114                                 mContexts;
    115 
    116 
    117             String8             mRootDir;
    118             bool                mThreadPoolStarted;
    119     volatile int32_t            mThreadPoolSeq;
    120 };
    121 
    122 }; // namespace android
    123 
    124 // ---------------------------------------------------------------------------
    125 
    126 #endif // ANDROID_PROCESS_STATE_H
    127