Fixed file names of ReadLock and UpradeableReadLock. Fixes up UpgradeableReadLock to ensure

that it it is upgraded it releases its writelock on dispose. Fixes up RouteLookups to have
correct locking. Fixes up library to resolve the NiceUrlResolver using the UmbracoContext.Current
singleton acessor.
This commit is contained in:
shannon@ShandemVaio
2012-07-23 23:03:00 +06:00
parent 3546b3e954
commit 5fcd8b1d52
5 changed files with 37 additions and 32 deletions

View File

@@ -12,29 +12,22 @@ namespace Umbraco.Core
/// <remarks>
/// Intended as an infrastructure class.
/// </remarks>
public class UpgradeableReadLock : IDisposable
public class ReadLock : IDisposable
{
private readonly ReaderWriterLockSlim _rwLock;
private bool _upgraded = false;
/// <summary>
/// Initializes a new instance of the <see cref="ReadLock"/> class.
/// </summary>
/// <param name="rwLock">The rw lock.</param>
public UpgradeableReadLock(ReaderWriterLockSlim rwLock)
public ReadLock(ReaderWriterLockSlim rwLock)
{
_rwLock = rwLock;
_rwLock.EnterUpgradeableReadLock();
_rwLock.EnterReadLock();
}
public void UpgradeToWriteLock()
{
_rwLock.EnterWriteLock();
}
void IDisposable.Dispose()
{
_rwLock.ExitUpgradeableReadLock();
_rwLock.ExitReadLock();
}
}
}

View File

@@ -45,8 +45,8 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="ApplicationContext.cs" />
<Compile Include="UpgradeableReadLock.cs" />
<Compile Include="ReadLock.cs" />
<Compile Include="UpgradeableReadLock.cs" />
<Compile Include="DelegateEqualityComparer.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="IfExtensions.cs" />

View File

@@ -12,23 +12,34 @@ namespace Umbraco.Core
/// <remarks>
/// Intended as an infrastructure class.
/// </remarks>
public class ReadLock : IDisposable
public class UpgradeableReadLock : IDisposable
{
private readonly ReaderWriterLockSlim _rwLock;
private bool _upgraded = false;
/// <summary>
/// Initializes a new instance of the <see cref="ReadLock"/> class.
/// </summary>
/// <ReadLock name="rwLock">The rw lock.</param>
public ReadLock(ReaderWriterLockSlim rwLock)
/// <param name="rwLock">The rw lock.</param>
public UpgradeableReadLock(ReaderWriterLockSlim rwLock)
{
_rwLock = rwLock;
_rwLock.EnterReadLock();
_rwLock.EnterUpgradeableReadLock();
}
public void UpgradeToWriteLock()
{
_rwLock.EnterWriteLock();
_upgraded = true;
}
void IDisposable.Dispose()
{
_rwLock.ExitReadLock();
if (_upgraded)
{
_rwLock.ExitWriteLock();
}
_rwLock.ExitUpgradeableReadLock();
}
}
}

View File

@@ -33,9 +33,10 @@ namespace Umbraco.Web.Routing
/// <returns></returns>
public IEnumerable<IRequestDocumentResolver> GetLookups()
{
// FIXME - and then, we return a non-thread-safe collection ... WTF?
return Lookups;
using(new ReadLock(Lock))
{
return Lookups;
}
}
/// <summary>
@@ -74,11 +75,11 @@ namespace Umbraco.Web.Routing
/// <param name="lookup"></param>
public void InsertLookup(int index, IRequestDocumentResolver lookup)
{
if (CheckExists(lookup))
throw new InvalidOperationException("The lookup type " + lookup + " already exists in the lookup collection");
using (new WriteLock(Lock))
using (var l = new UpgradeableReadLock(Lock))
{
if (CheckExists(lookup))
throw new InvalidOperationException("The lookup type " + lookup + " already exists in the lookup collection");
l.UpgradeToWriteLock();
Lookups.Insert(index, lookup);
}
}

View File

@@ -355,10 +355,10 @@ namespace umbraco
/// <returns>String with a friendly url from a node</returns>
public static string NiceUrl(int nodeID)
{
// OK, how are we supposed to retrieve the NiceUrlResolver from here,
// bearing in mind that we don't want to instanciate a resolver + content store + ...
// for every NiceUrl call ?!
return Umbraco.Core.UmbracoContainer.Get<Umbraco.Web.Routing.NiceUrls>().GetNiceUrl(nodeID);
return Umbraco.Web.UmbracoContext.Current
.DocumentRequest
.RoutingContext
.NiceUrlResolver.GetNiceUrl(nodeID);
}
/// <summary>
@@ -380,10 +380,10 @@ namespace umbraco
/// <returns>String with a friendly url with full domain from a node</returns>
public static string NiceUrlWithDomain(int nodeID)
{
// OK, how are we supposed to retrieve the NiceUrlResolver from here,
// bearing in mind that we don't want to instanciate a resolver + content store + ...
// for every NiceUrl call ?!
return Umbraco.Core.UmbracoContainer.Get<Umbraco.Web.Routing.NiceUrls>().GetNiceUrl(nodeID, Umbraco.Web.UmbracoContext.Current.UmbracoUrl, true);
return Umbraco.Web.UmbracoContext.Current
.DocumentRequest
.RoutingContext
.NiceUrlResolver.GetNiceUrl(nodeID, Umbraco.Web.UmbracoContext.Current.UmbracoUrl, true);
}