Mercurial > pub > ImplabNet
changeset 255:b00441e04738 v3
Adde workaround to the behaviour of the logical operations stack in conjuction
with async/await methods
author | cin |
---|---|
date | Wed, 04 Apr 2018 15:38:48 +0300 |
parents | 12c00235b105 |
children | c52691faaf21 |
files | Implab.Playground/App.config Implab.Playground/Implab.Playground.csproj Implab.Playground/Program.cs Implab.Test/Implab.Test.csproj Implab.sln Implab/Diagnostics/Trace.cs Implab/Implab.csproj |
diffstat | 7 files changed, 89 insertions(+), 82 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab.Playground/App.config Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab.Playground/App.config Wed Apr 04 15:38:48 2018 +0300 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <configuration> <startup> - <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> + <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/> </startup> </configuration>
--- a/Implab.Playground/Implab.Playground.csproj Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab.Playground/Implab.Playground.csproj Wed Apr 04 15:38:48 2018 +0300 @@ -9,7 +9,7 @@ <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>Implab.Playground</RootNamespace> <AssemblyName>Implab.Playground</AssemblyName> - <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <TargetFrameworkProfile />
--- a/Implab.Playground/Program.cs Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab.Playground/Program.cs Wed Apr 04 15:38:48 2018 +0300 @@ -1,4 +1,5 @@ -using Implab.Formats.Json; +using Implab.Diagnostics; +using Implab.Formats.Json; using Implab.Parallels; using Implab.Xml; using System; @@ -13,80 +14,42 @@ using System.Xml.Serialization; namespace Implab.Playground { + using System.Diagnostics; + using System.Runtime.Remoting.Messaging; + using static Trace<Program>; + public class Program { - static void EnqueueRange<T>(ConcurrentQueue<T> q, T[] data, int offset, int len) { - for (var i = offset; i < offset + len; i++) - q.Enqueue(data[i]); - } + static void Main(string[] args) { + var listener = new SimpleTraceListener(Console.Out); + + var source = Trace<Program>.TraceSource; + source.Switch.Level = SourceLevels.All; - static bool TryDequeueRange<T>(ConcurrentQueue<T> q,T[] buffer,int offset, int len, out int actual) { - actual = 0; - T res; - while(q.TryDequeue(out res)) { - buffer[offset + actual] = res; - actual++; - if (actual == len) - break; - } - return actual != 0; - } + source.Listeners.Add(listener); + + var t = Environment.TickCount; - static void EnqueueRange<T>(SimpleAsyncQueue<T> q, T[] data, int offset, int len) { - for (var i = offset; i < offset + len; i++) - q.Enqueue(data[i]); - } + Main().Wait(); - static bool TryDequeueRange<T>(SimpleAsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) { - actual = 0; - T res; - while (q.TryDequeue(out res)) { - buffer[offset + actual] = res; - actual++; - if (actual == len) - break; - } - return actual != 0; + Console.WriteLine($"Done: {Environment.TickCount - t} ms"); + Console.ReadKey(); } - static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) { - for (var i = offset; i < offset + len; i++) - q.Enqueue(data[i]); + static async Task Main() { + using (LogicalOperation(nameof(Main))) { + Log("Start"); + await SomeAsync(); + Log("End"); + } } - static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) { - actual = 0; - T res; - while (q.TryDequeue(out res)) { - buffer[offset + actual] = res; - actual++; - if (actual == len) - break; + static async Task SomeAsync() { + using (LogicalOperation(nameof(SomeAsync))) { + Log("Do prepare"); + await Task.Yield(); + Log("Yield"); } - return actual != 0; - } - - - /*static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) { - q.EnqueueRange(data, offset, len); - } - - static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) { - return q.TryDequeueRange(buffer, offset, len, out actual); - }*/ - - - static void Main(string[] args) { - - var t = Environment.TickCount; - using (var reader = JsonReader.Create("e:\\citylots.json")) { - while (reader.Read()) { - } - } - - Console.WriteLine($"JsonReader: {Environment.TickCount - t} ms"); - - Console.WriteLine("done"); } } }
--- a/Implab.Test/Implab.Test.csproj Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab.Test/Implab.Test.csproj Wed Apr 04 15:38:48 2018 +0300 @@ -1,12 +1,13 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> - <TargetFramework>netcoreapp2.0</TargetFramework> + <TargetFramework>net46</TargetFramework> + <FrameworkPathOverride Condition="'$(TargetFramework)'=='net45' and '$(OSTYPE)'=='linux'">/usr/lib/mono/4.5/</FrameworkPathOverride> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> - <ProjectReference Include="../Implab/Implab.csproj"/> + <ProjectReference Include="../Implab/Implab.csproj" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> <PackageReference Include="xunit" Version="2.3.1" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.sln Wed Apr 04 15:38:48 2018 +0300 @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2005 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Implab", "Implab\Implab.csproj", "{FF2052B6-9C8F-4022-A347-F07ABF635885}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Implab.Test", "Implab.Test\Implab.Test.csproj", "{6CD0DA18-8D9B-4AA8-A3DC-17322E27335E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Playground", "Implab.Playground\Implab.Playground.csproj", "{100DFEB0-75BE-436F-ADDF-1F46EF433F46}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FF2052B6-9C8F-4022-A347-F07ABF635885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF2052B6-9C8F-4022-A347-F07ABF635885}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF2052B6-9C8F-4022-A347-F07ABF635885}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF2052B6-9C8F-4022-A347-F07ABF635885}.Release|Any CPU.Build.0 = Release|Any CPU + {6CD0DA18-8D9B-4AA8-A3DC-17322E27335E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6CD0DA18-8D9B-4AA8-A3DC-17322E27335E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6CD0DA18-8D9B-4AA8-A3DC-17322E27335E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6CD0DA18-8D9B-4AA8-A3DC-17322E27335E}.Release|Any CPU.Build.0 = Release|Any CPU + {100DFEB0-75BE-436F-ADDF-1F46EF433F46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {100DFEB0-75BE-436F-ADDF-1F46EF433F46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {100DFEB0-75BE-436F-ADDF-1F46EF433F46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {100DFEB0-75BE-436F-ADDF-1F46EF433F46}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {36D837FC-4CDD-4AEA-87BF-F130FEB22E02} + EndGlobalSection +EndGlobal
--- a/Implab/Diagnostics/Trace.cs Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab/Diagnostics/Trace.cs Wed Apr 04 15:38:48 2018 +0300 @@ -3,21 +3,21 @@ using System.Diagnostics; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Implab.Diagnostics { public static class Trace<T> { - readonly static TraceSource _traceSource = new TraceSource(typeof(T).Name); - - public static TraceSource TraceSource { - get { return _traceSource; } - } - + public static TraceSource TraceSource { get; } = new TraceSource(typeof(T).Name); + +#if NETFX_TRACE_BUG + readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>(); +#endif + /// <summary> /// Starts the logical operation nested to the current operation nested to the current one. /// </summary> - [Conditional("TRACE")] public static void StartLogicalOperation() { Trace.CorrelationManager.StartLogicalOperation(); @@ -27,15 +27,20 @@ /// Starts the logical operation with the specified name, this name is usefull in logs. /// </summary> /// <param name="name">Name.</param> - [Conditional("TRACE")] - public static void StartLogicalOperation(string name) { +#if NETFX_TRACE_BUG + public static void StartLogicalOperation(object name) { + m_currentOperation.Value = name; Trace.CorrelationManager.StartLogicalOperation(name); } +#else + public static void StartLogicalOperation(object name) { + Trace.CorrelationManager.StartLogicalOperation(name); + } +#endif /// <summary> /// Ends the logical operation and restores the previous one. /// </summary> - [Conditional("TRACE")] public static void StopLogicalOperation() { Trace.CorrelationManager.StopLogicalOperation(); } @@ -120,7 +125,7 @@ public static LogicalOperationScope LogicalOperation(string name) { var operation = new LogicalOperation(name); TraceSource.TraceData(TraceEventType.Information, TraceEventCodes.StartLogicalOperation, operation); - Trace.CorrelationManager.StartLogicalOperation(operation); + StartLogicalOperation(operation); return new LogicalOperationScope(TraceSource, operation); } }
--- a/Implab/Implab.csproj Mon Feb 12 17:03:49 2018 +0300 +++ b/Implab/Implab.csproj Wed Apr 04 15:38:48 2018 +0300 @@ -11,8 +11,9 @@ <LicenseUrl>https://opensource.org/licenses/BSD-2-Clause</LicenseUrl> <ProjectUrl>https://implab.org</ProjectUrl> <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> - <TargetFrameworks>netstandard2.0;net45</TargetFrameworks> - <FrameworkPathOverride Condition="'$(TargetFramework)'=='net45' and '$(OSTYPE)'=='linux'">/usr/lib/mono/4.5/</FrameworkPathOverride> + <TargetFrameworks>netstandard2.0;net46</TargetFrameworks> + <FrameworkPathOverride Condition="'$(TargetFramework)'=='net46' and '$(OSTYPE)'=='linux'">/usr/lib/mono/4.5/</FrameworkPathOverride> + <DefineConstants Condition="'$(TargetFramework)'=='net46'">NETFX_TRACE_BUG;$(DefineConstants)</DefineConstants> </PropertyGroup> </Project>