forked from budgetdevv/PythonNETExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadStatic.cs
More file actions
36 lines (31 loc) · 999 Bytes
/
ThreadStatic.cs
File metadata and controls
36 lines (31 loc) · 999 Bytes
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
using System;
using System.Runtime.CompilerServices;
namespace PythonNETExtensions.Core
{
public interface IThreadStaticDefinition<T>
{
public static abstract T CreateItem();
public static abstract void OnGet(ref T item);
}
public static class ThreadStatic<ThreadStaticDefinitionT, ItemT>
where ThreadStaticDefinitionT: struct, IThreadStaticDefinition<ItemT>
{
[ThreadStatic]
private static ItemT ThreadLocal;
public static ItemT Item
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var item = ThreadLocal ?? CreateItem();
ThreadStaticDefinitionT.OnGet(ref item);
return item;
[MethodImpl(MethodImplOptions.NoInlining)]
ItemT CreateItem()
{
return ThreadStaticDefinitionT.CreateItem();
}
}
}
}
}