annotate Implab/ObjectPool.cs @ 107:f5220e5472ef v2

minor fixes and optimizations
author cin
date Tue, 11 Nov 2014 04:14:21 +0300
parents cdaaf4792c22
children 8beee0d11de6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
82
0363407ee75c added object pool
cin
parents:
diff changeset
1 using System;
0363407ee75c added object pool
cin
parents:
diff changeset
2 using Implab.Parallels;
0363407ee75c added object pool
cin
parents:
diff changeset
3 using System.Threading;
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
4 using System.Diagnostics;
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
5 using System.Diagnostics.CodeAnalysis;
82
0363407ee75c added object pool
cin
parents:
diff changeset
6
0363407ee75c added object pool
cin
parents:
diff changeset
7 namespace Implab {
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
8 public abstract class ObjectPool<T> : IDisposable {
82
0363407ee75c added object pool
cin
parents:
diff changeset
9 readonly int m_size;
0363407ee75c added object pool
cin
parents:
diff changeset
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
0363407ee75c added object pool
cin
parents:
diff changeset
11
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
12 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
13 static readonly bool _isValueType = typeof(T).IsValueType;
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
14
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
15 bool m_disposed;
82
0363407ee75c added object pool
cin
parents:
diff changeset
16
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
17 int m_count;
82
0363407ee75c added object pool
cin
parents:
diff changeset
18
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
19 protected ObjectPool(int size) {
82
0363407ee75c added object pool
cin
parents:
diff changeset
20 m_size = size;
0363407ee75c added object pool
cin
parents:
diff changeset
21 }
0363407ee75c added object pool
cin
parents:
diff changeset
22
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
23 protected ObjectPool() : this(Environment.ProcessorCount+1) {
82
0363407ee75c added object pool
cin
parents:
diff changeset
24 }
0363407ee75c added object pool
cin
parents:
diff changeset
25
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
26 public T Allocate() {
82
0363407ee75c added object pool
cin
parents:
diff changeset
27 if (m_disposed)
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
28 throw new ObjectDisposedException(ToString());
82
0363407ee75c added object pool
cin
parents:
diff changeset
29
0363407ee75c added object pool
cin
parents:
diff changeset
30 T instance;
0363407ee75c added object pool
cin
parents:
diff changeset
31 if (m_queue.TryDequeue(out instance)) {
0363407ee75c added object pool
cin
parents:
diff changeset
32 Interlocked.Decrement(ref m_count);
0363407ee75c added object pool
cin
parents:
diff changeset
33 } else {
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
34 instance = CreateInstance();
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
35 Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType);
82
0363407ee75c added object pool
cin
parents:
diff changeset
36 }
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
37 return instance;
82
0363407ee75c added object pool
cin
parents:
diff changeset
38 }
0363407ee75c added object pool
cin
parents:
diff changeset
39
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
40 protected abstract T CreateInstance();
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
41
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
42 protected virtual void CleanupInstance(T instance) {
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
43 }
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
44
82
0363407ee75c added object pool
cin
parents:
diff changeset
45 public void Release(T instance) {
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
46 if ( Object.Equals(instance,default(T)) && !_isValueType)
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
47 return;
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
48
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
49 Thread.MemoryBarrier();
82
0363407ee75c added object pool
cin
parents:
diff changeset
50 if (m_count < m_size && !m_disposed) {
0363407ee75c added object pool
cin
parents:
diff changeset
51 Interlocked.Increment(ref m_count);
0363407ee75c added object pool
cin
parents:
diff changeset
52
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
53 CleanupInstance(instance);
82
0363407ee75c added object pool
cin
parents:
diff changeset
54
0363407ee75c added object pool
cin
parents:
diff changeset
55 m_queue.Enqueue(instance);
0363407ee75c added object pool
cin
parents:
diff changeset
56
0363407ee75c added object pool
cin
parents:
diff changeset
57 // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
0363407ee75c added object pool
cin
parents:
diff changeset
58 // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
0363407ee75c added object pool
cin
parents:
diff changeset
59 // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
0363407ee75c added object pool
cin
parents:
diff changeset
60 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
61 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
62 ((IDisposable)instance).Dispose() ;
82
0363407ee75c added object pool
cin
parents:
diff changeset
63
0363407ee75c added object pool
cin
parents:
diff changeset
64 } else {
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
65 if (instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
66 ((IDisposable)instance).Dispose();
82
0363407ee75c added object pool
cin
parents:
diff changeset
67 }
0363407ee75c added object pool
cin
parents:
diff changeset
68 }
0363407ee75c added object pool
cin
parents:
diff changeset
69
0363407ee75c added object pool
cin
parents:
diff changeset
70 protected virtual void Dispose(bool disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
71 if (disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
72 m_disposed = true;
0363407ee75c added object pool
cin
parents:
diff changeset
73 T instance;
0363407ee75c added object pool
cin
parents:
diff changeset
74 while (m_queue.TryDequeue(out instance))
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
75 if (instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
76 ((IDisposable)instance).Dispose();
82
0363407ee75c added object pool
cin
parents:
diff changeset
77 }
0363407ee75c added object pool
cin
parents:
diff changeset
78 }
0363407ee75c added object pool
cin
parents:
diff changeset
79
0363407ee75c added object pool
cin
parents:
diff changeset
80 #region IDisposable implementation
0363407ee75c added object pool
cin
parents:
diff changeset
81
0363407ee75c added object pool
cin
parents:
diff changeset
82 public void Dispose() {
0363407ee75c added object pool
cin
parents:
diff changeset
83 Dispose(true);
0363407ee75c added object pool
cin
parents:
diff changeset
84 GC.SuppressFinalize(this);
0363407ee75c added object pool
cin
parents:
diff changeset
85 }
0363407ee75c added object pool
cin
parents:
diff changeset
86
0363407ee75c added object pool
cin
parents:
diff changeset
87 #endregion
0363407ee75c added object pool
cin
parents:
diff changeset
88 }
0363407ee75c added object pool
cin
parents:
diff changeset
89 }
0363407ee75c added object pool
cin
parents:
diff changeset
90