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_HARDWARE_PROCESS_STATE_H
18 #define ANDROID_HARDWARE_PROCESS_STATE_H
19 
20 #include <hwbinder/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 namespace hardware {
32 
33 class IPCThreadState;
34 
35 class ProcessState : public virtual RefBase
36 {
37 public:
38     static  sp<ProcessState>    self();
39 
40             void                setContextObject(const sp<IBinder>& object);
41             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
42 
43             void                setContextObject(const sp<IBinder>& object,
44                                                  const String16& name);
45             sp<IBinder>         getContextObject(const String16& name,
46                                                  const sp<IBinder>& caller);
47 
48             void                startThreadPool();
49 
50     typedef bool (*context_check_func)(const String16& name,
51                                        const sp<IBinder>& caller,
52                                        void* userData);
53 
54             bool                isContextManager(void) const;
55             bool                becomeContextManager(
56                                     context_check_func checkFunc,
57                                     void* userData);
58 
59             sp<IBinder>         getStrongProxyForHandle(int32_t handle);
60             wp<IBinder>         getWeakProxyForHandle(int32_t handle);
61             void                expungeHandle(int32_t handle, IBinder* binder);
62 
63             void                spawnPooledThread(bool isMain);
64 
65             status_t            setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool);
66             void                giveThreadPoolName();
67 
68 private:
69     friend class IPCThreadState;
70 
71                                 ProcessState();
72                                 ~ProcessState();
73 
74                                 ProcessState(const ProcessState& o);
75             ProcessState&       operator=(const ProcessState& o);
76             String8             makeBinderThreadName();
77 
78             struct handle_entry {
79                 IBinder* binder;
80                 RefBase::weakref_type* refs;
81             };
82 
83             handle_entry*       lookupHandleLocked(int32_t handle);
84 
85             int                 mDriverFD;
86             void*               mVMStart;
87 
88             // Protects thread count variable below.
89             pthread_mutex_t     mThreadCountLock;
90             pthread_cond_t      mThreadCountDecrement;
91             // Number of binder threads current executing a command.
92             size_t              mExecutingThreadsCount;
93             // Maximum number for binder threads allowed for this process.
94             size_t              mMaxThreads;
95             // Time when thread pool was emptied
96             int64_t             mStarvationStartTimeMs;
97 
98     mutable Mutex               mLock;  // protects everything below.
99 
100             Vector<handle_entry>mHandleToObject;
101 
102             bool                mManagesContexts;
103             context_check_func  mBinderContextCheckFunc;
104             void*               mBinderContextUserData;
105 
106             KeyedVector<String16, sp<IBinder> >
107                                 mContexts;
108 
109 
110             String8             mRootDir;
111             bool                mThreadPoolStarted;
112             bool                mSpawnThreadOnStart;
113     volatile int32_t            mThreadPoolSeq;
114 };
115 
116 }; // namespace hardware
117 }; // namespace android
118 
119 // ---------------------------------------------------------------------------
120 
121 #endif // ANDROID_HARDWARE_PROCESS_STATE_H
122