From a173d7f1ffedfe59c634c698fe00c071660531bf Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 16 Jun 2020 22:03:24 +0100 Subject: [PATCH] Use ConcurrentDictionary. Fix #1230 --- src/ImageSharp/Configuration.cs | 3 ++- .../GraphicOptionsDefaultsExtensions.cs | 4 +--- .../DefaultImageProcessorContext{TPixel}.cs | 3 ++- .../GraphicOptionsDefaultsExtensionsTests.cs | 15 +++++++++++++++ .../Processing/FakeImageOperationsProvider.cs | 3 ++- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 03e59d34b..067257132 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Net.Http; using SixLabors.ImageSharp.Formats; @@ -78,7 +79,7 @@ namespace SixLabors.ImageSharp /// Gets a set of properties for the Congiguration. /// /// This can be used for storing global settings and defaults to be accessable to processors. - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); /// /// Gets the currently registered s. diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index d1581a00e..80cdfd153 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -71,11 +71,9 @@ namespace SixLabors.ImageSharp return go; } - var configOptions = context.Configuration.GetGraphicsOptions(); - // do not cache the fall back to config into the the processing context // in case someone want to change the value on the config and expects it re trflow thru - return configOptions; + return context.Configuration.GetGraphicsOptions(); } /// diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 4f47545fa..7ed73d517 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Concurrent; using System.Collections.Generic; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; @@ -41,7 +42,7 @@ namespace SixLabors.ImageSharp.Processing public Configuration Configuration { get; } /// - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); /// public Image GetResultImage() diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs index 72ec35538..21f26cd01 100644 --- a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -1,7 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Threading.Tasks; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Tests.Processing; using SixLabors.ImageSharp.Tests.TestUtilities; using Xunit; @@ -168,5 +170,18 @@ namespace SixLabors.ImageSharp.Tests Assert.NotNull(options); Assert.IsType(options); } + + [Theory] + [WithBlankImages(100, 100, PixelTypes.Rgba32)] + public void CanGetGraphicsOptionsMultiThreaded(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // Could not get fake operations to trigger #1230 so using a real image. + Parallel.For(0, 10, _ => + { + using Image image = provider.GetImage(); + image.Mutate(x => x.BackgroundColor(Color.White)); + }); + } } } diff --git a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs index 5f673b9d2..03fa6dfc2 100644 --- a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs +++ b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using SixLabors.ImageSharp.PixelFormats; @@ -56,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests.Processing public Configuration Configuration { get; } - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); public Image GetResultImage() {