Make prefix add & remove thread safe.

This commit is contained in:
Chris Ross 2014-11-14 12:17:35 -08:00
parent 46a11be2ed
commit ecf9454efe
1 changed files with 66 additions and 33 deletions

View File

@ -23,7 +23,13 @@ namespace Microsoft.Net.Http.Server
public int Count
{
get { return _prefixes.Count; }
get
{
lock (_prefixes)
{
return _prefixes.Count;
}
}
}
public bool IsReadOnly
@ -37,6 +43,8 @@ namespace Microsoft.Net.Http.Server
}
public void Add(UrlPrefix item)
{
lock (_prefixes)
{
var id = _nextId++;
if (_webListener.IsListening)
@ -45,13 +53,19 @@ namespace Microsoft.Net.Http.Server
}
_prefixes.Add(id, item);
}
}
internal UrlPrefix GetPrefix(int id)
{
lock (_prefixes)
{
return _prefixes[id];
}
}
public void Clear()
{
lock (_prefixes)
{
if (_webListener.IsListening)
{
@ -59,16 +73,23 @@ namespace Microsoft.Net.Http.Server
}
_prefixes.Clear();
}
}
public bool Contains(UrlPrefix item)
{
lock (_prefixes)
{
return _prefixes.Values.Contains(item);
}
}
public void CopyTo(UrlPrefix[] array, int arrayIndex)
{
lock (_prefixes)
{
_prefixes.Values.CopyTo(array, arrayIndex);
}
}
public bool Remove(string prefix)
{
@ -76,6 +97,8 @@ namespace Microsoft.Net.Http.Server
}
public bool Remove(UrlPrefix item)
{
lock (_prefixes)
{
int? id = null;
foreach (var pair in _prefixes)
@ -96,11 +119,15 @@ namespace Microsoft.Net.Http.Server
}
return false;
}
}
public IEnumerator<UrlPrefix> GetEnumerator()
{
lock (_prefixes)
{
return _prefixes.Values.GetEnumerator();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
@ -108,6 +135,8 @@ namespace Microsoft.Net.Http.Server
}
internal void RegisterAllPrefixes()
{
lock (_prefixes)
{
// go through the uri list and register for each one of them
foreach (var pair in _prefixes)
@ -116,8 +145,11 @@ namespace Microsoft.Net.Http.Server
RegisterPrefix(pair.Value.Whole, pair.Key);
}
}
}
internal void UnregisterAllPrefixes()
{
lock (_prefixes)
{
// go through the uri list and unregister for each one of them
foreach (var prefix in _prefixes.Values)
@ -126,6 +158,7 @@ namespace Microsoft.Net.Http.Server
UnregisterPrefix(prefix.Whole);
}
}
}
private void RegisterPrefix(string uriPrefix, int contextId)
{