annotate Implab/ObjectPool.cs @ 82:0363407ee75c v2

added object pool
author cin
date Mon, 29 Sep 2014 05:04:32 +0400
parents
children 397fe8db0806
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;
0363407ee75c added object pool
cin
parents:
diff changeset
4
0363407ee75c added object pool
cin
parents:
diff changeset
5 namespace Implab {
0363407ee75c added object pool
cin
parents:
diff changeset
6 public class ObjectPool<T> : IDisposable {
0363407ee75c added object pool
cin
parents:
diff changeset
7 readonly Func<T> m_factory;
0363407ee75c added object pool
cin
parents:
diff changeset
8 readonly Action<T> m_cleanup;
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
0363407ee75c added object pool
cin
parents:
diff changeset
12 volatile bool m_disposed;
0363407ee75c added object pool
cin
parents:
diff changeset
13
0363407ee75c added object pool
cin
parents:
diff changeset
14 volatile int m_count;
0363407ee75c added object pool
cin
parents:
diff changeset
15
0363407ee75c added object pool
cin
parents:
diff changeset
16 public ObjectPool(Func<T> factory, Action<T> cleanup, int size) {
0363407ee75c added object pool
cin
parents:
diff changeset
17 Safe.ArgumentNotNull(factory, "factory");
0363407ee75c added object pool
cin
parents:
diff changeset
18 Safe.ArgumentInRange(size, 1, size, "size");
0363407ee75c added object pool
cin
parents:
diff changeset
19
0363407ee75c added object pool
cin
parents:
diff changeset
20 m_factory = factory;
0363407ee75c added object pool
cin
parents:
diff changeset
21 m_cleanup = cleanup;
0363407ee75c added object pool
cin
parents:
diff changeset
22 m_size = size;
0363407ee75c added object pool
cin
parents:
diff changeset
23 }
0363407ee75c added object pool
cin
parents:
diff changeset
24
0363407ee75c added object pool
cin
parents:
diff changeset
25 public ObjectPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) {
0363407ee75c added object pool
cin
parents:
diff changeset
26 }
0363407ee75c added object pool
cin
parents:
diff changeset
27
0363407ee75c added object pool
cin
parents:
diff changeset
28 public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) {
0363407ee75c added object pool
cin
parents:
diff changeset
29 }
0363407ee75c added object pool
cin
parents:
diff changeset
30
0363407ee75c added object pool
cin
parents:
diff changeset
31 public ObjectPoolWrapper<T> Allocate() {
0363407ee75c added object pool
cin
parents:
diff changeset
32 if (m_disposed)
0363407ee75c added object pool
cin
parents:
diff changeset
33 throw new ObjectDisposedException(this.ToString());
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 return instance;
0363407ee75c added object pool
cin
parents:
diff changeset
39 } else {
0363407ee75c added object pool
cin
parents:
diff changeset
40 instance = m_factory();
0363407ee75c added object pool
cin
parents:
diff changeset
41 }
0363407ee75c added object pool
cin
parents:
diff changeset
42 return new ObjectPoolWrapper<T>(instance, this);
0363407ee75c added object pool
cin
parents:
diff changeset
43 }
0363407ee75c added object pool
cin
parents:
diff changeset
44
0363407ee75c added object pool
cin
parents:
diff changeset
45 public void Release(T instance) {
0363407ee75c added object pool
cin
parents:
diff changeset
46 if (m_count < m_size && !m_disposed) {
0363407ee75c added object pool
cin
parents:
diff changeset
47 Interlocked.Increment(ref m_count);
0363407ee75c added object pool
cin
parents:
diff changeset
48
0363407ee75c added object pool
cin
parents:
diff changeset
49 if (m_cleanup != null)
0363407ee75c added object pool
cin
parents:
diff changeset
50 m_cleanup(instance);
0363407ee75c added object pool
cin
parents:
diff changeset
51
0363407ee75c added object pool
cin
parents:
diff changeset
52 m_queue.Enqueue(instance);
0363407ee75c added object pool
cin
parents:
diff changeset
53
0363407ee75c added object pool
cin
parents:
diff changeset
54 // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
0363407ee75c added object pool
cin
parents:
diff changeset
55 // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
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 if (m_disposed && m_queue.TryDequeue(out instance))
0363407ee75c added object pool
cin
parents:
diff changeset
59 Safe.Dispose(instance);
0363407ee75c added object pool
cin
parents:
diff changeset
60
0363407ee75c added object pool
cin
parents:
diff changeset
61 } else {
0363407ee75c added object pool
cin
parents:
diff changeset
62 Safe.Dispose(instance);
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
0363407ee75c added object pool
cin
parents:
diff changeset
66 protected virtual void Dispose(bool disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
67 if (disposing) {
0363407ee75c added object pool
cin
parents:
diff changeset
68 m_disposed = true;
0363407ee75c added object pool
cin
parents:
diff changeset
69 T instance;
0363407ee75c added object pool
cin
parents:
diff changeset
70 while (m_queue.TryDequeue(out instance))
0363407ee75c added object pool
cin
parents:
diff changeset
71 Safe.Dispose(instance);
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 #region IDisposable implementation
0363407ee75c added object pool
cin
parents:
diff changeset
76
0363407ee75c added object pool
cin
parents:
diff changeset
77 public void Dispose() {
0363407ee75c added object pool
cin
parents:
diff changeset
78 Dispose(true);
0363407ee75c added object pool
cin
parents:
diff changeset
79 GC.SuppressFinalize(this);
0363407ee75c added object pool
cin
parents:
diff changeset
80 }
0363407ee75c added object pool
cin
parents:
diff changeset
81
0363407ee75c added object pool
cin
parents:
diff changeset
82 #endregion
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