otp.net /Otp/GenericQueue.cs

Language C# Lines 242
MD5 Hash 0614dab5b638ded32e425fa60a38414e Estimated Cost $4,074 (why?)
Repository https://github.com/saleyn/otp.net.git View Raw File View Project SPDX
  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
* 
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
* 
 * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
*/
namespace Otp
{
	/*This class implements a generic FIFO queue. There is no upper
	* bound on the length of the queue, items are linked.
	*/
	using System;
	
	public class GenericQueue
	{
		private const int open = 0;
		private const int closing = 1;
		private const int closed = 2;
		
		private int status = closed;
		private Bucket head;
		private Bucket tail;
		private int count;
		
		private void  init()
		{
			head = null;
			tail = null;
			count = 0;
		}
		
		/*Create an empty queue */
		public GenericQueue()
		{
            init();
            status = open;
        }
		
		/*Clear a queue */
		public virtual void  flush()
		{
            close();
			init();
            status = open;
		}
		
		public virtual void  close()
		{
			status = closing;
            init();
            try
            {
                System.Threading.Monitor.PulseAll(this);
            }
            catch
            { 
            }
            status = closed;
        }
		
		/*Add an object to the tail of the queue.
		* @param o Object to insert in the queue
		*/
		public virtual void  put(System.Object o)
		{
			lock(this)
			{
				Bucket b = new Bucket(this, o);
				
				if (tail != null)
				{
					tail.setNext(b);
					tail = b;
				}
				else
				{
					// queue was empty but has one element now
					head = (tail = b);
				}
				count++;
				
				// notify any waiting tasks
                //UPGRADE_TODO: threading!
				System.Threading.Monitor.PulseAll(this);
			}
		}
		
        private System.Object get(bool once)
        {
			lock(this)
			{
				System.Object o = null;
				
				while ((o = tryGet()) == null && !once && status == open)
				{
					try
					{
						//UPGRADE_TODO: threading!
						System.Threading.Monitor.Wait(this);
					}
					catch (System.Threading.ThreadInterruptedException)
					{
					}
				}
				return o;
			}
        }
		/*Retrieve an object from the head of the queue, or block until
		* one arrives.
		*
		* @return  The object at the head of the queue.
		*/
		public virtual System.Object get()
		{
            return get(false);
		}
		
		/*Retrieve an object from the head of the queue, blocking until
		* one arrives or until timeout occurs.
		*
		* @param  timeout Maximum time to block on queue, in ms. Use 0 to poll the queue. 
		*
		* @exception InterruptedException if the operation times out.
		*
		* @return  The object at the head of the queue, or null if none arrived in time.
		*/
		public virtual System.Object get(long timeout)
		{
            if (timeout == -1)
                return get(false);
            else if (timeout == 0)
                return get(true);

			lock(this)
			{
				if (status != open)
					return null;

				long currentTime = SupportClass.currentTimeMillis();
				long stopTime = currentTime + timeout;
				System.Object o = null;
				
				while (true)
				{
                    if (status != open || (o = tryGet()) != null)
						return o;

					currentTime = SupportClass.currentTimeMillis();
					if (stopTime <= currentTime)
						return null;  // Timeout
					
					try
					{
						//UPGRADE_TODO: threading!
                        System.Threading.Monitor.Wait(this, new TimeSpan((stopTime - currentTime)*TimeSpan.TicksPerMillisecond));
					}
					catch (System.Threading.ThreadInterruptedException)
					{
						// ignore, but really should retry operation instead
					}
				}
			}
		}
		
		// attempt to retrieve message from queue head
		public virtual System.Object tryGet()
		{
			System.Object o = null;
			
			if (head != null)
			{
				o = head.getContents();
				head = head.getNext();
				count--;
				
				if (head == null)
				{
					tail = null;
					count = 0;
				}
			}
			
			return o;
		}

		public virtual int getCount()
		{
			lock(this)
			{
				return count;
			}
		}
		
		/*
		* The Bucket class. The queue is implemented as a linked list
		* of Buckets. The container holds the queued object and a 
		* reference to the next Bucket. */
		internal class Bucket
		{
			private void  InitBlock(GenericQueue enclosingInstance)
			{
				this.enclosingInstance = enclosingInstance;
			}
			private GenericQueue enclosingInstance;
			private Bucket next;
			private System.Object contents;
			
			public Bucket(GenericQueue enclosingInstance, System.Object o)
			{
				InitBlock(enclosingInstance);
				next = null;
				contents = o;
			}
			
			public virtual void  setNext(Bucket newNext)
			{
				next = newNext;
			}
			
			public virtual Bucket getNext()
			{
				return next;
			}
			
			public virtual System.Object getContents()
			{
				return contents;
			}
		}
	}
}
Back to Top