#region --------------- Copyright Dresser Wayne Pignone ------------- /* * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/SynchronousRootStateMachine.cs $ * * 5 08-03-10 15:01 Mattias.larsson * Removed Paused and StepEvent(). * * 4 08-01-23 16:34 Mattias.larsson * * 3 07-10-22 11:32 roger.månsson * Use new Reentrancy mutex to protect against reentrancy in the * HandledQueuedEvents method. * * 2 07-08-15 15:30 roger.månsson * Changed behaviour so that when IncomingEvent is called and someone is * already processing an event, the event is just queued and will be * processed by the thread that is currently executing. This will remove a * lot of delays waiting to run, and reduce the risk for deadlocking in * applications. * * 1 07-03-12 15:00 roger.månsson * Created. */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Threading; using Wayne.Lib.Log; namespace Wayne.Lib.StateEngine { class SynchronousRootStateMachine : EventBufferingRootStateMachine { #region Fields ReentrancyMutex processingEventMutex = new ReentrancyMutex(); #endregion #region Construction /// /// Initializes a new instance of the ThreadedRootStateMachine class. /// /// The name of the statemachine. /// The DebugLogger to use. /// The log category. public SynchronousRootStateMachine(string name, IDebugLogger debugLogger, object logCategory) : base(name, debugLogger, logCategory) { processingEventMutex.TryAquire(); //Acquire the Mutex, to be released after we have entered the initial state. } #endregion #region Public Methods public override void IncomingEvent(StateEngineEvent stateEngineEvent) { if (disposed || !Started) return; //We add the event to the event queue in any case, that is done by the EventBufferring //base statemachine's Incoming event. base.IncomingEvent(stateEngineEvent); if (processingEventMutex.TryAquire()) { HandleQueuedEvents(processingEventMutex); } } public override void Start() { Initialize(); CreateOnStateChangeEventHandler(this); PerformInitialTransition(); HandleQueuedEvents(processingEventMutex); } protected override void Dispose(bool disposing) { //Lock further event processing until fully disposed by locking the handlingEventLock processingEventMutex.Close(); base.Dispose(disposing); } #endregion } }