1 24 25 package org.objectweb.dream.control.lifecycle; 26 27 import org.objectweb.fractal.api.Component; 28 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 29 import org.objectweb.fractal.api.factory.InstantiationException; 30 import org.objectweb.fractal.julia.InitializationContext; 31 import org.objectweb.fractal.julia.control.lifecycle.LifeCycleCoordinator; 32 import org.objectweb.util.monolog.api.BasicLevel; 33 import org.objectweb.util.monolog.api.Logger; 34 35 50 public abstract class ThreadCounterMixin implements LifeCycleCoordinator 51 { 52 53 54 protected Thread [] fcExecutingThreads; 55 56 protected int[] fcInvocationCounters; 57 58 protected int fcNbExcutingThread; 59 60 protected LifeCycleCoordinator fcCoordinator; 61 62 66 69 public void initFcController(final InitializationContext ic) 70 throws InstantiationException 71 { 72 73 fcExecutingThreads = new Thread [2]; 75 fcInvocationCounters = new int[2]; 76 fcNbExcutingThread = 0; 77 78 _super_initFcController(ic); 79 } 80 81 84 public boolean setFcStarted() throws IllegalLifeCycleException 85 { 86 synchronized (this) 87 { 88 if (_super_setFcStarted()) 89 { 90 notifyAll(); 91 return true; 92 } 93 return false; 94 } 95 } 96 97 100 public void setFcStopping(LifeCycleCoordinator coordinator) 101 throws IllegalLifeCycleException 102 { 103 synchronized (this) 104 { 105 _this_weaveableLCCLogger.log(BasicLevel.DEBUG, "Stopping component"); 106 _this_weaveableFcState = 1; 107 fcCoordinator = coordinator; 108 if (!_this_isInactivated(coordinator)) 109 { 110 for (int i = 0; i < fcExecutingThreads.length; i++) 111 { 112 if (fcExecutingThreads[i] != null) 114 { 115 fcExecutingThreads[i].interrupt(); 116 } 117 } 118 } 119 } 120 } 121 122 125 public boolean setFcStopped() throws IllegalLifeCycleException 126 { 127 synchronized (this) 128 { 129 if (_super_setFcStopped()) 130 { 131 fcCoordinator = null; 132 return true; 133 } 134 return false; 135 } 136 } 137 138 142 158 public int addCurrentThread(String s) 159 { 160 synchronized (this) 161 { 162 boolean ok; 163 if (_this_weaveableFcState != 2) 164 { 165 do 166 { 167 if (_this_weaveableFcState == 0) 168 { 169 ok = false; 170 } 171 else if (_this_weaveableFcState == 1) 172 { 173 if (fcNbExcutingThread == 0) 174 { 175 ok = fcCoordinator.fcActivated(_this_getFcCoordinator()); 176 } 177 else 178 { 179 ok = true; 180 } 181 } 182 else 183 { 184 ok = true; 185 } 186 187 if (!ok) 188 { 189 try 190 { 191 wait(); 192 } 193 catch (InterruptedException e) 194 { 195 196 } 197 } 198 } 199 while (!ok); 200 } 201 202 return registerCurrentThread(); 203 } 204 } 205 206 215 public void removeCurrentThread(String s, int i) 216 { 217 synchronized (this) 218 { 219 unregisterCurrentThread(i); 220 } 221 } 222 223 228 public int registerCurrentThread() 229 { 230 int freeIndex = -1; 231 Thread currentThread = Thread.currentThread(); 232 for (int i = 0; i < fcExecutingThreads.length; i++) 233 { 234 Thread thread = fcExecutingThreads[i]; 235 if (thread == currentThread) 236 { 237 fcInvocationCounters[i]++; 238 return i; 239 } 240 else if (thread == null) 241 { 242 freeIndex = i; 243 } 244 } 245 if (freeIndex == -1) 246 { 247 Thread [] precThreads = fcExecutingThreads; 249 int[] precCounters = fcInvocationCounters; 250 fcExecutingThreads = new Thread [precThreads.length + 2]; 251 fcInvocationCounters = new int[precThreads.length + 2]; 252 253 System.arraycopy(precThreads, 0, fcExecutingThreads, 0, 254 precThreads.length); 255 System.arraycopy(precCounters, 0, fcInvocationCounters, 0, 256 precCounters.length); 257 freeIndex = precThreads.length; 258 } 259 260 fcExecutingThreads[freeIndex] = currentThread; 261 fcInvocationCounters[freeIndex] = 1; 262 fcNbExcutingThread++; 263 return freeIndex; 264 } 265 266 271 public void unregisterCurrentThread(int i) 272 { 273 fcInvocationCounters[i]--; 274 if (fcInvocationCounters[i] == 0) 275 { 276 fcExecutingThreads[i] = null; 277 fcNbExcutingThread--; 278 _this_isInactivated(fcCoordinator); 279 } 280 } 281 282 288 public boolean checkInactivity() 289 { 290 return _this_weaveableFcState != 2 && fcNbExcutingThread == 0 291 && _super_checkInactivity(); 292 } 293 294 298 303 public Component _this_weaveableC; 304 305 311 public int _this_weaveableFcState; 312 313 317 public Logger _this_weaveableLCCLogger; 318 319 325 public abstract boolean _super_setFcStarted() 326 throws IllegalLifeCycleException; 327 328 334 public abstract boolean _super_setFcStopped() 335 throws IllegalLifeCycleException; 336 337 344 public abstract boolean _super_checkInactivity(); 345 346 353 public abstract LifeCycleCoordinator _this_getFcCoordinator(); 354 355 362 public abstract boolean _this_isInactivated(LifeCycleCoordinator coordinator); 363 364 370 public abstract void _super_initFcController(InitializationContext ic) 371 throws InstantiationException ; 372 } | Popular Tags |