1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//
// ThreadLocal.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
// Rewritten by Paolo Molaro (lupus@ximian.com)
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if NET_4_0
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace System.Threading
{
[System.Diagnostics.DebuggerDisplay ("IsValueCreated={IsValueCreated}, Value={ValueForDebugDisplay}")]
public class ThreadLocal<T> : IDisposable
{
struct TlsDatum {
internal sbyte state; /* 0 uninitialized, < 0 initializing, > 0 inited */
internal Exception cachedException; /* this is per-thread */
internal T data;
}
Func<T> valueFactory;
/* The tlsdata field is handled magically by the JIT
* It must be a struct and it is always accessed by ldflda: the JIT, instead of
* computing the address inside the instance, will return the address of the variable
* for the current thread (based on tls_offset). This magic wouldn't be needed if C#
* let us declare an icall with a TlsDatum& return type...
* For this same reason, we must check tls_offset for != 0 to make sure it's valid before accessing tlsdata
* The address of the tls var is cached per method at the first IL ldflda instruction, so care must be taken
* not to cause it to be conditionally executed.
*/
uint tls_offset;
TlsDatum tlsdata;
public ThreadLocal ()
{
tls_offset = Thread.AllocTlsData (typeof (TlsDatum));
}
public ThreadLocal (Func<T> valueFactory) : this ()
{
if (valueFactory == null)
throw new ArgumentNullException ("valueFactory");
this.valueFactory = valueFactory;
}
#if NET_4_5
public ThreadLocal (bool trackAllValues) : this () {
if (trackAllValues)
throw new NotImplementedException ();
}
public ThreadLocal (Func<T> valueFactory, bool trackAllValues) : this (valueFactory) {
if (trackAllValues)
throw new NotImplementedException ();
}
#endif
public void Dispose ()
{
Dispose (true);
}
protected virtual void Dispose (bool disposing)
{
if (tls_offset != 0) {
uint o = tls_offset;
tls_offset = 0;
if (disposing)
valueFactory = null;
Thread.DestroyTlsData (o);
GC.SuppressFinalize (this);
}
}
~ThreadLocal ()
{
Dispose (false);
}
public bool IsValueCreated {
get {
if (tls_offset == 0)
throw new ObjectDisposedException ("ThreadLocal object");
/* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
return tlsdata.state > 0;
}
}
T GetSlowPath () {
/* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
if (tlsdata.cachedException != null)
throw tlsdata.cachedException;
if (tlsdata.state < 0)
throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
tlsdata.state = -1;
if (valueFactory != null) {
try {
tlsdata.data = valueFactory ();
} catch (Exception ex) {
tlsdata.cachedException = ex;
throw ex;
}
} else {
tlsdata.data = default (T);
}
tlsdata.state = 1;
return tlsdata.data;
}
[System.Diagnostics.DebuggerBrowsableAttribute (System.Diagnostics.DebuggerBrowsableState.Never)]
public T Value {
get {
if (tls_offset == 0)
throw new ObjectDisposedException ("ThreadLocal object");
/* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
if (tlsdata.state > 0)
return tlsdata.data;
return GetSlowPath ();
}
set {
if (tls_offset == 0)
throw new ObjectDisposedException ("ThreadLocal object");
/* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
tlsdata.state = 1;
tlsdata.data = value;
}
}
#if NET_4_5
public IList<T> Values {
get {
if (tls_offset == 0)
throw new ObjectDisposedException ("ThreadLocal object");
throw new NotImplementedException ();
}
}
#endif
public override string ToString ()
{
return Value.ToString ();
}
}
}
#endif
|