| | | 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.Diagnostics; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Runtime.Versioning; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Runtime |
| | | 12 | | { |
| | | 13 | | internal class IOThreadScheduler |
| | | 14 | | { |
| | | 15 | | // Do not increase the maximum capacity above 32k! It must be a power of two, 0x8000 or less, in order to |
| | | 16 | | // work with the strategy for 'headTail'. |
| | | 17 | | private const int MaximumCapacity = 0x8000; |
| | | 18 | | |
| | | 19 | | private static class Bits |
| | | 20 | | { |
| | | 21 | | public const int HiShift = 32 / 2; |
| | | 22 | | |
| | | 23 | | public const int HiOne = 1 << HiShift; |
| | | 24 | | public const int LoHiBit = HiOne >> 1; |
| | | 25 | | public const int HiHiBit = LoHiBit << HiShift; |
| | | 26 | | public const int LoCountMask = LoHiBit - 1; |
| | | 27 | | public const int HiCountMask = LoCountMask << HiShift; |
| | | 28 | | public const int LoMask = LoCountMask | LoHiBit; |
| | | 29 | | public const int HiMask = HiCountMask | HiHiBit; |
| | | 30 | | public const int HiBits = LoHiBit | HiHiBit; |
| | | 31 | | |
| | | 32 | | public static int Count(int slot) |
| | | 33 | | { |
| | 15566 | 34 | | return ((slot >> HiShift) - slot + 2 & LoMask) - 1; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public static int CountNoIdle(int slot) |
| | | 38 | | { |
| | 1108 | 39 | | return (slot >> HiShift) - slot + 1 & LoMask; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public static int IncrementLo(int slot) |
| | | 43 | | { |
| | 5216 | 44 | | return slot + 1 & LoMask | slot & HiMask; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // This method is only valid if you already know that (gate & HiBits) != 0. |
| | | 48 | | public static bool IsComplete(int gate) |
| | | 49 | | { |
| | 27 | 50 | | return (gate & HiMask) == gate << HiShift; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 10 | 54 | | private static IOThreadScheduler s_current = new IOThreadScheduler(32, 32); |
| | 10 | 55 | | private static SynchronizationContext s_syncContext = new IOThreadSchedulerSynchronizationContext(); |
| | | 56 | | private static TaskScheduler s_IOTaskScheduler; |
| | | 57 | | private readonly ScheduledOverlapped _overlapped; |
| | | 58 | | private readonly Slot[] _slots; |
| | | 59 | | private readonly Slot[] _slotsLowPri; |
| | 10 | 60 | | private static ThreadLocal<bool> s_isIoThread = new ThreadLocal<bool>(); |
| | | 61 | | |
| | | 62 | | // This field holds both the head (HiWord) and tail (LoWord) indices into the slot array. This limits each |
| | | 63 | | // value to 64k. In order to be able to distinguish wrapping the slot array (allowed) from wrapping the |
| | | 64 | | // indices relative to each other (not allowed), the size of the slot array is limited by an additional bit |
| | | 65 | | // to 32k. |
| | | 66 | | // |
| | | 67 | | // The HiWord (head) holds the index of the last slot to have been scheduled into. The LoWord (tail) holds |
| | | 68 | | // the index of the next slot to be dispatched from. When the queue is empty, the LoWord will be exactly |
| | | 69 | | // one slot ahead of the HiWord. When the two are equal, the queue holds one item. |
| | | 70 | | // |
| | | 71 | | // When the tail is *two* slots ahead of the head (equivalent to a count of -1), that means the IOTS is |
| | | 72 | | // idle. Hence, we start out headTail with a -2 (equivalent) in the head and zero in the tail. |
| | 19 | 73 | | private int _headTail = -2 << Bits.HiShift; |
| | | 74 | | |
| | | 75 | | // This field is the same except that it governs the low-priority work items. It doesn't have a concept |
| | | 76 | | // of idle (-2) so starts empty (-1). |
| | 19 | 77 | | private int _headTailLowPri = -1 << Bits.HiShift; |
| | | 78 | | |
| | 19 | 79 | | private IOThreadScheduler(int capacity, int capacityLowPri) |
| | | 80 | | { |
| | | 81 | | Fx.Assert(capacity > 0, "Capacity must be positive."); |
| | | 82 | | Fx.Assert(capacity <= 0x8000, "Capacity cannot exceed 32k."); |
| | | 83 | | |
| | | 84 | | Fx.Assert(capacityLowPri > 0, "Low-priority capacity must be positive."); |
| | | 85 | | Fx.Assert(capacityLowPri <= 0x8000, "Low-priority capacity cannot exceed 32k."); |
| | | 86 | | |
| | 19 | 87 | | _slots = new Slot[capacity]; |
| | | 88 | | Fx.Assert((_slots.Length & SlotMask) == 0, "Capacity must be a power of two."); |
| | | 89 | | |
| | 19 | 90 | | _slotsLowPri = new Slot[capacityLowPri]; |
| | | 91 | | Fx.Assert((_slotsLowPri.Length & SlotMaskLowPri) == 0, "Low-priority capacity must be a power of two."); |
| | | 92 | | |
| | 19 | 93 | | _overlapped = new ScheduledOverlapped(); |
| | 19 | 94 | | } |
| | | 95 | | |
| | | 96 | | public static TaskScheduler IOTaskScheduler |
| | | 97 | | { |
| | | 98 | | get |
| | | 99 | | { |
| | 4 | 100 | | if (s_IOTaskScheduler == null) |
| | | 101 | | { |
| | 1 | 102 | | var savedCtx = SynchronizationContext.Current; |
| | 1 | 103 | | SynchronizationContext.SetSynchronizationContext(s_syncContext); |
| | 1 | 104 | | s_IOTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
| | 1 | 105 | | SynchronizationContext.SetSynchronizationContext(savedCtx); |
| | | 106 | | } |
| | | 107 | | |
| | 4 | 108 | | return s_IOTaskScheduler; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public static void ScheduleCallbackNoFlow(Action<object> callback, object state) |
| | | 113 | | { |
| | 4877 | 114 | | if (callback == null) |
| | | 115 | | { |
| | 0 | 116 | | throw Fx.Exception.ArgumentNull(nameof(callback)); |
| | | 117 | | } |
| | | 118 | | |
| | 4877 | 119 | | bool queued = false; |
| | 9763 | 120 | | while (!queued) |
| | | 121 | | { |
| | 4886 | 122 | | try { } |
| | | 123 | | finally |
| | | 124 | | { |
| | | 125 | | // Called in a finally because it needs to run uninterrupted in order to maintain consistency. |
| | 4886 | 126 | | queued = s_current.ScheduleCallbackHelper(callback, state); |
| | 4886 | 127 | | } |
| | | 128 | | } |
| | 4877 | 129 | | } |
| | | 130 | | |
| | | 131 | | public static void ScheduleCallbackLowPriNoFlow(Action<object> callback, object state) |
| | | 132 | | { |
| | 0 | 133 | | if (callback == null) |
| | | 134 | | { |
| | 0 | 135 | | throw Fx.Exception.ArgumentNull(nameof(callback)); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | bool queued = false; |
| | 0 | 139 | | while (!queued) |
| | | 140 | | { |
| | 0 | 141 | | try { } |
| | | 142 | | finally |
| | | 143 | | { |
| | | 144 | | // Called in a finally because it needs to run uninterrupted in order to maintain consistency. |
| | 0 | 145 | | queued = s_current.ScheduleCallbackLowPriHelper(callback, state); |
| | 0 | 146 | | } |
| | | 147 | | } |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | // Returns true if successfully scheduled, false otherwise. |
| | | 151 | | private bool ScheduleCallbackHelper(Action<object> callback, object state) |
| | | 152 | | { |
| | | 153 | | // See if there's a free slot. Fortunately the overflow bit is simply lost. |
| | 4886 | 154 | | int slot = Interlocked.Add(ref _headTail, Bits.HiOne); |
| | | 155 | | |
| | | 156 | | // If this brings us to 'empty', then the IOTS used to be 'idle'. Remember that, and increment |
| | | 157 | | // again. This doesn't need to be in a loop, because until we call Post(), we can't go back to idle. |
| | 4886 | 158 | | bool wasIdle = Bits.Count(slot) == 0; |
| | 4886 | 159 | | if (wasIdle) |
| | | 160 | | { |
| | 265 | 161 | | slot = Interlocked.Add(ref _headTail, Bits.HiOne); |
| | | 162 | | Fx.Assert(Bits.Count(slot) != 0, "IOTS went idle when it shouldn't have."); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // Check if we wrapped *around* to idle. |
| | 4886 | 166 | | if (Bits.Count(slot) == -1) |
| | | 167 | | { |
| | | 168 | | // Since the capacity is limited to 32k, this means we wrapped the array at least twice. That's bad |
| | | 169 | | // because headTail no longer knows how many work items we have - it looks like zero. This can |
| | | 170 | | // only happen if 32k threads come through here while one is swapped out. |
| | 0 | 171 | | throw Fx.AssertAndThrowFatal("Head/Tail overflow!"); |
| | | 172 | | } |
| | | 173 | | |
| | 4886 | 174 | | bool queued = _slots[slot >> Bits.HiShift & SlotMask].TryEnqueueWorkItem(callback, state, out bool wrapped); |
| | | 175 | | |
| | 4886 | 176 | | if (wrapped) |
| | | 177 | | { |
| | | 178 | | // Wrapped around the circular buffer. Create a new, bigger IOThreadScheduler. |
| | 9 | 179 | | IOThreadScheduler next = |
| | 9 | 180 | | new IOThreadScheduler(Math.Min(_slots.Length * 2, MaximumCapacity), _slotsLowPri.Length); |
| | 9 | 181 | | Interlocked.CompareExchange<IOThreadScheduler>(ref s_current, next, this); |
| | | 182 | | } |
| | | 183 | | |
| | 4886 | 184 | | if (wasIdle) |
| | | 185 | | { |
| | | 186 | | // It's our responsibility to kick off the overlapped. |
| | 265 | 187 | | _overlapped.Post(this); |
| | | 188 | | } |
| | | 189 | | |
| | 4886 | 190 | | return queued; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // Returns true if successfully scheduled, false otherwise. |
| | | 194 | | private bool ScheduleCallbackLowPriHelper(Action<object> callback, object state) |
| | | 195 | | { |
| | | 196 | | // See if there's a free slot. Fortunately the overflow bit is simply lost. |
| | 0 | 197 | | int slot = Interlocked.Add(ref _headTailLowPri, Bits.HiOne); |
| | | 198 | | |
| | | 199 | | // If this is the first low-priority work item, make sure we're not idle. |
| | 0 | 200 | | bool wasIdle = false; |
| | 0 | 201 | | if (Bits.CountNoIdle(slot) == 1) |
| | | 202 | | { |
| | | 203 | | // Since Interlocked calls create a full thread barrier, this will read the value of headTail |
| | | 204 | | // at the time of the Interlocked.Add or later. The invariant is that the IOTS is unidle at some |
| | | 205 | | // point after the Add. |
| | 0 | 206 | | int ht = _headTail; |
| | | 207 | | |
| | 0 | 208 | | if (Bits.Count(ht) == -1) |
| | | 209 | | { |
| | | 210 | | // Use a temporary local here to store the result of the Interlocked.CompareExchange. This |
| | | 211 | | // works around a codegen bug in the 32-bit JIT (TFS 749182). |
| | 0 | 212 | | int interlockedResult = Interlocked.CompareExchange(ref _headTail, ht + Bits.HiOne, ht); |
| | 0 | 213 | | if (ht == interlockedResult) |
| | | 214 | | { |
| | 0 | 215 | | wasIdle = true; |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | // Check if we wrapped *around* to empty. |
| | 0 | 221 | | if (Bits.CountNoIdle(slot) == 0) |
| | | 222 | | { |
| | | 223 | | // Since the capacity is limited to 32k, this means we wrapped the array at least twice. That's bad |
| | | 224 | | // because headTail no longer knows how many work items we have - it looks like zero. This can |
| | | 225 | | // only happen if 32k threads come through here while one is swapped out. |
| | 0 | 226 | | throw Fx.AssertAndThrowFatal("Low-priority Head/Tail overflow!"); |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | bool queued = _slotsLowPri[slot >> Bits.HiShift & SlotMaskLowPri].TryEnqueueWorkItem( |
| | 0 | 230 | | callback, state, out bool wrapped); |
| | | 231 | | |
| | 0 | 232 | | if (wrapped) |
| | | 233 | | { |
| | 0 | 234 | | IOThreadScheduler next = |
| | 0 | 235 | | new IOThreadScheduler(_slots.Length, Math.Min(_slotsLowPri.Length * 2, MaximumCapacity)); |
| | 0 | 236 | | Interlocked.CompareExchange<IOThreadScheduler>(ref s_current, next, this); |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | if (wasIdle) |
| | | 240 | | { |
| | | 241 | | // It's our responsibility to kick off the overlapped. |
| | 0 | 242 | | _overlapped.Post(this); |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return queued; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private void CompletionCallback(out Action<object> callback, out object state) |
| | | 249 | | { |
| | 593 | 250 | | int slot = _headTail; |
| | | 251 | | int slotLowPri; |
| | | 252 | | while (true) |
| | | 253 | | { |
| | | 254 | | Fx.Assert(Bits.Count(slot) != -1, "CompletionCallback called on idle IOTS!"); |
| | | 255 | | |
| | 634 | 256 | | bool wasEmpty = Bits.Count(slot) == 0; |
| | 634 | 257 | | if (wasEmpty) |
| | | 258 | | { |
| | | 259 | | // We're about to set this to idle. First check the low-priority queue. This alone doesn't |
| | | 260 | | // guarantee we service all the low-pri items - there hasn't even been an Interlocked yet. But |
| | | 261 | | // we take care of that later. |
| | 265 | 262 | | slotLowPri = _headTailLowPri; |
| | 265 | 263 | | while (Bits.CountNoIdle(slotLowPri) != 0) |
| | | 264 | | { |
| | 0 | 265 | | if (slotLowPri == (slotLowPri = Interlocked.CompareExchange(ref _headTailLowPri, |
| | 0 | 266 | | Bits.IncrementLo(slotLowPri), slotLowPri))) |
| | | 267 | | { |
| | 0 | 268 | | _overlapped.Post(this); |
| | 0 | 269 | | _slotsLowPri[slotLowPri & SlotMaskLowPri].DequeueWorkItem(out callback, out state); |
| | 0 | 270 | | return; |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | 634 | 275 | | if (slot == (slot = Interlocked.CompareExchange(ref _headTail, Bits.IncrementLo(slot), slot))) |
| | | 276 | | { |
| | 593 | 277 | | if (!wasEmpty) |
| | | 278 | | { |
| | 328 | 279 | | _overlapped.Post(this); |
| | 328 | 280 | | _slots[slot & SlotMask].DequeueWorkItem(out callback, out state); |
| | 328 | 281 | | return; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | // We just set the IOThreadScheduler to idle. Check if a low-priority item got added in the |
| | | 285 | | // interim. |
| | | 286 | | // Interlocked calls create a thread barrier, so this read will give us the value of |
| | | 287 | | // headTailLowPri at the time of the interlocked that set us to idle, or later. The invariant |
| | | 288 | | // here is that either the low-priority queue was empty at some point after we set the IOTS to |
| | | 289 | | // idle (so that the next enqueue will notice, and issue a Post), or that the IOTS was unidle at |
| | | 290 | | // some point after we set it to idle (so that the next attempt to go idle will verify that the |
| | | 291 | | // low-priority queue is empty). |
| | 265 | 292 | | slotLowPri = _headTailLowPri; |
| | | 293 | | |
| | 265 | 294 | | if (Bits.CountNoIdle(slotLowPri) != 0) |
| | | 295 | | { |
| | | 296 | | // Whoops, go back from being idle (unless someone else already did). If we go back, start |
| | | 297 | | // over. (We still owe a Post.) |
| | 0 | 298 | | slot = Bits.IncrementLo(slot); |
| | 0 | 299 | | if (slot == Interlocked.CompareExchange(ref _headTail, slot + Bits.HiOne, slot)) |
| | | 300 | | { |
| | 0 | 301 | | slot += Bits.HiOne; |
| | 0 | 302 | | continue; |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | // We know that there's a low-priority work item. But we also know that the IOThreadScheduler |
| | | 306 | | // wasn't idle. It's best to let it take care of itself, since according to this method, we |
| | | 307 | | // just set the IOThreadScheduler to idle so shouldn't take on any tasks. |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | break; |
| | | 311 | | } |
| | | 312 | | } |
| | | 313 | | |
| | 265 | 314 | | callback = null; |
| | 265 | 315 | | state = null; |
| | 265 | 316 | | return; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private bool TryCoalesce(out Action<object> callback, out object state) |
| | | 320 | | { |
| | 5136 | 321 | | int slot = _headTail; |
| | | 322 | | int slotLowPri; |
| | | 323 | | while (true) |
| | | 324 | | { |
| | 5160 | 325 | | if (Bits.Count(slot) > 0) |
| | | 326 | | { |
| | 4582 | 327 | | if (slot == (slot = Interlocked.CompareExchange(ref _headTail, Bits.IncrementLo(slot), slot))) |
| | | 328 | | { |
| | 4558 | 329 | | _slots[slot & SlotMask].DequeueWorkItem(out callback, out state); |
| | 4558 | 330 | | return true; |
| | | 331 | | } |
| | | 332 | | continue; |
| | | 333 | | } |
| | | 334 | | |
| | 578 | 335 | | slotLowPri = _headTailLowPri; |
| | 578 | 336 | | if (Bits.CountNoIdle(slotLowPri) > 0) |
| | | 337 | | { |
| | 0 | 338 | | if (slotLowPri == (slotLowPri = Interlocked.CompareExchange(ref _headTailLowPri, |
| | 0 | 339 | | Bits.IncrementLo(slotLowPri), slotLowPri))) |
| | | 340 | | { |
| | 0 | 341 | | _slotsLowPri[slotLowPri & SlotMaskLowPri].DequeueWorkItem(out callback, out state); |
| | 0 | 342 | | return true; |
| | | 343 | | } |
| | 0 | 344 | | slot = _headTail; |
| | 0 | 345 | | continue; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | break; |
| | | 349 | | } |
| | | 350 | | |
| | 578 | 351 | | callback = null; |
| | 578 | 352 | | state = null; |
| | 578 | 353 | | return false; |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private int SlotMask |
| | | 357 | | { |
| | | 358 | | get |
| | | 359 | | { |
| | 9772 | 360 | | return _slots.Length - 1; |
| | | 361 | | } |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | private int SlotMaskLowPri |
| | | 365 | | { |
| | | 366 | | get |
| | | 367 | | { |
| | 0 | 368 | | return _slotsLowPri.Length - 1; |
| | | 369 | | } |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | public static bool IsRunningOnIOThread => s_isIoThread.IsValueCreated && s_isIoThread.Value; |
| | | 373 | | |
| | | 374 | | //TODO, Dev10,607596 cannot apply security critical on finalizer |
| | | 375 | | //[Fx.Tag.SecurityNote(Critical = "touches slots, may be called outside of user context")] |
| | | 376 | | //[SecurityCritical] |
| | | 377 | | ~IOThreadScheduler() |
| | | 378 | | { |
| | | 379 | | // If the AppDomain is shutting down, we may still have pending ops. The AppDomain shutdown will clean |
| | | 380 | | // everything up. |
| | 3 | 381 | | if (!Environment.HasShutdownStarted && !AppDomain.CurrentDomain.IsFinalizingForUnload()) |
| | | 382 | | { |
| | | 383 | | #if DEBUG |
| | | 384 | | DebugVerifyHeadTail(); |
| | | 385 | | #endif |
| | 3 | 386 | | Cleanup(); |
| | | 387 | | } |
| | 6 | 388 | | } |
| | | 389 | | |
| | | 390 | | private void Cleanup() |
| | | 391 | | { |
| | 3 | 392 | | if (_overlapped != null) |
| | | 393 | | { |
| | 3 | 394 | | _overlapped.Cleanup(); |
| | | 395 | | } |
| | 3 | 396 | | } |
| | | 397 | | |
| | | 398 | | #if DEBUG |
| | | 399 | | private void DebugVerifyHeadTail() |
| | | 400 | | { |
| | | 401 | | if (_slots != null) |
| | | 402 | | { |
| | | 403 | | // The headTail value could technically be zero if the constructor was aborted early. The |
| | | 404 | | // constructor wasn't aborted early if the slot array got created. |
| | | 405 | | Fx.Assert(Bits.Count(_headTail) == -1, "IOTS finalized while not idle."); |
| | | 406 | | |
| | | 407 | | for (int i = 0; i < _slots.Length; i++) |
| | | 408 | | { |
| | | 409 | | _slots[i].DebugVerifyEmpty(); |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | if (_slotsLowPri != null) |
| | | 414 | | { |
| | | 415 | | Fx.Assert(Bits.CountNoIdle(_headTailLowPri) == 0, "IOTS finalized with low-priority items queued."); |
| | | 416 | | |
| | | 417 | | for (int i = 0; i < _slotsLowPri.Length; i++) |
| | | 418 | | { |
| | | 419 | | _slotsLowPri[i].DebugVerifyEmpty(); |
| | | 420 | | } |
| | | 421 | | } |
| | | 422 | | } |
| | | 423 | | #endif |
| | | 424 | | |
| | | 425 | | // TryEnqueueWorkItem and DequeueWorkItem use the slot's 'gate' field for synchronization. Because the |
| | | 426 | | // slot array is circular and there are no locks, we must assume that multiple threads can be entering each |
| | | 427 | | // method simultaneously. If the first DequeueWorkItem occurs before the first TryEnqueueWorkItem, the |
| | | 428 | | // sequencing (and the enqueue) fails. |
| | | 429 | | // |
| | | 430 | | // The gate is a 32-bit int divided into four fields. The bottom 15 bits (0x00007fff) are the count of |
| | | 431 | | // threads that have entered TryEnqueueWorkItem. The first thread to enter is the one responsible for |
| | | 432 | | // filling the slot with work. The 16th bit (0x00008000) is a flag indicating that the slot has been |
| | | 433 | | // successfully filled. Only the first thread to enter TryEnqueueWorkItem can set this flag. The |
| | | 434 | | // high-word (0x7fff0000) is the count of threads entering DequeueWorkItem. The first thread to enter |
| | | 435 | | // is the one responsible for accepting (and eventually dispatching) the work in the slot. The |
| | | 436 | | // high-bit (0x80000000) is a flag indicating that the slot has been successfully emptied. |
| | | 437 | | // |
| | | 438 | | // When the low-word and high-work counters are equal, and both bit flags have been set, the gate is considered |
| | | 439 | | // 'complete' and can be reset back to zero. Any operation on the gate might bring it to this state. |
| | | 440 | | // It's the responsibility of the thread that brings the gate to a completed state to reset it to zero. |
| | | 441 | | // (It's possible that the gate will fall out of the completed state before it can be reset - that's ok, |
| | | 442 | | // the next time it becomes completed it can be reset.) |
| | | 443 | | // |
| | | 444 | | // It's unlikely either count will ever go higher than 2 or 3. |
| | | 445 | | // |
| | | 446 | | // The value of 'callback' has these properties: |
| | | 447 | | // - When the gate is zero, callback is null. |
| | | 448 | | // - When the low-word count is non-zero, but the 0x8000 bit is unset, callback is writable by the thread |
| | | 449 | | // that incremented the low word to 1. Its value is undefined for other threads. The thread that |
| | | 450 | | // sets callback is responsible for setting the 0x8000 bit when it's done. |
| | | 451 | | // - When the 0x8000 bit is set and the high-word count is zero, callback is valid. (It may be null.) |
| | | 452 | | // - When the 0x8000 bit is set, the high-word count is non-zero, and the high bit is unset, callback is |
| | | 453 | | // writable by the thread that incremented the high word to 1 *or* the thread that set the 0x8000 bit, |
| | | 454 | | // whichever happened last. That thread can read the value and set callback to null. Its value is |
| | | 455 | | // undefined for other threads. The thread that clears the callback is responsible for setting the |
| | | 456 | | // high bit. |
| | | 457 | | // - When the high bit is set, callback is null. |
| | | 458 | | // - It's illegal for the gate to be in a state that would satisfy more than one of these conditions. |
| | | 459 | | // - The state field follows the same rules as callback. |
| | | 460 | | private struct Slot |
| | | 461 | | { |
| | | 462 | | private int _gate; |
| | | 463 | | private Action<object> _callback; |
| | | 464 | | private object _state; |
| | | 465 | | |
| | | 466 | | public bool TryEnqueueWorkItem(Action<object> callback, object state, out bool wrapped) |
| | | 467 | | { |
| | | 468 | | // Register our arrival and check the state of this slot. If the slot was already full, we wrapped. |
| | 4886 | 469 | | int gateSnapshot = Interlocked.Increment(ref _gate); |
| | 4886 | 470 | | wrapped = (gateSnapshot & Bits.LoCountMask) != 1; |
| | 4886 | 471 | | if (wrapped) |
| | | 472 | | { |
| | 9 | 473 | | if ((gateSnapshot & Bits.LoHiBit) != 0 && Bits.IsComplete(gateSnapshot)) |
| | | 474 | | { |
| | 0 | 475 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot); |
| | | 476 | | } |
| | 9 | 477 | | return false; |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | Fx.Assert(_callback == null, "Slot already has a work item."); |
| | | 481 | | Fx.Assert((gateSnapshot & Bits.HiBits) == 0, "Slot already marked."); |
| | | 482 | | |
| | 4877 | 483 | | _state = state; |
| | 4877 | 484 | | _callback = callback; |
| | | 485 | | |
| | | 486 | | // Set the special bit to show that the slot is filled. |
| | 4877 | 487 | | gateSnapshot = Interlocked.Add(ref _gate, Bits.LoHiBit); |
| | | 488 | | Fx.Assert((gateSnapshot & Bits.HiBits) == Bits.LoHiBit, "Slot already empty."); |
| | | 489 | | |
| | 4877 | 490 | | if ((gateSnapshot & Bits.HiCountMask) == 0) |
| | | 491 | | { |
| | | 492 | | // Good - no one has shown up looking for this work yet. |
| | 4877 | 493 | | return true; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | // Oops - someone already came looking for this work. We have to abort and reschedule. |
| | 0 | 497 | | _state = null; |
| | 0 | 498 | | _callback = null; |
| | | 499 | | |
| | | 500 | | // Indicate that the slot is clear. We might be able to bypass setting the high bit. |
| | 0 | 501 | | if (gateSnapshot >> Bits.HiShift != (gateSnapshot & Bits.LoCountMask) || |
| | 0 | 502 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot) != gateSnapshot) |
| | | 503 | | { |
| | 0 | 504 | | gateSnapshot = Interlocked.Add(ref _gate, Bits.HiHiBit); |
| | 0 | 505 | | if (Bits.IsComplete(gateSnapshot)) |
| | | 506 | | { |
| | 0 | 507 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot); |
| | | 508 | | } |
| | | 509 | | } |
| | | 510 | | |
| | 0 | 511 | | return false; |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | public void DequeueWorkItem(out Action<object> callback, out object state) |
| | | 515 | | { |
| | | 516 | | // Stake our claim on the item. |
| | 4886 | 517 | | int gateSnapshot = Interlocked.Add(ref _gate, Bits.HiOne); |
| | | 518 | | |
| | 4886 | 519 | | if ((gateSnapshot & Bits.LoHiBit) == 0) |
| | | 520 | | { |
| | | 521 | | // Whoops, a race. The work item hasn't made it in yet. In this context, returning a null callback |
| | | 522 | | // is treated like a degenerate work item (rather than an empty queue). The enqueuing thread will |
| | | 523 | | // notice this race and reschedule the real work in a new slot. Do not reset the slot to zero, |
| | | 524 | | // since it's still going to get enqueued into. (The enqueueing thread will reset it.) |
| | 0 | 525 | | callback = null; |
| | 0 | 526 | | state = null; |
| | 0 | 527 | | return; |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | // If we're the first, we get to do the work. |
| | 4886 | 531 | | if ((gateSnapshot & Bits.HiCountMask) == Bits.HiOne) |
| | | 532 | | { |
| | 4877 | 533 | | callback = _callback; |
| | 4877 | 534 | | state = _state; |
| | 4877 | 535 | | _state = null; |
| | 4877 | 536 | | _callback = null; |
| | | 537 | | |
| | | 538 | | // Indicate that the slot is clear. |
| | | 539 | | // We should be able to bypass setting the high-bit in the common case. |
| | 4877 | 540 | | if ((gateSnapshot & Bits.LoCountMask) != 1 || |
| | 4877 | 541 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot) != gateSnapshot) |
| | | 542 | | { |
| | 9 | 543 | | gateSnapshot = Interlocked.Add(ref _gate, Bits.HiHiBit); |
| | 9 | 544 | | if (Bits.IsComplete(gateSnapshot)) |
| | | 545 | | { |
| | 0 | 546 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot); |
| | | 547 | | } |
| | | 548 | | } |
| | | 549 | | } |
| | | 550 | | else |
| | | 551 | | { |
| | 9 | 552 | | callback = null; |
| | 9 | 553 | | state = null; |
| | | 554 | | |
| | | 555 | | // If we're the last, we get to reset the slot. |
| | 9 | 556 | | if (Bits.IsComplete(gateSnapshot)) |
| | | 557 | | { |
| | 9 | 558 | | Interlocked.CompareExchange(ref _gate, 0, gateSnapshot); |
| | | 559 | | } |
| | | 560 | | } |
| | 4886 | 561 | | } |
| | | 562 | | |
| | | 563 | | #if DEBUG |
| | | 564 | | public void DebugVerifyEmpty() |
| | | 565 | | { |
| | | 566 | | Fx.Assert(_gate == 0, "Finalized with unfinished slot."); |
| | | 567 | | Fx.Assert(_callback == null, "Finalized with leaked callback."); |
| | | 568 | | Fx.Assert(_state == null, "Finalized with leaked state."); |
| | | 569 | | } |
| | | 570 | | #endif |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | // A note about the IOThreadScheduler and the ScheduledOverlapped references: |
| | | 574 | | // Although for each scheduler we have a single instance of overlapped, we cannot point to the scheduler from th |
| | | 575 | | // overlapped, through the entire lifetime of the overlapped. This is because the ScheduledOverlapped is pinned |
| | | 576 | | // and if it has a reference to the IOTS, it would be rooted and the finalizer will never get called. |
| | | 577 | | // Therefore, we are passing the reference, when we post a pending callback and reset it, once the callback was |
| | | 578 | | // invoked; during that time the scheduler is rooted but in that time we don't want that it would be collected |
| | | 579 | | // by the GC anyway. |
| | | 580 | | private unsafe class ScheduledOverlapped |
| | | 581 | | { |
| | | 582 | | private readonly NativeOverlapped* _nativeOverlapped; |
| | | 583 | | private IOThreadScheduler _scheduler; |
| | | 584 | | private readonly Action _postDelegate; |
| | | 585 | | |
| | 19 | 586 | | public ScheduledOverlapped() |
| | | 587 | | { |
| | 19 | 588 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | | 589 | | { |
| | 0 | 590 | | _nativeOverlapped = (new Overlapped()).UnsafePack( |
| | 0 | 591 | | Fx.ThunkCallback(new IOCompletionCallback(IOCallback)), null); |
| | 0 | 592 | | _postDelegate = PostIOCP; |
| | | 593 | | } |
| | | 594 | | else |
| | | 595 | | { |
| | 19 | 596 | | _postDelegate = PostNewThread; |
| | | 597 | | } |
| | 19 | 598 | | } |
| | | 599 | | |
| | | 600 | | private void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) |
| | | 601 | | { |
| | 0 | 602 | | Callback(); |
| | 0 | 603 | | } |
| | | 604 | | |
| | | 605 | | private void Callback() |
| | | 606 | | { |
| | | 607 | | try |
| | | 608 | | { |
| | | 609 | | InitThreadDebugData(); |
| | 593 | 610 | | CallbackCore(); |
| | 578 | 611 | | } |
| | | 612 | | finally |
| | | 613 | | { |
| | | 614 | | ClearThreadDebugData(); |
| | 578 | 615 | | } |
| | 578 | 616 | | } |
| | | 617 | | |
| | | 618 | | [Conditional("DEBUG")] |
| | | 619 | | private static void InitThreadDebugData() |
| | | 620 | | { |
| | 0 | 621 | | s_isIoThread.Value = true; |
| | 0 | 622 | | Thread.CurrentThread.Name = "IOThreadScheduler.IOCallback"; |
| | 0 | 623 | | } |
| | | 624 | | |
| | | 625 | | [Conditional("DEBUG")] |
| | | 626 | | private static void ClearThreadDebugData() |
| | | 627 | | { |
| | 0 | 628 | | s_isIoThread.Value = false; |
| | 0 | 629 | | } |
| | | 630 | | |
| | | 631 | | private void CallbackCore() |
| | | 632 | | { |
| | | 633 | | // Unhook the IOThreadScheduler ASAP to prevent it from leaking. |
| | 593 | 634 | | IOThreadScheduler iots = _scheduler; |
| | 593 | 635 | | _scheduler = null; |
| | | 636 | | Fx.Assert(iots != null, "Overlapped completed without a scheduler."); |
| | | 637 | | |
| | | 638 | | Action<object> callback; |
| | | 639 | | object state; |
| | 593 | 640 | | try { } |
| | | 641 | | finally |
| | | 642 | | { |
| | | 643 | | // Called in a finally because it needs to run uninterrupted in order to maintain consistency. |
| | 593 | 644 | | iots.CompletionCallback(out callback, out state); |
| | 593 | 645 | | } |
| | | 646 | | |
| | 593 | 647 | | bool found = true; |
| | 5729 | 648 | | while (found) |
| | | 649 | | { |
| | | 650 | | // The callback can be null if synchronization misses result in unusable slots. Keep going onto |
| | | 651 | | // the next slot in such cases until there are no more slots. |
| | 5151 | 652 | | if (callback != null) |
| | | 653 | | { |
| | 4877 | 654 | | callback(state); |
| | | 655 | | } |
| | | 656 | | |
| | 5136 | 657 | | try { } |
| | | 658 | | finally |
| | | 659 | | { |
| | | 660 | | // Called in a finally because it needs to run uninterrupted in order to maintain consistency. |
| | 5136 | 661 | | found = iots.TryCoalesce(out callback, out state); |
| | 5136 | 662 | | } |
| | | 663 | | } |
| | 578 | 664 | | } |
| | | 665 | | |
| | | 666 | | public void Post(IOThreadScheduler iots) |
| | | 667 | | { |
| | | 668 | | Fx.Assert(_scheduler == null, "Post called on an overlapped that is already posted."); |
| | | 669 | | Fx.Assert(iots != null, "Post called with a null scheduler."); |
| | | 670 | | |
| | 593 | 671 | | _scheduler = iots; |
| | 593 | 672 | | _postDelegate(); |
| | 593 | 673 | | } |
| | | 674 | | |
| | | 675 | | [SupportedOSPlatform("windows")] |
| | | 676 | | private void PostIOCP() |
| | | 677 | | { |
| | 0 | 678 | | ThreadPool.UnsafeQueueNativeOverlapped(_nativeOverlapped); |
| | 0 | 679 | | } |
| | | 680 | | |
| | | 681 | | private void PostNewThread() |
| | | 682 | | { |
| | | 683 | | // The waiter thread used on non-Windows platforms must be a background thread, |
| | | 684 | | // otherwise it keeps the process alive until WaitAny returns, which can take |
| | | 685 | | // up to the longest IOThreadTimer due time (default session idle timeout is |
| | | 686 | | // measured in minutes). Background threads are torn down by the runtime when |
| | | 687 | | // the process exits, mirroring the Windows IOCP behavior where the equivalent |
| | | 688 | | // work runs on thread-pool threads (which are background by default). |
| | 593 | 689 | | var thread = new Thread(new ThreadStart(Callback)) |
| | 593 | 690 | | { |
| | 593 | 691 | | IsBackground = true, |
| | 593 | 692 | | }; |
| | 593 | 693 | | thread.Start(); |
| | 593 | 694 | | } |
| | | 695 | | |
| | | 696 | | public void Cleanup() |
| | | 697 | | { |
| | 3 | 698 | | if (_scheduler != null) |
| | | 699 | | { |
| | 0 | 700 | | throw Fx.AssertAndThrowFatal("Cleanup called on an overlapped that is in-flight."); |
| | | 701 | | } |
| | | 702 | | |
| | 3 | 703 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | | 704 | | { |
| | 0 | 705 | | Overlapped.Free(_nativeOverlapped); |
| | | 706 | | } |
| | 3 | 707 | | } |
| | | 708 | | } |
| | | 709 | | |
| | | 710 | | private class IOThreadSchedulerSynchronizationContext : SynchronizationContext |
| | | 711 | | { |
| | | 712 | | public override void Post(SendOrPostCallback d, object state) |
| | | 713 | | { |
| | 8 | 714 | | ScheduleCallbackNoFlow((s) => d(s), state); |
| | 4 | 715 | | } |
| | | 716 | | } |
| | | 717 | | } |
| | | 718 | | } |