Skip to content

Commit 53da0e5

Browse files
authored
Merge pull request #1234 from SixLabors/js/fix-1230
Use ConcurrentDictionary for options properties.
2 parents 7a538d4 + e95c85a commit 53da0e5

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

src/ImageSharp/Configuration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.Net.Http;
78
using SixLabors.ImageSharp.Formats;
@@ -78,7 +79,7 @@ public int MaxDegreeOfParallelism
7879
/// Gets a set of properties for the Congiguration.
7980
/// </summary>
8081
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
81-
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
82+
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
8283

8384
/// <summary>
8485
/// Gets the currently registered <see cref="IImageFormat"/>s.

src/ImageSharp/GraphicOptionsDefaultsExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ public static GraphicsOptions GetGraphicsOptions(this IImageProcessingContext co
7171
return go;
7272
}
7373

74-
var configOptions = context.Configuration.GetGraphicsOptions();
75-
7674
// do not cache the fall back to config into the the processing context
7775
// in case someone want to change the value on the config and expects it re trflow thru
78-
return configOptions;
76+
return context.Configuration.GetGraphicsOptions();
7977
}
8078

8179
/// <summary>

src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using SixLabors.ImageSharp.PixelFormats;
67
using SixLabors.ImageSharp.Processing.Processors;
@@ -41,7 +42,7 @@ public DefaultImageProcessorContext(Configuration configuration, Image<TPixel> s
4142
public Configuration Configuration { get; }
4243

4344
/// <inheritdoc/>
44-
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
45+
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
4546

4647
/// <inheritdoc/>
4748
public Image<TPixel> GetResultImage()

tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Threading.Tasks;
45
using SixLabors.ImageSharp.PixelFormats;
6+
using SixLabors.ImageSharp.Processing;
57
using SixLabors.ImageSharp.Tests.Processing;
68
using SixLabors.ImageSharp.Tests.TestUtilities;
79
using Xunit;
@@ -168,5 +170,18 @@ public void GetDefaultOptionsFromProcessingContext_IgnoreIncorectlyTypesDictionE
168170
Assert.NotNull(options);
169171
Assert.IsType<GraphicsOptions>(options);
170172
}
173+
174+
[Theory]
175+
[WithBlankImages(100, 100, PixelTypes.Rgba32)]
176+
public void CanGetGraphicsOptionsMultiThreaded<TPixel>(TestImageProvider<TPixel> provider)
177+
where TPixel : unmanaged, IPixel<TPixel>
178+
{
179+
// Could not get fake operations to trigger #1230 so using a real image.
180+
Parallel.For(0, 10, _ =>
181+
{
182+
using Image<TPixel> image = provider.GetImage();
183+
image.Mutate(x => x.BackgroundColor(Color.White));
184+
});
185+
}
171186
}
172187
}

tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.Linq;
67
using SixLabors.ImageSharp.PixelFormats;
@@ -56,7 +57,7 @@ public FakeImageOperations(Configuration configuration, Image<TPixel> source, bo
5657

5758
public Configuration Configuration { get; }
5859

59-
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
60+
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
6061

6162
public Image<TPixel> GetResultImage()
6263
{

0 commit comments

Comments
 (0)