annotate Implab/SyncPool.cs @ 80:4f20870d0816 v2

added memory barriers
author cin
date Fri, 26 Sep 2014 03:32:34 +0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80
4f20870d0816 added memory barriers
cin
parents:
diff changeset
1 using System;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
2 using Implab.Parallels;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
3 using System.Threading;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
4
4f20870d0816 added memory barriers
cin
parents:
diff changeset
5 namespace Implab {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
6 /*public class SyncPool<T> : IDisposable {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
7 readonly Func<T> m_factory;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
8 readonly Action<T> m_cleanup;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
9 readonly int m_size;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
4f20870d0816 added memory barriers
cin
parents:
diff changeset
11
4f20870d0816 added memory barriers
cin
parents:
diff changeset
12 volatile bool m_disposed;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
13
4f20870d0816 added memory barriers
cin
parents:
diff changeset
14 volatile int m_count;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
15
4f20870d0816 added memory barriers
cin
parents:
diff changeset
16 public SyncPool(Func<T> factory, Action<T> cleanup, int size) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
17 Safe.ArgumentNotNull(factory, "factory");
4f20870d0816 added memory barriers
cin
parents:
diff changeset
18 Safe.ArgumentInRange(size, 1, size, "size");
4f20870d0816 added memory barriers
cin
parents:
diff changeset
19
4f20870d0816 added memory barriers
cin
parents:
diff changeset
20 m_factory = factory;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
21 m_cleanup = cleanup;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
22 m_size = size;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
23 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
24
4f20870d0816 added memory barriers
cin
parents:
diff changeset
25 public SyncPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
26 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
27
4f20870d0816 added memory barriers
cin
parents:
diff changeset
28 public SyncPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
29 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
30
4f20870d0816 added memory barriers
cin
parents:
diff changeset
31 public SyncPoolWrapper<T> Allocate() {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
32 if (m_disposed)
4f20870d0816 added memory barriers
cin
parents:
diff changeset
33 throw new ObjectDisposedException(this.ToString());
4f20870d0816 added memory barriers
cin
parents:
diff changeset
34
4f20870d0816 added memory barriers
cin
parents:
diff changeset
35 T instance;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
36 if (m_queue.TryDequeue(out instance)) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
37 Interlocked.Decrement(ref m_count);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
38 return instance;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
39 } else {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
40 instance = m_factory();
4f20870d0816 added memory barriers
cin
parents:
diff changeset
41 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
42 return new SyncPoolWrapper<T>(instance, this);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
43 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
44
4f20870d0816 added memory barriers
cin
parents:
diff changeset
45 public void Release(T instance) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
46 if (m_count < m_size && !m_disposed) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
47 Interlocked.Increment(ref m_count);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
48
4f20870d0816 added memory barriers
cin
parents:
diff changeset
49 if (m_cleanup != null)
4f20870d0816 added memory barriers
cin
parents:
diff changeset
50 m_cleanup(instance);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
51
4f20870d0816 added memory barriers
cin
parents:
diff changeset
52 m_queue.Enqueue(instance);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
53
4f20870d0816 added memory barriers
cin
parents:
diff changeset
54 // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
4f20870d0816 added memory barriers
cin
parents:
diff changeset
55 // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
4f20870d0816 added memory barriers
cin
parents:
diff changeset
56 // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
4f20870d0816 added memory barriers
cin
parents:
diff changeset
57 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
4f20870d0816 added memory barriers
cin
parents:
diff changeset
58 if (m_disposed && m_queue.TryDequeue(out instance))
4f20870d0816 added memory barriers
cin
parents:
diff changeset
59 Safe.Dispose(instance);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
60
4f20870d0816 added memory barriers
cin
parents:
diff changeset
61 } else {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
62 Safe.Dispose(instance);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
63 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
64 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
65
4f20870d0816 added memory barriers
cin
parents:
diff changeset
66 protected virtual void Dispose(bool disposing) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
67 if (disposing) {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
68 m_disposed = true;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
69 T instance;
4f20870d0816 added memory barriers
cin
parents:
diff changeset
70 while (m_queue.TryDequeue(out instance))
4f20870d0816 added memory barriers
cin
parents:
diff changeset
71 Safe.Dispose(instance);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
72 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
73 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
74
4f20870d0816 added memory barriers
cin
parents:
diff changeset
75 #region IDisposable implementation
4f20870d0816 added memory barriers
cin
parents:
diff changeset
76
4f20870d0816 added memory barriers
cin
parents:
diff changeset
77 public void Dispose() {
4f20870d0816 added memory barriers
cin
parents:
diff changeset
78 Dispose(true);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
79 GC.SuppressFinalize(this);
4f20870d0816 added memory barriers
cin
parents:
diff changeset
80 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
81
4f20870d0816 added memory barriers
cin
parents:
diff changeset
82 #endregion
4f20870d0816 added memory barriers
cin
parents:
diff changeset
83 }*/
4f20870d0816 added memory barriers
cin
parents:
diff changeset
84 }
4f20870d0816 added memory barriers
cin
parents:
diff changeset
85