StandardUserSystemClockCore.cs 3.6 KB

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