[DatFile] Fix locking on dictionary

This commit is contained in:
Matt Nadareski
2016-12-01 11:43:09 -08:00
parent 93ad95a218
commit 7dedf77950

View File

@@ -30,7 +30,7 @@ namespace SabreTools.Helper.Dats
private DatFormat _datFormat; private DatFormat _datFormat;
private bool _excludeOf; private bool _excludeOf;
private bool _mergeRoms; private bool _mergeRoms;
private SortedDictionary<string, List<DatItem>> _files; private SortedDictionary<string, List<DatItem>> _files = new SortedDictionary<string, List<DatItem>>();
private SortedBy _sortedBy; private SortedBy _sortedBy;
// Data specific to the Miss DAT type // Data specific to the Miss DAT type
@@ -274,6 +274,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is missing from the dictionary, add it // If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key)) if (!_files.ContainsKey(key))
{ {
@@ -283,6 +285,7 @@ namespace SabreTools.Helper.Dats
// Now return the value // Now return the value
return _files[key]; return _files[key];
} }
}
set set
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
@@ -291,6 +294,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is missing from the dictionary, add it // If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key)) if (!_files.ContainsKey(key))
{ {
@@ -301,14 +306,13 @@ namespace SabreTools.Helper.Dats
_files[key] = value; _files[key] = value;
} }
} }
}
/// <summary> /// <summary>
/// Add a new key to the file dictionary /// Add a new key to the file dictionary
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to add to</param> /// <param name="key">Key in the dictionary to add to</param>
public void Add(string key) public void Add(string key)
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -316,6 +320,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is missing from the dictionary, add it // If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key)) if (!_files.ContainsKey(key))
{ {
@@ -330,8 +336,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to add to</param> /// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param> /// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value) public void Add(string key, DatItem value)
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -339,6 +343,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is missing from the dictionary, add it // If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key)) if (!_files.ContainsKey(key))
{ {
@@ -356,8 +362,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to add to</param> /// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param> /// <param name="value">Value to add to the dictionary</param>
public void AddRange(string key, List<DatItem> value) public void AddRange(string key, List<DatItem> value)
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -365,6 +369,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is missing from the dictionary, add it // If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key)) if (!_files.ContainsKey(key))
{ {
@@ -382,8 +388,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to check</param> /// <param name="key">Key in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns> /// <returns>True if the key exists, false otherwise</returns>
public bool ContainsKey(string key) public bool ContainsKey(string key)
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -391,6 +395,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
return _files.ContainsKey(key); return _files.ContainsKey(key);
} }
} }
@@ -402,8 +408,6 @@ namespace SabreTools.Helper.Dats
public long Count public long Count
{ {
get get
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -411,6 +415,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
return _files.Count; return _files.Count;
} }
} }
@@ -431,8 +437,6 @@ namespace SabreTools.Helper.Dats
public IEnumerable<string> Keys public IEnumerable<string> Keys
{ {
get get
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -440,6 +444,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
return _files.Keys; return _files.Keys;
} }
} }
@@ -450,8 +456,6 @@ namespace SabreTools.Helper.Dats
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
public void Remove(string key) public void Remove(string key)
{
lock (_files)
{ {
// If the dictionary is null, create it // If the dictionary is null, create it
if (_files == null) if (_files == null)
@@ -459,6 +463,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>(); _files = new SortedDictionary<string, List<DatItem>>();
} }
lock (_files)
{
// If the key is in the dictionary, remove it // If the key is in the dictionary, remove it
if (_files.ContainsKey(key)) if (_files.ContainsKey(key))
{ {