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 size_t getMmapSize(); 74 private: 75 friend class IPCThreadState; 76 77 ProcessState(size_t mmap_size); 78 ~ProcessState(); 79 80 ProcessState(const ProcessState& o); 81 ProcessState& operator=(const ProcessState& o); 82 String8 makeBinderThreadName(); 83 84 struct handle_entry { 85 IBinder* binder; 86 RefBase::weakref_type* refs; 87 }; 88 89 handle_entry* lookupHandleLocked(int32_t handle); 90 91 int mDriverFD; 92 void* mVMStart; 93 94 // Protects thread count variable below. 95 pthread_mutex_t mThreadCountLock; 96 pthread_cond_t mThreadCountDecrement; 97 // Number of binder threads current executing a command. 98 size_t mExecutingThreadsCount; 99 // Maximum number for binder threads allowed for this process. 100 size_t mMaxThreads; 101 // Time when thread pool was emptied 102 int64_t mStarvationStartTimeMs; 103 104 mutable Mutex mLock; // protects everything below. 105 106 Vector<handle_entry>mHandleToObject; 107 108 bool mManagesContexts; 109 context_check_func mBinderContextCheckFunc; 110 void* mBinderContextUserData; 111 112 KeyedVector<String16, sp<IBinder> > 113 mContexts; 114 115 116 String8 mRootDir; 117 bool mThreadPoolStarted; 118 bool mSpawnThreadOnStart; 119 volatile int32_t mThreadPoolSeq; 120 size_t mMmapSize; 121 }; 122 123 }; // namespace hardware 124 }; // namespace android 125 126 // --------------------------------------------------------------------------- 127 128 #endif // ANDROID_HARDWARE_PROCESS_STATE_H 129