Ryujinx/Ryujinx.Core/OsHle/IdDictionary.cs
gdkchan 4314a8f3e5
[WIP] Add support for events (#60)
* Add support for events, move concept of domains to IpcService

* Support waiting for KThread, remove some test code, other tweaks

* Use move handle on NIFM since I can't test that now, it's better to leave it how it was
2018-03-19 15:58:46 -03:00

82 lines
1.6 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Ryujinx.Core.OsHle
{
class IdDictionary
{
private ConcurrentDictionary<int, object> Objs;
private int FreeIdHint = 1;
public IdDictionary()
{
Objs = new ConcurrentDictionary<int, object>();
}
public int Add(object Data)
{
if (Objs.TryAdd(FreeIdHint, Data))
{
return FreeIdHint++;
}
return AddSlow(Data);
}
private int AddSlow(object Data)
{
for (int Id = 1; Id < int.MaxValue; Id++)
{
if (Objs.TryAdd(Id, Data))
{
return Id;
}
}
throw new InvalidOperationException();
}
public object GetData(int Id)
{
if (Objs.TryGetValue(Id, out object Data))
{
return Data;
}
return null;
}
public T GetData<T>(int Id)
{
if (Objs.TryGetValue(Id, out object Data) && Data is T)
{
return (T)Data;
}
return default(T);
}
public object Delete(int Id)
{
if (Objs.TryRemove(Id, out object Obj))
{
FreeIdHint = Id;
return Obj;
}
return null;
}
public ICollection<object> Clear()
{
ICollection<object> Values = Objs.Values;
Objs.Clear();
return Values;
}
}
}