annotate Implab/Disposable.cs @ 102:b4c4d65b7def v2

code cleaunp
author cin
date Fri, 07 Nov 2014 09:36:12 +0300
parents 673947ce458a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
1 using Implab.Diagnostics;
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
2 using System;
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
3 using System.Threading;
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
4 using System.IO;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 namespace Implab {
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
7 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
8 /// Объект, поддерживающий освобождение ресурсов.
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
9 /// </summary>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
10 public class Disposable : IDisposable {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
12 int m_disposed;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 public event EventHandler Disposed;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 public bool IsDisposed {
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
17 get {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
18 Thread.MemoryBarrier();
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
19 return m_disposed != 0;
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
20 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
23 /// <summary>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
24 /// Asserts the object is not disposed.
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
25 /// </summary>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
26 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
27 /// <remarks>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
28 /// Успешная проверка того, что объект не освобожден еще не гарантирует, что он не
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
29 /// будет освобожден сразу после нее, поэтому методы использующие проверку должны
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
30 /// учитывать, что объект может быть освобожден из параллельного потока.
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
31 /// Данны метод служит для упрощения отладки ошибок при использовании объекта после его
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
32 /// освобождения.
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
33 /// </remarks>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
34 /// <example>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
35 /// // пример синхронизированного освобождения ресурсов
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
36 /// class FileStore : Disposable {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
37 /// readonly TextWriter m_file;
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
38 /// readonly obejct m_sync = new object();
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
39 ///
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
40 /// public FileStore(string file) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
41 /// m_file = new TextWriter(File.OpenWrite(file));
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
42 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
43 ///
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
44 /// public void Write(string text) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
45 /// lock(m_sync) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
46 /// AssertNotDisposed();
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
47 /// m_file.Write(text);
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
48 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
49 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
50 ///
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
51 /// protected override void Dispose(bool disposing) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
52 /// if (disposing)
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
53 /// lock(m_sync) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
54 /// m_file.Dipose();
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
55 /// base.Dispose(true);
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
56 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
57 /// else
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
58 /// base.Dispose(false);
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
59 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
60 /// }
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
61 /// <example>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
62 protected void AssertNotDisposed() {
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
63 Thread.MemoryBarrier();
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
64 if (m_disposed != 0)
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
65 throw new ObjectDisposedException(ToString());
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
66 }
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
67 /// <summary>
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
68 /// Вызывает событие <see cref="Disposed"/>
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
69 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
70 /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
71 /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
72 /// объекта.</param>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
73 /// <remarks>
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
74 /// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/>
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
75 /// из нескольких потоков.
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
76 /// </remarks>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 protected virtual void Dispose(bool disposing) {
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
78 if (disposing) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 EventHandler temp = Disposed;
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
80 if (temp != null)
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
81 temp(this, EventArgs.Empty);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
82 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
83 }
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
84
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85 public void Dispose() {
100
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
86 if (Interlocked.Increment(ref m_disposed) == 1) {
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
87 Dispose(true);
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
88 GC.SuppressFinalize(this);
673947ce458a added multithreading support to Disposable class
cin
parents: 48
diff changeset
89 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
92 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
93 /// Записывает сообщение об утечке объекта.
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
94 /// </summary>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95 protected virtual void ReportObjectLeaks() {
48
d9d794b61bb9 Interactive tracing
cin
parents: 40
diff changeset
96 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
97 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
98
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
99 ~Disposable() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
100 Dispose(false);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
101 ReportObjectLeaks();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
102 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 }