ThreadLocal.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // ThreadLocal.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Rewritten by Paolo Molaro ([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.Collections.Generic;
  29. using System.Runtime.Serialization;
  30. using System.Runtime.InteropServices;
  31. using System.Security.Permissions;
  32. namespace System.Threading
  33. {
  34. [System.Diagnostics.DebuggerDisplay ("IsValueCreated={IsValueCreated}, Value={ValueForDebugDisplay}")]
  35. public class ThreadLocal<T> : IDisposable
  36. {
  37. struct TlsDatum {
  38. internal sbyte state; /* 0 uninitialized, < 0 initializing, > 0 inited */
  39. internal Exception cachedException; /* this is per-thread */
  40. internal T data;
  41. }
  42. Func<T> valueFactory;
  43. /* The tlsdata field is handled magically by the JIT
  44. * It must be a struct and it is always accessed by ldflda: the JIT, instead of
  45. * computing the address inside the instance, will return the address of the variable
  46. * for the current thread (based on tls_offset). This magic wouldn't be needed if C#
  47. * let us declare an icall with a TlsDatum& return type...
  48. * For this same reason, we must check tls_offset for != 0 to make sure it's valid before accessing tlsdata
  49. * The address of the tls var is cached per method at the first IL ldflda instruction, so care must be taken
  50. * not to cause it to be conditionally executed.
  51. */
  52. uint tls_offset;
  53. TlsDatum tlsdata;
  54. public ThreadLocal ()
  55. {
  56. tls_offset = Thread.AllocTlsData (typeof (TlsDatum));
  57. }
  58. public ThreadLocal (Func<T> valueFactory) : this ()
  59. {
  60. if (valueFactory == null)
  61. throw new ArgumentNullException ("valueFactory");
  62. this.valueFactory = valueFactory;
  63. }
  64. public ThreadLocal (bool trackAllValues) : this () {
  65. if (trackAllValues)
  66. throw new NotImplementedException ();
  67. }
  68. public ThreadLocal (Func<T> valueFactory, bool trackAllValues) : this (valueFactory) {
  69. if (trackAllValues)
  70. throw new NotImplementedException ();
  71. }
  72. public void Dispose ()
  73. {
  74. Dispose (true);
  75. }
  76. protected virtual void Dispose (bool disposing)
  77. {
  78. if (tls_offset != 0) {
  79. uint o = tls_offset;
  80. tls_offset = 0;
  81. if (disposing)
  82. valueFactory = null;
  83. Thread.DestroyTlsData (o);
  84. GC.SuppressFinalize (this);
  85. }
  86. }
  87. ~ThreadLocal ()
  88. {
  89. Dispose (false);
  90. }
  91. public bool IsValueCreated {
  92. get {
  93. if (tls_offset == 0)
  94. throw new ObjectDisposedException ("ThreadLocal object");
  95. /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
  96. return tlsdata.state > 0;
  97. }
  98. }
  99. T GetSlowPath () {
  100. /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
  101. if (tlsdata.cachedException != null)
  102. throw tlsdata.cachedException;
  103. if (tlsdata.state < 0)
  104. throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
  105. tlsdata.state = -1;
  106. if (valueFactory != null) {
  107. try {
  108. tlsdata.data = valueFactory ();
  109. } catch (Exception ex) {
  110. tlsdata.cachedException = ex;
  111. throw ex;
  112. }
  113. } else {
  114. tlsdata.data = default (T);
  115. }
  116. tlsdata.state = 1;
  117. return tlsdata.data;
  118. }
  119. [System.Diagnostics.DebuggerBrowsableAttribute (System.Diagnostics.DebuggerBrowsableState.Never)]
  120. public T Value {
  121. get {
  122. if (tls_offset == 0)
  123. throw new ObjectDisposedException ("ThreadLocal object");
  124. /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
  125. if (tlsdata.state > 0)
  126. return tlsdata.data;
  127. return GetSlowPath ();
  128. }
  129. set {
  130. if (tls_offset == 0)
  131. throw new ObjectDisposedException ("ThreadLocal object");
  132. /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
  133. tlsdata.state = 1;
  134. tlsdata.data = value;
  135. }
  136. }
  137. public IList<T> Values {
  138. get {
  139. if (tls_offset == 0)
  140. throw new ObjectDisposedException ("ThreadLocal object");
  141. throw new NotImplementedException ();
  142. }
  143. }
  144. public override string ToString ()
  145. {
  146. return Value.ToString ();
  147. }
  148. }
  149. }