aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Common/ThreadStaticPool.cs
blob: 3fce28ec0f9fc368be8827e18add6f891249fd5b (plain)
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace ARMeilleure.Common
{
    class ThreadStaticPool<T> where T : class, new()
    {
        private const int PoolSizeIncrement = 200;

        [ThreadStatic]
        private static ThreadStaticPool<T> _instance;

        public static ThreadStaticPool<T> Instance
        {
            get
            {
                if (_instance == null)
                {
                    PreparePool(0); // So that we can still use a pool when blindly initializing one.
                }

                return _instance;
            }
        }

        private static ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>>();

        private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
        {
            return _pools.GetOrAdd(groupId, x => new Stack<ThreadStaticPool<T>>());
        }

        public static void PreparePool(int groupId)
        {
            // Prepare the pool for this thread, ideally using an existing one from the specified group.

            if (_instance == null)
            {
                var pools = GetPools(groupId);
                lock (pools)
                {
                    _instance = (pools.Count != 0) ? pools.Pop() : new ThreadStaticPool<T>(PoolSizeIncrement * 2);
                }
            }
        }

        public static void ReturnPool(int groupId)
        {
            // Reset and return the pool for this thread to the specified group.

            var pools = GetPools(groupId);
            lock (pools)
            {
                _instance.Clear();
                pools.Push(_instance);

                _instance = null;
            }
        }

        public static void ResetPools()
        {
            // Resets any static references to the pools used by threads for each group, allowing them to be garbage collected.

            foreach (var pools in _pools.Values)
            {
                pools.Clear();
            }

            _pools.Clear();
        }

        private T[] _pool;
        private int _poolUsed = -1;
        private int _poolSize;

        public ThreadStaticPool(int initialSize)
        {
            _pool = new T[initialSize];

            for (int i = 0; i < initialSize; i++)
            {
                _pool[i] = new T();
            }

            _poolSize = initialSize;
        }

        public T Allocate()
        {
            int index = Interlocked.Increment(ref _poolUsed);

            if (index >= _poolSize)
            {
                IncreaseSize();
            }

            return _pool[index];
        }

        private void IncreaseSize()
        {
            _poolSize += PoolSizeIncrement;

            T[] newArray = new T[_poolSize];
            Array.Copy(_pool, 0, newArray, 0, _pool.Length);

            for (int i = _pool.Length; i < _poolSize; i++)
            {
                newArray[i] = new T();
            }

            Interlocked.Exchange(ref _pool, newArray);
        }

        public void Clear()
        {
            _poolUsed = -1;
        }
    }
}