corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) 2006-2014, openmetaverse.org |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * - Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * |
||
8 | * - Redistributions of source code must retain the above copyright notice, this |
||
9 | * list of conditions and the following disclaimer. |
||
10 | * - Neither the name of the openmetaverse.org nor the names |
||
11 | * of its contributors may be used to endorse or promote products derived from |
||
12 | * this software without specific prior written permission. |
||
13 | * |
||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
24 | * POSSIBILITY OF SUCH DAMAGE. |
||
25 | */ |
||
26 | |||
27 | using System; |
||
28 | using System.Collections.Generic; |
||
29 | using System.Threading; |
||
30 | using OpenMetaverse; |
||
31 | |||
32 | namespace OpenMetaverse |
||
33 | { |
||
34 | /// <summary> |
||
35 | /// Same as Queue except Dequeue function blocks until there is an object to return. |
||
36 | /// Note: This class does not need to be synchronized |
||
37 | /// </summary> |
||
38 | public class BlockingQueue<T> : Queue<T> |
||
39 | { |
||
40 | private object SyncRoot; |
||
41 | private bool open; |
||
42 | |||
43 | /// <summary> |
||
44 | /// Create new BlockingQueue. |
||
45 | /// </summary> |
||
46 | /// <param name="col">The System.Collections.ICollection to copy elements from</param> |
||
47 | public BlockingQueue(IEnumerable<T> col) |
||
48 | : base(col) |
||
49 | { |
||
50 | SyncRoot = new object(); |
||
51 | open = true; |
||
52 | } |
||
53 | |||
54 | /// <summary> |
||
55 | /// Create new BlockingQueue. |
||
56 | /// </summary> |
||
57 | /// <param name="capacity">The initial number of elements that the queue can contain</param> |
||
58 | public BlockingQueue(int capacity) |
||
59 | : base(capacity) |
||
60 | { |
||
61 | SyncRoot = new object(); |
||
62 | open = true; |
||
63 | } |
||
64 | |||
65 | /// <summary> |
||
66 | /// Create new BlockingQueue. |
||
67 | /// </summary> |
||
68 | public BlockingQueue() |
||
69 | : base() |
||
70 | { |
||
71 | SyncRoot = new object(); |
||
72 | open = true; |
||
73 | } |
||
74 | |||
75 | /// <summary> |
||
76 | /// BlockingQueue Destructor (Close queue, resume any waiting thread). |
||
77 | /// </summary> |
||
78 | ~BlockingQueue() |
||
79 | { |
||
80 | Close(); |
||
81 | } |
||
82 | |||
83 | /// <summary> |
||
84 | /// Remove all objects from the Queue. |
||
85 | /// </summary> |
||
86 | public new void Clear() |
||
87 | { |
||
88 | lock (SyncRoot) |
||
89 | { |
||
90 | base.Clear(); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /// <summary> |
||
95 | /// Remove all objects from the Queue, resume all dequeue threads. |
||
96 | /// </summary> |
||
97 | public void Close() |
||
98 | { |
||
99 | lock (SyncRoot) |
||
100 | { |
||
101 | open = false; |
||
102 | base.Clear(); |
||
103 | Monitor.PulseAll(SyncRoot); // resume any waiting threads |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /// <summary> |
||
108 | /// Removes and returns the object at the beginning of the Queue. |
||
109 | /// </summary> |
||
110 | /// <returns>Object in queue.</returns> |
||
111 | public new T Dequeue() |
||
112 | { |
||
113 | return Dequeue(Timeout.Infinite); |
||
114 | } |
||
115 | |||
116 | /// <summary> |
||
117 | /// Removes and returns the object at the beginning of the Queue. |
||
118 | /// </summary> |
||
119 | /// <param name="timeout">time to wait before returning</param> |
||
120 | /// <returns>Object in queue.</returns> |
||
121 | public T Dequeue(TimeSpan timeout) |
||
122 | { |
||
123 | return Dequeue(timeout.Milliseconds); |
||
124 | } |
||
125 | |||
126 | /// <summary> |
||
127 | /// Removes and returns the object at the beginning of the Queue. |
||
128 | /// </summary> |
||
129 | /// <param name="timeout">time to wait before returning (in milliseconds)</param> |
||
130 | /// <returns>Object in queue.</returns> |
||
131 | public T Dequeue(int timeout) |
||
132 | { |
||
133 | lock (SyncRoot) |
||
134 | { |
||
135 | while (open && (base.Count == 0)) |
||
136 | { |
||
137 | if (!Monitor.Wait(SyncRoot, timeout)) |
||
138 | throw new InvalidOperationException("Timeout"); |
||
139 | } |
||
140 | if (open) |
||
141 | return base.Dequeue(); |
||
142 | else |
||
143 | throw new InvalidOperationException("Queue Closed"); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | public bool Dequeue(int timeout, ref T obj) |
||
148 | { |
||
149 | lock (SyncRoot) |
||
150 | { |
||
151 | while (open && (base.Count == 0)) |
||
152 | { |
||
153 | if (!Monitor.Wait(SyncRoot, timeout)) |
||
154 | return false; |
||
155 | } |
||
156 | if (open) |
||
157 | { |
||
158 | obj = base.Dequeue(); |
||
159 | return true; |
||
160 | } |
||
161 | else |
||
162 | { |
||
163 | obj = default(T); |
||
164 | return false; |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /// <summary> |
||
170 | /// Adds an object to the end of the Queue |
||
171 | /// </summary> |
||
172 | /// <param name="obj">Object to put in queue</param> |
||
173 | public new void Enqueue(T obj) |
||
174 | { |
||
175 | lock (SyncRoot) |
||
176 | { |
||
177 | base.Enqueue(obj); |
||
178 | Monitor.Pulse(SyncRoot); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /// <summary> |
||
183 | /// Open Queue. |
||
184 | /// </summary> |
||
185 | public void Open() |
||
186 | { |
||
187 | lock (SyncRoot) |
||
188 | { |
||
189 | open = true; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /// <summary> |
||
194 | /// Gets flag indicating if queue has been closed. |
||
195 | /// </summary> |
||
196 | public bool Closed |
||
197 | { |
||
198 | get { return !open; } |
||
199 | } |
||
200 | } |
||
201 | } |