annotate Implab/ObjectPool.cs @ 85:abe260860bd6 v2

fixed JSONXmlReader disposing under ugly mono ObjectPool is made abstract
author cin
date Tue, 30 Sep 2014 16:05:35 +0400
parents 397fe8db0806
children cdaaf4792c22
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 ObjectPoolWrapper<T> AllocateAuto() {
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
27
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
28 return new ObjectPoolWrapper<T>(Allocate(), this);
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
29 }
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
30
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
31 public T Allocate() {
82
0363407ee75c added object pool
cin
parents:
diff changeset
32 if (m_disposed)
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
33 throw new ObjectDisposedException(ToString());
82
0363407ee75c added object pool
cin
parents:
diff changeset
34
0363407ee75c added object pool
cin
parents:
diff changeset
35 T instance;
0363407ee75c added object pool
cin
parents:
diff changeset
36 if (m_queue.TryDequeue(out instance)) {
0363407ee75c added object pool
cin
parents:
diff changeset
37 Interlocked.Decrement(ref m_count);
0363407ee75c added object pool
cin
parents:
diff changeset
38 } else {
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
39 instance = CreateInstance();
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
40 Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType);
82
0363407ee75c added object pool
cin
parents:
diff changeset
41 }
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
42 return instance;
82
0363407ee75c added object pool
cin
parents:
diff changeset
43 }
0363407ee75c added object pool
cin
parents:
diff changeset
44
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
45 protected abstract T CreateInstance();
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
46
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
47 protected virtual void CleanupInstance(T instance) {
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
48 }
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
49
82
0363407ee75c added object pool
cin
parents:
diff changeset
50 public void Release(T instance) {
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
51 if ( Object.Equals(instance,default(T)) && !_isValueType)
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
52 return;
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
53
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
54 Thread.MemoryBarrier();
82
0363407ee75c added object pool
cin
parents:
diff changeset
55 if (m_count < m_size && !m_disposed) {
0363407ee75c added object pool
cin
parents:
diff changeset
56 Interlocked.Increment(ref m_count);
0363407ee75c added object pool
cin
parents:
diff changeset
57
85
abe260860bd6 fixed JSONXmlReader disposing under ugly mono
cin
parents: 83
diff changeset
58 CleanupInstance(instance);
82
0363407ee75c added object pool
cin
parents:
diff changeset
59
0363407ee75c added object pool
cin
parents:
diff changeset
60 m_queue.Enqueue(instance);
0363407ee75c added object pool
cin
parents:
diff changeset
61
0363407ee75c added object pool
cin
parents:
diff changeset
62 // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
0363407ee75c added object pool
cin
parents:
diff changeset
63 // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
0363407ee75c added object pool
cin
parents:
diff changeset
64 // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
0363407ee75c added object pool
cin
parents:
diff changeset
65 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
66 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
67 ((IDisposable)instance).Dispose() ;
82
0363407ee75c added object pool
cin
parents:
diff changeset
68
0363407ee75c added object pool
cin
parents:
diff changeset
69 } else {
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
70 if (instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
71 ((IDisposable)instance).Dispose();
82
0363407ee75c added object pool
cin
parents:
diff changeset
72 }
0363407ee75c added object pool
cin
parents:
diff changeset
73 }
0363407ee75c added object pool
cin
parents:
diff changeset
74
0363407ee75c added object pool
cin
parents:
diff changeset
75 protected virtual void Dispose(bool disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
76 if (disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
77 m_disposed = true;
0363407ee75c added object pool
cin
parents:
diff changeset
78 T instance;
0363407ee75c added object pool
cin
parents:
diff changeset
79 while (m_queue.TryDequeue(out instance))
83
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
80 if (instance is IDisposable)
397fe8db0806 fixed object pool
cin
parents: 82
diff changeset
81 ((IDisposable)instance).Dispose();
82
0363407ee75c added object pool
cin
parents:
diff changeset
82 }
0363407ee75c added object pool
cin
parents:
diff changeset
83 }
0363407ee75c added object pool
cin
parents:
diff changeset
84
0363407ee75c added object pool
cin
parents:
diff changeset
85 #region IDisposable implementation
0363407ee75c added object pool
cin
parents:
diff changeset
86
0363407ee75c added object pool
cin
parents:
diff changeset
87 public void Dispose() {
0363407ee75c added object pool
cin
parents:
diff changeset
88 Dispose(true);
0363407ee75c added object pool
cin
parents:
diff changeset
89 GC.SuppressFinalize(this);
0363407ee75c added object pool
cin
parents:
diff changeset
90 }
0363407ee75c added object pool
cin
parents:
diff changeset
91
0363407ee75c added object pool
cin
parents:
diff changeset
92 #endregion
0363407ee75c added object pool
cin
parents:
diff changeset
93 }
0363407ee75c added object pool
cin
parents:
diff changeset
94 }
0363407ee75c added object pool
cin
parents:
diff changeset
95