From 5fcd8b1d520ee7ffe6d16565e1428f76a111e43d Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Mon, 23 Jul 2012 23:03:00 +0600 Subject: [PATCH] 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. --- src/Umbraco.Core/ReadLock.cs | 15 ++++--------- src/Umbraco.Core/Umbraco.Core.csproj | 2 +- src/Umbraco.Core/UpgradeableReadLock.cs | 21 ++++++++++++++----- src/Umbraco.Web/Routing/RouteLookups.cs | 15 ++++++------- .../umbraco.presentation/library.cs | 16 +++++++------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/Umbraco.Core/ReadLock.cs b/src/Umbraco.Core/ReadLock.cs index 4b36860c2d..7a933e5301 100644 --- a/src/Umbraco.Core/ReadLock.cs +++ b/src/Umbraco.Core/ReadLock.cs @@ -12,29 +12,22 @@ namespace Umbraco.Core /// /// Intended as an infrastructure class. /// - public class UpgradeableReadLock : IDisposable + public class ReadLock : IDisposable { private readonly ReaderWriterLockSlim _rwLock; - private bool _upgraded = false; /// /// Initializes a new instance of the class. /// - /// The rw lock. - 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(); } } } diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 9c5101ff36..10179a1284 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -45,8 +45,8 @@ Properties\SolutionInfo.cs - + diff --git a/src/Umbraco.Core/UpgradeableReadLock.cs b/src/Umbraco.Core/UpgradeableReadLock.cs index d6787b56f8..8a1108fc6f 100644 --- a/src/Umbraco.Core/UpgradeableReadLock.cs +++ b/src/Umbraco.Core/UpgradeableReadLock.cs @@ -12,23 +12,34 @@ namespace Umbraco.Core /// /// Intended as an infrastructure class. /// - public class ReadLock : IDisposable + public class UpgradeableReadLock : IDisposable { private readonly ReaderWriterLockSlim _rwLock; + private bool _upgraded = false; /// /// Initializes a new instance of the class. /// - /// The rw lock. - public ReadLock(ReaderWriterLockSlim rwLock) + /// The rw lock. + 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(); } } } diff --git a/src/Umbraco.Web/Routing/RouteLookups.cs b/src/Umbraco.Web/Routing/RouteLookups.cs index 6abaa2e7f4..d7f6e0dbab 100644 --- a/src/Umbraco.Web/Routing/RouteLookups.cs +++ b/src/Umbraco.Web/Routing/RouteLookups.cs @@ -33,9 +33,10 @@ namespace Umbraco.Web.Routing /// public IEnumerable GetLookups() { - // FIXME - and then, we return a non-thread-safe collection ... WTF? - - return Lookups; + using(new ReadLock(Lock)) + { + return Lookups; + } } /// @@ -74,11 +75,11 @@ namespace Umbraco.Web.Routing /// 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); } } diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index d1df067bae..2eae155c4e 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -355,10 +355,10 @@ namespace umbraco /// String with a friendly url from a node 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().GetNiceUrl(nodeID); + return Umbraco.Web.UmbracoContext.Current + .DocumentRequest + .RoutingContext + .NiceUrlResolver.GetNiceUrl(nodeID); } /// @@ -380,10 +380,10 @@ namespace umbraco /// String with a friendly url with full domain from a node 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().GetNiceUrl(nodeID, Umbraco.Web.UmbracoContext.Current.UmbracoUrl, true); + return Umbraco.Web.UmbracoContext.Current + .DocumentRequest + .RoutingContext + .NiceUrlResolver.GetNiceUrl(nodeID, Umbraco.Web.UmbracoContext.Current.UmbracoUrl, true); }