annotate Implab/Components/DisposablePool.cs @ 152:240aa6994018 v2

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