##// END OF EJS Templates
Added SafePool
cin -
r61:90069a2ec20a default
parent child
Show More
@@ -0,0 +1,40
1 using Implab.Parallels;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading;
7
8 namespace Implab {
9 public class SafePool<T> where T : new() {
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
11 readonly int m_size;
12 int m_count = 0;
13
14 public SafePool() : this(10) {
15
16 }
17
18 public SafePool(int size) {
19 Safe.ArgumentInRange(size,1,size,"size");
20
21 m_size = size;
22 }
23
24 public T Allocate() {
25 T instance;
26 if (m_queue.TryDequeue(out instance)) {
27 Interlocked.Decrement(ref m_count);
28 return instance;
29 }
30 return new T();
31 }
32
33 public void Release(T instance) {
34 if (m_count < m_size) {
35 Interlocked.Increment(ref m_count);
36 m_queue.Enqueue(instance);
37 }
38 }
39 }
40 }
@@ -1,106 +1,107
1 1 ο»Ώ<?xml version="1.0" encoding="utf-8"?>
2 2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 3 <PropertyGroup>
4 4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 6 <ProductVersion>10.0.0</ProductVersion>
7 7 <SchemaVersion>2.0</SchemaVersion>
8 8 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
9 9 <OutputType>Library</OutputType>
10 10 <RootNamespace>Implab</RootNamespace>
11 11 <AssemblyName>Implab</AssemblyName>
12 12 </PropertyGroup>
13 13 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14 14 <DebugSymbols>true</DebugSymbols>
15 15 <DebugType>full</DebugType>
16 16 <Optimize>false</Optimize>
17 17 <OutputPath>bin\Debug</OutputPath>
18 18 <DefineConstants>TRACE;DEBUG;</DefineConstants>
19 19 <ErrorReport>prompt</ErrorReport>
20 20 <WarningLevel>4</WarningLevel>
21 21 <ConsolePause>false</ConsolePause>
22 22 <RunCodeAnalysis>true</RunCodeAnalysis>
23 23 </PropertyGroup>
24 24 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25 25 <DebugType>full</DebugType>
26 26 <Optimize>true</Optimize>
27 27 <OutputPath>bin\Release</OutputPath>
28 28 <ErrorReport>prompt</ErrorReport>
29 29 <WarningLevel>4</WarningLevel>
30 30 <ConsolePause>false</ConsolePause>
31 31 </PropertyGroup>
32 32 <ItemGroup>
33 33 <Reference Include="System" />
34 34 <Reference Include="System.XML" />
35 35 </ItemGroup>
36 36 <ItemGroup>
37 37 <Compile Include="Component.cs" />
38 38 <Compile Include="CustomEqualityComparer.cs" />
39 39 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
40 40 <Compile Include="Diagnostics\EventText.cs" />
41 41 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
42 42 <Compile Include="Diagnostics\LogChannel.cs" />
43 43 <Compile Include="Diagnostics\LogicalOperation.cs" />
44 44 <Compile Include="Diagnostics\TextFileListener.cs" />
45 45 <Compile Include="Diagnostics\TextListenerBase.cs" />
46 46 <Compile Include="Diagnostics\TraceLog.cs" />
47 47 <Compile Include="Diagnostics\TraceContext.cs" />
48 48 <Compile Include="Diagnostics\TraceEvent.cs" />
49 49 <Compile Include="Diagnostics\TraceEventType.cs" />
50 50 <Compile Include="Disposable.cs" />
51 51 <Compile Include="ICancellable.cs" />
52 52 <Compile Include="IProgressHandler.cs" />
53 53 <Compile Include="IProgressNotifier.cs" />
54 54 <Compile Include="IPromise.cs" />
55 55 <Compile Include="IPromiseBase.cs" />
56 56 <Compile Include="IServiceLocator.cs" />
57 57 <Compile Include="ITaskController.cs" />
58 58 <Compile Include="JSON\JSONElementContext.cs" />
59 59 <Compile Include="JSON\JSONElementType.cs" />
60 60 <Compile Include="JSON\JSONGrammar.cs" />
61 61 <Compile Include="JSON\JSONParser.cs" />
62 62 <Compile Include="JSON\JSONScanner.cs" />
63 63 <Compile Include="JSON\JsonTokenType.cs" />
64 64 <Compile Include="JSON\JSONWriter.cs" />
65 65 <Compile Include="JSON\JSONXmlReader.cs" />
66 66 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
67 67 <Compile Include="JSON\StringTranslator.cs" />
68 68 <Compile Include="Parallels\DispatchPool.cs" />
69 69 <Compile Include="Parallels\ArrayTraits.cs" />
70 70 <Compile Include="Parallels\MTQueue.cs" />
71 71 <Compile Include="Parallels\WorkerPool.cs" />
72 72 <Compile Include="Parsing\Alphabet.cs" />
73 73 <Compile Include="Parsing\AlphabetBase.cs" />
74 74 <Compile Include="Parsing\AltToken.cs" />
75 75 <Compile Include="Parsing\BinaryToken.cs" />
76 76 <Compile Include="Parsing\CatToken.cs" />
77 77 <Compile Include="Parsing\CDFADefinition.cs" />
78 78 <Compile Include="Parsing\DFABuilder.cs" />
79 79 <Compile Include="Parsing\DFADefinitionBase.cs" />
80 80 <Compile Include="Parsing\DFAStateDescriptor.cs" />
81 81 <Compile Include="Parsing\DFAutomaton.cs" />
82 82 <Compile Include="Parsing\EDFADefinition.cs" />
83 83 <Compile Include="Parsing\EmptyToken.cs" />
84 84 <Compile Include="Parsing\EndToken.cs" />
85 85 <Compile Include="Parsing\EnumAlphabet.cs" />
86 86 <Compile Include="Parsing\Grammar.cs" />
87 87 <Compile Include="Parsing\IAlphabet.cs" />
88 88 <Compile Include="Parsing\IDFADefinition.cs" />
89 89 <Compile Include="Parsing\IVisitor.cs" />
90 90 <Compile Include="Parsing\ParserException.cs" />
91 91 <Compile Include="Parsing\Scanner.cs" />
92 92 <Compile Include="Parsing\StarToken.cs" />
93 93 <Compile Include="Parsing\SymbolToken.cs" />
94 94 <Compile Include="Parsing\Token.cs" />
95 <Compile Include="SafePool.cs" />
95 96 <Compile Include="ServiceLocator.cs" />
96 97 <Compile Include="TaskController.cs" />
97 98 <Compile Include="ProgressInitEventArgs.cs" />
98 99 <Compile Include="Properties\AssemblyInfo.cs" />
99 100 <Compile Include="Promise.cs" />
100 101 <Compile Include="Parallels\AsyncPool.cs" />
101 102 <Compile Include="Safe.cs" />
102 103 <Compile Include="ValueEventArgs.cs" />
103 104 </ItemGroup>
104 105 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
105 106 <ItemGroup />
106 107 </Project> No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now