| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Runtime |
| | | 9 | | { |
| | | 10 | | // A simple synchronized pool would simply lock a stack and push/pop on return/take. |
| | | 11 | | // |
| | | 12 | | // This implementation tries to reduce locking by exploiting the case where an item |
| | | 13 | | // is taken and returned by the same thread, which turns out to be common in our |
| | | 14 | | // scenarios. |
| | | 15 | | // |
| | | 16 | | // Initially, all the quota is allocated to a global (non-thread-specific) pool, |
| | | 17 | | // which takes locks. As different threads take and return values, we record their IDs, |
| | | 18 | | // and if we detect that a thread is taking and returning "enough" on the same thread, |
| | | 19 | | // then we decide to "promote" the thread. When a thread is promoted, we decrease the |
| | | 20 | | // quota of the global pool by one, and allocate a thread-specific entry for the thread |
| | | 21 | | // to store it's value. Once this entry is allocated, the thread can take and return |
| | | 22 | | // it's value from that entry without taking any locks. Not only does this avoid |
| | | 23 | | // locks, but it affinitizes pooled items to a particular thread. |
| | | 24 | | // |
| | | 25 | | // There are a couple of additional things worth noting: |
| | | 26 | | // |
| | | 27 | | // It is possible for a thread that we have reserved an entry for to exit. This means |
| | | 28 | | // we will still have a entry allocated for it, but the pooled item stored there |
| | | 29 | | // will never be used. After a while, we could end up with a number of these, and |
| | | 30 | | // as a result we would begin to exhaust the quota of the overall pool. To mitigate this |
| | | 31 | | // case, we throw away the entire per-thread pool, and return all the quota back to |
| | | 32 | | // the global pool if we are unable to promote a thread (due to lack of space). Then |
| | | 33 | | // the set of active threads will be re-promoted as they take and return items. |
| | | 34 | | // |
| | | 35 | | // You may notice that the code does not immediately promote a thread, and does not |
| | | 36 | | // immediately throw away the entire per-thread pool when it is unable to promote a |
| | | 37 | | // thread. Instead, it uses counters (based on the number of calls to the pool) |
| | | 38 | | // and a threshold to figure out when to do these operations. In the case where the |
| | | 39 | | // pool to misconfigured to have too few items for the workload, this avoids constant |
| | | 40 | | // promoting and rebuilding of the per thread entries. |
| | | 41 | | // |
| | | 42 | | // You may also notice that we do not use interlocked methods when adjusting statistics. |
| | | 43 | | // Since the statistics are a heuristic as to how often something is happening, they |
| | | 44 | | // do not need to be perfect. |
| | | 45 | | // |
| | | 46 | | internal class SynchronizedPool<T> where T : class |
| | | 47 | | { |
| | | 48 | | private const int maxPendingEntries = 128; |
| | | 49 | | private const int maxPromotionFailures = 64; |
| | | 50 | | private const int maxReturnsBeforePromotion = 64; |
| | | 51 | | private const int maxThreadItemsPerProcessor = 16; |
| | | 52 | | private Entry[] _entries; |
| | | 53 | | private readonly GlobalPool _globalPool; |
| | | 54 | | private readonly int _maxCount; |
| | | 55 | | private PendingEntry[] _pending; |
| | | 56 | | private int _promotionFailures; |
| | | 57 | | |
| | 7189 | 58 | | public SynchronizedPool(int maxCount) |
| | | 59 | | { |
| | 7189 | 60 | | int threadCount = maxCount; |
| | 7189 | 61 | | int maxThreadCount = maxThreadItemsPerProcessor + SynchronizedPoolHelper.ProcessorCount; |
| | 7189 | 62 | | if (threadCount > maxThreadCount) |
| | | 63 | | { |
| | 1165 | 64 | | threadCount = maxThreadCount; |
| | | 65 | | } |
| | 7189 | 66 | | _maxCount = maxCount; |
| | 7189 | 67 | | _entries = new Entry[threadCount]; |
| | 7189 | 68 | | _pending = new PendingEntry[4]; |
| | 7189 | 69 | | _globalPool = new GlobalPool(maxCount); |
| | 7189 | 70 | | } |
| | | 71 | | |
| | | 72 | | private object ThisLock |
| | | 73 | | { |
| | | 74 | | get |
| | | 75 | | { |
| | 1 | 76 | | return this; |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public void Clear() |
| | | 81 | | { |
| | 0 | 82 | | Entry[] entries = _entries; |
| | | 83 | | |
| | 0 | 84 | | for (int i = 0; i < entries.Length; i++) |
| | | 85 | | { |
| | 0 | 86 | | entries[i].value = null; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | _globalPool.Clear(); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | private void HandlePromotionFailure(int thisThreadID) |
| | | 93 | | { |
| | 0 | 94 | | int newPromotionFailures = _promotionFailures + 1; |
| | | 95 | | |
| | 0 | 96 | | if (newPromotionFailures >= maxPromotionFailures) |
| | | 97 | | { |
| | 0 | 98 | | lock (ThisLock) |
| | | 99 | | { |
| | 0 | 100 | | _entries = new Entry[_entries.Length]; |
| | | 101 | | |
| | 0 | 102 | | _globalPool.MaxCount = _maxCount; |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | PromoteThread(thisThreadID); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 0 | 109 | | _promotionFailures = newPromotionFailures; |
| | | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | private bool PromoteThread(int thisThreadID) |
| | | 114 | | { |
| | 1 | 115 | | lock (ThisLock) |
| | | 116 | | { |
| | 2 | 117 | | for (int i = 0; i < _entries.Length; i++) |
| | | 118 | | { |
| | 1 | 119 | | int threadID = _entries[i].threadID; |
| | | 120 | | |
| | 1 | 121 | | if (threadID == thisThreadID) |
| | | 122 | | { |
| | 0 | 123 | | return true; |
| | | 124 | | } |
| | 1 | 125 | | else if (threadID == 0) |
| | | 126 | | { |
| | 1 | 127 | | _globalPool.DecrementMaxCount(); |
| | 1 | 128 | | _entries[i].threadID = thisThreadID; |
| | 1 | 129 | | return true; |
| | | 130 | | } |
| | | 131 | | } |
| | 0 | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | return false; |
| | 1 | 135 | | } |
| | | 136 | | |
| | | 137 | | private void RecordReturnToGlobalPool(int thisThreadID) |
| | | 138 | | { |
| | 2980 | 139 | | PendingEntry[] localPending = _pending; |
| | | 140 | | |
| | 10452 | 141 | | for (int i = 0; i < localPending.Length; i++) |
| | | 142 | | { |
| | 5211 | 143 | | int threadID = localPending[i].threadID; |
| | | 144 | | |
| | 5211 | 145 | | if (threadID == thisThreadID) |
| | | 146 | | { |
| | 2767 | 147 | | int newReturnCount = localPending[i].returnCount + 1; |
| | | 148 | | |
| | 2767 | 149 | | if (newReturnCount >= maxReturnsBeforePromotion) |
| | | 150 | | { |
| | 1 | 151 | | localPending[i].returnCount = 0; |
| | | 152 | | |
| | 1 | 153 | | if (!PromoteThread(thisThreadID)) |
| | | 154 | | { |
| | 0 | 155 | | HandlePromotionFailure(thisThreadID); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 2766 | 160 | | localPending[i].returnCount = newReturnCount; |
| | | 161 | | } |
| | 2766 | 162 | | break; |
| | | 163 | | } |
| | 2444 | 164 | | else if (threadID == 0) |
| | | 165 | | { |
| | | 166 | | break; |
| | | 167 | | } |
| | | 168 | | } |
| | 214 | 169 | | } |
| | | 170 | | |
| | | 171 | | private void RecordTakeFromGlobalPool(int thisThreadID) |
| | | 172 | | { |
| | 7638 | 173 | | PendingEntry[] localPending = _pending; |
| | | 174 | | |
| | 20504 | 175 | | for (int i = 0; i < localPending.Length; i++) |
| | | 176 | | { |
| | 10221 | 177 | | int threadID = localPending[i].threadID; |
| | | 178 | | |
| | 10221 | 179 | | if (threadID == thisThreadID) |
| | | 180 | | { |
| | 4496 | 181 | | return; |
| | | 182 | | } |
| | 5725 | 183 | | else if (threadID == 0) |
| | | 184 | | { |
| | 3113 | 185 | | lock (localPending) |
| | | 186 | | { |
| | 3113 | 187 | | if (localPending[i].threadID == 0) |
| | | 188 | | { |
| | 3111 | 189 | | localPending[i].threadID = thisThreadID; |
| | 3111 | 190 | | return; |
| | | 191 | | } |
| | 2 | 192 | | } |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | 31 | 196 | | if (localPending.Length >= maxPendingEntries) |
| | | 197 | | { |
| | 0 | 198 | | _pending = new PendingEntry[localPending.Length]; |
| | | 199 | | } |
| | | 200 | | else |
| | | 201 | | { |
| | 31 | 202 | | PendingEntry[] newPending = new PendingEntry[localPending.Length * 2]; |
| | 31 | 203 | | Array.Copy(localPending, newPending, localPending.Length); |
| | 31 | 204 | | _pending = newPending; |
| | | 205 | | } |
| | 3142 | 206 | | } |
| | | 207 | | |
| | | 208 | | public bool Return(T value) |
| | | 209 | | { |
| | 2980 | 210 | | int thisThreadID = Thread.CurrentThread.ManagedThreadId; |
| | | 211 | | |
| | 2980 | 212 | | if (thisThreadID == 0) |
| | | 213 | | { |
| | 0 | 214 | | return false; |
| | | 215 | | } |
| | | 216 | | |
| | 2980 | 217 | | if (ReturnToPerThreadPool(thisThreadID, value)) |
| | | 218 | | { |
| | 0 | 219 | | return true; |
| | | 220 | | } |
| | | 221 | | |
| | 2980 | 222 | | return ReturnToGlobalPool(thisThreadID, value); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | private bool ReturnToPerThreadPool(int thisThreadID, T value) |
| | | 226 | | { |
| | 2980 | 227 | | Entry[] entries = _entries; |
| | | 228 | | |
| | 5960 | 229 | | for (int i = 0; i < entries.Length; i++) |
| | | 230 | | { |
| | 2979 | 231 | | int threadID = entries[i].threadID; |
| | | 232 | | |
| | 2979 | 233 | | if (threadID == thisThreadID) |
| | | 234 | | { |
| | 0 | 235 | | if (entries[i].value == null) |
| | | 236 | | { |
| | 0 | 237 | | entries[i].value = value; |
| | 0 | 238 | | return true; |
| | | 239 | | } |
| | | 240 | | else |
| | | 241 | | { |
| | 0 | 242 | | return false; |
| | | 243 | | } |
| | | 244 | | } |
| | 2979 | 245 | | else if (threadID == 0) |
| | | 246 | | { |
| | | 247 | | break; |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | |
| | 2980 | 251 | | return false; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private bool ReturnToGlobalPool(int thisThreadID, T value) |
| | | 255 | | { |
| | 2980 | 256 | | RecordReturnToGlobalPool(thisThreadID); |
| | | 257 | | |
| | 2980 | 258 | | return _globalPool.Return(value); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public T Take() |
| | | 262 | | { |
| | 7638 | 263 | | int thisThreadID = Thread.CurrentThread.ManagedThreadId; |
| | | 264 | | |
| | 7638 | 265 | | if (thisThreadID == 0) |
| | | 266 | | { |
| | 0 | 267 | | return null; |
| | | 268 | | } |
| | | 269 | | |
| | 7638 | 270 | | T value = TakeFromPerThreadPool(thisThreadID); |
| | | 271 | | |
| | 7638 | 272 | | if (value != null) |
| | | 273 | | { |
| | 0 | 274 | | return value; |
| | | 275 | | } |
| | | 276 | | |
| | 7638 | 277 | | return TakeFromGlobalPool(thisThreadID); |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | private T TakeFromPerThreadPool(int thisThreadID) |
| | | 281 | | { |
| | 7638 | 282 | | Entry[] entries = _entries; |
| | | 283 | | |
| | 15278 | 284 | | for (int i = 0; i < entries.Length; i++) |
| | | 285 | | { |
| | 7638 | 286 | | int threadID = entries[i].threadID; |
| | | 287 | | |
| | 7638 | 288 | | if (threadID == thisThreadID) |
| | | 289 | | { |
| | 0 | 290 | | T value = entries[i].value; |
| | | 291 | | |
| | 0 | 292 | | if (value != null) |
| | | 293 | | { |
| | 0 | 294 | | entries[i].value = null; |
| | 0 | 295 | | return value; |
| | | 296 | | } |
| | | 297 | | else |
| | | 298 | | { |
| | 0 | 299 | | return null; |
| | | 300 | | } |
| | | 301 | | } |
| | 7638 | 302 | | else if (threadID == 0) |
| | | 303 | | { |
| | | 304 | | break; |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | 7638 | 308 | | return null; |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | private T TakeFromGlobalPool(int thisThreadID) |
| | | 312 | | { |
| | 7638 | 313 | | RecordTakeFromGlobalPool(thisThreadID); |
| | | 314 | | |
| | 7638 | 315 | | return _globalPool.Take(); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | private struct Entry |
| | | 319 | | { |
| | | 320 | | public int threadID; |
| | | 321 | | public T value; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | private struct PendingEntry |
| | | 325 | | { |
| | | 326 | | public int returnCount; |
| | | 327 | | public int threadID; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private static class SynchronizedPoolHelper |
| | | 331 | | { |
| | 35 | 332 | | public static readonly int ProcessorCount = GetProcessorCount(); |
| | | 333 | | |
| | | 334 | | private static int GetProcessorCount() |
| | | 335 | | { |
| | 35 | 336 | | return Environment.ProcessorCount; |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private class GlobalPool |
| | | 341 | | { |
| | | 342 | | private readonly Stack<T> _items; |
| | | 343 | | private int _maxCount; |
| | | 344 | | |
| | 7189 | 345 | | public GlobalPool(int maxCount) |
| | | 346 | | { |
| | 7189 | 347 | | _items = new Stack<T>(); |
| | 7189 | 348 | | _maxCount = maxCount; |
| | 7189 | 349 | | } |
| | | 350 | | |
| | | 351 | | public int MaxCount |
| | | 352 | | { |
| | | 353 | | get |
| | | 354 | | { |
| | 5708 | 355 | | return _maxCount; |
| | | 356 | | } |
| | | 357 | | set |
| | | 358 | | { |
| | 0 | 359 | | lock (ThisLock) |
| | | 360 | | { |
| | 0 | 361 | | while (_items.Count > value) |
| | | 362 | | { |
| | 0 | 363 | | _items.Pop(); |
| | | 364 | | } |
| | 0 | 365 | | _maxCount = value; |
| | 0 | 366 | | } |
| | 0 | 367 | | } |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | private object ThisLock |
| | | 371 | | { |
| | | 372 | | get |
| | | 373 | | { |
| | 3845 | 374 | | return this; |
| | | 375 | | } |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | public void DecrementMaxCount() |
| | | 379 | | { |
| | 1 | 380 | | lock (ThisLock) |
| | | 381 | | { |
| | 1 | 382 | | if (_items.Count == _maxCount) |
| | | 383 | | { |
| | 0 | 384 | | _items.Pop(); |
| | | 385 | | } |
| | 1 | 386 | | _maxCount--; |
| | 1 | 387 | | } |
| | 1 | 388 | | } |
| | | 389 | | |
| | | 390 | | public T Take() |
| | | 391 | | { |
| | 7638 | 392 | | if (_items.Count > 0) |
| | | 393 | | { |
| | 1116 | 394 | | lock (ThisLock) |
| | | 395 | | { |
| | 1116 | 396 | | if (_items.Count > 0) |
| | | 397 | | { |
| | 1116 | 398 | | return _items.Pop(); |
| | | 399 | | } |
| | 0 | 400 | | } |
| | | 401 | | } |
| | 6522 | 402 | | return null; |
| | 1116 | 403 | | } |
| | | 404 | | |
| | | 405 | | public bool Return(T value) |
| | | 406 | | { |
| | 2980 | 407 | | if (_items.Count < MaxCount) |
| | | 408 | | { |
| | 2728 | 409 | | lock (ThisLock) |
| | | 410 | | { |
| | 2728 | 411 | | if (_items.Count < MaxCount) |
| | | 412 | | { |
| | 2728 | 413 | | _items.Push(value); |
| | 2728 | 414 | | return true; |
| | | 415 | | } |
| | 0 | 416 | | } |
| | | 417 | | } |
| | 252 | 418 | | return false; |
| | 2728 | 419 | | } |
| | | 420 | | |
| | | 421 | | public void Clear() |
| | | 422 | | { |
| | 0 | 423 | | lock (ThisLock) |
| | | 424 | | { |
| | 0 | 425 | | _items.Clear(); |
| | 0 | 426 | | } |
| | 0 | 427 | | } |
| | | 428 | | } |
| | | 429 | | } |
| | | 430 | | } |