Skip to content

Commit 2a0e1ee

Browse files
authored
Add SpinWait (#136)
1 parent 061edcf commit 2a0e1ee

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

nanoFramework.CoreLibrary/CoreLibrary.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<Compile Include="System\Threading\ThreadStart.cs" />
200200
<Compile Include="System\Threading\ThreadState.cs" />
201201
<Compile Include="System\Threading\Timeout.cs" />
202+
<Compile Include="System\Threading\SpinWait.cs" />
202203
<Compile Include="System\Threading\Timer.cs" />
203204
<Compile Include="System\Threading\WaitHandle.cs" />
204205
<Compile Include="System\TimeSpan.cs" />

nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
1414
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
1515

16-
[assembly: AssemblyNativeVersion("100.5.0.7")]
16+
[assembly: AssemblyNativeVersion("100.5.0.8")]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

nanoFramework.CoreLibrary/System/Threading/Thread.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ public static void Sleep(TimeSpan timeout)
150150
Sleep((int)tm);
151151
}
152152

153+
/// <summary>
154+
/// Causes a thread to wait the number of times defined by the <paramref name="iterations"/> parameter.
155+
/// </summary>
156+
/// <param name="iterations">A 32-bit signed integer that defines how long a thread is to wait.</param>
157+
/// <remarks>
158+
/// The <see cref="SpinWait"/> method is useful for implementing locks. Classes in the .NET Framework, such as <see cref="Monitor"/> use this method internally. <see cref="SpinWait"/> essentially puts the processor into a very tight loop, with the loop count specified by the <paramref name="iterations"/> parameter. The duration of the wait therefore depends on the speed of the processor.
159+
/// Contrast this with the <see cref="Sleep"/> method. A thread that calls <see cref="Sleep"/> yields the rest of its current slice of processor time, even if the specified interval is zero. Specifying a non-zero interval for <see cref="Sleep"/> removes the thread from consideration by the thread scheduler until the time interval has elapsed.
160+
/// </remarks>
161+
[MethodImpl(MethodImplOptions.InternalCall)]
162+
public static extern void SpinWait(int iterations);
163+
153164
/// <summary>
154165
/// Gets the currently running thread.
155166
/// </summary>

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.10.3-preview.{height}",
3+
"version": "1.10.4-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)