##// END OF EJS Templates
code cleanup
cin -
r91:cdaaf4792c22 v2
parent child
Show More
@@ -1,217 +1,216
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 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
7 7 <OutputType>Library</OutputType>
8 8 <RootNamespace>Implab</RootNamespace>
9 9 <AssemblyName>Implab</AssemblyName>
10 10 </PropertyGroup>
11 11 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
12 12 <DebugSymbols>true</DebugSymbols>
13 13 <DebugType>full</DebugType>
14 14 <Optimize>false</Optimize>
15 15 <OutputPath>bin\Debug</OutputPath>
16 16 <DefineConstants>TRACE;DEBUG;</DefineConstants>
17 17 <ErrorReport>prompt</ErrorReport>
18 18 <WarningLevel>4</WarningLevel>
19 19 <ConsolePause>false</ConsolePause>
20 20 <RunCodeAnalysis>true</RunCodeAnalysis>
21 21 </PropertyGroup>
22 22 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23 23 <DebugType>full</DebugType>
24 24 <Optimize>true</Optimize>
25 25 <OutputPath>bin\Release</OutputPath>
26 26 <ErrorReport>prompt</ErrorReport>
27 27 <WarningLevel>4</WarningLevel>
28 28 <ConsolePause>false</ConsolePause>
29 29 </PropertyGroup>
30 30 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
31 31 <DebugSymbols>true</DebugSymbols>
32 32 <DebugType>full</DebugType>
33 33 <Optimize>false</Optimize>
34 34 <OutputPath>bin\Debug</OutputPath>
35 35 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
36 36 <ErrorReport>prompt</ErrorReport>
37 37 <WarningLevel>4</WarningLevel>
38 38 <RunCodeAnalysis>true</RunCodeAnalysis>
39 39 <ConsolePause>false</ConsolePause>
40 40 </PropertyGroup>
41 41 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
42 42 <Optimize>true</Optimize>
43 43 <OutputPath>bin\Release</OutputPath>
44 44 <ErrorReport>prompt</ErrorReport>
45 45 <WarningLevel>4</WarningLevel>
46 46 <ConsolePause>false</ConsolePause>
47 47 <DefineConstants>NET_4_5</DefineConstants>
48 48 </PropertyGroup>
49 49 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' ">
50 50 <DebugSymbols>true</DebugSymbols>
51 51 <DebugType>full</DebugType>
52 52 <Optimize>false</Optimize>
53 53 <OutputPath>bin\Debug</OutputPath>
54 54 <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants>
55 55 <ErrorReport>prompt</ErrorReport>
56 56 <WarningLevel>4</WarningLevel>
57 57 <RunCodeAnalysis>true</RunCodeAnalysis>
58 58 <ConsolePause>false</ConsolePause>
59 59 </PropertyGroup>
60 60 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' ">
61 61 <Optimize>true</Optimize>
62 62 <OutputPath>bin\Release</OutputPath>
63 63 <DefineConstants>NET_4_5;MONO;</DefineConstants>
64 64 <ErrorReport>prompt</ErrorReport>
65 65 <WarningLevel>4</WarningLevel>
66 66 <ConsolePause>false</ConsolePause>
67 67 </PropertyGroup>
68 68 <ItemGroup>
69 69 <Reference Include="System" />
70 70 <Reference Include="System.Xml" />
71 71 </ItemGroup>
72 72 <ItemGroup>
73 73 <Compile Include="Component.cs" />
74 74 <Compile Include="CustomEqualityComparer.cs" />
75 75 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
76 76 <Compile Include="Diagnostics\EventText.cs" />
77 77 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
78 78 <Compile Include="Diagnostics\LogChannel.cs" />
79 79 <Compile Include="Diagnostics\LogicalOperation.cs" />
80 80 <Compile Include="Diagnostics\TextFileListener.cs" />
81 81 <Compile Include="Diagnostics\TextListenerBase.cs" />
82 82 <Compile Include="Diagnostics\TraceLog.cs" />
83 83 <Compile Include="Diagnostics\TraceContext.cs" />
84 84 <Compile Include="Diagnostics\TraceEvent.cs" />
85 85 <Compile Include="Diagnostics\TraceEventType.cs" />
86 86 <Compile Include="Disposable.cs" />
87 87 <Compile Include="ICancellable.cs" />
88 88 <Compile Include="IProgressHandler.cs" />
89 89 <Compile Include="IProgressNotifier.cs" />
90 90 <Compile Include="IPromiseT.cs" />
91 91 <Compile Include="IPromise.cs" />
92 92 <Compile Include="IServiceLocator.cs" />
93 93 <Compile Include="ITaskController.cs" />
94 94 <Compile Include="JSON\JSONElementContext.cs" />
95 95 <Compile Include="JSON\JSONElementType.cs" />
96 96 <Compile Include="JSON\JSONGrammar.cs" />
97 97 <Compile Include="JSON\JSONParser.cs" />
98 98 <Compile Include="JSON\JSONScanner.cs" />
99 99 <Compile Include="JSON\JsonTokenType.cs" />
100 100 <Compile Include="JSON\JSONWriter.cs" />
101 101 <Compile Include="JSON\JSONXmlReader.cs" />
102 102 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
103 103 <Compile Include="JSON\StringTranslator.cs" />
104 104 <Compile Include="Parallels\DispatchPool.cs" />
105 105 <Compile Include="Parallels\ArrayTraits.cs" />
106 106 <Compile Include="Parallels\MTQueue.cs" />
107 107 <Compile Include="Parallels\WorkerPool.cs" />
108 108 <Compile Include="Parsing\Alphabet.cs" />
109 109 <Compile Include="Parsing\AlphabetBase.cs" />
110 110 <Compile Include="Parsing\AltToken.cs" />
111 111 <Compile Include="Parsing\BinaryToken.cs" />
112 112 <Compile Include="Parsing\CatToken.cs" />
113 113 <Compile Include="Parsing\CDFADefinition.cs" />
114 114 <Compile Include="Parsing\DFABuilder.cs" />
115 115 <Compile Include="Parsing\DFADefinitionBase.cs" />
116 116 <Compile Include="Parsing\DFAStateDescriptor.cs" />
117 117 <Compile Include="Parsing\DFAutomaton.cs" />
118 118 <Compile Include="Parsing\EDFADefinition.cs" />
119 119 <Compile Include="Parsing\EmptyToken.cs" />
120 120 <Compile Include="Parsing\EndToken.cs" />
121 121 <Compile Include="Parsing\EnumAlphabet.cs" />
122 122 <Compile Include="Parsing\Grammar.cs" />
123 123 <Compile Include="Parsing\IAlphabet.cs" />
124 124 <Compile Include="Parsing\IDFADefinition.cs" />
125 125 <Compile Include="Parsing\IVisitor.cs" />
126 126 <Compile Include="Parsing\ParserException.cs" />
127 127 <Compile Include="Parsing\Scanner.cs" />
128 128 <Compile Include="Parsing\StarToken.cs" />
129 129 <Compile Include="Parsing\SymbolToken.cs" />
130 130 <Compile Include="Parsing\Token.cs" />
131 131 <Compile Include="SafePool.cs" />
132 132 <Compile Include="ServiceLocator.cs" />
133 133 <Compile Include="TaskController.cs" />
134 134 <Compile Include="ProgressInitEventArgs.cs" />
135 135 <Compile Include="Properties\AssemblyInfo.cs" />
136 136 <Compile Include="Promise.cs" />
137 137 <Compile Include="Parallels\AsyncPool.cs" />
138 138 <Compile Include="Safe.cs" />
139 139 <Compile Include="ValueEventArgs.cs" />
140 140 <Compile Include="PromiseExtensions.cs" />
141 141 <Compile Include="TransientPromiseException.cs" />
142 142 <Compile Include="SyncContextPromise.cs" />
143 143 <Compile Include="ObjectPool.cs" />
144 <Compile Include="ObjectPoolWrapper.cs" />
145 144 </ItemGroup>
146 145 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
147 146 <ItemGroup />
148 147 <ProjectExtensions>
149 148 <MonoDevelop>
150 149 <Properties>
151 150 <Policies>
152 151 <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
153 152 <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
154 153 <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" />
155 154 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" />
156 155 <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" />
157 156 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" />
158 157 <NameConventionPolicy>
159 158 <Rules>
160 159 <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
161 160 <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
162 161 <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
163 162 <RequiredPrefixes>
164 163 <String>I</String>
165 164 </RequiredPrefixes>
166 165 </NamingRule>
167 166 <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
168 167 <RequiredSuffixes>
169 168 <String>Attribute</String>
170 169 </RequiredSuffixes>
171 170 </NamingRule>
172 171 <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
173 172 <RequiredSuffixes>
174 173 <String>EventArgs</String>
175 174 </RequiredSuffixes>
176 175 </NamingRule>
177 176 <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
178 177 <RequiredSuffixes>
179 178 <String>Exception</String>
180 179 </RequiredSuffixes>
181 180 </NamingRule>
182 181 <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
183 182 <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" />
184 183 <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
185 184 <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" />
186 185 <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
187 186 <RequiredPrefixes>
188 187 <String>m_</String>
189 188 </RequiredPrefixes>
190 189 </NamingRule>
191 190 <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True">
192 191 <RequiredPrefixes>
193 192 <String>_</String>
194 193 </RequiredPrefixes>
195 194 </NamingRule>
196 195 <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
197 196 <RequiredPrefixes>
198 197 <String>m_</String>
199 198 </RequiredPrefixes>
200 199 </NamingRule>
201 200 <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
202 201 <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
203 202 <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
204 203 <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
205 204 <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
206 205 <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
207 206 <RequiredPrefixes>
208 207 <String>T</String>
209 208 </RequiredPrefixes>
210 209 </NamingRule>
211 210 </Rules>
212 211 </NameConventionPolicy>
213 212 </Policies>
214 213 </Properties>
215 214 </MonoDevelop>
216 215 </ProjectExtensions>
217 216 </Project> No newline at end of file
@@ -1,95 +1,90
1 1 using System;
2 2 using Implab.Parallels;
3 3 using System.Threading;
4 4 using System.Diagnostics;
5 5 using System.Diagnostics.CodeAnalysis;
6 6
7 7 namespace Implab {
8 8 public abstract class ObjectPool<T> : IDisposable {
9 9 readonly int m_size;
10 10 readonly MTQueue<T> m_queue = new MTQueue<T>();
11 11
12 12 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
13 13 static readonly bool _isValueType = typeof(T).IsValueType;
14 14
15 15 bool m_disposed;
16 16
17 17 int m_count;
18 18
19 19 protected ObjectPool(int size) {
20 20 m_size = size;
21 21 }
22 22
23 23 protected ObjectPool() : this(Environment.ProcessorCount+1) {
24 24 }
25 25
26 public ObjectPoolWrapper<T> AllocateAuto() {
27
28 return new ObjectPoolWrapper<T>(Allocate(), this);
29 }
30
31 26 public T Allocate() {
32 27 if (m_disposed)
33 28 throw new ObjectDisposedException(ToString());
34 29
35 30 T instance;
36 31 if (m_queue.TryDequeue(out instance)) {
37 32 Interlocked.Decrement(ref m_count);
38 33 } else {
39 34 instance = CreateInstance();
40 35 Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType);
41 36 }
42 37 return instance;
43 38 }
44 39
45 40 protected abstract T CreateInstance();
46 41
47 42 protected virtual void CleanupInstance(T instance) {
48 43 }
49 44
50 45 public void Release(T instance) {
51 46 if ( Object.Equals(instance,default(T)) && !_isValueType)
52 47 return;
53 48
54 49 Thread.MemoryBarrier();
55 50 if (m_count < m_size && !m_disposed) {
56 51 Interlocked.Increment(ref m_count);
57 52
58 53 CleanupInstance(instance);
59 54
60 55 m_queue.Enqueue(instance);
61 56
62 57 // пока элемент возвращался в кеш, была начата операция освобождения всего кеша
63 58 // и возможно уже законцена, в таком случае следует извлечь элемент обратно и
64 59 // освободить его. Если операция освобождения кеша еще не заврешилась, то будет
65 60 // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса.
66 61 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
67 62 ((IDisposable)instance).Dispose() ;
68 63
69 64 } else {
70 65 if (instance is IDisposable)
71 66 ((IDisposable)instance).Dispose();
72 67 }
73 68 }
74 69
75 70 protected virtual void Dispose(bool disposing) {
76 71 if (disposing) {
77 72 m_disposed = true;
78 73 T instance;
79 74 while (m_queue.TryDequeue(out instance))
80 75 if (instance is IDisposable)
81 76 ((IDisposable)instance).Dispose();
82 77 }
83 78 }
84 79
85 80 #region IDisposable implementation
86 81
87 82 public void Dispose() {
88 83 Dispose(true);
89 84 GC.SuppressFinalize(this);
90 85 }
91 86
92 87 #endregion
93 88 }
94 89 }
95 90
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now