1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #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
- /// <summary>
- /// Initializes a new instance of the ThreadedRootStateMachine class.
- /// </summary>
- /// <param name="name">The name of the statemachine.</param>
- /// <param name="debugLogger">The DebugLogger to use.</param>
- /// <param name="logCategory">The log category.</param>
- 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
- }
- }
|