| @@ -0,0 +1,31 | |||||
|
|
1 | using System; | |||
|
|
2 | using System.Threading; | |||
|
|
3 | ||||
|
|
4 | namespace Implab.Parallels { | |||
|
|
5 | /// <summary> | |||
|
|
6 | /// Implements simple signalling logic using <see cref="Monitor.PulseAll(object)"/>. | |||
|
|
7 | /// </summary> | |||
|
|
8 | public class Signal { | |||
|
|
9 | readonly object m_lock = new object(); | |||
|
|
10 | bool m_state; | |||
|
|
11 | ||||
|
|
12 | public void Set() { | |||
|
|
13 | lock(m_lock) { | |||
|
|
14 | m_state = true; | |||
|
|
15 | Monitor.PulseAll(m_lock); | |||
|
|
16 | } | |||
|
|
17 | } | |||
|
|
18 | ||||
|
|
19 | public void Wait() { | |||
|
|
20 | lock (m_lock) | |||
|
|
21 | if (!m_state) | |||
|
|
22 | Monitor.Wait(m_lock); | |||
|
|
23 | } | |||
|
|
24 | ||||
|
|
25 | public bool Wait(int timeout) { | |||
|
|
26 | lock (m_lock) | |||
|
|
27 | return m_state || Monitor.Wait(m_lock, timeout); | |||
|
|
28 | } | |||
|
|
29 | } | |||
|
|
30 | } | |||
|
|
31 | ||||
| @@ -1,230 +1,231 | |||||
| 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> | |
| 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
| 3 | <PropertyGroup> |
|
3 | <PropertyGroup> | |
| 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |
| 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |
| 6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> | |
| 7 | <OutputType>Library</OutputType> |
|
7 | <OutputType>Library</OutputType> | |
| 8 | <RootNamespace>Implab</RootNamespace> |
|
8 | <RootNamespace>Implab</RootNamespace> | |
| 9 | <AssemblyName>Implab</AssemblyName> |
|
9 | <AssemblyName>Implab</AssemblyName> | |
| 10 | </PropertyGroup> |
|
10 | </PropertyGroup> | |
| 11 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
11 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |
| 12 | <DebugSymbols>true</DebugSymbols> |
|
12 | <DebugSymbols>true</DebugSymbols> | |
| 13 | <DebugType>full</DebugType> |
|
13 | <DebugType>full</DebugType> | |
| 14 | <Optimize>false</Optimize> |
|
14 | <Optimize>false</Optimize> | |
| 15 | <OutputPath>bin\Debug</OutputPath> |
|
15 | <OutputPath>bin\Debug</OutputPath> | |
| 16 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
16 | <DefineConstants>TRACE;DEBUG;</DefineConstants> | |
| 17 | <ErrorReport>prompt</ErrorReport> |
|
17 | <ErrorReport>prompt</ErrorReport> | |
| 18 | <WarningLevel>4</WarningLevel> |
|
18 | <WarningLevel>4</WarningLevel> | |
| 19 | <ConsolePause>false</ConsolePause> |
|
19 | <ConsolePause>false</ConsolePause> | |
| 20 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
20 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
| 21 | </PropertyGroup> |
|
21 | </PropertyGroup> | |
| 22 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
22 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |
| 23 | <DebugType>full</DebugType> |
|
23 | <DebugType>full</DebugType> | |
| 24 | <Optimize>true</Optimize> |
|
24 | <Optimize>true</Optimize> | |
| 25 | <OutputPath>bin\Release</OutputPath> |
|
25 | <OutputPath>bin\Release</OutputPath> | |
| 26 | <ErrorReport>prompt</ErrorReport> |
|
26 | <ErrorReport>prompt</ErrorReport> | |
| 27 | <WarningLevel>4</WarningLevel> |
|
27 | <WarningLevel>4</WarningLevel> | |
| 28 | <ConsolePause>false</ConsolePause> |
|
28 | <ConsolePause>false</ConsolePause> | |
| 29 | </PropertyGroup> |
|
29 | </PropertyGroup> | |
| 30 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
30 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> | |
| 31 | <DebugSymbols>true</DebugSymbols> |
|
31 | <DebugSymbols>true</DebugSymbols> | |
| 32 | <DebugType>full</DebugType> |
|
32 | <DebugType>full</DebugType> | |
| 33 | <Optimize>false</Optimize> |
|
33 | <Optimize>false</Optimize> | |
| 34 | <OutputPath>bin\Debug</OutputPath> |
|
34 | <OutputPath>bin\Debug</OutputPath> | |
| 35 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
35 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> | |
| 36 | <ErrorReport>prompt</ErrorReport> |
|
36 | <ErrorReport>prompt</ErrorReport> | |
| 37 | <WarningLevel>4</WarningLevel> |
|
37 | <WarningLevel>4</WarningLevel> | |
| 38 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
38 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
| 39 | <ConsolePause>false</ConsolePause> |
|
39 | <ConsolePause>false</ConsolePause> | |
| 40 | </PropertyGroup> |
|
40 | </PropertyGroup> | |
| 41 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
41 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> | |
| 42 | <Optimize>true</Optimize> |
|
42 | <Optimize>true</Optimize> | |
| 43 | <OutputPath>bin\Release</OutputPath> |
|
43 | <OutputPath>bin\Release</OutputPath> | |
| 44 | <ErrorReport>prompt</ErrorReport> |
|
44 | <ErrorReport>prompt</ErrorReport> | |
| 45 | <WarningLevel>4</WarningLevel> |
|
45 | <WarningLevel>4</WarningLevel> | |
| 46 | <ConsolePause>false</ConsolePause> |
|
46 | <ConsolePause>false</ConsolePause> | |
| 47 | <DefineConstants>NET_4_5</DefineConstants> |
|
47 | <DefineConstants>NET_4_5</DefineConstants> | |
| 48 | </PropertyGroup> |
|
48 | </PropertyGroup> | |
| 49 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> |
|
49 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> | |
| 50 | <DebugSymbols>true</DebugSymbols> |
|
50 | <DebugSymbols>true</DebugSymbols> | |
| 51 | <DebugType>full</DebugType> |
|
51 | <DebugType>full</DebugType> | |
| 52 | <Optimize>false</Optimize> |
|
52 | <Optimize>false</Optimize> | |
| 53 | <OutputPath>bin\Debug</OutputPath> |
|
53 | <OutputPath>bin\Debug</OutputPath> | |
| 54 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> |
|
54 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> | |
| 55 | <ErrorReport>prompt</ErrorReport> |
|
55 | <ErrorReport>prompt</ErrorReport> | |
| 56 | <WarningLevel>4</WarningLevel> |
|
56 | <WarningLevel>4</WarningLevel> | |
| 57 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
57 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
| 58 | <ConsolePause>false</ConsolePause> |
|
58 | <ConsolePause>false</ConsolePause> | |
| 59 | </PropertyGroup> |
|
59 | </PropertyGroup> | |
| 60 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> |
|
60 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> | |
| 61 | <Optimize>true</Optimize> |
|
61 | <Optimize>true</Optimize> | |
| 62 | <OutputPath>bin\Release</OutputPath> |
|
62 | <OutputPath>bin\Release</OutputPath> | |
| 63 | <DefineConstants>NET_4_5;MONO;</DefineConstants> |
|
63 | <DefineConstants>NET_4_5;MONO;</DefineConstants> | |
| 64 | <ErrorReport>prompt</ErrorReport> |
|
64 | <ErrorReport>prompt</ErrorReport> | |
| 65 | <WarningLevel>4</WarningLevel> |
|
65 | <WarningLevel>4</WarningLevel> | |
| 66 | <ConsolePause>false</ConsolePause> |
|
66 | <ConsolePause>false</ConsolePause> | |
| 67 | </PropertyGroup> |
|
67 | </PropertyGroup> | |
| 68 | <ItemGroup> |
|
68 | <ItemGroup> | |
| 69 | <Reference Include="System" /> |
|
69 | <Reference Include="System" /> | |
| 70 | <Reference Include="System.Xml" /> |
|
70 | <Reference Include="System.Xml" /> | |
| 71 | </ItemGroup> |
|
71 | </ItemGroup> | |
| 72 | <ItemGroup> |
|
72 | <ItemGroup> | |
| 73 | <Compile Include="Component.cs" /> |
|
73 | <Compile Include="Component.cs" /> | |
| 74 | <Compile Include="CustomEqualityComparer.cs" /> |
|
74 | <Compile Include="CustomEqualityComparer.cs" /> | |
| 75 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
75 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> | |
| 76 | <Compile Include="Diagnostics\EventText.cs" /> |
|
76 | <Compile Include="Diagnostics\EventText.cs" /> | |
| 77 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> |
|
77 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> | |
| 78 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
78 | <Compile Include="Diagnostics\LogChannel.cs" /> | |
| 79 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
79 | <Compile Include="Diagnostics\LogicalOperation.cs" /> | |
| 80 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
80 | <Compile Include="Diagnostics\TextFileListener.cs" /> | |
| 81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> | |
| 82 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
82 | <Compile Include="Diagnostics\TraceLog.cs" /> | |
| 83 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
83 | <Compile Include="Diagnostics\TraceEvent.cs" /> | |
| 84 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
84 | <Compile Include="Diagnostics\TraceEventType.cs" /> | |
| 85 | <Compile Include="Disposable.cs" /> |
|
85 | <Compile Include="Disposable.cs" /> | |
| 86 | <Compile Include="ICancellable.cs" /> |
|
86 | <Compile Include="ICancellable.cs" /> | |
| 87 | <Compile Include="IProgressHandler.cs" /> |
|
87 | <Compile Include="IProgressHandler.cs" /> | |
| 88 | <Compile Include="IProgressNotifier.cs" /> |
|
88 | <Compile Include="IProgressNotifier.cs" /> | |
| 89 | <Compile Include="IPromiseT.cs" /> |
|
89 | <Compile Include="IPromiseT.cs" /> | |
| 90 | <Compile Include="IPromise.cs" /> |
|
90 | <Compile Include="IPromise.cs" /> | |
| 91 | <Compile Include="IServiceLocator.cs" /> |
|
91 | <Compile Include="IServiceLocator.cs" /> | |
| 92 | <Compile Include="ITaskController.cs" /> |
|
92 | <Compile Include="ITaskController.cs" /> | |
| 93 | <Compile Include="JSON\JSONElementContext.cs" /> |
|
93 | <Compile Include="JSON\JSONElementContext.cs" /> | |
| 94 | <Compile Include="JSON\JSONElementType.cs" /> |
|
94 | <Compile Include="JSON\JSONElementType.cs" /> | |
| 95 | <Compile Include="JSON\JSONGrammar.cs" /> |
|
95 | <Compile Include="JSON\JSONGrammar.cs" /> | |
| 96 | <Compile Include="JSON\JSONParser.cs" /> |
|
96 | <Compile Include="JSON\JSONParser.cs" /> | |
| 97 | <Compile Include="JSON\JSONScanner.cs" /> |
|
97 | <Compile Include="JSON\JSONScanner.cs" /> | |
| 98 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
98 | <Compile Include="JSON\JsonTokenType.cs" /> | |
| 99 | <Compile Include="JSON\JSONWriter.cs" /> |
|
99 | <Compile Include="JSON\JSONWriter.cs" /> | |
| 100 | <Compile Include="JSON\JSONXmlReader.cs" /> |
|
100 | <Compile Include="JSON\JSONXmlReader.cs" /> | |
| 101 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> |
|
101 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> | |
| 102 | <Compile Include="JSON\StringTranslator.cs" /> |
|
102 | <Compile Include="JSON\StringTranslator.cs" /> | |
| 103 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
103 | <Compile Include="Parallels\DispatchPool.cs" /> | |
| 104 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
104 | <Compile Include="Parallels\ArrayTraits.cs" /> | |
| 105 | <Compile Include="Parallels\MTQueue.cs" /> |
|
105 | <Compile Include="Parallels\MTQueue.cs" /> | |
| 106 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
106 | <Compile Include="Parallels\WorkerPool.cs" /> | |
| 107 | <Compile Include="Parsing\Alphabet.cs" /> |
|
107 | <Compile Include="Parsing\Alphabet.cs" /> | |
| 108 | <Compile Include="Parsing\AlphabetBase.cs" /> |
|
108 | <Compile Include="Parsing\AlphabetBase.cs" /> | |
| 109 | <Compile Include="Parsing\AltToken.cs" /> |
|
109 | <Compile Include="Parsing\AltToken.cs" /> | |
| 110 | <Compile Include="Parsing\BinaryToken.cs" /> |
|
110 | <Compile Include="Parsing\BinaryToken.cs" /> | |
| 111 | <Compile Include="Parsing\CatToken.cs" /> |
|
111 | <Compile Include="Parsing\CatToken.cs" /> | |
| 112 | <Compile Include="Parsing\CDFADefinition.cs" /> |
|
112 | <Compile Include="Parsing\CDFADefinition.cs" /> | |
| 113 | <Compile Include="Parsing\DFABuilder.cs" /> |
|
113 | <Compile Include="Parsing\DFABuilder.cs" /> | |
| 114 | <Compile Include="Parsing\DFADefinitionBase.cs" /> |
|
114 | <Compile Include="Parsing\DFADefinitionBase.cs" /> | |
| 115 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> |
|
115 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> | |
| 116 | <Compile Include="Parsing\DFAutomaton.cs" /> |
|
116 | <Compile Include="Parsing\DFAutomaton.cs" /> | |
| 117 | <Compile Include="Parsing\EDFADefinition.cs" /> |
|
117 | <Compile Include="Parsing\EDFADefinition.cs" /> | |
| 118 | <Compile Include="Parsing\EmptyToken.cs" /> |
|
118 | <Compile Include="Parsing\EmptyToken.cs" /> | |
| 119 | <Compile Include="Parsing\EndToken.cs" /> |
|
119 | <Compile Include="Parsing\EndToken.cs" /> | |
| 120 | <Compile Include="Parsing\EnumAlphabet.cs" /> |
|
120 | <Compile Include="Parsing\EnumAlphabet.cs" /> | |
| 121 | <Compile Include="Parsing\Grammar.cs" /> |
|
121 | <Compile Include="Parsing\Grammar.cs" /> | |
| 122 | <Compile Include="Parsing\IAlphabet.cs" /> |
|
122 | <Compile Include="Parsing\IAlphabet.cs" /> | |
| 123 | <Compile Include="Parsing\IDFADefinition.cs" /> |
|
123 | <Compile Include="Parsing\IDFADefinition.cs" /> | |
| 124 | <Compile Include="Parsing\IVisitor.cs" /> |
|
124 | <Compile Include="Parsing\IVisitor.cs" /> | |
| 125 | <Compile Include="Parsing\ParserException.cs" /> |
|
125 | <Compile Include="Parsing\ParserException.cs" /> | |
| 126 | <Compile Include="Parsing\Scanner.cs" /> |
|
126 | <Compile Include="Parsing\Scanner.cs" /> | |
| 127 | <Compile Include="Parsing\StarToken.cs" /> |
|
127 | <Compile Include="Parsing\StarToken.cs" /> | |
| 128 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
128 | <Compile Include="Parsing\SymbolToken.cs" /> | |
| 129 | <Compile Include="Parsing\Token.cs" /> |
|
129 | <Compile Include="Parsing\Token.cs" /> | |
| 130 | <Compile Include="ServiceLocator.cs" /> |
|
130 | <Compile Include="ServiceLocator.cs" /> | |
| 131 | <Compile Include="TaskController.cs" /> |
|
131 | <Compile Include="TaskController.cs" /> | |
| 132 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
132 | <Compile Include="ProgressInitEventArgs.cs" /> | |
| 133 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
133 | <Compile Include="Properties\AssemblyInfo.cs" /> | |
| 134 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
134 | <Compile Include="Parallels\AsyncPool.cs" /> | |
| 135 | <Compile Include="Safe.cs" /> |
|
135 | <Compile Include="Safe.cs" /> | |
| 136 | <Compile Include="ValueEventArgs.cs" /> |
|
136 | <Compile Include="ValueEventArgs.cs" /> | |
| 137 | <Compile Include="PromiseExtensions.cs" /> |
|
137 | <Compile Include="PromiseExtensions.cs" /> | |
| 138 | <Compile Include="SyncContextPromise.cs" /> |
|
138 | <Compile Include="SyncContextPromise.cs" /> | |
| 139 | <Compile Include="Diagnostics\OperationContext.cs" /> |
|
139 | <Compile Include="Diagnostics\OperationContext.cs" /> | |
| 140 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
140 | <Compile Include="Diagnostics\TraceContext.cs" /> | |
| 141 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
141 | <Compile Include="Diagnostics\LogEventArgs.cs" /> | |
| 142 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
142 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> | |
| 143 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
143 | <Compile Include="Diagnostics\Extensions.cs" /> | |
| 144 | <Compile Include="IComponentContainer.cs" /> |
|
144 | <Compile Include="IComponentContainer.cs" /> | |
| 145 | <Compile Include="PromiseEventType.cs" /> |
|
145 | <Compile Include="PromiseEventType.cs" /> | |
| 146 | <Compile Include="Parallels\MTCustomQueue.cs" /> |
|
146 | <Compile Include="Parallels\MTCustomQueue.cs" /> | |
| 147 | <Compile Include="Parallels\MTCustomQueueNode.cs" /> |
|
147 | <Compile Include="Parallels\MTCustomQueueNode.cs" /> | |
| 148 | <Compile Include="ComponentContainer.cs" /> |
|
148 | <Compile Include="ComponentContainer.cs" /> | |
| 149 | <Compile Include="DisposablePool.cs" /> |
|
149 | <Compile Include="DisposablePool.cs" /> | |
| 150 | <Compile Include="ObjectPool.cs" /> |
|
150 | <Compile Include="ObjectPool.cs" /> | |
| 151 | <Compile Include="Parallels\AsyncQueue.cs" /> |
|
151 | <Compile Include="Parallels\AsyncQueue.cs" /> | |
| 152 | <Compile Include="PromiseT.cs" /> |
|
152 | <Compile Include="PromiseT.cs" /> | |
| 153 | <Compile Include="IDeferred.cs" /> |
|
153 | <Compile Include="IDeferred.cs" /> | |
| 154 | <Compile Include="IDeferredT.cs" /> |
|
154 | <Compile Include="IDeferredT.cs" /> | |
| 155 | <Compile Include="AbstractPromise.cs" /> |
|
155 | <Compile Include="AbstractPromise.cs" /> | |
| 156 | <Compile Include="Promise.cs" /> |
|
156 | <Compile Include="Promise.cs" /> | |
| 157 | <Compile Include="PromiseTransientException.cs" /> |
|
157 | <Compile Include="PromiseTransientException.cs" /> | |
|
|
158 | <Compile Include="Parallels\Signal.cs" /> | |||
| 158 | </ItemGroup> |
|
159 | </ItemGroup> | |
| 159 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
160 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
| 160 | <ItemGroup /> |
|
161 | <ItemGroup /> | |
| 161 | <ProjectExtensions> |
|
162 | <ProjectExtensions> | |
| 162 | <MonoDevelop> |
|
163 | <MonoDevelop> | |
| 163 | <Properties> |
|
164 | <Properties> | |
| 164 | <Policies> |
|
165 | <Policies> | |
| 165 | <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" /> |
|
166 | <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" /> | |
| 166 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
167 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> | |
| 167 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
168 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> | |
| 168 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
169 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> | |
| 169 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
170 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> | |
| 170 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
171 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> | |
| 171 | <NameConventionPolicy> |
|
172 | <NameConventionPolicy> | |
| 172 | <Rules> |
|
173 | <Rules> | |
| 173 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
174 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 174 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
175 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 175 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
176 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
| 176 | <RequiredPrefixes> |
|
177 | <RequiredPrefixes> | |
| 177 | <String>I</String> |
|
178 | <String>I</String> | |
| 178 | </RequiredPrefixes> |
|
179 | </RequiredPrefixes> | |
| 179 | </NamingRule> |
|
180 | </NamingRule> | |
| 180 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
181 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
| 181 | <RequiredSuffixes> |
|
182 | <RequiredSuffixes> | |
| 182 | <String>Attribute</String> |
|
183 | <String>Attribute</String> | |
| 183 | </RequiredSuffixes> |
|
184 | </RequiredSuffixes> | |
| 184 | </NamingRule> |
|
185 | </NamingRule> | |
| 185 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
186 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
| 186 | <RequiredSuffixes> |
|
187 | <RequiredSuffixes> | |
| 187 | <String>EventArgs</String> |
|
188 | <String>EventArgs</String> | |
| 188 | </RequiredSuffixes> |
|
189 | </RequiredSuffixes> | |
| 189 | </NamingRule> |
|
190 | </NamingRule> | |
| 190 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
191 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
| 191 | <RequiredSuffixes> |
|
192 | <RequiredSuffixes> | |
| 192 | <String>Exception</String> |
|
193 | <String>Exception</String> | |
| 193 | </RequiredSuffixes> |
|
194 | </RequiredSuffixes> | |
| 194 | </NamingRule> |
|
195 | </NamingRule> | |
| 195 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
196 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 196 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
197 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> | |
| 197 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
198 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 198 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
199 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> | |
| 199 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
200 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> | |
| 200 | <RequiredPrefixes> |
|
201 | <RequiredPrefixes> | |
| 201 | <String>m_</String> |
|
202 | <String>m_</String> | |
| 202 | </RequiredPrefixes> |
|
203 | </RequiredPrefixes> | |
| 203 | </NamingRule> |
|
204 | </NamingRule> | |
| 204 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
205 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> | |
| 205 | <RequiredPrefixes> |
|
206 | <RequiredPrefixes> | |
| 206 | <String>_</String> |
|
207 | <String>_</String> | |
| 207 | </RequiredPrefixes> |
|
208 | </RequiredPrefixes> | |
| 208 | </NamingRule> |
|
209 | </NamingRule> | |
| 209 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
210 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> | |
| 210 | <RequiredPrefixes> |
|
211 | <RequiredPrefixes> | |
| 211 | <String>m_</String> |
|
212 | <String>m_</String> | |
| 212 | </RequiredPrefixes> |
|
213 | </RequiredPrefixes> | |
| 213 | </NamingRule> |
|
214 | </NamingRule> | |
| 214 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
215 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 215 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
216 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 216 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
217 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 217 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
218 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 218 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
219 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
| 219 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
220 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
| 220 | <RequiredPrefixes> |
|
221 | <RequiredPrefixes> | |
| 221 | <String>T</String> |
|
222 | <String>T</String> | |
| 222 | </RequiredPrefixes> |
|
223 | </RequiredPrefixes> | |
| 223 | </NamingRule> |
|
224 | </NamingRule> | |
| 224 | </Rules> |
|
225 | </Rules> | |
| 225 | </NameConventionPolicy> |
|
226 | </NameConventionPolicy> | |
| 226 | </Policies> |
|
227 | </Policies> | |
| 227 | </Properties> |
|
228 | </Properties> | |
| 228 | </MonoDevelop> |
|
229 | </MonoDevelop> | |
| 229 | </ProjectExtensions> |
|
230 | </ProjectExtensions> | |
| 230 | </Project> No newline at end of file |
|
231 | </Project> | |
| @@ -1,629 +1,629 | |||||
| 1 | using System.Threading; |
|
1 | using System.Threading; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System; |
|
3 | using System; | |
| 4 | using System.Collections; |
|
4 | using System.Collections; | |
| 5 | using System.Diagnostics; |
|
5 | using System.Diagnostics; | |
| 6 |
|
6 | |||
| 7 | namespace Implab.Parallels { |
|
7 | namespace Implab.Parallels { | |
| 8 | public class AsyncQueue<T> : IEnumerable<T> { |
|
8 | public class AsyncQueue<T> : IEnumerable<T> { | |
| 9 | class Chunk { |
|
9 | class Chunk { | |
| 10 | public Chunk next; |
|
10 | public Chunk next; | |
| 11 |
|
11 | |||
| 12 | int m_low; |
|
12 | int m_low; | |
| 13 | int m_hi; |
|
13 | int m_hi; | |
| 14 | int m_alloc; |
|
14 | int m_alloc; | |
| 15 | readonly int m_size; |
|
15 | readonly int m_size; | |
| 16 | readonly T[] m_data; |
|
16 | readonly T[] m_data; | |
| 17 |
|
17 | |||
| 18 | public Chunk(int size) { |
|
18 | public Chunk(int size) { | |
| 19 | m_size = size; |
|
19 | m_size = size; | |
| 20 | m_data = new T[size]; |
|
20 | m_data = new T[size]; | |
| 21 | } |
|
21 | } | |
| 22 |
|
22 | |||
| 23 | public Chunk(int size, T value) { |
|
23 | public Chunk(int size, T value) { | |
| 24 | m_size = size; |
|
24 | m_size = size; | |
| 25 | m_hi = 1; |
|
25 | m_hi = 1; | |
| 26 | m_alloc = 1; |
|
26 | m_alloc = 1; | |
| 27 | m_data = new T[size]; |
|
27 | m_data = new T[size]; | |
| 28 | m_data[0] = value; |
|
28 | m_data[0] = value; | |
| 29 | } |
|
29 | } | |
| 30 |
|
30 | |||
| 31 | public Chunk(int size, T[] data, int offset, int length, int alloc) { |
|
31 | public Chunk(int size, T[] data, int offset, int length, int alloc) { | |
| 32 | m_size = size; |
|
32 | m_size = size; | |
| 33 | m_hi = length; |
|
33 | m_hi = length; | |
| 34 | m_alloc = alloc; |
|
34 | m_alloc = alloc; | |
| 35 | m_data = new T[size]; |
|
35 | m_data = new T[size]; | |
| 36 | Array.Copy(data, offset, m_data, 0, length); |
|
36 | Array.Copy(data, offset, m_data, 0, length); | |
| 37 | } |
|
37 | } | |
| 38 |
|
38 | |||
| 39 | public int Low { |
|
39 | public int Low { | |
| 40 | get { return m_low; } |
|
40 | get { return m_low; } | |
| 41 | } |
|
41 | } | |
| 42 |
|
42 | |||
| 43 | public int Hi { |
|
43 | public int Hi { | |
| 44 | get { return m_hi; } |
|
44 | get { return m_hi; } | |
| 45 | } |
|
45 | } | |
| 46 |
|
46 | |||
| 47 | public int Size { |
|
47 | public int Size { | |
| 48 | get { return m_size; } |
|
48 | get { return m_size; } | |
| 49 | } |
|
49 | } | |
| 50 |
|
50 | |||
| 51 | public bool TryEnqueue(T value, out bool extend) { |
|
51 | public bool TryEnqueue(T value, out bool extend) { | |
| 52 | var alloc = Interlocked.Increment(ref m_alloc) - 1; |
|
52 | var alloc = Interlocked.Increment(ref m_alloc) - 1; | |
| 53 |
|
53 | |||
| 54 | if (alloc >= m_size) { |
|
54 | if (alloc >= m_size) { | |
| 55 | extend = alloc == m_size; |
|
55 | extend = alloc == m_size; | |
| 56 | return false; |
|
56 | return false; | |
| 57 | } |
|
57 | } | |
| 58 |
|
58 | |||
| 59 | extend = false; |
|
59 | extend = false; | |
| 60 | m_data[alloc] = value; |
|
60 | m_data[alloc] = value; | |
| 61 |
|
61 | |||
| 62 | while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) { |
|
62 | while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) { | |
| 63 | // spin wait for commit |
|
63 | // spin wait for commit | |
| 64 | } |
|
64 | } | |
| 65 | return true; |
|
65 | return true; | |
| 66 | } |
|
66 | } | |
| 67 |
|
67 | |||
| 68 | /// <summary> |
|
68 | /// <summary> | |
| 69 | /// Prevents from allocating new space in the chunk and waits for all write operations to complete |
|
69 | /// Prevents from allocating new space in the chunk and waits for all write operations to complete | |
| 70 | /// </summary> |
|
70 | /// </summary> | |
| 71 | public void Commit() { |
|
71 | public void Commit() { | |
| 72 | var actual = Math.Min(Interlocked.Exchange(ref m_alloc, m_size + 1), m_size); |
|
72 | var actual = Math.Min(Interlocked.Exchange(ref m_alloc, m_size + 1), m_size); | |
| 73 |
|
73 | |||
| 74 | while (m_hi != actual) |
|
74 | while (m_hi != actual) | |
| 75 | Thread.MemoryBarrier(); |
|
75 | Thread.MemoryBarrier(); | |
| 76 | } |
|
76 | } | |
| 77 |
|
77 | |||
| 78 | public bool TryDequeue(out T value, out bool recycle) { |
|
78 | public bool TryDequeue(out T value, out bool recycle) { | |
| 79 | int low; |
|
79 | int low; | |
| 80 | do { |
|
80 | do { | |
| 81 | low = m_low; |
|
81 | low = m_low; | |
| 82 | if (low >= m_hi) { |
|
82 | if (low >= m_hi) { | |
| 83 | value = default(T); |
|
83 | value = default(T); | |
| 84 | recycle = (low == m_size); |
|
84 | recycle = (low == m_size); | |
| 85 | return false; |
|
85 | return false; | |
| 86 | } |
|
86 | } | |
| 87 | } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low)); |
|
87 | } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low)); | |
| 88 |
|
88 | |||
| 89 | recycle = (low == m_size - 1); |
|
89 | recycle = (low == m_size - 1); | |
| 90 | value = m_data[low]; |
|
90 | value = m_data[low]; | |
| 91 |
|
91 | |||
| 92 | return true; |
|
92 | return true; | |
| 93 | } |
|
93 | } | |
| 94 |
|
94 | |||
| 95 | public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) { |
|
95 | public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) { | |
| 96 | //int alloc; |
|
96 | //int alloc; | |
| 97 | //int allocSize; |
|
97 | //int allocSize; | |
| 98 |
|
98 | |||
| 99 | var alloc = Interlocked.Add(ref m_alloc, length) - length; |
|
99 | var alloc = Interlocked.Add(ref m_alloc, length) - length; | |
| 100 | if (alloc > m_size) { |
|
100 | if (alloc > m_size) { | |
| 101 | // the chunk is full and someone already |
|
101 | // the chunk is full and someone already | |
| 102 | // creating the new one |
|
102 | // creating the new one | |
| 103 | enqueued = 0; // nothing was added |
|
103 | enqueued = 0; // nothing was added | |
| 104 | extend = false; // the caller shouldn't try to extend the queue |
|
104 | extend = false; // the caller shouldn't try to extend the queue | |
| 105 | return false; // nothing was added |
|
105 | return false; // nothing was added | |
| 106 | } |
|
106 | } | |
| 107 |
|
107 | |||
| 108 | enqueued = Math.Min(m_size - alloc, length); |
|
108 | enqueued = Math.Min(m_size - alloc, length); | |
| 109 | extend = length > enqueued; |
|
109 | extend = length > enqueued; | |
| 110 |
|
110 | |||
| 111 | if (enqueued == 0) |
|
111 | if (enqueued == 0) | |
| 112 | return false; |
|
112 | return false; | |
| 113 |
|
113 | |||
| 114 |
|
114 | |||
| 115 | Array.Copy(batch, offset, m_data, alloc, enqueued); |
|
115 | Array.Copy(batch, offset, m_data, alloc, enqueued); | |
| 116 |
|
116 | |||
| 117 | while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + enqueued, alloc)) { |
|
117 | while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + enqueued, alloc)) { | |
| 118 | // spin wait for commit |
|
118 | // spin wait for commit | |
| 119 | } |
|
119 | } | |
| 120 |
|
120 | |||
| 121 | return true; |
|
121 | return true; | |
| 122 | } |
|
122 | } | |
| 123 |
|
123 | |||
| 124 | public bool TryDequeueBatch(T[] buffer, int offset, int length,out int dequeued, out bool recycle) { |
|
124 | public bool TryDequeueBatch(T[] buffer, int offset, int length,out int dequeued, out bool recycle) { | |
| 125 | int low, hi, batchSize; |
|
125 | int low, hi, batchSize; | |
| 126 |
|
126 | |||
| 127 | do { |
|
127 | do { | |
| 128 | low = m_low; |
|
128 | low = m_low; | |
| 129 | hi = m_hi; |
|
129 | hi = m_hi; | |
| 130 | if (low >= hi) { |
|
130 | if (low >= hi) { | |
| 131 | dequeued = 0; |
|
131 | dequeued = 0; | |
| 132 | recycle = (low == m_size); // recycling could be restarted and we need to signal again |
|
132 | recycle = (low == m_size); // recycling could be restarted and we need to signal again | |
| 133 | return false; |
|
133 | return false; | |
| 134 | } |
|
134 | } | |
| 135 | batchSize = Math.Min(hi - low, length); |
|
135 | batchSize = Math.Min(hi - low, length); | |
| 136 | } while(low != Interlocked.CompareExchange(ref m_low, low + batchSize, low)); |
|
136 | } while(low != Interlocked.CompareExchange(ref m_low, low + batchSize, low)); | |
| 137 |
|
137 | |||
| 138 | recycle = (low == m_size - batchSize); |
|
138 | recycle = (low == m_size - batchSize); | |
| 139 | dequeued = batchSize; |
|
139 | dequeued = batchSize; | |
| 140 |
|
140 | |||
| 141 | Array.Copy(m_data, low, buffer, offset, batchSize); |
|
141 | Array.Copy(m_data, low, buffer, offset, batchSize); | |
| 142 |
|
142 | |||
| 143 | return true; |
|
143 | return true; | |
| 144 | } |
|
144 | } | |
| 145 |
|
145 | |||
| 146 | public T GetAt(int pos) { |
|
146 | public T GetAt(int pos) { | |
| 147 | return m_data[pos]; |
|
147 | return m_data[pos]; | |
| 148 | } |
|
148 | } | |
| 149 | } |
|
149 | } | |
| 150 |
|
150 | |||
| 151 | public const int DEFAULT_CHUNK_SIZE = 32; |
|
151 | public const int DEFAULT_CHUNK_SIZE = 32; | |
| 152 | public const int MAX_CHUNK_SIZE = 262144; |
|
152 | public const int MAX_CHUNK_SIZE = 262144; | |
| 153 |
|
153 | |||
| 154 | Chunk m_first; |
|
154 | Chunk m_first; | |
| 155 | Chunk m_last; |
|
155 | Chunk m_last; | |
| 156 |
|
156 | |||
| 157 | /// <summary> |
|
157 | /// <summary> | |
| 158 | /// Adds the specified value to the queue. |
|
158 | /// Adds the specified value to the queue. | |
| 159 | /// </summary> |
|
159 | /// </summary> | |
| 160 | /// <param name="value">Tha value which will be added to the queue.</param> |
|
160 | /// <param name="value">Tha value which will be added to the queue.</param> | |
| 161 | public void Enqueue(T value) { |
|
161 | public void Enqueue(T value) { | |
| 162 | var last = m_last; |
|
162 | var last = m_last; | |
| 163 | // spin wait to the new chunk |
|
163 | // spin wait to the new chunk | |
| 164 | bool extend = true; |
|
164 | bool extend = true; | |
| 165 | while (last == null || !last.TryEnqueue(value, out extend)) { |
|
165 | while (last == null || !last.TryEnqueue(value, out extend)) { | |
| 166 | // try to extend queue |
|
166 | // try to extend queue | |
| 167 | if (extend || last == null) { |
|
167 | if (extend || last == null) { | |
| 168 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE, value); |
|
168 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE, value); | |
| 169 | if (EnqueueChunk(last, chunk)) |
|
169 | if (EnqueueChunk(last, chunk)) | |
| 170 | break; // success! exit! |
|
170 | break; // success! exit! | |
| 171 | last = m_last; |
|
171 | last = m_last; | |
| 172 | } else { |
|
172 | } else { | |
| 173 | while (last == m_last) { |
|
173 | while (last == m_last) { | |
| 174 | Thread.MemoryBarrier(); |
|
174 | Thread.MemoryBarrier(); | |
| 175 | } |
|
175 | } | |
| 176 | last = m_last; |
|
176 | last = m_last; | |
| 177 | } |
|
177 | } | |
| 178 | } |
|
178 | } | |
| 179 | } |
|
179 | } | |
| 180 |
|
180 | |||
| 181 | /// <summary> |
|
181 | /// <summary> | |
| 182 | /// Adds the specified data to the queue. |
|
182 | /// Adds the specified data to the queue. | |
| 183 | /// </summary> |
|
183 | /// </summary> | |
| 184 | /// <param name="data">The buffer which contains the data to be enqueued.</param> |
|
184 | /// <param name="data">The buffer which contains the data to be enqueued.</param> | |
| 185 | /// <param name="offset">The offset of the data in the buffer.</param> |
|
185 | /// <param name="offset">The offset of the data in the buffer.</param> | |
| 186 | /// <param name="length">The size of the data to read from the buffer.</param> |
|
186 | /// <param name="length">The size of the data to read from the buffer.</param> | |
| 187 | public void EnqueueRange(T[] data, int offset, int length) { |
|
187 | public void EnqueueRange(T[] data, int offset, int length) { | |
| 188 | if (data == null) |
|
188 | if (data == null) | |
| 189 | throw new ArgumentNullException("data"); |
|
189 | throw new ArgumentNullException("data"); | |
| 190 | if (offset < 0) |
|
190 | if (offset < 0) | |
| 191 | throw new ArgumentOutOfRangeException("offset"); |
|
191 | throw new ArgumentOutOfRangeException("offset"); | |
| 192 | if (length < 1 || offset + length > data.Length) |
|
192 | if (length < 1 || offset + length > data.Length) | |
| 193 | throw new ArgumentOutOfRangeException("length"); |
|
193 | throw new ArgumentOutOfRangeException("length"); | |
| 194 |
|
194 | |||
| 195 | var last = m_last; |
|
195 | var last = m_last; | |
| 196 |
|
196 | |||
| 197 | bool extend; |
|
197 | bool extend; | |
| 198 | int enqueued; |
|
198 | int enqueued; | |
| 199 |
|
199 | |||
| 200 | while (length > 0) { |
|
200 | while (length > 0) { | |
| 201 | extend = true; |
|
201 | extend = true; | |
| 202 | if (last != null && last.TryEnqueueBatch(data, offset, length, out enqueued, out extend)) { |
|
202 | if (last != null && last.TryEnqueueBatch(data, offset, length, out enqueued, out extend)) { | |
| 203 | length -= enqueued; |
|
203 | length -= enqueued; | |
| 204 | offset += enqueued; |
|
204 | offset += enqueued; | |
| 205 | } |
|
205 | } | |
| 206 |
|
206 | |||
| 207 | if (extend) { |
|
207 | if (extend) { | |
| 208 | // there was no enough space in the chunk |
|
208 | // there was no enough space in the chunk | |
| 209 | // or there was no chunks in the queue |
|
209 | // or there was no chunks in the queue | |
| 210 |
|
210 | |||
| 211 | while (length > 0) { |
|
211 | while (length > 0) { | |
| 212 |
|
212 | |||
| 213 | var size = Math.Min(length, MAX_CHUNK_SIZE); |
|
213 | var size = Math.Min(length, MAX_CHUNK_SIZE); | |
| 214 |
|
214 | |||
| 215 | var chunk = new Chunk( |
|
215 | var chunk = new Chunk( | |
| 216 | Math.Max(size, DEFAULT_CHUNK_SIZE), |
|
216 | Math.Max(size, DEFAULT_CHUNK_SIZE), | |
| 217 | data, |
|
217 | data, | |
| 218 | offset, |
|
218 | offset, | |
| 219 | size, |
|
219 | size, | |
| 220 | length // length >= size |
|
220 | length // length >= size | |
| 221 | ); |
|
221 | ); | |
| 222 |
|
222 | |||
| 223 | if (!EnqueueChunk(last, chunk)) { |
|
223 | if (!EnqueueChunk(last, chunk)) { | |
| 224 | // looks like the queue has been updated then proceed from the beginning |
|
224 | // looks like the queue has been updated then proceed from the beginning | |
| 225 | last = m_last; |
|
225 | last = m_last; | |
| 226 | break; |
|
226 | break; | |
| 227 | } |
|
227 | } | |
| 228 |
|
228 | |||
| 229 | // we have successfully added the new chunk |
|
229 | // we have successfully added the new chunk | |
| 230 | last = chunk; |
|
230 | last = chunk; | |
| 231 | length -= size; |
|
231 | length -= size; | |
| 232 | offset += size; |
|
232 | offset += size; | |
| 233 | } |
|
233 | } | |
| 234 | } else { |
|
234 | } else { | |
| 235 | // we don't need to extend the queue, if we successfully enqueued data |
|
235 | // we don't need to extend the queue, if we successfully enqueued data | |
| 236 | if (length == 0) |
|
236 | if (length == 0) | |
| 237 | break; |
|
237 | break; | |
| 238 |
|
238 | |||
| 239 | // if we need to wait while someone is extending the queue |
|
239 | // if we need to wait while someone is extending the queue | |
| 240 | // spinwait |
|
240 | // spinwait | |
| 241 | while (last == m_last) { |
|
241 | while (last == m_last) { | |
| 242 | Thread.MemoryBarrier(); |
|
242 | Thread.MemoryBarrier(); | |
| 243 | } |
|
243 | } | |
| 244 |
|
244 | |||
| 245 | last = m_last; |
|
245 | last = m_last; | |
| 246 | } |
|
246 | } | |
| 247 | } |
|
247 | } | |
| 248 | } |
|
248 | } | |
| 249 |
|
249 | |||
| 250 | /// <summary> |
|
250 | /// <summary> | |
| 251 | /// Tries to retrieve the first element from the queue. |
|
251 | /// Tries to retrieve the first element from the queue. | |
| 252 | /// </summary> |
|
252 | /// </summary> | |
| 253 | /// <returns><c>true</c>, if element is dequeued, <c>false</c> otherwise.</returns> |
|
253 | /// <returns><c>true</c>, if element is dequeued, <c>false</c> otherwise.</returns> | |
| 254 | /// <param name="value">The value of the dequeued element.</param> |
|
254 | /// <param name="value">The value of the dequeued element.</param> | |
| 255 | public bool TryDequeue(out T value) { |
|
255 | public bool TryDequeue(out T value) { | |
| 256 | var chunk = m_first; |
|
256 | var chunk = m_first; | |
| 257 | bool recycle; |
|
257 | bool recycle; | |
| 258 | while (chunk != null) { |
|
258 | while (chunk != null) { | |
| 259 |
|
259 | |||
| 260 | var result = chunk.TryDequeue(out value, out recycle); |
|
260 | var result = chunk.TryDequeue(out value, out recycle); | |
| 261 |
|
261 | |||
| 262 | if (recycle) // this chunk is waste |
|
262 | if (recycle) // this chunk is waste | |
| 263 | RecycleFirstChunk(chunk); |
|
263 | RecycleFirstChunk(chunk); | |
| 264 | else |
|
264 | else | |
| 265 | return result; // this chunk is usable and returned actual result |
|
265 | return result; // this chunk is usable and returned actual result | |
| 266 |
|
266 | |||
| 267 | if (result) // this chunk is waste but the true result is always actual |
|
267 | if (result) // this chunk is waste but the true result is always actual | |
| 268 | return true; |
|
268 | return true; | |
| 269 |
|
269 | |||
| 270 | // try again |
|
270 | // try again | |
| 271 | chunk = m_first; |
|
271 | chunk = m_first; | |
| 272 | } |
|
272 | } | |
| 273 |
|
273 | |||
| 274 | // the queue is empty |
|
274 | // the queue is empty | |
| 275 | value = default(T); |
|
275 | value = default(T); | |
| 276 | return false; |
|
276 | return false; | |
| 277 | } |
|
277 | } | |
| 278 |
|
278 | |||
| 279 | /// <summary> |
|
279 | /// <summary> | |
| 280 | /// Tries to dequeue the specified amount of data from the queue. |
|
280 | /// Tries to dequeue the specified amount of data from the queue. | |
| 281 | /// </summary> |
|
281 | /// </summary> | |
| 282 | /// <returns><c>true</c>, if data was deuqueued, <c>false</c> otherwise.</returns> |
|
282 | /// <returns><c>true</c>, if data was deuqueued, <c>false</c> otherwise.</returns> | |
| 283 | /// <param name="buffer">The buffer to which the data will be written.</param> |
|
283 | /// <param name="buffer">The buffer to which the data will be written.</param> | |
| 284 | /// <param name="offset">The offset in the buffer at which the data will be written.</param> |
|
284 | /// <param name="offset">The offset in the buffer at which the data will be written.</param> | |
| 285 | /// <param name="length">The maximum amount of data to be retrieved.</param> |
|
285 | /// <param name="length">The maximum amount of data to be retrieved.</param> | |
| 286 | /// <param name="dequeued">The actual amout of the retrieved data.</param> |
|
286 | /// <param name="dequeued">The actual amout of the retrieved data.</param> | |
| 287 | public bool TryDequeueRange(T[] buffer, int offset, int length, out int dequeued) { |
|
287 | public bool TryDequeueRange(T[] buffer, int offset, int length, out int dequeued) { | |
| 288 | if (buffer == null) |
|
288 | if (buffer == null) | |
| 289 | throw new ArgumentNullException("buffer"); |
|
289 | throw new ArgumentNullException("buffer"); | |
| 290 | if (offset < 0) |
|
290 | if (offset < 0) | |
| 291 | throw new ArgumentOutOfRangeException("offset"); |
|
291 | throw new ArgumentOutOfRangeException("offset"); | |
| 292 | if (length < 1 || offset + length > buffer.Length) |
|
292 | if (length < 1 || offset + length > buffer.Length) | |
| 293 | throw new ArgumentOutOfRangeException("length"); |
|
293 | throw new ArgumentOutOfRangeException("length"); | |
| 294 |
|
294 | |||
| 295 | var chunk = m_first; |
|
295 | var chunk = m_first; | |
| 296 | bool recycle; |
|
296 | bool recycle; | |
| 297 | dequeued = 0; |
|
297 | dequeued = 0; | |
| 298 | while (chunk != null) { |
|
298 | while (chunk != null) { | |
| 299 |
|
299 | |||
| 300 | int actual; |
|
300 | int actual; | |
| 301 | if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { |
|
301 | if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { | |
| 302 | offset += actual; |
|
302 | offset += actual; | |
| 303 | length -= actual; |
|
303 | length -= actual; | |
| 304 | dequeued += actual; |
|
304 | dequeued += actual; | |
| 305 | } |
|
305 | } | |
| 306 |
|
306 | |||
| 307 | if (recycle) // this chunk is waste |
|
307 | if (recycle) // this chunk is waste | |
| 308 | RecycleFirstChunk(chunk); |
|
308 | RecycleFirstChunk(chunk); | |
| 309 | else if (actual == 0) |
|
309 | else if (actual == 0) | |
| 310 | break; // the chunk is usable, but doesn't contain any data (it's the last chunk in the queue) |
|
310 | break; // the chunk is usable, but doesn't contain any data (it's the last chunk in the queue) | |
| 311 |
|
311 | |||
| 312 | if (length == 0) |
|
312 | if (length == 0) | |
| 313 | return true; |
|
313 | return true; | |
| 314 |
|
314 | |||
| 315 | // we still may dequeue something |
|
315 | // we still may dequeue something | |
| 316 | // try again |
|
316 | // try again | |
| 317 | chunk = m_first; |
|
317 | chunk = m_first; | |
| 318 | } |
|
318 | } | |
| 319 |
|
319 | |||
| 320 | return dequeued != 0; |
|
320 | return dequeued != 0; | |
| 321 | } |
|
321 | } | |
| 322 |
|
322 | |||
| 323 | /// <summary> |
|
323 | /// <summary> | |
| 324 | /// Tries to dequeue all remaining data in the first chunk. |
|
324 | /// Tries to dequeue all remaining data in the first chunk. | |
| 325 | /// </summary> |
|
325 | /// </summary> | |
| 326 | /// <returns><c>true</c>, if data was dequeued, <c>false</c> otherwise.</returns> |
|
326 | /// <returns><c>true</c>, if data was dequeued, <c>false</c> otherwise.</returns> | |
| 327 | /// <param name="buffer">The buffer to which the data will be written.</param> |
|
327 | /// <param name="buffer">The buffer to which the data will be written.</param> | |
| 328 | /// <param name="offset">The offset in the buffer at which the data will be written.</param> |
|
328 | /// <param name="offset">The offset in the buffer at which the data will be written.</param> | |
| 329 | /// <param name="length">Tha maximum amount of the data to be dequeued.</param> |
|
329 | /// <param name="length">Tha maximum amount of the data to be dequeued.</param> | |
| 330 | /// <param name="dequeued">The actual amount of the dequeued data.</param> |
|
330 | /// <param name="dequeued">The actual amount of the dequeued data.</param> | |
| 331 | public bool TryDequeueChunk(T[] buffer, int offset, int length, out int dequeued) { |
|
331 | public bool TryDequeueChunk(T[] buffer, int offset, int length, out int dequeued) { | |
| 332 | if (buffer == null) |
|
332 | if (buffer == null) | |
| 333 | throw new ArgumentNullException("buffer"); |
|
333 | throw new ArgumentNullException("buffer"); | |
| 334 | if (offset < 0) |
|
334 | if (offset < 0) | |
| 335 | throw new ArgumentOutOfRangeException("offset"); |
|
335 | throw new ArgumentOutOfRangeException("offset"); | |
| 336 | if (length < 1 || offset + length > buffer.Length) |
|
336 | if (length < 1 || offset + length > buffer.Length) | |
| 337 | throw new ArgumentOutOfRangeException("length"); |
|
337 | throw new ArgumentOutOfRangeException("length"); | |
| 338 |
|
338 | |||
| 339 | var chunk = m_first; |
|
339 | var chunk = m_first; | |
| 340 | bool recycle; |
|
340 | bool recycle; | |
| 341 | dequeued = 0; |
|
341 | dequeued = 0; | |
| 342 |
|
342 | |||
| 343 | while (chunk != null) { |
|
343 | while (chunk != null) { | |
| 344 |
|
344 | |||
| 345 | int actual; |
|
345 | int actual; | |
| 346 | if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { |
|
346 | if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) { | |
| 347 | dequeued = actual; |
|
347 | dequeued = actual; | |
| 348 | } |
|
348 | } | |
| 349 |
|
349 | |||
| 350 | if (recycle) // this chunk is waste |
|
350 | if (recycle) // this chunk is waste | |
| 351 | RecycleFirstChunk(chunk); |
|
351 | RecycleFirstChunk(chunk); | |
| 352 |
|
352 | |||
| 353 | // if we have dequeued any data, then return |
|
353 | // if we have dequeued any data, then return | |
| 354 | if (dequeued != 0) |
|
354 | if (dequeued != 0) | |
| 355 | return true; |
|
355 | return true; | |
| 356 |
|
356 | |||
| 357 | // we still may dequeue something |
|
357 | // we still may dequeue something | |
| 358 | // try again |
|
358 | // try again | |
| 359 | chunk = m_first; |
|
359 | chunk = m_first; | |
| 360 | } |
|
360 | } | |
| 361 |
|
361 | |||
| 362 | return false; |
|
362 | return false; | |
| 363 | } |
|
363 | } | |
| 364 |
|
364 | |||
| 365 | bool EnqueueChunk(Chunk last, Chunk chunk) { |
|
365 | bool EnqueueChunk(Chunk last, Chunk chunk) { | |
| 366 | if (Interlocked.CompareExchange(ref m_last, chunk, last) != last) |
|
366 | if (Interlocked.CompareExchange(ref m_last, chunk, last) != last) | |
| 367 | return false; |
|
367 | return false; | |
| 368 |
|
368 | |||
| 369 | if (last != null) |
|
369 | if (last != null) | |
| 370 | last.next = chunk; |
|
370 | last.next = chunk; | |
| 371 | else { |
|
371 | else { | |
| 372 | m_first = chunk; |
|
372 | m_first = chunk; | |
| 373 | } |
|
373 | } | |
| 374 | return true; |
|
374 | return true; | |
| 375 | } |
|
375 | } | |
| 376 |
|
376 | |||
| 377 | void RecycleFirstChunk(Chunk first) { |
|
377 | void RecycleFirstChunk(Chunk first) { | |
| 378 | var next = first.next; |
|
378 | var next = first.next; | |
| 379 |
|
379 | |||
| 380 | if (first != Interlocked.CompareExchange(ref m_first, next, first)) |
|
380 | if (first != Interlocked.CompareExchange(ref m_first, next, first)) | |
| 381 | return; |
|
381 | return; | |
| 382 |
|
382 | |||
| 383 | if (next == null) { |
|
383 | if (next == null) { | |
| 384 |
|
384 | |||
| 385 | if (first != Interlocked.CompareExchange(ref m_last, null, first)) { |
|
385 | if (first != Interlocked.CompareExchange(ref m_last, null, first)) { | |
| 386 | /*while (first.next == null) |
|
386 | /*while (first.next == null) | |
| 387 | Thread.MemoryBarrier();*/ |
|
387 | Thread.MemoryBarrier();*/ | |
| 388 |
|
388 | |||
| 389 | // race |
|
389 | // race | |
| 390 | // someone already updated the tail, restore the pointer to the queue head |
|
390 | // someone already updated the tail, restore the pointer to the queue head | |
| 391 | m_first = first; |
|
391 | m_first = first; | |
| 392 | } |
|
392 | } | |
| 393 | // the tail is updated |
|
393 | // the tail is updated | |
| 394 | } |
|
394 | } | |
| 395 |
|
395 | |||
| 396 | // we need to update the head |
|
396 | // we need to update the head | |
| 397 | //Interlocked.CompareExchange(ref m_first, next, first); |
|
397 | //Interlocked.CompareExchange(ref m_first, next, first); | |
| 398 | // if the head is already updated then give up |
|
398 | // if the head is already updated then give up | |
| 399 | //return; |
|
399 | //return; | |
| 400 |
|
400 | |||
| 401 | } |
|
401 | } | |
| 402 |
|
402 | |||
| 403 | public void Clear() { |
|
403 | public void Clear() { | |
| 404 | // start the new queue |
|
404 | // start the new queue | |
| 405 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE); |
|
405 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE); | |
| 406 |
|
406 | |||
| 407 | do { |
|
407 | do { | |
| 408 | Thread.MemoryBarrier(); |
|
408 | Thread.MemoryBarrier(); | |
| 409 | var first = m_first; |
|
409 | var first = m_first; | |
| 410 | var last = m_last; |
|
410 | var last = m_last; | |
| 411 |
|
411 | |||
| 412 | if (last == null) // nothing to clear |
|
412 | if (last == null) // nothing to clear | |
| 413 | return; |
|
413 | return; | |
| 414 |
|
414 | |||
| 415 | if (first == null || (first.next == null && first != last)) // inconcistency |
|
415 | if (first == null || (first.next == null && first != last)) // inconcistency | |
| 416 | continue; |
|
416 | continue; | |
| 417 |
|
417 | |||
| 418 | // here we will create inconsistency which will force others to spin |
|
418 | // here we will create inconsistency which will force others to spin | |
| 419 | // and prevent from fetching. chunk.next = null |
|
419 | // and prevent from fetching. chunk.next = null | |
| 420 | if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) |
|
420 | if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) | |
| 421 | continue;// inconsistent |
|
421 | continue;// inconsistent | |
| 422 |
|
422 | |||
| 423 | m_last = chunk; |
|
423 | m_last = chunk; | |
| 424 |
|
424 | |||
| 425 | return; |
|
425 | return; | |
| 426 |
|
426 | |||
| 427 | } while(true); |
|
427 | } while(true); | |
| 428 | } |
|
428 | } | |
| 429 |
|
429 | |||
| 430 | public T[] Drain() { |
|
430 | public T[] Drain() { | |
| 431 | // start the new queue |
|
431 | // start the new queue | |
| 432 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE); |
|
432 | var chunk = new Chunk(DEFAULT_CHUNK_SIZE); | |
| 433 |
|
433 | |||
| 434 | do { |
|
434 | do { | |
| 435 | Thread.MemoryBarrier(); |
|
435 | Thread.MemoryBarrier(); | |
| 436 | var first = m_first; |
|
436 | var first = m_first; | |
| 437 | var last = m_last; |
|
437 | var last = m_last; | |
| 438 |
|
438 | |||
| 439 | if (last == null) |
|
439 | if (last == null) | |
| 440 | return new T[0]; |
|
440 | return new T[0]; | |
| 441 |
|
441 | |||
| 442 | if (first == null || (first.next == null && first != last)) |
|
442 | if (first == null || (first.next == null && first != last)) | |
| 443 | continue; |
|
443 | continue; | |
| 444 |
|
444 | |||
| 445 | // here we will create inconsistency which will force others to spin |
|
445 | // here we will create inconsistency which will force others to spin | |
| 446 | // and prevent from fetching. chunk.next = null |
|
446 | // and prevent from fetching. chunk.next = null | |
| 447 | if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) |
|
447 | if (first != Interlocked.CompareExchange(ref m_first, chunk, first)) | |
| 448 | continue;// inconsistent |
|
448 | continue;// inconsistent | |
| 449 |
|
449 | |||
| 450 | last = Interlocked.Exchange(ref m_last, chunk); |
|
450 | last = Interlocked.Exchange(ref m_last, chunk); | |
| 451 |
|
451 | |||
| 452 | return ReadChunks(first, last); |
|
452 | return ReadChunks(first, last); | |
| 453 |
|
453 | |||
| 454 | } while(true); |
|
454 | } while(true); | |
| 455 | } |
|
455 | } | |
| 456 |
|
456 | |||
| 457 | T[] ReadChunks(Chunk chunk, object last) { |
|
457 | static T[] ReadChunks(Chunk chunk, object last) { | |
| 458 | var result = new List<T>(); |
|
458 | var result = new List<T>(); | |
| 459 | var buffer = new T[DEFAULT_CHUNK_SIZE]; |
|
459 | var buffer = new T[DEFAULT_CHUNK_SIZE]; | |
| 460 | int actual; |
|
460 | int actual; | |
| 461 | bool recycle; |
|
461 | bool recycle; | |
| 462 | while (chunk != null) { |
|
462 | while (chunk != null) { | |
| 463 | // ensure all write operations on the chunk are complete |
|
463 | // ensure all write operations on the chunk are complete | |
| 464 | chunk.Commit(); |
|
464 | chunk.Commit(); | |
| 465 |
|
465 | |||
| 466 | // we need to read the chunk using this way |
|
466 | // we need to read the chunk using this way | |
| 467 | // since some client still may completing the dequeue |
|
467 | // since some client still may completing the dequeue | |
| 468 | // operation, such clients most likely won't get results |
|
468 | // operation, such clients most likely won't get results | |
| 469 | while (chunk.TryDequeueBatch(buffer, 0, buffer.Length, out actual, out recycle)) |
|
469 | while (chunk.TryDequeueBatch(buffer, 0, buffer.Length, out actual, out recycle)) | |
| 470 | result.AddRange(new ArraySegmentCollection(buffer, 0, actual)); |
|
470 | result.AddRange(new ArraySegmentCollection(buffer, 0, actual)); | |
| 471 |
|
471 | |||
| 472 | if (chunk == last) { |
|
472 | if (chunk == last) { | |
| 473 | chunk = null; |
|
473 | chunk = null; | |
| 474 | } else { |
|
474 | } else { | |
| 475 | while (chunk.next == null) |
|
475 | while (chunk.next == null) | |
| 476 | Thread.MemoryBarrier(); |
|
476 | Thread.MemoryBarrier(); | |
| 477 | chunk = chunk.next; |
|
477 | chunk = chunk.next; | |
| 478 | } |
|
478 | } | |
| 479 | } |
|
479 | } | |
| 480 |
|
480 | |||
| 481 | return result.ToArray(); |
|
481 | return result.ToArray(); | |
| 482 | } |
|
482 | } | |
| 483 |
|
483 | |||
| 484 | struct ArraySegmentCollection : ICollection<T> { |
|
484 | struct ArraySegmentCollection : ICollection<T> { | |
| 485 | readonly T[] m_data; |
|
485 | readonly T[] m_data; | |
| 486 | readonly int m_offset; |
|
486 | readonly int m_offset; | |
| 487 | readonly int m_length; |
|
487 | readonly int m_length; | |
| 488 |
|
488 | |||
| 489 | public ArraySegmentCollection(T[] data, int offset, int length) { |
|
489 | public ArraySegmentCollection(T[] data, int offset, int length) { | |
| 490 | m_data = data; |
|
490 | m_data = data; | |
| 491 | m_offset = offset; |
|
491 | m_offset = offset; | |
| 492 | m_length = length; |
|
492 | m_length = length; | |
| 493 | } |
|
493 | } | |
| 494 |
|
494 | |||
| 495 | #region ICollection implementation |
|
495 | #region ICollection implementation | |
| 496 |
|
496 | |||
| 497 | public void Add(T item) { |
|
497 | public void Add(T item) { | |
| 498 | throw new InvalidOperationException(); |
|
498 | throw new InvalidOperationException(); | |
| 499 | } |
|
499 | } | |
| 500 |
|
500 | |||
| 501 | public void Clear() { |
|
501 | public void Clear() { | |
| 502 | throw new InvalidOperationException(); |
|
502 | throw new InvalidOperationException(); | |
| 503 | } |
|
503 | } | |
| 504 |
|
504 | |||
| 505 | public bool Contains(T item) { |
|
505 | public bool Contains(T item) { | |
| 506 | return false; |
|
506 | return false; | |
| 507 | } |
|
507 | } | |
| 508 |
|
508 | |||
| 509 | public void CopyTo(T[] array, int arrayIndex) { |
|
509 | public void CopyTo(T[] array, int arrayIndex) { | |
| 510 | Array.Copy(m_data,m_offset,array,arrayIndex, m_length); |
|
510 | Array.Copy(m_data,m_offset,array,arrayIndex, m_length); | |
| 511 | } |
|
511 | } | |
| 512 |
|
512 | |||
| 513 | public bool Remove(T item) { |
|
513 | public bool Remove(T item) { | |
| 514 | throw new NotImplementedException(); |
|
514 | throw new NotImplementedException(); | |
| 515 | } |
|
515 | } | |
| 516 |
|
516 | |||
| 517 | public int Count { |
|
517 | public int Count { | |
| 518 | get { |
|
518 | get { | |
| 519 | return m_length; |
|
519 | return m_length; | |
| 520 | } |
|
520 | } | |
| 521 | } |
|
521 | } | |
| 522 |
|
522 | |||
| 523 | public bool IsReadOnly { |
|
523 | public bool IsReadOnly { | |
| 524 | get { |
|
524 | get { | |
| 525 | return true; |
|
525 | return true; | |
| 526 | } |
|
526 | } | |
| 527 | } |
|
527 | } | |
| 528 |
|
528 | |||
| 529 | #endregion |
|
529 | #endregion | |
| 530 |
|
530 | |||
| 531 | #region IEnumerable implementation |
|
531 | #region IEnumerable implementation | |
| 532 |
|
532 | |||
| 533 | public IEnumerator<T> GetEnumerator() { |
|
533 | public IEnumerator<T> GetEnumerator() { | |
| 534 | for (int i = m_offset; i < m_length + m_offset; i++) |
|
534 | for (int i = m_offset; i < m_length + m_offset; i++) | |
| 535 | yield return m_data[i]; |
|
535 | yield return m_data[i]; | |
| 536 | } |
|
536 | } | |
| 537 |
|
537 | |||
| 538 | #endregion |
|
538 | #endregion | |
| 539 |
|
539 | |||
| 540 | #region IEnumerable implementation |
|
540 | #region IEnumerable implementation | |
| 541 |
|
541 | |||
| 542 | IEnumerator IEnumerable.GetEnumerator() { |
|
542 | IEnumerator IEnumerable.GetEnumerator() { | |
| 543 | return GetEnumerator(); |
|
543 | return GetEnumerator(); | |
| 544 | } |
|
544 | } | |
| 545 |
|
545 | |||
| 546 | #endregion |
|
546 | #endregion | |
| 547 | } |
|
547 | } | |
| 548 |
|
548 | |||
| 549 | #region IEnumerable implementation |
|
549 | #region IEnumerable implementation | |
| 550 |
|
550 | |||
| 551 | class Enumerator : IEnumerator<T> { |
|
551 | class Enumerator : IEnumerator<T> { | |
| 552 | Chunk m_current; |
|
552 | Chunk m_current; | |
| 553 | int m_pos = -1; |
|
553 | int m_pos = -1; | |
| 554 |
|
554 | |||
| 555 | public Enumerator(Chunk fisrt) { |
|
555 | public Enumerator(Chunk fisrt) { | |
| 556 | m_current = fisrt; |
|
556 | m_current = fisrt; | |
| 557 | } |
|
557 | } | |
| 558 |
|
558 | |||
| 559 | #region IEnumerator implementation |
|
559 | #region IEnumerator implementation | |
| 560 |
|
560 | |||
| 561 | public bool MoveNext() { |
|
561 | public bool MoveNext() { | |
| 562 | if (m_current == null) |
|
562 | if (m_current == null) | |
| 563 | return false; |
|
563 | return false; | |
| 564 |
|
564 | |||
| 565 | if (m_pos == -1) |
|
565 | if (m_pos == -1) | |
| 566 | m_pos = m_current.Low; |
|
566 | m_pos = m_current.Low; | |
| 567 | else |
|
567 | else | |
| 568 | m_pos++; |
|
568 | m_pos++; | |
| 569 |
|
569 | |||
| 570 | if (m_pos == m_current.Hi) { |
|
570 | if (m_pos == m_current.Hi) { | |
| 571 |
|
571 | |||
| 572 | m_current = m_pos == m_current.Size ? m_current.next : null; |
|
572 | m_current = m_pos == m_current.Size ? m_current.next : null; | |
| 573 |
|
573 | |||
| 574 | m_pos = 0; |
|
574 | m_pos = 0; | |
| 575 |
|
575 | |||
| 576 | if (m_current == null) |
|
576 | if (m_current == null) | |
| 577 | return false; |
|
577 | return false; | |
| 578 | } |
|
578 | } | |
| 579 |
|
579 | |||
| 580 | return true; |
|
580 | return true; | |
| 581 | } |
|
581 | } | |
| 582 |
|
582 | |||
| 583 | public void Reset() { |
|
583 | public void Reset() { | |
| 584 | throw new NotSupportedException(); |
|
584 | throw new NotSupportedException(); | |
| 585 | } |
|
585 | } | |
| 586 |
|
586 | |||
| 587 | object IEnumerator.Current { |
|
587 | object IEnumerator.Current { | |
| 588 | get { |
|
588 | get { | |
| 589 | return Current; |
|
589 | return Current; | |
| 590 | } |
|
590 | } | |
| 591 | } |
|
591 | } | |
| 592 |
|
592 | |||
| 593 | #endregion |
|
593 | #endregion | |
| 594 |
|
594 | |||
| 595 | #region IDisposable implementation |
|
595 | #region IDisposable implementation | |
| 596 |
|
596 | |||
| 597 | public void Dispose() { |
|
597 | public void Dispose() { | |
| 598 | } |
|
598 | } | |
| 599 |
|
599 | |||
| 600 | #endregion |
|
600 | #endregion | |
| 601 |
|
601 | |||
| 602 | #region IEnumerator implementation |
|
602 | #region IEnumerator implementation | |
| 603 |
|
603 | |||
| 604 | public T Current { |
|
604 | public T Current { | |
| 605 | get { |
|
605 | get { | |
| 606 | if (m_pos == -1 || m_current == null) |
|
606 | if (m_pos == -1 || m_current == null) | |
| 607 | throw new InvalidOperationException(); |
|
607 | throw new InvalidOperationException(); | |
| 608 | return m_current.GetAt(m_pos); |
|
608 | return m_current.GetAt(m_pos); | |
| 609 | } |
|
609 | } | |
| 610 | } |
|
610 | } | |
| 611 |
|
611 | |||
| 612 | #endregion |
|
612 | #endregion | |
| 613 | } |
|
613 | } | |
| 614 |
|
614 | |||
| 615 | public IEnumerator<T> GetEnumerator() { |
|
615 | public IEnumerator<T> GetEnumerator() { | |
| 616 | return new Enumerator(m_first); |
|
616 | return new Enumerator(m_first); | |
| 617 | } |
|
617 | } | |
| 618 |
|
618 | |||
| 619 | #endregion |
|
619 | #endregion | |
| 620 |
|
620 | |||
| 621 | #region IEnumerable implementation |
|
621 | #region IEnumerable implementation | |
| 622 |
|
622 | |||
| 623 | IEnumerator IEnumerable.GetEnumerator() { |
|
623 | IEnumerator IEnumerable.GetEnumerator() { | |
| 624 | return GetEnumerator(); |
|
624 | return GetEnumerator(); | |
| 625 | } |
|
625 | } | |
| 626 |
|
626 | |||
| 627 | #endregion |
|
627 | #endregion | |
| 628 | } |
|
628 | } | |
| 629 | } |
|
629 | } | |
| @@ -1,86 +1,99 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.Linq; |
|
3 | using System.Linq; | |
| 4 | using System.Text; |
|
4 | using System.Text; | |
| 5 | using System.Text.RegularExpressions; |
|
5 | using System.Text.RegularExpressions; | |
| 6 | using System.Diagnostics; |
|
6 | using System.Diagnostics; | |
| 7 |
|
7 | |||
| 8 | namespace Implab |
|
8 | namespace Implab | |
| 9 | { |
|
9 | { | |
| 10 | public static class Safe |
|
10 | public static class Safe | |
| 11 | { |
|
11 | { | |
| 12 | public static void ArgumentMatch(string value, string paramName, Regex rx) { |
|
12 | public static void ArgumentMatch(string value, string paramName, Regex rx) { | |
| 13 | if (rx == null) |
|
13 | if (rx == null) | |
| 14 | throw new ArgumentNullException("rx"); |
|
14 | throw new ArgumentNullException("rx"); | |
| 15 | if (!rx.IsMatch(value)) |
|
15 | if (!rx.IsMatch(value)) | |
| 16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
| 17 | } |
|
17 | } | |
| 18 |
|
18 | |||
| 19 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
19 | public static void ArgumentNotEmpty(string value, string paramName) { | |
| 20 | if (String.IsNullOrEmpty(value)) |
|
20 | if (String.IsNullOrEmpty(value)) | |
| 21 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
21 | throw new ArgumentException("The parameter can't be empty", paramName); | |
| 22 | } |
|
22 | } | |
| 23 |
|
23 | |||
| 24 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
24 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { | |
| 25 | if (value == null || value.Length == 0) |
|
25 | if (value == null || value.Length == 0) | |
| 26 | throw new ArgumentException("The array must be not emty", paramName); |
|
26 | throw new ArgumentException("The array must be not emty", paramName); | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public static void ArgumentNotNull(object value, string paramName) { |
|
29 | public static void ArgumentNotNull(object value, string paramName) { | |
| 30 | if (value == null) |
|
30 | if (value == null) | |
| 31 | throw new ArgumentNullException(paramName); |
|
31 | throw new ArgumentNullException(paramName); | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
34 | public static void ArgumentInRange(int value, int min, int max, string paramName) { | |
| 35 | if (value < min || value > max) |
|
35 | if (value < min || value > max) | |
| 36 | throw new ArgumentOutOfRangeException(paramName); |
|
36 | throw new ArgumentOutOfRangeException(paramName); | |
| 37 | } |
|
37 | } | |
| 38 |
|
38 | |||
| 39 | public static void Dispose(params IDisposable[] objects) |
|
39 | public static void Dispose(params IDisposable[] objects) { | |
| 40 | { |
|
40 | foreach (var d in objects) | |
| 41 | foreach(var d in objects) |
|
|||
| 42 | if (d != null) |
|
41 | if (d != null) | |
| 43 | d.Dispose(); |
|
42 | d.Dispose(); | |
| 44 | } |
|
43 | } | |
| 45 |
|
44 | |||
|
|
45 | public static void Dispose(params object[] objects) { | |||
|
|
46 | foreach (var obj in objects) { | |||
|
|
47 | var d = obj as IDisposable; | |||
|
|
48 | if (d != null) | |||
|
|
49 | d.Dispose(); | |||
|
|
50 | } | |||
|
|
51 | } | |||
|
|
52 | ||||
|
|
53 | public static void Dispose(object obj) { | |||
|
|
54 | var d = obj as IDisposable; | |||
|
|
55 | if (d != null) | |||
|
|
56 | d.Dispose(); | |||
|
|
57 | } | |||
|
|
58 | ||||
| 46 | [DebuggerStepThrough] |
|
59 | [DebuggerStepThrough] | |
| 47 | public static IPromise<T> InvokePromise<T>(Func<T> action) { |
|
60 | public static IPromise<T> InvokePromise<T>(Func<T> action) { | |
| 48 | ArgumentNotNull(action, "action"); |
|
61 | ArgumentNotNull(action, "action"); | |
| 49 |
|
62 | |||
| 50 | var p = new Promise<T>(); |
|
63 | var p = new Promise<T>(); | |
| 51 | try { |
|
64 | try { | |
| 52 | p.Resolve(action()); |
|
65 | p.Resolve(action()); | |
| 53 | } catch (Exception err) { |
|
66 | } catch (Exception err) { | |
| 54 | p.Reject(err); |
|
67 | p.Reject(err); | |
| 55 | } |
|
68 | } | |
| 56 |
|
69 | |||
| 57 | return p; |
|
70 | return p; | |
| 58 | } |
|
71 | } | |
| 59 |
|
72 | |||
| 60 | [DebuggerStepThrough] |
|
73 | [DebuggerStepThrough] | |
| 61 | public static IPromise InvokePromise(Action action) { |
|
74 | public static IPromise InvokePromise(Action action) { | |
| 62 | ArgumentNotNull(action, "action"); |
|
75 | ArgumentNotNull(action, "action"); | |
| 63 |
|
76 | |||
| 64 | var p = new Promise(); |
|
77 | var p = new Promise(); | |
| 65 | try { |
|
78 | try { | |
| 66 | action(); |
|
79 | action(); | |
| 67 | p.Resolve(); |
|
80 | p.Resolve(); | |
| 68 | } catch (Exception err) { |
|
81 | } catch (Exception err) { | |
| 69 | p.Reject(err); |
|
82 | p.Reject(err); | |
| 70 | } |
|
83 | } | |
| 71 |
|
84 | |||
| 72 | return p; |
|
85 | return p; | |
| 73 | } |
|
86 | } | |
| 74 |
|
87 | |||
| 75 | [DebuggerStepThrough] |
|
88 | [DebuggerStepThrough] | |
| 76 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
|
89 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { | |
| 77 | ArgumentNotNull(action, "action"); |
|
90 | ArgumentNotNull(action, "action"); | |
| 78 |
|
91 | |||
| 79 | try { |
|
92 | try { | |
| 80 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); |
|
93 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); | |
| 81 | } catch (Exception err) { |
|
94 | } catch (Exception err) { | |
| 82 | return Promise<T>.ExceptionToPromise(err); |
|
95 | return Promise<T>.ExceptionToPromise(err); | |
| 83 | } |
|
96 | } | |
| 84 | } |
|
97 | } | |
| 85 | } |
|
98 | } | |
| 86 | } |
|
99 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
