1 9 package javolution.realtime; 10 11 41 public final class PoolContext extends Context { 42 43 46 final ObjectPool[] _pools = new ObjectPool[ObjectFactory.MAX]; 47 48 51 private final ObjectPool[] _inUsePools = new ObjectPool[ObjectFactory.MAX]; 52 53 56 private int _inUsePoolsLength; 57 58 61 PoolContext() { 62 for (int i=_pools.length; i > 0;) { 63 _pools[--i] = ObjectPool.NULL; 64 } 65 } 66 67 70 public static void enter() { 71 PoolContext ctx = (PoolContext) push(POOL_CONTEXT_CLASS); 72 if (ctx == null) { 73 ctx = new PoolContext(); 74 push(ctx); 75 } 76 PoolContext outer = ctx.getOuter().poolContext(); 77 if (outer != null) { 78 outer.setInUsePoolsLocal(false); 79 } 80 } 81 private static final Class POOL_CONTEXT_CLASS = new PoolContext().getClass(); 82 83 89 public static void exit() { 90 PoolContext ctx = (PoolContext) pop(); 91 ctx.recyclePools(); 92 PoolContext outer = ctx.getOuter().poolContext(); 93 if (outer != null) { 94 outer.setInUsePoolsLocal(true); 95 } 96 } 97 98 protected void dispose() { 100 for (int i = ObjectFactory.Count; i > 0;) { 101 ObjectPool pool = _pools[--i]; 102 if (pool != ObjectPool.NULL) { 103 pool.clearAll(); 104 } 105 } 106 _inUsePoolsLength = 0; 107 } 108 109 115 void setInUsePoolsLocal(boolean areLocal) { 116 Thread user = areLocal ? getOwner() : null; 117 for (int i = _inUsePoolsLength; i > 0;) { 118 _inUsePools[--i].user = user; 119 } 120 } 121 122 128 ObjectPool getLocalPool(int index) { 129 ObjectPool pool = _pools[index]; 130 return (pool.user != null) ? pool : getLocalPool2(index); 131 } 132 private ObjectPool getLocalPool2(int index) { 133 ObjectPool pool = getPool(index); 134 pool.user = getOwner(); 135 return pool; 136 } 137 138 145 private ObjectPool getPool(int index) { 146 ObjectPool pool = _pools[index]; 147 if (pool == ObjectPool.NULL) { pool = ObjectFactory.INSTANCES[index].newPool(); 149 _pools[index] = pool; 150 } 151 if (!pool.inUse) { pool.inUse = true; 153 _inUsePools[_inUsePoolsLength++] = pool; 154 PoolContext outerPoolContext = this.getOuter().poolContext(); 155 if (outerPoolContext != null) { 156 synchronized (outerPoolContext) { pool.outer = outerPoolContext.getPool(index); 158 } 159 } else { 160 pool.outer = null; 161 } 162 } 163 return pool; 164 } 165 166 169 void recyclePools() { 170 for (int i = _inUsePoolsLength; i > 0;) { 172 ObjectPool pool = _inUsePools[--i]; 173 pool.recycleAll(); 174 pool.user = null; 175 pool.inUse = false; 176 } 177 _inUsePoolsLength = 0; 178 } 179 180 } | Popular Tags |