|
1 |
| -// AsyncEvaluationTracker.cs |
2 |
| -// |
3 |
| -// Author: |
4 |
| -// Lluis Sanchez Gual <[email protected]> |
5 |
| -// |
6 |
| -// Copyright (c) 2008 Novell, Inc (http://www.novell.com) |
7 |
| -// |
8 |
| -// Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
| -// of this software and associated documentation files (the "Software"), to deal |
10 |
| -// in the Software without restriction, including without limitation the rights |
11 |
| -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 |
| -// copies of the Software, and to permit persons to whom the Software is |
13 |
| -// furnished to do so, subject to the following conditions: |
14 |
| -// |
15 |
| -// The above copyright notice and this permission notice shall be included in |
16 |
| -// all copies or substantial portions of the Software. |
17 |
| -// |
18 |
| -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 |
| -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 |
| -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 |
| -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 |
| -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 |
| -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 |
| -// THE SOFTWARE. |
25 |
| -// |
26 |
| -// |
27 |
| - |
28 |
| -using System; |
29 |
| -using System.Collections.Generic; |
30 |
| -using Mono.Debugging.Client; |
31 |
| -using Mono.Debugging.Backend; |
32 |
| - |
33 |
| -namespace Mono.Debugging.Evaluation |
34 |
| -{ |
35 |
| - public delegate ObjectValue ObjectEvaluatorDelegate (); |
36 |
| - |
37 |
| - /// <summary> |
38 |
| - /// This class can be used to generate an ObjectValue using a provided evaluation delegate. |
39 |
| - /// The value is initialy evaluated synchronously (blocking the caller). If no result |
40 |
| - /// is obtained after a short period (provided in the WaitTime property), evaluation |
41 |
| - /// will then be made asynchronous and the Run method will immediately return an ObjectValue |
42 |
| - /// with the Evaluating state. |
43 |
| - /// </summary> |
44 |
| - public class AsyncEvaluationTracker: IObjectValueUpdater, IDisposable |
45 |
| - { |
46 |
| - Dictionary<string, UpdateCallback> asyncCallbacks = new Dictionary<string, UpdateCallback> (); |
47 |
| - Dictionary<string, ObjectValue> asyncResults = new Dictionary<string, ObjectValue> (); |
48 |
| - int asyncCounter = 0; |
49 |
| - int cancelTimestamp = 0; |
50 |
| - TimedEvaluator runner = new TimedEvaluator (); |
51 |
| - |
52 |
| - public int WaitTime { |
53 |
| - get { return runner.RunTimeout; } |
54 |
| - set { runner.RunTimeout = value; } |
55 |
| - } |
56 |
| - |
57 |
| - public bool IsEvaluating { |
58 |
| - get { return runner.IsEvaluating; } |
59 |
| - } |
60 |
| - |
61 |
| - public ObjectValue Run (string name, ObjectValueFlags flags, ObjectEvaluatorDelegate evaluator) |
62 |
| - { |
63 |
| - string id; |
64 |
| - int tid; |
65 |
| - lock (asyncCallbacks) { |
66 |
| - tid = asyncCounter++; |
67 |
| - id = tid.ToString (); |
68 |
| - } |
69 |
| - |
70 |
| - ObjectValue val = null; |
71 |
| - bool done = runner.Run (delegate { |
72 |
| - if (tid >= cancelTimestamp) |
73 |
| - val = evaluator (); |
74 |
| - }, |
75 |
| - delegate { |
76 |
| - if (tid >= cancelTimestamp) |
77 |
| - OnEvaluationDone (id, val); |
78 |
| - }); |
79 |
| - |
80 |
| - if (done) { |
81 |
| - // 'val' may be null if the timed evaluator is disposed while evaluating |
82 |
| - return val ?? ObjectValue.CreateUnknown (name); |
83 |
| - } |
84 |
| - |
85 |
| - return ObjectValue.CreateEvaluating (this, new ObjectPath (id, name), flags); |
86 |
| - } |
87 |
| - |
88 |
| - public void Dispose () |
89 |
| - { |
90 |
| - runner.Dispose (); |
91 |
| - } |
92 |
| - |
93 |
| - |
94 |
| - public void Stop () |
95 |
| - { |
96 |
| - lock (asyncCallbacks) { |
97 |
| - cancelTimestamp = asyncCounter; |
98 |
| - runner.CancelAll (); |
99 |
| - foreach (var cb in asyncCallbacks.Values) { |
100 |
| - try { |
101 |
| - cb.UpdateValue (ObjectValue.CreateFatalError ("", "Canceled", ObjectValueFlags.None)); |
102 |
| - } catch { |
103 |
| - } |
104 |
| - } |
105 |
| - asyncCallbacks.Clear (); |
106 |
| - asyncResults.Clear (); |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - public void WaitForStopped () |
111 |
| - { |
112 |
| - runner.WaitForStopped (); |
113 |
| - } |
114 |
| - |
115 |
| - void OnEvaluationDone (string id, ObjectValue val) |
116 |
| - { |
117 |
| - if (val == null) |
118 |
| - val = ObjectValue.CreateUnknown (null); |
119 |
| - UpdateCallback cb = null; |
120 |
| - lock (asyncCallbacks) { |
121 |
| - if (asyncCallbacks.TryGetValue (id, out cb)) { |
122 |
| - try { |
123 |
| - cb.UpdateValue (val); |
124 |
| - } catch {} |
125 |
| - asyncCallbacks.Remove (id); |
126 |
| - } |
127 |
| - else |
128 |
| - asyncResults [id] = val; |
129 |
| - } |
130 |
| - } |
131 |
| - |
132 |
| - void IObjectValueUpdater.RegisterUpdateCallbacks (UpdateCallback[] callbacks) |
133 |
| - { |
134 |
| - foreach (UpdateCallback c in callbacks) { |
135 |
| - lock (asyncCallbacks) { |
136 |
| - ObjectValue val; |
137 |
| - string id = c.Path[0]; |
138 |
| - if (asyncResults.TryGetValue (id, out val)) { |
139 |
| - c.UpdateValue (val); |
140 |
| - asyncResults.Remove (id); |
141 |
| - } else { |
142 |
| - asyncCallbacks [id] = c; |
143 |
| - } |
144 |
| - } |
145 |
| - } |
146 |
| - } |
147 |
| - } |
148 |
| -} |
| 1 | +// AsyncEvaluationTracker.cs |
| 2 | +// |
| 3 | +// Author: |
| 4 | +// Lluis Sanchez Gual <[email protected]> |
| 5 | +// |
| 6 | +// Copyright (c) 2008 Novell, Inc (http://www.novell.com) |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | +// |
| 26 | +// |
| 27 | + |
| 28 | +using System; |
| 29 | +using System.Collections.Generic; |
| 30 | +using Mono.Debugging.Client; |
| 31 | +using Mono.Debugging.Backend; |
| 32 | + |
| 33 | +namespace Mono.Debugging.Evaluation |
| 34 | +{ |
| 35 | + public delegate ObjectValue ObjectEvaluatorDelegate (); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// This class can be used to generate an ObjectValue using a provided evaluation delegate. |
| 39 | + /// The value is initialy evaluated synchronously (blocking the caller). If no result |
| 40 | + /// is obtained after a short period (provided in the WaitTime property), evaluation |
| 41 | + /// will then be made asynchronous and the Run method will immediately return an ObjectValue |
| 42 | + /// with the Evaluating state. |
| 43 | + /// </summary> |
| 44 | + public class AsyncEvaluationTracker: RemoteFrameObject, IObjectValueUpdater, IDisposable |
| 45 | + { |
| 46 | + Dictionary<string, UpdateCallback> asyncCallbacks = new Dictionary<string, UpdateCallback> (); |
| 47 | + Dictionary<string, ObjectValue> asyncResults = new Dictionary<string, ObjectValue> (); |
| 48 | + int asyncCounter = 0; |
| 49 | + int cancelTimestamp = 0; |
| 50 | + TimedEvaluator runner = new TimedEvaluator (); |
| 51 | + |
| 52 | + public int WaitTime { |
| 53 | + get { return runner.RunTimeout; } |
| 54 | + set { runner.RunTimeout = value; } |
| 55 | + } |
| 56 | + |
| 57 | + public bool IsEvaluating { |
| 58 | + get { return runner.IsEvaluating; } |
| 59 | + } |
| 60 | + |
| 61 | + public ObjectValue Run (string name, ObjectValueFlags flags, ObjectEvaluatorDelegate evaluator) |
| 62 | + { |
| 63 | + string id; |
| 64 | + int tid; |
| 65 | + lock (asyncCallbacks) { |
| 66 | + tid = asyncCounter++; |
| 67 | + id = tid.ToString (); |
| 68 | + } |
| 69 | + |
| 70 | + ObjectValue val = null; |
| 71 | + bool done = runner.Run (delegate { |
| 72 | + if (tid >= cancelTimestamp) |
| 73 | + val = evaluator (); |
| 74 | + }, |
| 75 | + delegate { |
| 76 | + if (tid >= cancelTimestamp) |
| 77 | + OnEvaluationDone (id, val); |
| 78 | + }); |
| 79 | + |
| 80 | + if (done) { |
| 81 | + // 'val' may be null if the timed evaluator is disposed while evaluating |
| 82 | + return val ?? ObjectValue.CreateUnknown (name); |
| 83 | + } |
| 84 | + |
| 85 | + return ObjectValue.CreateEvaluating (this, new ObjectPath (id, name), flags); |
| 86 | + } |
| 87 | + |
| 88 | + public void Dispose () |
| 89 | + { |
| 90 | + runner.Dispose (); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public void Stop () |
| 95 | + { |
| 96 | + lock (asyncCallbacks) { |
| 97 | + cancelTimestamp = asyncCounter; |
| 98 | + runner.CancelAll (); |
| 99 | + foreach (var cb in asyncCallbacks.Values) { |
| 100 | + try { |
| 101 | + cb.UpdateValue (ObjectValue.CreateFatalError ("", "Canceled", ObjectValueFlags.None)); |
| 102 | + } catch { |
| 103 | + } |
| 104 | + } |
| 105 | + asyncCallbacks.Clear (); |
| 106 | + asyncResults.Clear (); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void WaitForStopped () |
| 111 | + { |
| 112 | + runner.WaitForStopped (); |
| 113 | + } |
| 114 | + |
| 115 | + void OnEvaluationDone (string id, ObjectValue val) |
| 116 | + { |
| 117 | + if (val == null) |
| 118 | + val = ObjectValue.CreateUnknown (null); |
| 119 | + UpdateCallback cb = null; |
| 120 | + lock (asyncCallbacks) { |
| 121 | + if (asyncCallbacks.TryGetValue (id, out cb)) { |
| 122 | + try { |
| 123 | + cb.UpdateValue (val); |
| 124 | + } catch {} |
| 125 | + asyncCallbacks.Remove (id); |
| 126 | + } |
| 127 | + else |
| 128 | + asyncResults [id] = val; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + void IObjectValueUpdater.RegisterUpdateCallbacks (UpdateCallback[] callbacks) |
| 133 | + { |
| 134 | + foreach (UpdateCallback c in callbacks) { |
| 135 | + lock (asyncCallbacks) { |
| 136 | + ObjectValue val; |
| 137 | + string id = c.Path[0]; |
| 138 | + if (asyncResults.TryGetValue (id, out val)) { |
| 139 | + c.UpdateValue (val); |
| 140 | + asyncResults.Remove (id); |
| 141 | + } else { |
| 142 | + asyncCallbacks [id] = c; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments