annotate Implab/ComponentContainer.cs @ 149:eb793fbbe4ea v2

fixed promises cancellation
author cin
date Wed, 06 May 2015 17:11:27 +0300
parents 2573b562e328
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
116
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
1 using System;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
2 using Implab.Parallels;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
3 using System.Threading;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
4
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
5 namespace Implab {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
6 public class ComponentContainer : IComponentContainer, IDisposable {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
7 static readonly ComponentContainer _appContainer;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
8
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
9 static ComponentContainer() {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
10 _appContainer = new ComponentContainer();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
11 AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
12 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
13
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
14 public static ComponentContainer Global {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
15 get {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
16 return _appContainer;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
17 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
18 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
19
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
20 bool m_disposed;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 116
diff changeset
21 readonly AsyncQueue<IDisposable> m_components = new AsyncQueue<IDisposable>();
116
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
22
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
23 public void Add(IDisposable item) {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
24 Safe.ArgumentNotNull(item, "item");
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
25 Thread.MemoryBarrier();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
26 if (m_disposed) {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
27 item.Dispose();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
28 } else {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
29 m_components.Enqueue(item);
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
30 if (m_disposed && m_components.TryDequeue(out item))
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
31 item.Dispose();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
32 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
33 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
34
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
35 public void Dispose() {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
36 m_disposed = true;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
37 IDisposable item;
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
38 while (m_components.TryDequeue(out item))
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
39 item.Dispose();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
40 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
41
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
42 static void HandleProcessExit (object sender, EventArgs e)
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
43 {
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
44 _appContainer.Dispose();
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
45 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
46 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
47 }
da56ba7b1aab minor refactoring
cin
parents:
diff changeset
48