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