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