blob: d78bfac1f87886eb7a7d030c53acba38fe752ef1 [file] [log] [blame]
Svetoslav Ganov758143e2012-08-06 16:40:27 -07001/*
2 * Copyright (C) 2012 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
17package com.android.internal.os;
18
Mathew Inwoodc185f082018-08-20 14:28:54 +010019import android.annotation.UnsupportedAppUsage;
20
Svetoslav Ganov758143e2012-08-06 16:40:27 -070021/**
22 * Helper class for passing more arguments though a message
23 * and avoiding allocation of a custom class for wrapping the
24 * arguments. This class maintains a pool of instances and
25 * it is responsibility of the client to recycle and instance
26 * once it is no longer used.
27 */
28public final class SomeArgs {
29
30 private static final int MAX_POOL_SIZE = 10;
31
32 private static SomeArgs sPool;
33 private static int sPoolSize;
34 private static Object sPoolLock = new Object();
35
36 private SomeArgs mNext;
37
38 private boolean mInPool;
39
Dianne Hackborn91097de2014-04-04 18:02:06 -070040 static final int WAIT_NONE = 0;
41 static final int WAIT_WAITING = 1;
42 static final int WAIT_FINISHED = 2;
43 int mWaitState = WAIT_NONE;
44
Mathew Inwoodc185f082018-08-20 14:28:54 +010045 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070046 public Object arg1;
Mathew Inwoodc185f082018-08-20 14:28:54 +010047 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070048 public Object arg2;
Mathew Inwoodc185f082018-08-20 14:28:54 +010049 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070050 public Object arg3;
51 public Object arg4;
Dianne Hackbornc4aad012013-02-22 15:05:25 -080052 public Object arg5;
Svetoslava798c0a2014-05-15 10:47:19 -070053 public Object arg6;
Filip Gruszczynski2217f612015-05-26 11:32:08 -070054 public Object arg7;
Jorim Jaggia7262a82015-11-03 15:15:40 +010055 public Object arg8;
Adrian Roos5c6b6222017-11-07 17:36:10 +010056 public Object arg9;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070057 public int argi1;
Mathew Inwoodc185f082018-08-20 14:28:54 +010058 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070059 public int argi2;
Mathew Inwoodc185f082018-08-20 14:28:54 +010060 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070061 public int argi3;
62 public int argi4;
63 public int argi5;
64 public int argi6;
65
66 private SomeArgs() {
67 /* do nothing - reduce visibility */
68 }
69
Mathew Inwoodc185f082018-08-20 14:28:54 +010070 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070071 public static SomeArgs obtain() {
72 synchronized (sPoolLock) {
73 if (sPoolSize > 0) {
74 SomeArgs args = sPool;
75 sPool = sPool.mNext;
76 args.mNext = null;
77 args.mInPool = false;
78 sPoolSize--;
79 return args;
80 } else {
81 return new SomeArgs();
82 }
83 }
84 }
85
Dianne Hackbornd0a15902015-07-15 11:18:09 -070086 public void complete() {
87 synchronized (this) {
88 if (mWaitState != WAIT_WAITING) {
89 throw new IllegalStateException("Not waiting");
90 }
91 mWaitState = WAIT_FINISHED;
92 notifyAll();
93 }
94 }
95
Mathew Inwoodc185f082018-08-20 14:28:54 +010096 @UnsupportedAppUsage
Svetoslav Ganov758143e2012-08-06 16:40:27 -070097 public void recycle() {
98 if (mInPool) {
99 throw new IllegalStateException("Already recycled.");
100 }
Dianne Hackborn91097de2014-04-04 18:02:06 -0700101 if (mWaitState != WAIT_NONE) {
102 return;
103 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700104 synchronized (sPoolLock) {
105 clear();
106 if (sPoolSize < MAX_POOL_SIZE) {
107 mNext = sPool;
108 mInPool = true;
109 sPool = this;
110 sPoolSize++;
111 }
112 }
113 }
114
115 private void clear() {
116 arg1 = null;
117 arg2 = null;
118 arg3 = null;
119 arg4 = null;
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800120 arg5 = null;
Svetoslava798c0a2014-05-15 10:47:19 -0700121 arg6 = null;
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700122 arg7 = null;
Bin Chen5f0521e2019-02-20 17:34:04 +0800123 arg8 = null;
124 arg9 = null;
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700125 argi1 = 0;
126 argi2 = 0;
127 argi3 = 0;
128 argi4 = 0;
129 argi5 = 0;
130 argi6 = 0;
131 }
132}