SynchronousRootStateMachine.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/SynchronousRootStateMachine.cs $
  4. *
  5. * 5 08-03-10 15:01 Mattias.larsson
  6. * Removed Paused and StepEvent().
  7. *
  8. * 4 08-01-23 16:34 Mattias.larsson
  9. *
  10. * 3 07-10-22 11:32 roger.månsson
  11. * Use new Reentrancy mutex to protect against reentrancy in the
  12. * HandledQueuedEvents method.
  13. *
  14. * 2 07-08-15 15:30 roger.månsson
  15. * Changed behaviour so that when IncomingEvent is called and someone is
  16. * already processing an event, the event is just queued and will be
  17. * processed by the thread that is currently executing. This will remove a
  18. * lot of delays waiting to run, and reduce the risk for deadlocking in
  19. * applications.
  20. *
  21. * 1 07-03-12 15:00 roger.månsson
  22. * Created.
  23. */
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.Threading;
  29. using Wayne.Lib.Log;
  30. namespace Wayne.Lib.StateEngine
  31. {
  32. class SynchronousRootStateMachine : EventBufferingRootStateMachine
  33. {
  34. #region Fields
  35. ReentrancyMutex processingEventMutex = new ReentrancyMutex();
  36. #endregion
  37. #region Construction
  38. /// <summary>
  39. /// Initializes a new instance of the ThreadedRootStateMachine class.
  40. /// </summary>
  41. /// <param name="name">The name of the statemachine.</param>
  42. /// <param name="debugLogger">The DebugLogger to use.</param>
  43. /// <param name="logCategory">The log category.</param>
  44. public SynchronousRootStateMachine(string name, IDebugLogger debugLogger, object logCategory)
  45. : base(name, debugLogger, logCategory)
  46. {
  47. processingEventMutex.TryAquire(); //Acquire the Mutex, to be released after we have entered the initial state.
  48. }
  49. #endregion
  50. #region Public Methods
  51. public override void IncomingEvent(StateEngineEvent stateEngineEvent)
  52. {
  53. if (disposed || !Started)
  54. return;
  55. //We add the event to the event queue in any case, that is done by the EventBufferring
  56. //base statemachine's Incoming event.
  57. base.IncomingEvent(stateEngineEvent);
  58. if (processingEventMutex.TryAquire())
  59. {
  60. HandleQueuedEvents(processingEventMutex);
  61. }
  62. }
  63. public override void Start()
  64. {
  65. Initialize();
  66. CreateOnStateChangeEventHandler(this);
  67. PerformInitialTransition();
  68. HandleQueuedEvents(processingEventMutex);
  69. }
  70. protected override void Dispose(bool disposing)
  71. {
  72. //Lock further event processing until fully disposed by locking the handlingEventLock
  73. processingEventMutex.Close();
  74. base.Dispose(disposing);
  75. }
  76. #endregion
  77. }
  78. }