SyncMap.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace Ryujinx.Graphics.GAL.Multithreading
  5. {
  6. class SyncMap : IDisposable
  7. {
  8. private HashSet<ulong> _inFlight = new HashSet<ulong>();
  9. private AutoResetEvent _inFlightChanged = new AutoResetEvent(false);
  10. internal void CreateSyncHandle(ulong id)
  11. {
  12. lock (_inFlight)
  13. {
  14. _inFlight.Add(id);
  15. }
  16. }
  17. internal void AssignSync(ulong id)
  18. {
  19. lock (_inFlight)
  20. {
  21. _inFlight.Remove(id);
  22. }
  23. _inFlightChanged.Set();
  24. }
  25. internal void WaitSyncAvailability(ulong id)
  26. {
  27. // Blocks until the handle is available.
  28. bool signal = false;
  29. while (true)
  30. {
  31. lock (_inFlight)
  32. {
  33. if (!_inFlight.Contains(id))
  34. {
  35. break;
  36. }
  37. }
  38. _inFlightChanged.WaitOne();
  39. signal = true;
  40. }
  41. if (signal)
  42. {
  43. // Signal other threads which might still be waiting.
  44. _inFlightChanged.Set();
  45. }
  46. }
  47. public void Dispose()
  48. {
  49. _inFlightChanged.Dispose();
  50. }
  51. }
  52. }