|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +// |
| 6 | + |
| 7 | +namespace System.Threading |
| 8 | +{ |
| 9 | + using System; |
| 10 | + using Runtime.CompilerServices; |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// Provides support for spin-based waiting. |
| 14 | + /// </summary> |
| 15 | + /// <remarks> |
| 16 | + /// <para> |
| 17 | + /// <see cref="SpinWait"/> encapsulates common spinning logic. On single-processor machines, yields are |
| 18 | + /// always used instead of busy waits, and on computers with Intel™ processors employing Hyper-Threading™ |
| 19 | + /// technology, it helps to prevent hardware thread starvation. SpinWait encapsulates a good mixture of |
| 20 | + /// spinning and true yielding. |
| 21 | + /// </para> |
| 22 | + /// <para> |
| 23 | + /// <see cref="SpinWait"/> is a value type, which means that low-level code can utilize SpinWait without |
| 24 | + /// fear of unnecessary allocation overheads. SpinWait is not generally useful for ordinary applications. |
| 25 | + /// In most cases, you should use the synchronization classes provided by the .NET Framework, such as |
| 26 | + /// <see cref="System.Threading.Monitor"/>. For most purposes where spin waiting is required, however, |
| 27 | + /// the <see cref="SpinWait"/> type should be preferred over the <see |
| 28 | + /// cref="Thread.SpinWait"/> method. |
| 29 | + /// </para> |
| 30 | + /// <para> |
| 31 | + /// While SpinWait is designed to be used in concurrent applications, it is not designed to be |
| 32 | + /// used from multiple threads concurrently. SpinWait's members are not thread-safe. If multiple |
| 33 | + /// threads must spin, each should use its own instance of SpinWait. |
| 34 | + /// </para> |
| 35 | + /// </remarks> |
| 36 | + public struct SpinWait |
| 37 | + { |
| 38 | + /// <summary> |
| 39 | + /// Performs a single spin. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 42 | + /// This is typically called in a loop, and may change in behavior based on the number of times a |
| 43 | + /// <see cref="SpinOnce"/> has been called thus far on this instance. |
| 44 | + /// </remarks> |
| 45 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 46 | + public extern void SpinOnce(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Spins until the specified timeout is expired. |
| 50 | + /// </summary> |
| 51 | + /// <param name="timeout"> |
| 52 | + /// A <see cref="TimeSpan"/> that represents the number of milliseconds to wait.</param> |
| 53 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is a negative number |
| 54 | + /// other than -1 milliseconds, which represents an infinite time-out |
| 55 | + /// -or- timeout is greater than <see cref="Int32.MaxValue"/>.</exception> |
| 56 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 57 | + public static extern void SpinUntil(TimeSpan timeout); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Spins until the specified timeout is expired. |
| 61 | + /// </summary> |
| 62 | + /// <param name="millisecondsTimeout">The number of milliseconds to wait.</param> |
| 63 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="millisecondsTimeout"/> is a |
| 64 | + /// negative number other than -1, which represents an infinite time-out.</exception> |
| 65 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 66 | + public static extern void SpinUntil(int millisecondsTimeout); |
| 67 | + } |
| 68 | +} |
0 commit comments