• 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     // Note: don't call self() or selfOrNull() before initWithMmapSize()
41     static  sp<ProcessState>    initWithMmapSize(size_t mmapSize); // size in bytes
42 
43             void                setContextObject(const sp<IBinder>& object);
44             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
45 
46             void                setContextObject(const sp<IBinder>& object,
47                                                  const String16& name);
48             sp<IBinder>         getContextObject(const String16& name,
49                                                  const sp<IBinder>& caller);
50 
51             void                startThreadPool();
52 
53     typedef bool (*context_check_func)(const String16& name,
54                                        const sp<IBinder>& caller,
55                                        void* userData);
56 
57             bool                isContextManager(void) const;
58             bool                becomeContextManager(
59                                     context_check_func checkFunc,
60                                     void* userData);
61 
62             sp<IBinder>         getStrongProxyForHandle(int32_t handle);
63             wp<IBinder>         getWeakProxyForHandle(int32_t handle);
64             void                expungeHandle(int32_t handle, IBinder* binder);
65 
66             void                spawnPooledThread(bool isMain);
67 
68             status_t            setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool);
69             size_t              getMaxThreads();
70             void                giveThreadPoolName();
71 
72             ssize_t             getKernelReferences(size_t count, uintptr_t* buf);
73                                 // This refcount includes:
74                                 // 1. Strong references to the node by this  and other processes
75                                 // 2. Temporary strong references held by the kernel during a
76                                 //    transaction on the node.
77                                 // It does NOT include local strong references to the node
78             ssize_t             getStrongRefCountForNodeByHandle(int32_t handle);
79             size_t              getMmapSize();
80 
81             enum class CallRestriction {
82                 // all calls okay
83                 NONE,
84                 // log when calls are blocking
85                 ERROR_IF_NOT_ONEWAY,
86                 // abort process on blocking calls
87                 FATAL_IF_NOT_ONEWAY,
88             };
89             // Sets calling restrictions for all transactions in this process. This must be called
90             // before any threads are spawned.
91             void setCallRestriction(CallRestriction restriction);
92 
93 private:
94     friend class IPCThreadState;
95             explicit            ProcessState(size_t mmap_size);
96                                 ~ProcessState();
97 
98                                 ProcessState(const ProcessState& o);
99             ProcessState&       operator=(const ProcessState& o);
100             String8             makeBinderThreadName();
101 
102             struct handle_entry {
103                 IBinder* binder;
104                 RefBase::weakref_type* refs;
105             };
106 
107             handle_entry*       lookupHandleLocked(int32_t handle);
108 
109             int                 mDriverFD;
110             void*               mVMStart;
111 
112             // Protects thread count variable below.
113             pthread_mutex_t     mThreadCountLock;
114             pthread_cond_t      mThreadCountDecrement;
115             // Number of binder threads current executing a command.
116             size_t              mExecutingThreadsCount;
117             // Maximum number for binder threads allowed for this process.
118             size_t              mMaxThreads;
119             // Time when thread pool was emptied
120             int64_t             mStarvationStartTimeMs;
121 
122     mutable Mutex               mLock;  // protects everything below.
123 
124             Vector<handle_entry>mHandleToObject;
125 
126             bool                mManagesContexts;
127             context_check_func  mBinderContextCheckFunc;
128             void*               mBinderContextUserData;
129 
130             KeyedVector<String16, sp<IBinder> >
131                                 mContexts;
132 
133 
134             String8             mRootDir;
135             bool                mThreadPoolStarted;
136             bool                mSpawnThreadOnStart;
137     volatile int32_t            mThreadPoolSeq;
138             const size_t        mMmapSize;
139 
140             CallRestriction     mCallRestriction;
141 };
142 
143 }; // namespace hardware
144 }; // namespace android
145 
146 // ---------------------------------------------------------------------------
147 
148 #endif // ANDROID_HARDWARE_PROCESS_STATE_H
149