ThreadLocal.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if NET_4_0
  2. //
  3. // ThreadLocal.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. using System;
  28. using System.Runtime.Serialization;
  29. using System.Runtime.InteropServices;
  30. using System.Security.Permissions;
  31. namespace System.Threading
  32. {
  33. [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
  34. ExternalThreading = true)]
  35. public class ThreadLocal<T> : IDisposable
  36. {
  37. readonly Func<T> initializer;
  38. LocalDataStoreSlot localStore;
  39. class DataSlotWrapper
  40. {
  41. public bool Init;
  42. public Func<T> Getter;
  43. }
  44. public ThreadLocal () : this (LazyInitializer.GetDefaultCtorValue<T>)
  45. {
  46. }
  47. public ThreadLocal (Func<T> initializer)
  48. {
  49. if (initializer == null)
  50. throw new ArgumentNullException ("initializer");
  51. localStore = Thread.AllocateDataSlot ();
  52. this.initializer = initializer;
  53. }
  54. public void Dispose ()
  55. {
  56. Dispose(true);
  57. }
  58. protected virtual void Dispose (bool dispManagedRes)
  59. {
  60. }
  61. public bool IsValueCreated {
  62. get {
  63. return IsInitializedThreadLocal ();
  64. }
  65. }
  66. public T Value {
  67. get {
  68. return GetValueThreadLocal ();
  69. }
  70. }
  71. public override string ToString ()
  72. {
  73. return string.Format("[ThreadLocal: IsValueCreated={0}, Value={1}]", IsValueCreated, Value);
  74. }
  75. T GetValueThreadLocal ()
  76. {
  77. DataSlotWrapper myWrapper = Thread.GetData (localStore) as DataSlotWrapper;
  78. // In case it's the first time the Thread access its data
  79. if (myWrapper == null) {
  80. myWrapper = DataSlotCreator ();
  81. Thread.SetData (localStore, myWrapper);
  82. }
  83. return myWrapper.Getter();
  84. }
  85. bool IsInitializedThreadLocal ()
  86. {
  87. DataSlotWrapper myWrapper = (DataSlotWrapper)Thread.GetData (localStore);
  88. if (myWrapper == null) {
  89. myWrapper = DataSlotCreator ();
  90. Thread.SetData (localStore, myWrapper);
  91. }
  92. return myWrapper.Init;
  93. }
  94. DataSlotWrapper DataSlotCreator ()
  95. {
  96. DataSlotWrapper wrapper = new DataSlotWrapper ();
  97. Func<T> valSelector = initializer;
  98. wrapper.Getter = delegate {
  99. T val = valSelector ();
  100. wrapper.Init = true;
  101. wrapper.Getter = delegate { return val; };
  102. return val;
  103. };
  104. return wrapper;
  105. }
  106. }
  107. }
  108. #endif