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