Mercurial > pub > ImplabNet
diff Implab/ComponentContainer.cs @ 116:da56ba7b1aab v2
minor refactoring
author | cin |
---|---|
date | Tue, 23 Dec 2014 03:20:39 +0300 |
parents | |
children | 2573b562e328 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/ComponentContainer.cs Tue Dec 23 03:20:39 2014 +0300 @@ -0,0 +1,48 @@ +using System; +using Implab.Parallels; +using System.Threading; + +namespace Implab { + public class ComponentContainer : IComponentContainer, IDisposable { + static readonly ComponentContainer _appContainer; + + static ComponentContainer() { + _appContainer = new ComponentContainer(); + AppDomain.CurrentDomain.ProcessExit += HandleProcessExit; + } + + public static ComponentContainer Global { + get { + return _appContainer; + } + } + + bool m_disposed; + readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>(); + + 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(); + } + } +} +