Browse Source

Use ConcurrentDictionary. Fix #1230

pull/1574/head
James Jackson-South 6 years ago
parent
commit
a173d7f1ff
  1. 3
      src/ImageSharp/Configuration.cs
  2. 4
      src/ImageSharp/GraphicOptionsDefaultsExtensions.cs
  3. 3
      src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs
  4. 15
      tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs
  5. 3
      tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs

3
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.
/// </summary>
/// <remarks>This can be used for storing global settings and defaults to be accessable to processors.</remarks>
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
/// <summary>
/// Gets the currently registered <see cref="IImageFormat"/>s.

4
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();
}
/// <summary>

3
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; }
/// <inheritdoc/>
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
/// <inheritdoc/>
public Image<TPixel> GetResultImage()

15
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<GraphicsOptions>(options);
}
[Theory]
[WithBlankImages(100, 100, PixelTypes.Rgba32)]
public void CanGetGraphicsOptionsMultiThreaded<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// Could not get fake operations to trigger #1230 so using a real image.
Parallel.For(0, 10, _ =>
{
using Image<TPixel> image = provider.GetImage();
image.Mutate(x => x.BackgroundColor(Color.White));
});
}
}
}

3
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<object, object> Properties { get; } = new Dictionary<object, object>();
public IDictionary<object, object> Properties { get; } = new ConcurrentDictionary<object, object>();
public Image<TPixel> GetResultImage()
{

Loading…
Cancel
Save