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// ---------------------------------------------------------------------------
30namespace android {
31
32class IPCThreadState;
33
34class ProcessState : public virtual RefBase
35{
36public:
37    static  sp<ProcessState>    self();
38    static  sp<ProcessState>    selfOrNull();
39    /* initWithDriver() can be used to configure libbinder to use
40     * a different binder driver dev node. It must be called *before*
41     * any call to ProcessState::self(). /dev/binder remains the default.
42     */
43    static  sp<ProcessState>    initWithDriver(const char *driver);
44
45            void                setContextObject(const sp<IBinder>& object);
46            sp<IBinder>         getContextObject(const sp<IBinder>& caller);
47
48            void                setContextObject(const sp<IBinder>& object,
49                                                 const String16& name);
50            sp<IBinder>         getContextObject(const String16& name,
51                                                 const sp<IBinder>& caller);
52
53            void                startThreadPool();
54
55    typedef bool (*context_check_func)(const String16& name,
56                                       const sp<IBinder>& caller,
57                                       void* userData);
58
59            bool                isContextManager(void) const;
60            bool                becomeContextManager(
61                                    context_check_func checkFunc,
62                                    void* userData);
63
64            sp<IBinder>         getStrongProxyForHandle(int32_t handle);
65            wp<IBinder>         getWeakProxyForHandle(int32_t handle);
66            void                expungeHandle(int32_t handle, IBinder* binder);
67
68            void                spawnPooledThread(bool isMain);
69
70            status_t            setThreadPoolMaxThreadCount(size_t maxThreads);
71            void                giveThreadPoolName();
72
73            String8             getDriverName();
74
75            ssize_t             getKernelReferences(size_t count, uintptr_t* buf);
76
77private:
78    friend class IPCThreadState;
79
80                                ProcessState(const char* driver);
81                                ~ProcessState();
82
83                                ProcessState(const ProcessState& o);
84            ProcessState&       operator=(const ProcessState& o);
85            String8             makeBinderThreadName();
86
87            struct handle_entry {
88                IBinder* binder;
89                RefBase::weakref_type* refs;
90            };
91
92            handle_entry*       lookupHandleLocked(int32_t handle);
93
94            String8             mDriverName;
95            int                 mDriverFD;
96            void*               mVMStart;
97
98            // Protects thread count variable below.
99            pthread_mutex_t     mThreadCountLock;
100            pthread_cond_t      mThreadCountDecrement;
101            // Number of binder threads current executing a command.
102            size_t              mExecutingThreadsCount;
103            // Maximum number for binder threads allowed for this process.
104            size_t              mMaxThreads;
105            // Time when thread pool was emptied
106            int64_t             mStarvationStartTimeMs;
107
108    mutable Mutex               mLock;  // protects everything below.
109
110            Vector<handle_entry>mHandleToObject;
111
112            bool                mManagesContexts;
113            context_check_func  mBinderContextCheckFunc;
114            void*               mBinderContextUserData;
115
116            KeyedVector<String16, sp<IBinder> >
117                                mContexts;
118
119
120            String8             mRootDir;
121            bool                mThreadPoolStarted;
122    volatile int32_t            mThreadPoolSeq;
123};
124
125}; // namespace android
126
127// ---------------------------------------------------------------------------
128
129#endif // ANDROID_PROCESS_STATE_H
130