daily update on 20241002 by nitload youmu, intrinsic function rebuild, syntax style arrange
This commit is contained in:
@@ -29,104 +29,110 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace StgSharp.Timing
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A microsecond-accuracy timer. It will refresh every a certain time span.
|
||||
/// </summary>
|
||||
public class TimeSpanProvider
|
||||
{
|
||||
|
||||
private Action _frameUpdateMissCallback = () => { };
|
||||
private Action _refreshCallback;
|
||||
private bool _subscribed;
|
||||
private Counter<int> timeSpanCount = new Counter<int>(-1, 1, 1);
|
||||
private readonly int _maxSpanCount;
|
||||
|
||||
private ConcurrentQueue<TimeSpanAwaitingToken> _tokenToAdd = new ConcurrentQueue<TimeSpanAwaitingToken>(
|
||||
);
|
||||
private ConcurrentQueue<TimeSpanAwaitingToken> _tokenToQuit;
|
||||
private Counter<int> timeSpanCount = new Counter<int>( -1, 1, 1 );
|
||||
private HashSet<TimeSpanAwaitingToken> _awaitingTokens;
|
||||
private long _maxSpanCount;
|
||||
private long _spanBegin;
|
||||
private readonly long _spanLength;
|
||||
private SemaphoreSlim _refreshSemaphore = new SemaphoreSlim(0, 1);
|
||||
private readonly TimeSourceProviderBase _timeSource;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span. Length of a single time span is defined in microseconds.
|
||||
/// Create a new <see cref="TimeSpanProvider" /> and define the length of time span. Length
|
||||
/// of a single time span is defined in microseconds.
|
||||
/// </summary>
|
||||
/// <param name="spanMicroSeconds"></param>
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(long spanMicroSeconds, TimeSourceProviderBase provider)
|
||||
public TimeSpanProvider(
|
||||
long spanMicroSeconds,
|
||||
TimeSourceProviderBase provider )
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
_timeSource = provider;
|
||||
_spanLength = spanMicroSeconds;
|
||||
_maxSpanCount = int.MaxValue;
|
||||
provider.AddSubscriber(this);
|
||||
provider.AddSubscriber( this );
|
||||
_subscribed = true;
|
||||
_refreshCallback = new Action(() => { });
|
||||
_awaitingTokens = new HashSet<TimeSpanAwaitingToken>();
|
||||
_tokenToQuit = new ConcurrentQueue<TimeSpanAwaitingToken>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span.
|
||||
/// Length of a single time span is defined in seconds.
|
||||
/// Create a new <see cref="TimeSpanProvider" /> and define the length of time span. Length
|
||||
/// of a single time span is defined in seconds.
|
||||
/// </summary>
|
||||
/// <param name="spanSeconds"></param>
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(double spanSeconds, TimeSourceProviderBase provider)
|
||||
public TimeSpanProvider(
|
||||
double spanSeconds,
|
||||
TimeSourceProviderBase provider )
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
_spanLength = (long)(spanSeconds * 1000L * 1000L);
|
||||
_timeSource = provider;
|
||||
_spanLength = ( long )( spanSeconds * 1000L * 1000L );
|
||||
_spanBegin = -_spanLength;
|
||||
_maxSpanCount = int.MaxValue;
|
||||
provider.AddSubscriber(this);
|
||||
_maxSpanCount = long.MaxValue;
|
||||
provider.AddSubscriber( this );
|
||||
_subscribed = true;
|
||||
_refreshCallback = new Action(() => { });
|
||||
_awaitingTokens = new HashSet<TimeSpanAwaitingToken>();
|
||||
_tokenToQuit = new ConcurrentQueue<TimeSpanAwaitingToken>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="TimeSpanProvider"/> and define the length of time span. Length of a single time span is defined in microseconds.
|
||||
/// Create a new <see cref="TimeSpanProvider" /> and define the length of time span. Length
|
||||
/// of a single time span is defined in microseconds.
|
||||
/// </summary>
|
||||
/// <param name="spanLength"></param>
|
||||
/// <param name="maxSpan"></param>
|
||||
/// <param name="provider"></param>
|
||||
public TimeSpanProvider(long spanLength, int maxSpan, TimeSourceProviderBase provider)
|
||||
public TimeSpanProvider(
|
||||
long spanLength,
|
||||
long maxSpan,
|
||||
TimeSourceProviderBase provider )
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(provider));
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
if (maxSpan <= 0)
|
||||
{
|
||||
maxSpan = int.MaxValue;
|
||||
if( maxSpan <= 0 ) {
|
||||
throw new ArgumentException(
|
||||
"Sapn count cannot be smaller than zero" );
|
||||
}
|
||||
_spanLength = spanLength; _spanBegin = -_spanLength;
|
||||
provider.AddSubscriber(this);
|
||||
_timeSource = provider;
|
||||
_spanLength = spanLength;
|
||||
_spanBegin = -_spanLength;
|
||||
provider.AddSubscriber( this );
|
||||
_subscribed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The method this <see cref="TimeSpanProvider"/> will call every time a new time span started.
|
||||
/// </summary>
|
||||
public Action SpanRefreshCallback
|
||||
{
|
||||
get => _refreshCallback;
|
||||
set => _refreshCallback = value;
|
||||
_awaitingTokens = new HashSet<TimeSpanAwaitingToken>();
|
||||
_tokenToQuit = new ConcurrentQueue<TimeSpanAwaitingToken>();
|
||||
}
|
||||
|
||||
public double SpanLength
|
||||
{
|
||||
get => (_spanLength * 1.0) / (1000L * 1000L);
|
||||
get => ( _spanLength * 1.0 ) / ( 1000L * 1000L );
|
||||
}
|
||||
|
||||
public float CurrentSecond
|
||||
@@ -136,7 +142,7 @@ namespace StgSharp.Timing
|
||||
|
||||
public float SpanLengthLowPrecession
|
||||
{
|
||||
get => (_spanLength * 1.0f) / (1000 * 1000);
|
||||
get => ( _spanLength * 1.0f ) / ( 1000 * 1000 );
|
||||
}
|
||||
|
||||
public int CurrentSpan
|
||||
@@ -145,22 +151,39 @@ namespace StgSharp.Timing
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Time source provider provided in <see cref="StgSharp"/> internal framework.
|
||||
/// Time source provider provided in <see cref="StgSharp" /> internal framework.
|
||||
/// </summary>
|
||||
public static TimeSourceProviderBase DefaultProvider => StgSharpTime.OnlyInstance;
|
||||
|
||||
public void SubscribeToTimeSource(TimeSourceProviderBase serviceHandler)
|
||||
public TimeSpanAwaitingToken ParticipantSpanAwaiting( int count )
|
||||
{
|
||||
if (serviceHandler == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceHandler));
|
||||
TimeSpanAwaitingToken token = new TimeSpanAwaitingToken(
|
||||
this, count );
|
||||
_tokenToAdd.Enqueue( token );
|
||||
return token;
|
||||
}
|
||||
|
||||
public void QuitSpanAwaiting( TimeSpanAwaitingToken token )
|
||||
{
|
||||
_tokenToQuit.Enqueue( token );
|
||||
}
|
||||
|
||||
public void StopSpanProviding()
|
||||
{
|
||||
Interlocked.Exchange( ref _maxSpanCount, 0 );
|
||||
}
|
||||
|
||||
public void SubscribeToTimeSource(
|
||||
TimeSourceProviderBase serviceHandler )
|
||||
{
|
||||
if( serviceHandler == null ) {
|
||||
throw new ArgumentNullException( nameof( serviceHandler ) );
|
||||
}
|
||||
if (_subscribed)
|
||||
{
|
||||
if( _subscribed ) {
|
||||
return;
|
||||
}
|
||||
_subscribed = true;
|
||||
serviceHandler.AddSubscriber(this);
|
||||
serviceHandler.AddSubscriber( this );
|
||||
}
|
||||
|
||||
public int ToInt32()
|
||||
@@ -168,49 +191,41 @@ namespace StgSharp.Timing
|
||||
return timeSpanCount.Value;
|
||||
}
|
||||
|
||||
public void WaitNextSpan()
|
||||
internal bool CheckTime( long microseconds )
|
||||
{
|
||||
_refreshSemaphore.Wait();
|
||||
}
|
||||
|
||||
internal bool CheckTime(long microseconds)
|
||||
{
|
||||
//Console.Write("1 ");
|
||||
if (microseconds - _spanBegin < _spanLength)
|
||||
{
|
||||
if( microseconds - _spanBegin < _spanLength ) {
|
||||
return true;
|
||||
}
|
||||
//Console.Write("2 ");
|
||||
while( !_tokenToAdd.IsEmpty ) {
|
||||
_tokenToAdd.TryDequeue( out TimeSpanAwaitingToken tokenAdd );
|
||||
_awaitingTokens.Add( tokenAdd );
|
||||
}
|
||||
while( !_tokenToQuit.IsEmpty ) {
|
||||
_tokenToQuit.TryDequeue( out TimeSpanAwaitingToken tokenQuit );
|
||||
_awaitingTokens.Remove( tokenQuit );
|
||||
InternalIO.InternalWriteLog(
|
||||
$"Awaiting token {tokenQuit!.TokenID} has been removed.",
|
||||
LogType.Info );
|
||||
}
|
||||
|
||||
_spanBegin = microseconds;
|
||||
timeSpanCount++;
|
||||
//Console.Write("3 ");
|
||||
if (_refreshCallback != null)
|
||||
{
|
||||
_refreshCallback();
|
||||
}
|
||||
//Console.Write("4 ");
|
||||
if (_refreshSemaphore.CurrentCount == 0)
|
||||
{
|
||||
_refreshSemaphore.Release();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_frameUpdateMissCallback != null)
|
||||
{
|
||||
_frameUpdateMissCallback();
|
||||
|
||||
foreach( TimeSpanAwaitingToken item in _awaitingTokens ) {
|
||||
if( item.AwaitingSemaphoreSlim.CurrentCount == 0 ) {
|
||||
item.AwaitingSemaphoreSlim.Release();
|
||||
item.Refresh();
|
||||
} else {
|
||||
item.MissRefresh();
|
||||
}
|
||||
}
|
||||
//Console.Write("5 ");
|
||||
//Console.WriteLine( $"{timeSpanCount}\n" );
|
||||
|
||||
return timeSpanCount < _maxSpanCount;
|
||||
}
|
||||
|
||||
public static implicit operator int(TimeSpanProvider provider)
|
||||
public static explicit operator int(
|
||||
[NotNull]TimeSpanProvider provider )
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return provider.ToInt32();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user