annotate Implab/SafePool.cs @ 241:c19cee55e85e v2-1

close v2-1 bad idea
author cin
date Thu, 10 Nov 2016 00:31:34 +0300
parents 90069a2ec20a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
90069a2ec20a Added SafePool
cin
parents:
diff changeset
1 using Implab.Parallels;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
2 using System;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
3 using System.Collections.Generic;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
4 using System.Linq;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
5 using System.Text;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
6 using System.Threading;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
7
90069a2ec20a Added SafePool
cin
parents:
diff changeset
8 namespace Implab {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
9 public class SafePool<T> where T : new() {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
90069a2ec20a Added SafePool
cin
parents:
diff changeset
11 readonly int m_size;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
12 int m_count = 0;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
13
90069a2ec20a Added SafePool
cin
parents:
diff changeset
14 public SafePool() : this(10) {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
15
90069a2ec20a Added SafePool
cin
parents:
diff changeset
16 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
17
90069a2ec20a Added SafePool
cin
parents:
diff changeset
18 public SafePool(int size) {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
19 Safe.ArgumentInRange(size,1,size,"size");
90069a2ec20a Added SafePool
cin
parents:
diff changeset
20
90069a2ec20a Added SafePool
cin
parents:
diff changeset
21 m_size = size;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
22 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
23
90069a2ec20a Added SafePool
cin
parents:
diff changeset
24 public T Allocate() {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
25 T instance;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
26 if (m_queue.TryDequeue(out instance)) {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
27 Interlocked.Decrement(ref m_count);
90069a2ec20a Added SafePool
cin
parents:
diff changeset
28 return instance;
90069a2ec20a Added SafePool
cin
parents:
diff changeset
29 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
30 return new T();
90069a2ec20a Added SafePool
cin
parents:
diff changeset
31 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
32
90069a2ec20a Added SafePool
cin
parents:
diff changeset
33 public void Release(T instance) {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
34 if (m_count < m_size) {
90069a2ec20a Added SafePool
cin
parents:
diff changeset
35 Interlocked.Increment(ref m_count);
90069a2ec20a Added SafePool
cin
parents:
diff changeset
36 m_queue.Enqueue(instance);
90069a2ec20a Added SafePool
cin
parents:
diff changeset
37 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
38 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
39 }
90069a2ec20a Added SafePool
cin
parents:
diff changeset
40 }