forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingCollection.cs
More file actions
132 lines (115 loc) · 4.45 KB
/
Copy pathBlockingCollection.cs
File metadata and controls
132 lines (115 loc) · 4.45 KB
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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed 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.
//
namespace Microsoft.PackageManagement.Internal.Utility.Collections {
using System.Collections;
using System.Collections.Generic;
using System.Threading;
internal class BlockingCollection<T> : System.Collections.Concurrent.BlockingCollection<T>, IEnumerable<T> {
private MutableEnumerable<T> _blockingEnumerable;
private readonly ManualResetEventSlim _activity = new ManualResetEventSlim(false);
private readonly object _lock = new object();
public WaitHandle Ready {
get {
return _activity.WaitHandle;
}
}
public bool HasData {
get {
return Count > 0;
}
}
public IEnumerator<T> GetEnumerator() {
// make sure that iterating on this as enumerable is blocking.
return this.GetBlockingEnumerable().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
protected override void Dispose(bool isDisposing) {
if (isDisposing) {
_blockingEnumerable = null;
}
}
private void SetActivity() {
// setting _activity to true allows consumers to Wait for data to show up or for the producer to complete.
if (HasData || IsCompleted) {
_activity.Set();
} else {
_activity.Reset();
}
}
public new void Add(T item) {
lock (_lock) {
if (!IsAddingCompleted) {
base.Add(item);
}
}
SetActivity();
}
public new void Add(T item, CancellationToken cancellationToken) {
lock (_lock) {
if (!IsAddingCompleted) {
base.Add(item, cancellationToken);
}
}
SetActivity();
}
public new IEnumerable<T> GetConsumingEnumerable() {
return GetConsumingEnumerable(CancellationToken.None);
}
public new IEnumerable<T> GetConsumingEnumerable(CancellationToken cancellationToken) {
T item;
while (!IsCompleted && SafeTryTake(out item, 0, cancellationToken)) {
yield return item;
}
}
private bool SafeTryTake(out T item, int time, CancellationToken cancellationToken) {
try {
if (!cancellationToken.IsCancellationRequested && Count > 0) {
return TryTake(out item, time, cancellationToken);
}
} catch {
// if this throws, that just means that we're done. (ie, canceled)
} finally {
SetActivity();
}
item = default(T);
return false;
}
public IEnumerable<T> GetBlockingEnumerable() {
return GetBlockingEnumerable(CancellationToken.None);
}
public IEnumerable<T> GetBlockingEnumerable(CancellationToken cancellationToken) {
return _blockingEnumerable ?? (_blockingEnumerable = SafeGetBlockingEnumerable(cancellationToken).ReEnumerable());
}
private IEnumerable<T> SafeGetBlockingEnumerable(CancellationToken cancellationToken) {
while (!IsCompleted && !cancellationToken.IsCancellationRequested) {
T item;
if (SafeTryTake(out item, -1, cancellationToken)) {
yield return item;
}
else {
_activity.Wait(10,cancellationToken);
//cancellationToken.WaitHandle.WaitOne(10);
}
}
}
public new void CompleteAdding() {
lock (_lock) {
base.CompleteAdding();
}
SetActivity();
}
}
}