forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelayHelper.cs
90 lines (82 loc) · 3.47 KB
/
DelayHelper.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Threading;
namespace System.Device
{
/// <summary>
/// Helpers for short waits.
/// </summary>
public static class DelayHelper
{
/* GetTimestamp() currently can take ~300ns. We hope to improve this to get better
* fidelity for very tight spins.
*
* SpinWait currently spins to approximately 1μs before it will yield the thread.
*/
private const long TicksPerSecond = TimeSpan.TicksPerSecond;
private const long TicksPerMillisecond = TimeSpan.TicksPerMillisecond;
private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
/// <summary>
/// Delay for at least the specified <paramref name="time" />.
/// </summary>
/// <param name="time">The amount of time to delay.</param>
/// <param name="allowThreadYield">
/// True to allow yielding the thread. If this is set to false, on single-proc systems
/// this will prevent all other code from running.
/// </param>
public static void Delay(TimeSpan time, bool allowThreadYield)
{
long start = Stopwatch.GetTimestamp();
long target = start + time.Ticks;
if (!allowThreadYield)
{
do
{
Thread.SpinWait(1);
}
while (Stopwatch.GetTimestamp() < target);
}
else
{
do
{
// No spin wait
}
while (Stopwatch.GetTimestamp() < target);
}
}
/// <summary>
/// Delay for at least the specified <paramref name="microseconds"/>.
/// </summary>
/// <param name="microseconds">The number of microseconds to delay.</param>
/// <param name="allowThreadYield">
/// True to allow yielding the thread. If this is set to false, on single-proc systems
/// this will prevent all other code from running.
/// </param>
public static void DelayMicroseconds(int microseconds, bool allowThreadYield)
{
var time = TimeSpan.FromTicks(microseconds * TicksPerMicrosecond);
Delay(time, allowThreadYield);
}
/// <summary>
/// Delay for at least the specified <paramref name="milliseconds"/>
/// </summary>
/// <param name="milliseconds">The number of milliseconds to delay.</param>
/// <param name="allowThreadYield">
/// True to allow yielding the thread. If this is set to false, on single-proc systems
/// this will prevent all other code from running.
/// </param>
public static void DelayMilliseconds(int milliseconds, bool allowThreadYield)
{
/* We have this as a separate method for now to make calling code clearer
* and to allow us to add additional logic to the millisecond wait in the
* future. If waiting only 1 millisecond we still have ample room for more
* complicated logic. For 1 microsecond that isn't the case.
*/
var time = TimeSpan.FromTicks(milliseconds * TicksPerMillisecond);
Delay(time, allowThreadYield);
}
}
}