forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentDictionary.cs
More file actions
192 lines (164 loc) · 4.81 KB
/
Copy pathConcurrentDictionary.cs
File metadata and controls
192 lines (164 loc) · 4.81 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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NETFX_CORE // This file should only be included by the NetCore version of the formatting project, but adding a guard here just in case.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http.Internal
{
// TODO: Remove this class after BCL makes their portable library version.
internal sealed class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
private object _lock = new object();
public ICollection<TKey> Keys
{
get
{
throw new NotImplementedException();
}
}
public ICollection<TValue> Values
{
get
{
throw new NotImplementedException();
}
}
public int Count
{
get
{
throw new NotImplementedException();
}
}
public bool IsReadOnly
{
get
{
return ((IDictionary)_dictionary).IsReadOnly;
}
}
public TValue this[TKey key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void Add(TKey key, TValue value)
{
throw new NotImplementedException();
}
public bool ContainsKey(TKey key)
{
lock (_lock)
{
return _dictionary.ContainsKey(key);
}
}
public bool Remove(TKey key)
{
throw new NotImplementedException();
}
public bool TryGetValue(TKey key, out TValue value)
{
lock (_lock)
{
return _dictionary.TryGetValue(key, out value);
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
// ConcurrentDictionary members
public bool TryRemove(TKey key, out TValue removedValue)
{
lock (_lock)
{
if (_dictionary.TryGetValue(key, out removedValue))
{
return _dictionary.Remove(key);
}
return false;
}
}
public TValue GetOrAdd(TKey key, Func<TKey, TValue> addValueFactory)
{
lock (_lock)
{
TValue value;
if (!_dictionary.TryGetValue(key, out value))
{
value = addValueFactory.Invoke(key);
_dictionary.Add(key, value);
}
return value;
}
}
public bool TryAdd(TKey key, TValue value)
{
lock (_lock)
{
if (_dictionary.ContainsKey(key))
{
return false;
}
_dictionary.Add(key, value);
return true;
}
}
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
{
lock (_lock)
{
TValue value;
// update
if (_dictionary.TryGetValue(key, out value))
{
value = updateValueFactory.Invoke(key, value);
_dictionary[key] = value;
return value;
}
// add
_dictionary.Add(key, addValue);
return addValue;
}
}
}
}
#endif