# HG changeset patch # User cin # Date 1522845528 -10800 # Node ID b00441e04738bb9ea06c37f7b59fd753461d7731 # Parent 12c00235b105360c4c23c245ed97b50e28bae275 Adde workaround to the behaviour of the logical operations stack in conjuction with async/await methods diff -r 12c00235b105 -r b00441e04738 Implab.Playground/App.config --- 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 @@ - + diff -r 12c00235b105 -r b00441e04738 Implab.Playground/Implab.Playground.csproj --- 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 @@ Properties Implab.Playground Implab.Playground - v4.5 + v4.6 512 true diff -r 12c00235b105 -r b00441e04738 Implab.Playground/Program.cs --- 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; + public class Program { - static void EnqueueRange(ConcurrentQueue 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.TraceSource; + source.Switch.Level = SourceLevels.All; - static bool TryDequeueRange(ConcurrentQueue 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(SimpleAsyncQueue q, T[] data, int offset, int len) { - for (var i = offset; i < offset + len; i++) - q.Enqueue(data[i]); - } + Main().Wait(); - static bool TryDequeueRange(SimpleAsyncQueue 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(AsyncQueue 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(AsyncQueue 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(AsyncQueue q, T[] data, int offset, int len) { - q.EnqueueRange(data, offset, len); - } - - static bool TryDequeueRange(AsyncQueue 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"); } } } diff -r 12c00235b105 -r b00441e04738 Implab.Test/Implab.Test.csproj --- 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 @@ - netcoreapp2.0 + net46 + /usr/lib/mono/4.5/ false - + diff -r 12c00235b105 -r b00441e04738 Implab.sln --- /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 diff -r 12c00235b105 -r b00441e04738 Implab/Diagnostics/Trace.cs --- 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 { - 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 m_currentOperation = new AsyncLocal(); +#endif + /// /// Starts the logical operation nested to the current operation nested to the current one. /// - [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. /// /// Name. - [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 /// /// Ends the logical operation and restores the previous one. /// - [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); } } diff -r 12c00235b105 -r b00441e04738 Implab/Implab.csproj --- 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 @@ https://opensource.org/licenses/BSD-2-Clause https://implab.org https://hg.implab.org/pub/ImplabNet/ - netstandard2.0;net45 - /usr/lib/mono/4.5/ + netstandard2.0;net46 + /usr/lib/mono/4.5/ + NETFX_TRACE_BUG;$(DefineConstants)