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 #pragma once 18 19 #include <binder/IBinder.h> 20 #include <utils/KeyedVector.h> 21 #include <utils/Mutex.h> 22 #include <utils/String16.h> 23 #include <utils/String8.h> 24 25 #include <pthread.h> 26 27 // --------------------------------------------------------------------------- 28 namespace android { 29 30 class IPCThreadState; 31 32 /** 33 * Kernel binder process state. All operations here refer to kernel binder. This 34 * object is allocated per process. 35 */ 36 class ProcessState : public virtual RefBase { 37 public: 38 static sp<ProcessState> self(); 39 static sp<ProcessState> selfOrNull(); 40 41 static bool isVndservicemanagerEnabled(); 42 43 /* initWithDriver() can be used to configure libbinder to use 44 * a different binder driver dev node. It must be called *before* 45 * any call to ProcessState::self(). The default is /dev/vndbinder 46 * for processes built with the VNDK and /dev/binder for those 47 * which are not. 48 * 49 * If this is called with nullptr, the behavior is the same as selfOrNull. 50 */ 51 static sp<ProcessState> initWithDriver(const char* driver); 52 53 sp<IBinder> getContextObject(const sp<IBinder>& caller); 54 55 // For main functions - dangerous for libraries to use 56 void startThreadPool(); 57 58 bool becomeContextManager(); 59 60 sp<IBinder> getStrongProxyForHandle(int32_t handle); 61 void expungeHandle(int32_t handle, IBinder* binder); 62 63 // TODO: deprecate. 64 void spawnPooledThread(bool isMain); 65 66 // For main functions - dangerous for libraries to use 67 status_t setThreadPoolMaxThreadCount(size_t maxThreads); 68 status_t enableOnewaySpamDetection(bool enable); 69 70 // Set the name of the current thread to look like a threadpool 71 // thread. Typically this is called before joinThreadPool. 72 // 73 // TODO: remove this API, and automatically set it intelligently. 74 void giveThreadPoolName(); 75 76 String8 getDriverName(); 77 78 ssize_t getKernelReferences(size_t count, uintptr_t* buf); 79 80 // Only usable by the context manager. 81 // This refcount includes: 82 // 1. Strong references to the node by this and other processes 83 // 2. Temporary strong references held by the kernel during a 84 // transaction on the node. 85 // It does NOT include local strong references to the node 86 ssize_t getStrongRefCountForNode(const sp<BpBinder>& binder); 87 88 enum class CallRestriction { 89 // all calls okay 90 NONE, 91 // log when calls are blocking 92 ERROR_IF_NOT_ONEWAY, 93 // abort process on blocking calls 94 FATAL_IF_NOT_ONEWAY, 95 }; 96 // Sets calling restrictions for all transactions in this process. This must be called 97 // before any threads are spawned. 98 void setCallRestriction(CallRestriction restriction); 99 100 /** 101 * Get the max number of threads that have joined the thread pool. 102 * This includes kernel started threads, user joined threads and polling 103 * threads if used. 104 */ 105 size_t getThreadPoolMaxTotalThreadCount() const; 106 107 /** 108 * Check to see if the thread pool has started. 109 */ 110 bool isThreadPoolStarted() const; 111 112 enum class DriverFeature { 113 ONEWAY_SPAM_DETECTION, 114 EXTENDED_ERROR, 115 }; 116 // Determine whether a feature is supported by the binder driver. 117 static bool isDriverFeatureEnabled(const DriverFeature feature); 118 119 private: 120 static sp<ProcessState> init(const char* defaultDriver, bool requireDefault); 121 122 static void onFork(); 123 static void parentPostFork(); 124 static void childPostFork(); 125 126 friend class IPCThreadState; 127 friend class sp<ProcessState>; 128 129 explicit ProcessState(const char* driver); 130 ~ProcessState(); 131 132 ProcessState(const ProcessState& o); 133 ProcessState& operator=(const ProcessState& o); 134 String8 makeBinderThreadName(); 135 136 struct handle_entry { 137 IBinder* binder; 138 RefBase::weakref_type* refs; 139 }; 140 141 handle_entry* lookupHandleLocked(int32_t handle); 142 143 String8 mDriverName; 144 int mDriverFD; 145 void* mVMStart; 146 147 // Protects thread count and wait variables below. 148 mutable pthread_mutex_t mThreadCountLock; 149 // Broadcast whenever mWaitingForThreads > 0 150 pthread_cond_t mThreadCountDecrement; 151 // Number of binder threads current executing a command. 152 size_t mExecutingThreadsCount; 153 // Number of threads calling IPCThreadState::blockUntilThreadAvailable() 154 size_t mWaitingForThreads; 155 // Maximum number of lazy threads to be started in the threadpool by the kernel. 156 size_t mMaxThreads; 157 // Current number of threads inside the thread pool. 158 size_t mCurrentThreads; 159 // Current number of pooled threads inside the thread pool. 160 size_t mKernelStartedThreads; 161 // Time when thread pool was emptied 162 int64_t mStarvationStartTimeMs; 163 164 mutable Mutex mLock; // protects everything below. 165 166 Vector<handle_entry> mHandleToObject; 167 168 bool mForked; 169 bool mThreadPoolStarted; 170 volatile int32_t mThreadPoolSeq; 171 172 CallRestriction mCallRestriction; 173 }; 174 175 } // namespace android 176 177 // --------------------------------------------------------------------------- 178