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 // WARNING: this code is part of libhwbinder, a fork of libbinder. Generally, 30 // this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific 31 // code should not try to use these things. 32 33 // --------------------------------------------------------------------------- 34 namespace android { 35 namespace hardware { 36 37 class IPCThreadState; 38 39 class ProcessState : public virtual RefBase 40 { 41 public: 42 static sp<ProcessState> self(); 43 static sp<ProcessState> selfOrNull(); 44 // Note: don't call self() or selfOrNull() before initWithMmapSize() 45 // with '0' as an argument, this is the same as selfOrNull 46 static sp<ProcessState> initWithMmapSize(size_t mmapSize); // size in bytes 47 48 void startThreadPool(); 49 50 sp<IBinder> getContextObject(const sp<IBinder>& /*caller*/); 51 // only call once, without creating a pool 52 void becomeContextManager(); 53 54 sp<IBinder> getStrongProxyForHandle(int32_t handle); 55 wp<IBinder> getWeakProxyForHandle(int32_t handle); 56 void expungeHandle(int32_t handle, IBinder* binder); 57 58 void spawnPooledThread(bool isMain); 59 60 status_t setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool); 61 status_t enableOnewaySpamDetection(bool enable); 62 size_t getMaxThreads(); 63 void giveThreadPoolName(); 64 65 ssize_t getKernelReferences(size_t count, uintptr_t* buf); 66 // This refcount includes: 67 // 1. Strong references to the node by this and other processes 68 // 2. Temporary strong references held by the kernel during a 69 // transaction on the node. 70 // It does NOT include local strong references to the node 71 ssize_t getStrongRefCountForNodeByHandle(int32_t handle); 72 size_t getMmapSize(); 73 74 enum class CallRestriction { 75 // all calls okay 76 NONE, 77 // log when calls are blocking 78 ERROR_IF_NOT_ONEWAY, 79 // abort process on blocking calls 80 FATAL_IF_NOT_ONEWAY, 81 }; 82 // Sets calling restrictions for all transactions in this process. This must be called 83 // before any threads are spawned. 84 void setCallRestriction(CallRestriction restriction); 85 86 private: 87 static sp<ProcessState> init(size_t mmapSize, bool requireMmapSize); 88 89 friend class IPCThreadState; 90 explicit ProcessState(size_t mmapSize); 91 ~ProcessState(); 92 93 ProcessState(const ProcessState& o); 94 ProcessState& operator=(const ProcessState& o); 95 String8 makeBinderThreadName(); 96 97 struct handle_entry { 98 IBinder* binder; 99 RefBase::weakref_type* refs; 100 }; 101 102 handle_entry* lookupHandleLocked(int32_t handle); 103 104 int mDriverFD; 105 void* mVMStart; 106 107 // Protects thread count variable below. 108 pthread_mutex_t mThreadCountLock; 109 // Number of binder threads current executing a command. 110 size_t mExecutingThreadsCount; 111 // Maximum number for binder threads allowed for this process. 112 size_t mMaxThreads; 113 // Time when thread pool was emptied 114 int64_t mStarvationStartTimeMs; 115 116 mutable Mutex mLock; // protects everything below. 117 118 Vector<handle_entry>mHandleToObject; 119 120 String8 mRootDir; 121 bool mThreadPoolStarted; 122 bool mSpawnThreadOnStart; 123 volatile int32_t mThreadPoolSeq; 124 const size_t mMmapSize; 125 126 CallRestriction mCallRestriction; 127 }; 128 129 } // namespace hardware 130 } // namespace android 131 132 // --------------------------------------------------------------------------- 133 134 #endif // ANDROID_HARDWARE_PROCESS_STATE_H 135