public void BossWorkerDemo() { int n = 10; Latch startPermit = new Latch(); Semaphore wait = new Semaphore(-(n - 1)); Worker [] workers = new Worker[n]; for (int i = 0; i < n; ++i) { workers[i] = new Worker(startPermit, wait); new Thread(new ThreadStart(workers[i].Work)).Start(); } // very slow main initialization ... // ... parse configuration // ... initialize other resources used by workers startPermit.Release(); // now it is our turn to wait for workers wait.Acquire(); for (int i = 0; i < n; ++i) { Assert.IsTrue(workers[i].worked); } }
protected void SetUp_ () { deployPath = Guid.NewGuid ().ToString (); sampleDir = Path.Combine (deployPath, deploy); sampleDir2 = Path.Combine (deployPath, deploy + "2"); serviceXml = Path.Combine (sampleDir, Application.ServiceXml); watcherXml = Path.Combine (sampleDir, Application.WatcherXml); triggeredLatch = new Latch(); trigger = new ThreadingTimerTrigger (50); trigger.Triggered += new EventHandler(trigger_Triggered); dispatcher = new AggregatedDeployEventDispatcher (trigger); TestUtils.ConfigureLog4Net (); }
public void UsingLikeAMutex() { Semaphore semaphore = new Semaphore(1); Latch latch = new Latch(); Helper helper = new Helper(semaphore, latch); Thread thread = new Thread(new ThreadStart(helper.Go)); semaphore.Acquire(); thread.Start(); latch.Acquire(); Assert.IsFalse(helper.gone); semaphore.Release(); thread.Join(); Assert.IsTrue(helper.gone, "not gone"); }
public void UsingLikeAMutex () { Semaphore semaphore = new Semaphore (1); Latch latch = new Latch (); Helper helper = new Helper (semaphore, latch); Thread thread = new Thread (new ThreadStart (helper.Go)); semaphore.Acquire (); thread.Start (); latch.Acquire (); Assert.IsFalse (helper.gone); semaphore.Release (); thread.Join (); Assert.IsTrue (helper.gone, "not gone"); }
public void CanBeInterruptedAgainAfterInterruption() { Latch stop = new Latch (); Latch go = new Latch (); InterruptAgainStrategy strategy = new InterruptAgainStrategy (go, stop); Runner runner = new Runner (strategy); Thread t = StartedThread (runner); Log ("Thread #" + t.GetHashCode () + " started: interrupting it"); t.Interrupt (); Log ("Thread #" + t.GetHashCode () + " interrupted"); runner.Interrupted (); Log ("artificially signalled interruption"); go.Acquire (); t.Interrupt (); stop.Release (); Log ("Joining thread #" + t.GetHashCode ()); t.Join (); Assert.IsTrue (strategy.interrupted, "thread could not take any action after interruption"); Assert.IsTrue (strategy.reInterrupted, "thread could not interrupt itself after interruption"); }
public void CanBeInterruptedAgainAfterInterruption() { Latch stop = new Latch(); Latch go = new Latch(); InterruptAgainStrategy strategy = new InterruptAgainStrategy(go, stop); Runner runner = new Runner(strategy); Thread t = StartedThread(runner); Log("Thread #" + t.GetHashCode() + " started: interrupting it"); t.Interrupt(); Log("Thread #" + t.GetHashCode() + " interrupted"); runner.Interrupted(); Log("artificially signalled interruption"); go.Acquire(); t.Interrupt(); stop.Release(); Log("Joining thread #" + t.GetHashCode()); t.Join(); Assert.IsTrue(strategy.interrupted, "thread could not take any action after interruption"); Assert.IsTrue(strategy.reInterrupted, "thread could not interrupt itself after interruption"); }
public void BossWorkerDemo() { int n = 10; Latch startPermit = new Latch(); Semaphore wait = new Semaphore(-(n - 1)); Worker [] workers = new Worker[n]; for (int i=0; i<n; ++i) { workers[i] = new Worker(startPermit, wait); new Thread(new ThreadStart(workers[i].Work)).Start(); } // very slow main initialization ... // ... parse configuration // ... initialize other resources used by workers startPermit.Release(); // now it is our turn to wait for workers wait.Acquire(); for (int i=0; i<n; ++i) Assert.IsTrue(workers[i].worked); }
public void Init(Latch latch, ISync sync, IObjectPool objectPool) { this.sync = sync; this.latch = latch; this.objectPool = objectPool; }
public Helper(Latch latch, ISync sync, IObjectPool objectPool) { Init(latch, sync, objectPool); }
public Helper(ISync sync, Latch latch) { Init(latch, sync, null); }
public void WaitOnBorrowWhenExausted() { int n = 100; object[] objects = new object[n]; pool = new SimplePool(new MyFactory(), n); for (int i = 0; i < n; i++) { objects[i] = pool.BorrowObject(); } Latch latch = new Latch(); ISync sync = new Latch(); Helper helper = new Helper(latch, sync, pool); Thread thread = new Thread(new ThreadStart(helper.UsePool)); thread.Start(); Assert.AreEqual(n, pool.NumActive); Assert.AreEqual(0, pool.NumIdle); object released = objects[n - 1]; pool.ReturnObject(released); latch.Acquire(); Assert.AreEqual(n, pool.NumActive); Assert.AreEqual(0, pool.NumIdle); Assert.IsNotNull(helper.gotFromPool, "helper did not get from pool"); Assert.AreSame(released, helper.gotFromPool, "got unexpected object"); }
public void WaitForStartedBeforeStopping() { MyService serviceable = new MyService(sync1, sync2); ISync starting = new Latch(); ISync started = new Latch(); BlockWhileStartingExecutor executor = new BlockWhileStartingExecutor(starting, started); MyServiceSupport support = new MyServiceSupport(executor, serviceable); executor.ServiceSupport = support; Thread startThread = new Thread(new ThreadStart(executor.Start)); startThread.Name = "start"; startThread.Start(); Log ("start thread started"); Latch stopping = new Latch(); Latch stopped = new Latch(); StoppingHelper helper = new StoppingHelper(support, stopping, stopped); Thread stopThread = new Thread(new ThreadStart(helper.Stop)); stopThread.Name = "stop"; stopThread.Start(); Log ("stop thread started: waiting for stopping ..."); stopping.Acquire(); Log ("stopping in progress ..."); Assert.IsFalse(executor.wasStarted); Assert.IsFalse(helper.wasStopped, "helper could stop before expected"); Log ("allow to start ..."); starting.Release(); Log ("waiting for started ..."); started.Acquire(); Assert.IsTrue(executor.wasStarted); stopped.Acquire(); Log ("waiting for stop ..."); Assert.IsTrue(helper.wasStopped); Log ("stopped ..."); }
public void SetUp () { springAssembliesDeployer = new SpringAssembliesDeployer("."); location = new FileSystemDeployLocation(deployPath); location.StartWatching(); sync = new Latch(); defaultDeployer = new SyncedDeployer(sync); deployManager = new DeployManager(springAssembliesDeployer, location, defaultDeployer); deployManager.Start(); }
public void SetUp () { TestUtils.ConfigureLog4Net(); dispatched = false; appName = Guid.NewGuid().ToString(); appFullPath = Path.GetFullPath(appName); anotherAppName = Guid.NewGuid().ToString(); anotherAppFullPath = Path.GetFullPath(anotherAppName); Directory.CreateDirectory(appFullPath); Directory.CreateDirectory(anotherAppFullPath); eventLatch = new Latch(); controlledEventLatch = new Latch(); dispatcher = new ForwardingDeployEventDispatcher(); application = new Application(appFullPath); dispatcher.DeployEvent += new DeployEventHandler(dispatcher_DeployEvent); watcher = new FileSystemApplicationWatcher(application); }