summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Common/Collections/SynchronousQueue.cs
blob: 3c12df60672b8e27c1b7cd3b4b0ac9056846163e (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
using System;
using System.Threading;

namespace Apache.Qpid.Collections
{
    public class SynchronousQueue : BlockingQueue
    {
        /// <summary>
        /// Lock protecting both wait queues
        /// </summary> 
//        private readonly object _qlock = new object();
        
        /// <summary>
        /// Queue holding waiting puts
        /// </summary> 
//        private readonly WaitQueue _waitingProducers;
        
        /// <summary>
        /// Queue holding waiting takes
        /// </summary> 
//        private readonly WaitQueue _waitingConsumers;
        
        /**
         * Queue to hold waiting puts/takes; specialized to Fifo/Lifo below.
         * These queues have all transient fields, but are serializable
         * in order to recover fairness settings when deserialized.
         */
        internal abstract class WaitQueue
        {
            /** Creates, adds, and returns node for x. */
            internal abstract Node Enq(Object x);
            /** Removes and returns node, or null if empty. */
            internal abstract Node Deq();
            /** Removes a cancelled node to avoid garbage retention. */
            internal abstract void Unlink(Node node);
            /** Returns true if a cancelled node might be on queue. */
            internal abstract bool ShouldUnlink(Node node);
        }

        /**
         * FIFO queue to hold waiting puts/takes.
         */
        sealed class FifoWaitQueue : WaitQueue
        {            
            private Node head;
            private Node last;

            internal override Node Enq(Object x)
            {
                Node p = new Node(x);
                if (last == null)
                {
                    last = head = p;
                }
                else
                {
                    last = last.next = p;
                }
                return p;
            }

            internal override Node Deq()
            {
                Node p = head;
                if (p != null)
                {
                    if ((head = p.next) == null)
                    {
                        last = null;
                    }
                    p.next = null;
                }
                return p;
            }

            internal override bool ShouldUnlink(Node node)
            {
                return (node == last || node.next != null);
            }

            internal override void Unlink(Node node)
            {
                Node p = head;
                Node trail = null;
                while (p != null)
                {
                    if (p == node)
                    {
                        Node next = p.next;
                        if (trail == null)
                        {
                            head = next;
                        }
                        else
                        {
                            trail.next = next;
                        }
                        if (last == node)
                        {
                            last = trail;
                        }
                        break;
                    }
                    trail = p;
                    p = p.next;
                }
            }
        }

        /**
         * LIFO queue to hold waiting puts/takes.
         */
        sealed class LifoWaitQueue : WaitQueue
        {        
            private Node head;

            internal override Node Enq(Object x)
            {
                return head = new Node(x, head);
            }

            internal override Node Deq()
            {
                Node p = head;
                if (p != null)
                {
                    head = p.next;
                    p.next = null;
                }
                return p;
            }

            internal override bool ShouldUnlink(Node node)
            {
                // Return false if already dequeued or is bottom node (in which
                // case we might retain at most one garbage node)
                return (node == head || node.next != null);
            }

            internal override void Unlink(Node node)
            {
                Node p = head;
                Node trail = null;
                while (p != null)
                {
                    if (p == node)
                    {
                        Node next = p.next;
                        if (trail == null)
                            head = next;
                        else
                            trail.next = next;
                        break;
                    }
                    trail = p;
                    p = p.next;
                }
            }
        }

        /**
         * Nodes each maintain an item and handle waits and signals for
         * getting and setting it. The class extends
         * AbstractQueuedSynchronizer to manage blocking, using AQS state
         *  0 for waiting, 1 for ack, -1 for cancelled.
         */
        sealed internal class Node
        {            

            /** Synchronization state value representing that node acked */
            private const int ACK    =  1;
            /** Synchronization state value representing that node cancelled */
            private const int CANCEL = -1;

            internal int state = 0;

            /** The item being transferred */
            internal Object item;
            /** Next node in wait queue */
            internal Node next;

            /** Creates a node with initial item */
            internal Node(Object x)
            {
                item = x;
            }

            /** Creates a node with initial item and next */
            internal Node(Object x, Node n)
            {
                item = x;
                next = n;
            }

            /**
             * Takes item and nulls out field (for sake of GC)
             *
             * PRE: lock owned
             */
            private Object Extract()
            {
                Object x = item;
                item = null;
                return x;
            }

            /**
             * Tries to cancel on interrupt; if so rethrowing,
             * else setting interrupt state
             *
             * PRE: lock owned
             */
            /*private void checkCancellationOnInterrupt(InterruptedException ie)
                throws InterruptedException
            {
                if (state == 0) {
                    state = CANCEL;
                    notify();
                    throw ie;
                }
                Thread.currentThread().interrupt();
            }*/

            /**
             * Fills in the slot created by the consumer and signal consumer to
             * continue.
             */
            internal bool SetItem(Object x)            
            {
                lock (this)
                {
                    if (state != 0) return false;
                    item = x;
                    state = ACK;                    
                    Monitor.Pulse(this);
                    return true;
                }
            }

            /**
             * Removes item from slot created by producer and signal producer
             * to continue.
             */
            internal Object GetItem()
            {
                if (state != 0) return null;
                state = ACK;
                Monitor.Pulse(this);
                return Extract();
            }

            /**
             * Waits for a consumer to take item placed by producer.
             */
            internal void WaitForTake() //throws InterruptedException {
            {                
                while (state == 0)
                {
                    Monitor.Wait(this);
                }                
            }

            /**
             * Waits for a producer to put item placed by consumer.
             */
            internal object WaitForPut()
            {
                lock (this)
                {
                    while (state == 0) Monitor.Wait(this);
                } 
                return Extract();
            }

            private bool Attempt(long nanos)
            {
                if (state != 0) return true;
                if (nanos <= 0) {
                    state = CANCEL;
                    Monitor.Pulse(this);
                    return false;
                }
                
                while (true)
                {
                    Monitor.Wait(nanos);
                    //TimeUnit.NANOSECONDS.timedWait(this, nanos);
                    if (state != 0)
                    {
                        return true;
                    }
                        //nanos = deadline - Utils.nanoTime();
                        //if (nanos <= 0)
                    else
                    {
                        state = CANCEL;
                        Monitor.Pulse(this);
                        return false;
                    }
                }
            }

            /**
             * Waits for a consumer to take item placed by producer or time out.
             */
            internal bool WaitForTake(long nanos)
            {                
                return Attempt(nanos);                
            }

            /**
             * Waits for a producer to put item placed by consumer, or time out.
             */
            internal object WaitForPut(long nanos)
            {
                if (!Attempt(nanos))
                {
                    return null;
                }
                else
                {
                    return Extract();
                }
            }
        }

        public SynchronousQueue(bool strict)
        {
            // TODO !!!!
        }
        
        public override bool EnqueueNoThrow(object e)
        {
            throw new NotImplementedException();
        }

        public override void EnqueueBlocking(object e)
        {
            throw new NotImplementedException();
        }        

        public override object DequeueBlocking()
        {
            throw new NotImplementedException();
        }

        public override int RemainingCapacity
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    }
}