• 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_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     static  sp<ProcessState>    selfOrNull();
39 
40     /* initWithDriver() can be used to configure libbinder to use
41      * a different binder driver dev node. It must be called *before*
42      * any call to ProcessState::self(). The default is /dev/vndbinder
43      * for processes built with the VNDK and /dev/binder for those
44      * which are not.
45      */
46     static  sp<ProcessState>    initWithDriver(const char *driver);
47 
48             sp<IBinder>         getContextObject(const sp<IBinder>& caller);
49 
50             void                startThreadPool();
51 
52     typedef bool (*context_check_func)(const String16& name,
53                                        const sp<IBinder>& caller,
54                                        void* userData);
55 
56             bool                becomeContextManager(
57                                     context_check_func checkFunc,
58                                     void* userData);
59 
60             sp<IBinder>         getStrongProxyForHandle(int32_t handle);
61             void                expungeHandle(int32_t handle, IBinder* binder);
62 
63             void                spawnPooledThread(bool isMain);
64 
65             status_t            setThreadPoolMaxThreadCount(size_t maxThreads);
66             void                giveThreadPoolName();
67 
68             String8             getDriverName();
69 
70             ssize_t             getKernelReferences(size_t count, uintptr_t* buf);
71 
72                                 // Only usable by the context manager.
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 
80             enum class CallRestriction {
81                 // all calls okay
82                 NONE,
83                 // log when calls are blocking
84                 ERROR_IF_NOT_ONEWAY,
85                 // abort process on blocking calls
86                 FATAL_IF_NOT_ONEWAY,
87             };
88             // Sets calling restrictions for all transactions in this process. This must be called
89             // before any threads are spawned.
90             void setCallRestriction(CallRestriction restriction);
91 
92 private:
93     friend class IPCThreadState;
94 
95             explicit            ProcessState(const char* driver);
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             String8             mDriverName;
110             int                 mDriverFD;
111             void*               mVMStart;
112 
113             // Protects thread count variable below.
114             pthread_mutex_t     mThreadCountLock;
115             pthread_cond_t      mThreadCountDecrement;
116             // Number of binder threads current executing a command.
117             size_t              mExecutingThreadsCount;
118             // Maximum number for binder threads allowed for this process.
119             size_t              mMaxThreads;
120             // Time when thread pool was emptied
121             int64_t             mStarvationStartTimeMs;
122 
123     mutable Mutex               mLock;  // protects everything below.
124 
125             Vector<handle_entry>mHandleToObject;
126 
127             context_check_func  mBinderContextCheckFunc;
128             void*               mBinderContextUserData;
129 
130             String8             mRootDir;
131             bool                mThreadPoolStarted;
132     volatile int32_t            mThreadPoolSeq;
133 
134             CallRestriction     mCallRestriction;
135 };
136 
137 } // namespace android
138 
139 // ---------------------------------------------------------------------------
140 
141 #endif // ANDROID_PROCESS_STATE_H
142