HouseGateImpl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Threading;
  3. namespace IvfTl.Hardware.Impl
  4. {
  5. /// <summary>
  6. /// 按舱借用闸门实现。同一舱同一时刻只有一个使用者持有 lease。
  7. /// 优先级:前台(OperateDebug/AutoFocus) > 后台(ControlCapture)。
  8. /// · 前台申请借用时,先置暂停标志并触发 OnPauseCapture(采集线程每节拍据此让路);
  9. /// · 用 SemaphoreSlim(1) 保证舱级独占;后台采集 Acquire 用短超时,拿不到即返回 null 让调用方跳过本轮;
  10. /// · 归还(lease.Dispose) 时若已无前台占用则触发 OnResumeCapture。
  11. /// ⚠ 真机验证 V-012:借用→暂停→归还恢复时序(调试↔采集切换不占用/不死锁)。
  12. /// </summary>
  13. public sealed class HouseGateImpl : IHouseGate
  14. {
  15. private readonly SemaphoreSlim _sem = new SemaphoreSlim(1, 1);
  16. private readonly object _stateLock = new object();
  17. private readonly Func<ISerialChannel> _serialFactory;
  18. private readonly Func<ICamera> _cameraFactory;
  19. private int _foregroundHolders = 0; // 当前前台借用计数(>0 时采集应暂停)
  20. public HouseGateImpl(int houseSn, Func<ISerialChannel> serialFactory, Func<ICamera> cameraFactory)
  21. {
  22. HouseSn = houseSn;
  23. _serialFactory = serialFactory;
  24. _cameraFactory = cameraFactory;
  25. }
  26. public int HouseSn { get; }
  27. public bool IsCapturePaused { get; private set; }
  28. public event Action OnPauseCapture;
  29. public event Action OnResumeCapture;
  30. public IHardwareLease Acquire(HardwareUser user, int timeoutMs = 30000)
  31. {
  32. bool foreground = user != HardwareUser.ControlCapture;
  33. // 前台借用:先标记暂停采集,让后台节拍尽快让路,再排队拿独占锁。
  34. if (foreground)
  35. {
  36. MarkPause();
  37. }
  38. bool got = _sem.Wait(timeoutMs);
  39. if (!got)
  40. {
  41. // 拿不到:前台撤销暂停标记(避免误挂起采集);后台直接返回 null 让调用方跳过本轮。
  42. if (foreground) MarkResume();
  43. return null;
  44. }
  45. return new HardwareLeaseImpl(this, user, _serialFactory(), _cameraFactory(), foreground);
  46. }
  47. public bool TryAcquire(HardwareUser user, out IHardwareLease lease)
  48. {
  49. bool foreground = user != HardwareUser.ControlCapture;
  50. if (foreground) MarkPause();
  51. if (!_sem.Wait(0))
  52. {
  53. if (foreground) MarkResume();
  54. lease = null;
  55. return false;
  56. }
  57. lease = new HardwareLeaseImpl(this, user, _serialFactory(), _cameraFactory(), foreground);
  58. return true;
  59. }
  60. public void PauseCapture() => MarkPause();
  61. public void ResumeCapture() => MarkResume();
  62. private void MarkPause()
  63. {
  64. bool fire = false;
  65. lock (_stateLock)
  66. {
  67. _foregroundHolders++;
  68. if (!IsCapturePaused)
  69. {
  70. IsCapturePaused = true;
  71. fire = true;
  72. }
  73. }
  74. if (fire) OnPauseCapture?.Invoke();
  75. }
  76. private void MarkResume()
  77. {
  78. bool fire = false;
  79. lock (_stateLock)
  80. {
  81. if (_foregroundHolders > 0) _foregroundHolders--;
  82. if (_foregroundHolders == 0 && IsCapturePaused)
  83. {
  84. IsCapturePaused = false;
  85. fire = true;
  86. }
  87. }
  88. if (fire) OnResumeCapture?.Invoke();
  89. }
  90. // 归还:释放独占锁;若是前台借用,撤销其暂停占用并按需恢复采集。
  91. internal void Release(bool wasForeground)
  92. {
  93. try { _sem.Release(); } catch { }
  94. if (wasForeground) MarkResume();
  95. }
  96. }
  97. /// <summary>借用凭证实现。Dispose 即归还闸门并(前台)触发 ResumeCapture。</summary>
  98. internal sealed class HardwareLeaseImpl : IHardwareLease
  99. {
  100. private readonly HouseGateImpl _gate;
  101. private readonly bool _foreground;
  102. private int _disposed = 0;
  103. public HardwareLeaseImpl(HouseGateImpl gate, HardwareUser owner, ISerialChannel serial, ICamera camera, bool foreground)
  104. {
  105. _gate = gate;
  106. _foreground = foreground;
  107. Owner = owner;
  108. Serial = serial;
  109. Camera = camera;
  110. }
  111. public HardwareUser Owner { get; }
  112. public ISerialChannel Serial { get; }
  113. public ICamera Camera { get; }
  114. public void Dispose()
  115. {
  116. // 幂等:多次 Dispose / using 异常路径只归还一次。
  117. if (Interlocked.Exchange(ref _disposed, 1) == 1) return;
  118. _gate.Release(_foreground);
  119. }
  120. }
  121. }