You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. #if NET20
  28. using Newtonsoft.Json.Utilities.LinqBridge;
  29. #endif
  30. #if !NET20
  31. using System.Collections.Concurrent;
  32. #endif
  33. using System.Threading;
  34. using Newtonsoft.Json.Serialization;
  35. namespace Newtonsoft.Json.Utilities
  36. {
  37. internal class ThreadSafeStore<TKey, TValue>
  38. {
  39. #if !NET20
  40. private readonly ConcurrentDictionary<TKey, TValue> _concurrentStore;
  41. #else
  42. private readonly object _lock = new object();
  43. private Dictionary<TKey, TValue> _store;
  44. #endif
  45. private readonly Func<TKey, TValue> _creator;
  46. public ThreadSafeStore(Func<TKey, TValue> creator)
  47. {
  48. ValidationUtils.ArgumentNotNull(creator, nameof(creator));
  49. _creator = creator;
  50. #if !NET20
  51. _concurrentStore = new ConcurrentDictionary<TKey, TValue>();
  52. #else
  53. _store = new Dictionary<TKey, TValue>();
  54. #endif
  55. }
  56. public TValue Get(TKey key)
  57. {
  58. #if !NET20
  59. return _concurrentStore.GetOrAdd(key, _creator);
  60. #else
  61. if (!_store.TryGetValue(key, out TValue value))
  62. {
  63. return AddValue(key);
  64. }
  65. return value;
  66. #endif
  67. }
  68. #if NET20
  69. private TValue AddValue(TKey key)
  70. {
  71. TValue value = _creator(key);
  72. lock (_lock)
  73. {
  74. if (_store == null)
  75. {
  76. _store = new Dictionary<TKey, TValue>();
  77. _store[key] = value;
  78. }
  79. else
  80. {
  81. // double check locking
  82. if (_store.TryGetValue(key, out TValue checkValue))
  83. {
  84. return checkValue;
  85. }
  86. Dictionary<TKey, TValue> newStore = new Dictionary<TKey, TValue>(_store);
  87. newStore[key] = value;
  88. Thread.MemoryBarrier();
  89. _store = newStore;
  90. }
  91. return value;
  92. }
  93. }
  94. #endif
  95. }
  96. }