diff --git a/src/PoolSharp.Android/PoolSharp.Android.csproj b/src/PoolSharp.Android/PoolSharp.Android.csproj index ebde15f..559f5d2 100644 --- a/src/PoolSharp.Android/PoolSharp.Android.csproj +++ b/src/PoolSharp.Android/PoolSharp.Android.csproj @@ -16,6 +16,7 @@ Off False v9.0 + true true diff --git a/src/PoolSharp.Bait/GlobalSuppressions.cs b/src/PoolSharp.Bait/GlobalSuppressions.cs new file mode 100644 index 0000000..2a79098 Binary files /dev/null and b/src/PoolSharp.Bait/GlobalSuppressions.cs differ diff --git a/src/PoolSharp.Bait/PoolSharp.Bait.csproj b/src/PoolSharp.Bait/PoolSharp.Bait.csproj index 861fb32..9de122b 100644 --- a/src/PoolSharp.Bait/PoolSharp.Bait.csproj +++ b/src/PoolSharp.Bait/PoolSharp.Bait.csproj @@ -15,6 +15,7 @@ {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} Profile328 v4.0 + true true @@ -24,7 +25,7 @@ DEBUG;TRACE prompt 4 - true + false ..\PoolSharpAnalysis.ruleset bin\Debug\PoolSharp.XML @@ -35,12 +36,13 @@ TRACE prompt 4 - true + false ..\PoolSharpAnalysis.ruleset bin\Release\PoolSharp.XML + diff --git a/src/PoolSharp.Net40/GlobalSuppressions.cs b/src/PoolSharp.Net40/GlobalSuppressions.cs index 6d6b158..fdf9ec5 100644 Binary files a/src/PoolSharp.Net40/GlobalSuppressions.cs and b/src/PoolSharp.Net40/GlobalSuppressions.cs differ diff --git a/src/PoolSharp.Net40/PoolSharp.Net40.csproj b/src/PoolSharp.Net40/PoolSharp.Net40.csproj index b5ec4e4..dc449a3 100644 --- a/src/PoolSharp.Net40/PoolSharp.Net40.csproj +++ b/src/PoolSharp.Net40/PoolSharp.Net40.csproj @@ -11,6 +11,7 @@ PoolSharp v4.0 512 + true true @@ -20,7 +21,7 @@ TRACE;DEBUG;CODE_ANALYSIS;SUPPORTS_THREADS prompt 4 - true + false ..\PoolSharpAnalysis.ruleset bin\Debug\PoolSharp.XML @@ -31,7 +32,7 @@ TRACE;CODE_ANALYSIS;SUPPORTS_THREADS prompt 4 - true + false ..\PoolSharpAnalysis.ruleset bin\Release\PoolSharp.XML diff --git a/src/PoolSharp.NetStandard20/PoolSharp.NetStandard20.csproj b/src/PoolSharp.NetStandard20/PoolSharp.NetStandard20.csproj index a780e73..f8c6759 100644 --- a/src/PoolSharp.NetStandard20/PoolSharp.NetStandard20.csproj +++ b/src/PoolSharp.NetStandard20/PoolSharp.NetStandard20.csproj @@ -4,6 +4,8 @@ netstandard2.0 PoolSharp false + false + true diff --git a/src/PoolSharp.NetStandard20/PoolSharp.xml b/src/PoolSharp.NetStandard20/PoolSharp.xml index a167f4f..47c053c 100644 --- a/src/PoolSharp.NetStandard20/PoolSharp.xml +++ b/src/PoolSharp.NetStandard20/PoolSharp.xml @@ -66,6 +66,15 @@ The type of value being pooled. + + + Raised if an error is thrown by the callback. + + + This event is raised when an exception is thrown during reinitialisation of an object as this may occur on a background thread where the caller + otherwise couldn't catch and handle the exception. If this event is raised, the item was being reinitialised is not added to the pool. + + Full constructor. @@ -123,6 +132,12 @@ Returns a boolean indicating if {T} is actually a . + + + Raises the event. + + The event arguments to pass to handlers of the event. + Disposes if it is not null and supports , otherwise does nothing. If is actually a instance, then disposes the property instead. @@ -257,6 +272,29 @@ If enabled the function will throw if an item already in the pool as added a second time. + + + Event arguments for the event. + + The type of value stored in the pool and being reinitialised. + + + + Full constructor. + + The exception that was thrown from the reinitialisation callback. + The item that was being reinitialised when the exception were thrown. + + + + The exception that was thrown from the reinitialisation callback. + + + + + The item that was being reinitialised when the exception were thrown. + + A non-blocking object pool optimised for situations involving heavily concurrent access. diff --git a/src/PoolSharp.Shared.ConcurrentBagImplementation/Pool.cs b/src/PoolSharp.Shared.ConcurrentBagImplementation/Pool.cs index d4856c3..319c57a 100644 --- a/src/PoolSharp.Shared.ConcurrentBagImplementation/Pool.cs +++ b/src/PoolSharp.Shared.ConcurrentBagImplementation/Pool.cs @@ -32,7 +32,7 @@ public class Pool : PoolBase private long _PoolInstancesCount; #if SUPPORTS_THREADS - System.Threading.Thread _ReinitialiseThread; + System.Threading.Thread _ReinitialiseThread; #endif #endregion @@ -225,10 +225,19 @@ private void BackgroundReinitialise() SafeDispose(item); else { - if (PoolPolicy.ReinitializeObject != null) - PoolPolicy.ReinitializeObject(item); + try + { + if (PoolPolicy.ReinitializeObject != null) + PoolPolicy.ReinitializeObject(item); + } + catch (Exception ex) + { + OnReinitialiseError(new ReinitialiseErrorEventArgs(ex, item)); + SafeDispose(item); + item = default; + } - if (ShouldReturnToPool(item)) + if (item != null && ShouldReturnToPool(item)) { _Pool.Add(item); @@ -274,7 +283,17 @@ private void ReinitialiseAndReturnToPoolOrDispose(T value) { if (ShouldReturnToPool(value)) { - PoolPolicy.ReinitializeObject(value); + try + { + PoolPolicy.ReinitializeObject(value); + } + catch (Exception ex) + { + OnReinitialiseError(new ReinitialiseErrorEventArgs(ex, value)); + SafeDispose(value); + return; + } + _Pool.Add(value); Interlocked.Increment(ref _PoolInstancesCount); } diff --git a/src/PoolSharp.Shared/PoolBase.cs b/src/PoolSharp.Shared/PoolBase.cs index b247ed8..cd7c54a 100644 --- a/src/PoolSharp.Shared/PoolBase.cs +++ b/src/PoolSharp.Shared/PoolBase.cs @@ -24,6 +24,19 @@ public abstract class PoolBase : IPool #endregion + #region Events + + /// + /// Raised if an error is thrown by the callback. + /// + /// + /// This event is raised when an exception is thrown during reinitialisation of an object as this may occur on a background thread where the caller + /// otherwise couldn't catch and handle the exception. If this event is raised, the item was being reinitialised is not added to the pool. + /// + public event EventHandler> ReinitialiseError; + + #endregion + #region Constructors /// @@ -145,6 +158,15 @@ protected bool IsPooledTypeWrapped #region Public Methods + /// + /// Raises the event. + /// + /// The event arguments to pass to handlers of the event. + protected virtual void OnReinitialiseError(ReinitialiseErrorEventArgs e) + { + ReinitialiseError?.Invoke(this, e); + } + #if NETFX_CORE /// /// Disposes if it is not null and supports , otherwise does nothing. If is actually a instance, then disposes the property instead. @@ -168,7 +190,6 @@ protected void SafeDispose(object pooledObject) /// Disposes if it is not null and supports , otherwise does nothing. If is actually a instance, then disposes the property instead. /// /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")] protected void SafeDispose(object pooledObject) { if (IsPooledTypeWrapped) diff --git a/src/PoolSharp.Shared/PoolSharp.Shared.projitems b/src/PoolSharp.Shared/PoolSharp.Shared.projitems index a5aee53..cb2fc25 100644 --- a/src/PoolSharp.Shared/PoolSharp.Shared.projitems +++ b/src/PoolSharp.Shared/PoolSharp.Shared.projitems @@ -16,5 +16,6 @@ + \ No newline at end of file diff --git a/src/PoolSharp.Shared/ReinitialiseErrorEventArgs.cs b/src/PoolSharp.Shared/ReinitialiseErrorEventArgs.cs new file mode 100644 index 0000000..f9f4caf --- /dev/null +++ b/src/PoolSharp.Shared/ReinitialiseErrorEventArgs.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PoolSharp +{ + /// + /// Event arguments for the event. + /// + /// The type of value stored in the pool and being reinitialised. + public class ReinitialiseErrorEventArgs : EventArgs + { + private readonly Exception _Exception; + private readonly T _Item; + + /// + /// Full constructor. + /// + /// The exception that was thrown from the reinitialisation callback. + /// The item that was being reinitialised when the exception were thrown. + public ReinitialiseErrorEventArgs(Exception exception, T item) + { + _Exception = exception; + _Item = item; + } + + /// + /// The exception that was thrown from the reinitialisation callback. + /// + public Exception Exception => _Exception; + + /// + /// The item that was being reinitialised when the exception were thrown. + /// + public T Item => _Item; + } +} diff --git a/src/PoolSharp.iOS/GlobalSuppressions.cs b/src/PoolSharp.iOS/GlobalSuppressions.cs new file mode 100644 index 0000000..5b6798e Binary files /dev/null and b/src/PoolSharp.iOS/GlobalSuppressions.cs differ diff --git a/src/PoolSharp.iOS/PoolSharp.iOS.csproj b/src/PoolSharp.iOS/PoolSharp.iOS.csproj index 66e783b..5ab3dea 100644 --- a/src/PoolSharp.iOS/PoolSharp.iOS.csproj +++ b/src/PoolSharp.iOS/PoolSharp.iOS.csproj @@ -11,6 +11,7 @@ PoolSharp Resources PoolSharp + true true @@ -21,7 +22,6 @@ prompt 4 false - true ..\PoolSharpAnalysis.ruleset bin\Debug\PoolSharp.XML @@ -32,13 +32,13 @@ prompt 4 false - true + false ..\PoolSharpAnalysis.ruleset CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;SUPPORTS_THREADS;SUPPORTS_AGGRESSIVEINLINING bin\Release\PoolSharp.XML - true + false ..\..\PoolNetAnalysis.ruleset CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;SUPPORTS_THREADS @@ -52,6 +52,7 @@ + diff --git a/src/PoolSharp.nuspec b/src/PoolSharp.nuspec index f28e3c7..40220c5 100644 --- a/src/PoolSharp.nuspec +++ b/src/PoolSharp.nuspec @@ -13,7 +13,7 @@ https://github.com/Yortw/PoolSharp object pool sharp winrt uwp net40 ios android xamarin portable - Removed WinRT support. Added NetStandard 2.0 support. + Removed WinRT support. Added NetStandard 2.0 support. Improved error handling during item reinitialisation.