1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Threading;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.ExternalEvaluation.Service {
|
---|
29 | internal class BlockingQueue<T> {
|
---|
30 | private readonly Queue<T> queue = new Queue<T>();
|
---|
31 | private readonly int maxSize;
|
---|
32 | private bool closing;
|
---|
33 | public BlockingQueue(int maxSize) { this.maxSize = maxSize; }
|
---|
34 |
|
---|
35 | public void Enqueue(T item) {
|
---|
36 | lock (queue) {
|
---|
37 | while (queue.Count >= maxSize) {
|
---|
38 | Monitor.Wait(queue);
|
---|
39 | }
|
---|
40 | if (closing) throw new QueueClosedException();
|
---|
41 | queue.Enqueue(item);
|
---|
42 | if (queue.Count == 1) {
|
---|
43 | // wake up any blocked dequeue
|
---|
44 | Monitor.PulseAll(queue);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | public T Dequeue() {
|
---|
49 | lock (queue) {
|
---|
50 | while (queue.Count == 0) {
|
---|
51 | Monitor.Wait(queue);
|
---|
52 | }
|
---|
53 | if (closing) throw new QueueClosedException();
|
---|
54 | T item = queue.Dequeue();
|
---|
55 | if (queue.Count == maxSize - 1) {
|
---|
56 | // wake up any blocked enqueue
|
---|
57 | Monitor.PulseAll(queue);
|
---|
58 | }
|
---|
59 | return item;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | public void Close() {
|
---|
63 | lock (queue) {
|
---|
64 | closing = true;
|
---|
65 | Monitor.PulseAll(queue);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | public bool TryDequeue(out T value) {
|
---|
69 | lock (queue) {
|
---|
70 | while (queue.Count == 0) {
|
---|
71 | if (closing) {
|
---|
72 | value = default(T);
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | Monitor.Wait(queue);
|
---|
76 | }
|
---|
77 | value = queue.Dequeue();
|
---|
78 | if (queue.Count == maxSize - 1) {
|
---|
79 | // wake up any blocked enqueue
|
---|
80 | Monitor.PulseAll(queue);
|
---|
81 | }
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void Clear() {
|
---|
87 | lock (queue) {
|
---|
88 | queue.Clear();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | internal class QueueClosedException : Exception {
|
---|
94 | public QueueClosedException() : base() { }
|
---|
95 | public QueueClosedException(string message) : base(message) { }
|
---|
96 | public QueueClosedException(string message, Exception innerException) : base(message, innerException) { }
|
---|
97 | }
|
---|
98 | }
|
---|