# HG changeset patch # User cin # Date 2014-10-22 21:13:57 # Node ID dc4942d09e7445620b625df27ccd34a9816b14ba # Parent 4c0e5ef99986818ce3b22f8dd2ed60583d7df8c4 improved tracing added the application components container MTComponentContainer.AppContainer diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -13,3 +13,4 @@ Implab.Fx.Test/obj/ _ReSharper.Implab/ Implab.Diagnostics.Interactive/bin/ Implab.Diagnostics.Interactive/obj/ +MonoPlay/bin/ diff --git a/Implab.mono.sln b/Implab.mono.sln --- a/Implab.mono.sln +++ b/Implab.mono.sln @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Test.mono", "Implab.Test\Implab.Test.mono.csproj", "{2BD05F84-E067-4B87-9477-FDC2676A21C6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoPlay", "MonoPlay\MonoPlay.csproj", "{15DD7123-D504-4627-8B4F-D00C7F04D033}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -32,22 +34,30 @@ Global {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.Build.0 = Release|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.ActiveCfg = Debug|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.Build.0 = Debug|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.Build.0 = Debug|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.ActiveCfg = Release|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.Build.0 = Release|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.ActiveCfg = Release|Any CPU + {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.Build.0 = Release|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU - {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -64,20 +74,12 @@ Global {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.Build.0 = Release|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {2BD05F84-E067-4B87-9477-FDC2676A21C6} = {BCA337C3-BFDC-4825-BBDB-E6D467E4E452} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = Implab\Implab.csproj + StartupItem = MonoPlay\MonoPlay.csproj Policies = $0 $0.CSharpFormattingPolicy = $1 $1.IndentSwitchBody = True diff --git a/Implab/Diagnostics/OperationContext.cs b/Implab/Diagnostics/OperationContext.cs --- a/Implab/Diagnostics/OperationContext.cs +++ b/Implab/Diagnostics/OperationContext.cs @@ -28,22 +28,32 @@ m_current = m_current.Parent; else if (m_ownership) m_current = LogicalOperation.EMPTY; - else + else { + TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); detached = LogicalOperation.EMPTY; + } + } else { + TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); } - TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); + return detached; } public void EndLogicalOperation() { if (m_current != m_initial) { m_current = m_current.Parent; - } else if (m_current != null && m_ownership) { - m_current = null; + } else if (m_current != LogicalOperation.EMPTY && m_ownership) { + m_current = LogicalOperation.EMPTY; } else { TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); } } + + public void Leave() { + + if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) ) + TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name); + } } } diff --git a/Implab/Diagnostics/TraceContext.cs b/Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs +++ b/Implab/Diagnostics/TraceContext.cs @@ -41,35 +41,36 @@ namespace Implab.Diagnostics { } public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { - // TODO Emit event + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId))); m_stack.Push(m_current); m_current = new OperationContext(operation, takeOwnership); } public void StartLogicalOperation(string name) { m_current.BeginLogicalOperation(name); + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name))); } public void StartLogicalOperation() { - // TODO Emit Event - m_current.BeginLogicalOperation(String.Empty); + StartLogicalOperation(String.Empty); } public void EndLogicalOperation() { - // TODO Emit event + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration))); m_current.EndLogicalOperation(); } public LogicalOperation DetachLogicalOperation() { - // TODO Emit event - return m_current.DetachLogicalOperation(); + var op = m_current.DetachLogicalOperation(); + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("[{0}] -> {1}", m_threadId, op.Name))); + return op; } public void Leave() { - // TODO Emit event - if (m_stack.Count > 0) + if (m_stack.Count > 0) { + m_current.Leave(); m_current = m_stack.Pop(); - else { + } else { TraceLog.TraceWarning("Attemtp to leave the last operation context"); m_current = OperationContext.EMPTY; } diff --git a/Implab/Diagnostics/TraceEvent.cs b/Implab/Diagnostics/TraceEvent.cs --- a/Implab/Diagnostics/TraceEvent.cs +++ b/Implab/Diagnostics/TraceEvent.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Implab.Diagnostics { public class TraceEvent { @@ -21,10 +18,7 @@ namespace Implab.Diagnostics { } public override string ToString() { - if (EventType == TraceEventType.Information) - return Message; - else - return String.Format("{0}: {1}", EventType, Message); + return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message); } public static TraceEvent Create(TraceEventType type, string format, params object[] args) { diff --git a/Implab/Diagnostics/TraceEventType.cs b/Implab/Diagnostics/TraceEventType.cs --- a/Implab/Diagnostics/TraceEventType.cs +++ b/Implab/Diagnostics/TraceEventType.cs @@ -11,9 +11,7 @@ namespace Implab.Diagnostics { Error, OperationStarted, OperationCompleted, - Fork, Attach, Detach, - Created } } diff --git a/Implab/IComponentContainer.cs b/Implab/IComponentContainer.cs new file mode 100644 --- /dev/null +++ b/Implab/IComponentContainer.cs @@ -0,0 +1,8 @@ +using System; + +namespace Implab { + public interface IComponentContainer { + void Add(IDisposable component); + } +} + diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -7,6 +7,8 @@ Library Implab Implab + 8.0.30703 + 2.0 true @@ -145,6 +147,8 @@ + + diff --git a/Implab/MTComponentContainer.cs b/Implab/MTComponentContainer.cs new file mode 100644 --- /dev/null +++ b/Implab/MTComponentContainer.cs @@ -0,0 +1,48 @@ +using System; +using Implab.Parallels; +using System.Threading; + +namespace Implab { + public class MTComponentContainer : IComponentContainer, IDisposable { + static readonly MTComponentContainer _appContainer; + + static MTComponentContainer() { + _appContainer = new MTComponentContainer(); + AppDomain.CurrentDomain.ProcessExit += HandleProcessExit; + } + + public static MTComponentContainer AppContainer { + get { + return _appContainer; + } + } + + bool m_disposed; + readonly MTQueue m_components = new MTQueue(); + + public void Add(IDisposable item) { + Safe.ArgumentNotNull(item, "item"); + Thread.MemoryBarrier(); + if (m_disposed) { + item.Dispose(); + } else { + m_components.Enqueue(item); + if (m_disposed && m_components.TryDequeue(out item)) + item.Dispose(); + } + } + + public void Dispose() { + m_disposed = true; + IDisposable item; + while (m_components.TryDequeue(out item)) + item.Dispose(); + } + + static void HandleProcessExit (object sender, EventArgs e) + { + _appContainer.Dispose(); + } + } +} + diff --git a/Implab/Parallels/DispatchPool.cs b/Implab/Parallels/DispatchPool.cs --- a/Implab/Parallels/DispatchPool.cs +++ b/Implab/Parallels/DispatchPool.cs @@ -7,9 +7,9 @@ namespace Implab.Parallels { readonly int m_maxThreadsLimit; readonly int m_releaseTimeout = 1000; // the timeout while the working thread will wait for the new tasks before exit - int m_threads = 0; // the current size of the pool - int m_maxRunningThreads = 0; // the meximum reached size of the pool - int m_exit = 0; // the pool is going to shutdown, all unused workers are released + int m_threads; // the current size of the pool + int m_maxRunningThreads; // the meximum reached size of the pool + int m_exit; // the pool is going to shutdown, all unused workers are released readonly object m_signal = new object(); // used to pulse waiting threads diff --git a/Implab/Parallels/MTQueue.cs b/Implab/Parallels/MTQueue.cs --- a/Implab/Parallels/MTQueue.cs +++ b/Implab/Parallels/MTQueue.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; +using System.Threading; namespace Implab.Parallels { public class MTQueue { @@ -34,7 +30,7 @@ namespace Implab.Parallels { public bool TryDequeue(out T value) { Node first; - Node next = null; + Node next; value = default(T); Thread.MemoryBarrier(); @@ -64,11 +60,10 @@ namespace Implab.Parallels { Interlocked.CompareExchange(ref m_first, null, first); break; - } else { - if (first == Interlocked.CompareExchange(ref m_first, next, first)) - // head succesfully updated - break; } + if (first == Interlocked.CompareExchange(ref m_first, next, first)) + // head succesfully updated + break; } while (true); value = first.value; diff --git a/MonoPlay/MonoPlay.csproj b/MonoPlay/MonoPlay.csproj new file mode 100644 --- /dev/null +++ b/MonoPlay/MonoPlay.csproj @@ -0,0 +1,46 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {15DD7123-D504-4627-8B4F-D00C7F04D033} + Exe + MonoPlay + MonoPlay + v4.5 + + + true + full + false + bin\Debug + DEBUG;TRACE; + prompt + 4 + false + + + full + true + bin\Release + prompt + 4 + true + + + + + + + + + + + + {F550F1F8-8746-4AD0-9614-855F4C4B7F05} + Implab + + + \ No newline at end of file diff --git a/MonoPlay/Program.cs b/MonoPlay/Program.cs new file mode 100644 --- /dev/null +++ b/MonoPlay/Program.cs @@ -0,0 +1,31 @@ +using System; +using Implab.Diagnostics; +using Implab.Parallels; +using Implab; + +namespace MonoPlay { + class MainClass { + public static void Main(string[] args) { + var listener = new ConsoleTraceListener(true); + listener.Subscribe(); + + MTComponentContainer.AppContainer.Add(listener); + + TraceLog.StartLogicalOperation("program"); + + Console.WriteLine("Hello World!"); + + TraceLog.StartLogicalOperation("async"); + AsyncPool.Invoke(() => { + TraceLog.TraceInformation("Hello async"); + TraceLog.StartLogicalOperation(); + return 0; + }) + .EndLogicalOperation() + .Join(); + + TraceLog.EndLogicalOperation(); + + } + } +} diff --git a/MonoPlay/Properties/AssemblyInfo.cs b/MonoPlay/Properties/AssemblyInfo.cs new file mode 100644 --- /dev/null +++ b/MonoPlay/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("MonoPlay")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("sergey")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] +