-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathFastThreadLocal.cs
195 lines (167 loc) · 7.38 KB
/
FastThreadLocal.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Common
{
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public abstract class FastThreadLocal
{
static readonly int VariablesToRemoveIndex = InternalThreadLocalMap.NextVariableIndex();
/// <summary>
/// Removes all {@link FastThreadLocal} variables bound to the current thread. This operation is useful when you
/// are in a container environment, and you don't want to leave the thread local variables in the threads you do not
/// manage.
/// </summary>
public static void RemoveAll()
{
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.GetIfSet();
if (threadLocalMap == null)
{
return;
}
try
{
object v = threadLocalMap.GetIndexedVariable(VariablesToRemoveIndex);
if (v != null && v != InternalThreadLocalMap.Unset)
{
var variablesToRemove = (HashSet<FastThreadLocal>)v;
foreach (FastThreadLocal tlv in variablesToRemove) // todo: do we need to make a snapshot?
{
tlv.Remove(threadLocalMap);
}
}
}
finally
{
InternalThreadLocalMap.Remove();
}
}
/// Destroys the data structure that keeps all {@link FastThreadLocal} variables accessed from
/// non-{@link FastThreadLocalThread}s. This operation is useful when you are in a container environment, and you
/// do not want to leave the thread local variables in the threads you do not manage. Call this method when your
/// application is being unloaded from the container.
public static void Destroy() => InternalThreadLocalMap.Destroy();
protected static void AddToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal variable)
{
object v = threadLocalMap.GetIndexedVariable(VariablesToRemoveIndex);
HashSet<FastThreadLocal> variablesToRemove;
if (v == InternalThreadLocalMap.Unset || v == null)
{
variablesToRemove = new HashSet<FastThreadLocal>(); // Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
threadLocalMap.SetIndexedVariable(VariablesToRemoveIndex, variablesToRemove);
}
else
{
variablesToRemove = (HashSet<FastThreadLocal>)v;
}
variablesToRemove.Add(variable);
}
protected static void RemoveFromVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal variable)
{
object v = threadLocalMap.GetIndexedVariable(VariablesToRemoveIndex);
if (v == InternalThreadLocalMap.Unset || v == null)
{
return;
}
var variablesToRemove = (HashSet<FastThreadLocal>)v;
variablesToRemove.Remove(variable);
}
/// <summary>
/// Sets the value to uninitialized; a proceeding call to get() will trigger a call to GetInitialValue().
/// </summary>
/// <param name="threadLocalMap"></param>
public abstract void Remove(InternalThreadLocalMap threadLocalMap);
}
public class FastThreadLocal<T> : FastThreadLocal
where T : class
{
readonly int index;
/// <summary>
/// Returns the number of thread local variables bound to the current thread.
/// </summary>
public static int Count => InternalThreadLocalMap.GetIfSet()?.Count ?? 0;
public FastThreadLocal()
{
this.index = InternalThreadLocalMap.NextVariableIndex();
}
/// <summary>
/// Gets or sets current value for the current thread.
/// </summary>
public T Value
{
get { return this.Get(InternalThreadLocalMap.Get()); }
set { this.Set(InternalThreadLocalMap.Get(), value); }
}
/// <summary>
/// Returns the current value for the specified thread local map.
/// The specified thread local map must be for the current thread.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Get(InternalThreadLocalMap threadLocalMap)
{
object v = threadLocalMap.GetIndexedVariable(this.index);
if (v != InternalThreadLocalMap.Unset)
{
return (T)v;
}
return this.Initialize(threadLocalMap);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
T Initialize(InternalThreadLocalMap threadLocalMap)
{
T v = this.GetInitialValue();
threadLocalMap.SetIndexedVariable(this.index, v);
AddToVariablesToRemove(threadLocalMap, this);
return v;
}
/// <summary>
/// Set the value for the specified thread local map. The specified thread local map must be for the current thread.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(InternalThreadLocalMap threadLocalMap, T value)
{
if (threadLocalMap.SetIndexedVariable(this.index, value))
{
AddToVariablesToRemove(threadLocalMap, this);
}
}
/// <summary>
/// Returns {@code true} if and only if this thread-local variable is set.
/// </summary>
public bool IsSet() => this.IsSet(InternalThreadLocalMap.GetIfSet());
/// <summary>
/// Returns {@code true} if and only if this thread-local variable is set.
/// The specified thread local map must be for the current thread.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsSet(InternalThreadLocalMap threadLocalMap) => threadLocalMap != null && threadLocalMap.IsIndexedVariableSet(this.index);
/// <summary>
/// Returns the initial value for this thread-local variable.
/// </summary>
protected virtual T GetInitialValue() => null;
public void Remove() => this.Remove(InternalThreadLocalMap.GetIfSet());
/// Sets the value to uninitialized for the specified thread local map;
/// a proceeding call to get() will trigger a call to GetInitialValue().
/// The specified thread local map must be for the current thread.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Remove(InternalThreadLocalMap threadLocalMap)
{
if (threadLocalMap == null)
{
return;
}
object v = threadLocalMap.RemoveIndexedVariable(this.index);
RemoveFromVariablesToRemove(threadLocalMap, this);
if (v != InternalThreadLocalMap.Unset)
{
this.OnRemoval((T)v);
}
}
/// <summary>
/// Invoked when this thread local variable is removed by {@link #remove()}.
/// </summary>
protected virtual void OnRemoval(T value)
{
}
}
}