File: ResourcePool.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (178 lines) | stat: -rw-r--r-- 6,270 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//------------------------------------------------------------------------------
// <copyright file="ResourcePool.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

namespace System.Web.Util {
    using System.Collections;
    using System.Threading;

    /*
     * ResourcePool provides a place to store expensive resources,
     * such as network connections, that you want to dispose of when
     * they are underused. A resource pool can be configured to timeout
     * resources at a given interval, and to have a max limit of resources.
     */
    class ResourcePool : IDisposable {
        ArrayList       _resources;     // the resources
        int             _iDisposable;   // resources below this index are candidates for disposal
        int             _max;           // max number of resources
        Timer           _timer;         // periodic timer
        TimerCallback   _callback;      // callback delegate
        TimeSpan        _interval;      // callback interval
        bool            _disposed;

        internal ResourcePool(TimeSpan interval, int max) {
            _interval = interval;
            _resources = new ArrayList(4);
            _max = max;
            _callback = new TimerCallback(this.TimerProc);

            Debug.Validate("ResourcePool", this);
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        
        protected virtual void Dispose(bool disposing) {
            if (disposing) {
                lock (this) {
                    if (!_disposed) {
                        if (_resources != null) {
                            foreach (IDisposable resource in _resources) {
                                resource.Dispose();
                            }

                            _resources.Clear();
                        }

                        if (_timer != null) {
                            _timer.Dispose();
                        }

                        Debug.Trace("ResourcePool", "Disposed");
                        _disposed = true;
                    }
                }
            }
        }

        internal object RetrieveResource() {
            object result = null;

            // avoid lock in common case
            if (_resources.Count != 0) {
                lock (this) {
                    Debug.Validate("ResourcePool", this);

                    if (!_disposed) {
                        if (_resources.Count == 0) {
                            result = null;
                            Debug.Trace("ResourcePool", "RetrieveResource returned null");
                        } else {
                            result = _resources[_resources.Count-1];
                            _resources.RemoveAt(_resources.Count-1);
                            if (_resources.Count < _iDisposable) {
                                _iDisposable = _resources.Count;
                            }
                        }

                        Debug.Validate("ResourcePool", this);
                    }
                }
            }
    
            return result;
        }

        internal void StoreResource(IDisposable o) {

            lock (this) {
                Debug.Validate("ResourcePool", this);

                if (!_disposed) {
                    if (_resources.Count < _max) {
                        _resources.Add(o);
                        o = null;
                        if (_timer == null) {

#if DBG
                            if (!Debug.IsTagPresent("Timer") || Debug.IsTagEnabled("Timer"))
#endif
                            {
                                _timer = new Timer(_callback, null, _interval, _interval);
                            }
                        }
                    }

                    Debug.Validate("ResourcePool", this);
                }
            }

            if (o != null) {
                Debug.Trace("ResourcePool", "StoreResource reached max=" + _max);
                o.Dispose();
            }
        }

        void TimerProc(Object userData) {
            IDisposable[] a = null;

            lock (this) {
                Debug.Validate("ResourcePool", this);

                if (!_disposed) {
                    if (_resources.Count == 0) {
                        if (_timer != null) {
                            _timer.Dispose();
                            _timer = null;
                        }

                        Debug.Validate("ResourcePool", this);
                        return;
                    }

                    a = new IDisposable[_iDisposable];
                    _resources.CopyTo(0, a, 0, _iDisposable);
                    _resources.RemoveRange(0, _iDisposable);

                    // It means that whatever remain in _resources will be disposed 
                    // next time the timer proc is called.
                    _iDisposable = _resources.Count;

                    Debug.Trace("ResourcePool", "Timer disposing " + a.Length + "; remaining=" + _resources.Count);
                    Debug.Validate("ResourcePool", this);
                }
            }

            if (a != null) {
                for (int i = 0; i < a.Length; i++) {
                    try {
                        a[i].Dispose();
                    }
                    catch {
                        // ignore all errors
                    }
                }
            }
        }

#if DBG
        internal void DebugValidate() {
            Debug.CheckValid(_resources != null, "_resources != null");

            Debug.CheckValid(0 <= _iDisposable && _iDisposable <= _resources.Count,
                             "0 <= _iDisposable && _iDisposable <= _resources.Count" +
                             ";_iDisposable=" + _iDisposable +
                             ";_resources.Count=" + _resources.Count);

            Debug.CheckValid(_interval > TimeSpan.Zero, "_interval > TimeSpan.Zero" +
                             ";_interval=" + _interval);
        }
#endif
    }
}