blob: 05e9d09ee86d28509c17c8095459ea568a4a6a0d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/IBinder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/KeyedVector.h>
22#include <utils/String8.h>
23#include <utils/String16.h>
24
25#include <utils/threads.h>
26
Wale Ogunwale376b8222015-04-13 16:16:10 -070027#include <pthread.h>
28
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029// ---------------------------------------------------------------------------
30namespace android {
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class IPCThreadState;
33
34class ProcessState : public virtual RefBase
35{
36public:
37 static sp<ProcessState> self();
Martijn Coenen55d871c2017-03-21 15:56:40 -070038 /* initWithDriver() can be used to configure libbinder to use
39 * a different binder driver dev node. It must be called *before*
40 * any call to ProcessState::self(). /dev/binder remains the default.
41 */
42 static sp<ProcessState> initWithDriver(const char *driver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 void setContextObject(const sp<IBinder>& object);
45 sp<IBinder> getContextObject(const sp<IBinder>& caller);
46
47 void setContextObject(const sp<IBinder>& object,
48 const String16& name);
49 sp<IBinder> getContextObject(const String16& name,
50 const sp<IBinder>& caller);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52 void startThreadPool();
53
54 typedef bool (*context_check_func)(const String16& name,
55 const sp<IBinder>& caller,
56 void* userData);
57
58 bool isContextManager(void) const;
59 bool becomeContextManager(
60 context_check_func checkFunc,
61 void* userData);
62
63 sp<IBinder> getStrongProxyForHandle(int32_t handle);
64 wp<IBinder> getWeakProxyForHandle(int32_t handle);
65 void expungeHandle(int32_t handle, IBinder* binder);
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 void spawnPooledThread(bool isMain);
68
Mathias Agopian1b80f792012-04-17 16:11:08 -070069 status_t setThreadPoolMaxThreadCount(size_t maxThreads);
Mathias Agopiane3e43b32013-03-07 15:34:28 -080070 void giveThreadPoolName();
Mathias Agopian1b80f792012-04-17 16:11:08 -070071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072private:
73 friend class IPCThreadState;
74
Martijn Coenen55d871c2017-03-21 15:56:40 -070075 ProcessState(const char* driver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076 ~ProcessState();
77
78 ProcessState(const ProcessState& o);
79 ProcessState& operator=(const ProcessState& o);
Mathias Agopiane3e43b32013-03-07 15:34:28 -080080 String8 makeBinderThreadName();
Wale Ogunwale376b8222015-04-13 16:16:10 -070081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 struct handle_entry {
83 IBinder* binder;
84 RefBase::weakref_type* refs;
85 };
Wale Ogunwale376b8222015-04-13 16:16:10 -070086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 handle_entry* lookupHandleLocked(int32_t handle);
88
89 int mDriverFD;
90 void* mVMStart;
Wale Ogunwale376b8222015-04-13 16:16:10 -070091
92 // Protects thread count variable below.
93 pthread_mutex_t mThreadCountLock;
94 pthread_cond_t mThreadCountDecrement;
95 // Number of binder threads current executing a command.
96 size_t mExecutingThreadsCount;
97 // Maximum number for binder threads allowed for this process.
98 size_t mMaxThreads;
Colin Cross96e83222016-04-15 14:29:55 -070099 // Time when thread pool was emptied
100 int64_t mStarvationStartTimeMs;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mutable Mutex mLock; // protects everything below.
Wale Ogunwale376b8222015-04-13 16:16:10 -0700103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 Vector<handle_entry>mHandleToObject;
105
106 bool mManagesContexts;
107 context_check_func mBinderContextCheckFunc;
108 void* mBinderContextUserData;
Wale Ogunwale376b8222015-04-13 16:16:10 -0700109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 KeyedVector<String16, sp<IBinder> >
111 mContexts;
112
113
114 String8 mRootDir;
115 bool mThreadPoolStarted;
116 volatile int32_t mThreadPoolSeq;
117};
118
119}; // namespace android
120
121// ---------------------------------------------------------------------------
122
123#endif // ANDROID_PROCESS_STATE_H