StandardUserSystemClockCore.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Ryujinx.Cpu;
  2. using Ryujinx.HLE.HOS.Kernel.Threading;
  3. using System;
  4. namespace Ryujinx.HLE.HOS.Services.Time.Clock
  5. {
  6. class StandardUserSystemClockCore : SystemClockCore
  7. {
  8. private StandardLocalSystemClockCore _localSystemClockCore;
  9. private StandardNetworkSystemClockCore _networkSystemClockCore;
  10. private bool _autoCorrectionEnabled;
  11. private SteadyClockTimePoint _autoCorrectionTime;
  12. private KEvent _autoCorrectionEvent;
  13. public StandardUserSystemClockCore(StandardLocalSystemClockCore localSystemClockCore, StandardNetworkSystemClockCore networkSystemClockCore) : base(localSystemClockCore.GetSteadyClockCore())
  14. {
  15. _localSystemClockCore = localSystemClockCore;
  16. _networkSystemClockCore = networkSystemClockCore;
  17. _autoCorrectionEnabled = false;
  18. _autoCorrectionTime = SteadyClockTimePoint.GetRandom();
  19. _autoCorrectionEvent = null;
  20. }
  21. protected override ResultCode Flush(SystemClockContext context)
  22. {
  23. // As UserSystemClock isn't a real system clock, this shouldn't happens.
  24. throw new NotImplementedException();
  25. }
  26. public override ResultCode GetClockContext(ITickSource tickSource, out SystemClockContext context)
  27. {
  28. ResultCode result = ApplyAutomaticCorrection(tickSource, false);
  29. context = new SystemClockContext();
  30. if (result == ResultCode.Success)
  31. {
  32. return _localSystemClockCore.GetClockContext(tickSource, out context);
  33. }
  34. return result;
  35. }
  36. public override ResultCode SetClockContext(SystemClockContext context)
  37. {
  38. return ResultCode.NotImplemented;
  39. }
  40. private ResultCode ApplyAutomaticCorrection(ITickSource tickSource, bool autoCorrectionEnabled)
  41. {
  42. ResultCode result = ResultCode.Success;
  43. if (_autoCorrectionEnabled != autoCorrectionEnabled && _networkSystemClockCore.IsClockSetup(tickSource))
  44. {
  45. result = _networkSystemClockCore.GetClockContext(tickSource, out SystemClockContext context);
  46. if (result == ResultCode.Success)
  47. {
  48. _localSystemClockCore.SetClockContext(context);
  49. }
  50. }
  51. return result;
  52. }
  53. internal void CreateAutomaticCorrectionEvent(Horizon system)
  54. {
  55. _autoCorrectionEvent = new KEvent(system.KernelContext);
  56. }
  57. public ResultCode SetAutomaticCorrectionEnabled(ITickSource tickSource, bool autoCorrectionEnabled)
  58. {
  59. ResultCode result = ApplyAutomaticCorrection(tickSource, autoCorrectionEnabled);
  60. if (result == ResultCode.Success)
  61. {
  62. _autoCorrectionEnabled = autoCorrectionEnabled;
  63. }
  64. return result;
  65. }
  66. public bool IsAutomaticCorrectionEnabled()
  67. {
  68. return _autoCorrectionEnabled;
  69. }
  70. public KReadableEvent GetAutomaticCorrectionReadableEvent()
  71. {
  72. return _autoCorrectionEvent.ReadableEvent;
  73. }
  74. public void SetAutomaticCorrectionUpdatedTime(SteadyClockTimePoint steadyClockTimePoint)
  75. {
  76. _autoCorrectionTime = steadyClockTimePoint;
  77. }
  78. public SteadyClockTimePoint GetAutomaticCorrectionUpdatedTime()
  79. {
  80. return _autoCorrectionTime;
  81. }
  82. public void SignalAutomaticCorrectionEvent()
  83. {
  84. _autoCorrectionEvent.WritableEvent.Signal();
  85. }
  86. }
  87. }