Daily update on 20250618-051714 by Nitload Youmu. Additional Message : Brief support to collection operation in EXPRESS. A collection type added in core lib and compilestack.
Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
@@ -44,9 +44,9 @@ namespace StgSharp.Timing
|
||||
internal TimeFunction() { }
|
||||
|
||||
public TimeFunction(
|
||||
TimeSpanProvider timeSpanProvider,
|
||||
Func<float, float> timeRelatedCalculation,
|
||||
float range )
|
||||
TimeSpanProvider timeSpanProvider,
|
||||
Func<float, float> timeRelatedCalculation,
|
||||
float range )
|
||||
{
|
||||
_timeProvider = timeSpanProvider;
|
||||
_timeRelatedCalculation = timeRelatedCalculation;
|
||||
@@ -84,28 +84,20 @@ namespace StgSharp.Timing
|
||||
if( _timeProvider.CurrentSecond < _ending ) {
|
||||
return 0;
|
||||
}
|
||||
return _timeRelatedCalculation(
|
||||
_timeProvider.CurrentSecond - _beginning );
|
||||
return _timeRelatedCalculation( _timeProvider.CurrentSecond - _beginning );
|
||||
}
|
||||
|
||||
public static TimeFunction Proportional(
|
||||
TimeSpanProvider time,
|
||||
float begin,
|
||||
float range )
|
||||
public static TimeFunction Proportional( TimeSpanProvider time, float begin, float range )
|
||||
{
|
||||
return new TimeFunction( time, ( float t ) => t + begin, range );
|
||||
}
|
||||
|
||||
public virtual TimeFunction Renew(
|
||||
(Func<float, float> func, float range) funcPair )
|
||||
public virtual TimeFunction Renew( (Func<float, float> func, float range) funcPair )
|
||||
{
|
||||
return new TimeFunction(
|
||||
_timeProvider, funcPair.func, funcPair.range );
|
||||
return new TimeFunction( _timeProvider, funcPair.func, funcPair.range );
|
||||
}
|
||||
|
||||
public virtual TimeFunction Renew(
|
||||
Func<float, float> func,
|
||||
float range )
|
||||
public virtual TimeFunction Renew( Func<float, float> func, float range )
|
||||
{
|
||||
return new TimeFunction( _timeProvider, func, range );
|
||||
}
|
||||
@@ -121,8 +113,7 @@ namespace StgSharp.Timing
|
||||
{
|
||||
float range = _ending - _beginning;
|
||||
return new TimeFunction(
|
||||
_timeProvider, p => _timeRelatedCalculation( range - p ),
|
||||
range );
|
||||
_timeProvider, p => _timeRelatedCalculation( range - p ), range );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -142,15 +133,12 @@ namespace StgSharp.Timing
|
||||
return 1;
|
||||
}
|
||||
|
||||
public override TimeFunction Renew(
|
||||
(Func<float, float> func, float range) funcPair )
|
||||
public override TimeFunction Renew( (Func<float, float> func, float range) funcPair )
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override TimeFunction Renew(
|
||||
Func<float, float> func,
|
||||
float range )
|
||||
public override TimeFunction Renew( Func<float, float> func, float range )
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
using StgSharp.Timing;
|
||||
|
||||
using System;
|
||||
@@ -40,7 +39,7 @@ using System.Threading;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public static partial class StgSharp
|
||||
public static partial class World
|
||||
{
|
||||
|
||||
internal static StgSharpTime mainTimeProvider = new StgSharpTime();
|
||||
@@ -52,15 +51,14 @@ namespace StgSharp
|
||||
public sealed class StgSharpTime : TimeSourceProviderBase
|
||||
{
|
||||
|
||||
private static List<TimeSpanProvider> subscriberList = new List<TimeSpanProvider>(
|
||||
);
|
||||
private static List<TimeSpanProvider> subscriberList = new List<TimeSpanProvider>();
|
||||
private static long accuracy;
|
||||
private static Stopwatch mainProvider = new Stopwatch();
|
||||
private static Thread timeProvideThread;
|
||||
|
||||
internal StgSharpTime() { }
|
||||
|
||||
public static TimeSourceProviderBase OnlyInstance => StgSharp.MainTimeProvider;
|
||||
public static TimeSourceProviderBase OnlyInstance => World.MainTimeProvider;
|
||||
|
||||
public override sealed void StartProvidingTime()
|
||||
{
|
||||
@@ -71,13 +69,14 @@ namespace StgSharp
|
||||
|
||||
//speed test
|
||||
long totalMS, internalFrequncy = Stopwatch.Frequency;
|
||||
for( int i = 0; i < 100; i++ ) {
|
||||
for( int i = 0; i < 100; i++ )
|
||||
{
|
||||
totalMS = mainProvider.ElapsedTicks / ( internalFrequncy / ( 1000L * 1000L ) );
|
||||
for( int t = 0; t < 10; t++ ) {
|
||||
for( int t = 0; t < 10; t++ )
|
||||
{
|
||||
if( totalMS < 1000 ) {
|
||||
subscriberList.Add(
|
||||
new TimeSpanProvider(
|
||||
100L, TimeSpanProvider.DefaultProvider ) );
|
||||
new TimeSpanProvider( 100L, TimeSpanProvider.DefaultProvider ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,14 +103,12 @@ namespace StgSharp
|
||||
}
|
||||
}
|
||||
|
||||
protected override sealed void AddSubscriberUnsynced(
|
||||
TimeSpanProvider subscriber )
|
||||
protected override sealed void AddSubscriberUnsynced( TimeSpanProvider subscriber )
|
||||
{
|
||||
subscriberList.Add( subscriber );
|
||||
}
|
||||
|
||||
protected override sealed void RemoveSubscriberUnsynced(
|
||||
TimeSpanProvider subscriber )
|
||||
protected override sealed void RemoveSubscriberUnsynced( TimeSpanProvider subscriber )
|
||||
{
|
||||
subscriberList.Remove( subscriber );
|
||||
}
|
||||
@@ -122,24 +119,27 @@ namespace StgSharp
|
||||
lock( mainProvider ) {
|
||||
mainProvider.Restart();
|
||||
}
|
||||
while( mainProvider.IsRunning ) {
|
||||
while( mainProvider.IsRunning )
|
||||
{
|
||||
totalMS = mainProvider.ElapsedTicks / ( internalFrequncy / ( 1000L * 1000L ) );
|
||||
if( subscriberList.Count == 0 ) {
|
||||
if( subscriberList.Count == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
CheckSubscriberSemaphore.Wait();
|
||||
List<TimeSpanProvider> toRemoveList = new List<TimeSpanProvider>(
|
||||
);
|
||||
foreach( TimeSpanProvider subscriber in subscriberList ) {
|
||||
if( !subscriber.CheckTime( totalMS ) ) {
|
||||
List<TimeSpanProvider> toRemoveList = new List<TimeSpanProvider>();
|
||||
foreach( TimeSpanProvider subscriber in subscriberList )
|
||||
{
|
||||
if( !subscriber.CheckTime( totalMS ) )
|
||||
{
|
||||
toRemoveList.Add( subscriber );
|
||||
InternalIO.InternalWriteLog(
|
||||
$"Time subscriber {subscriber} is to be removed.",
|
||||
LogType.Info );
|
||||
$"Time subscriber {subscriber} is to be removed.", LogType.Info );
|
||||
}
|
||||
;
|
||||
}
|
||||
if( toRemoveList.Count != 0 ) {
|
||||
if( toRemoveList.Count != 0 )
|
||||
{
|
||||
foreach( TimeSpanProvider subscriber in toRemoveList ) {
|
||||
subscriberList.Remove( subscriber );
|
||||
}
|
||||
|
||||
@@ -39,8 +39,7 @@ namespace StgSharp.Timing
|
||||
public abstract class TimeSourceProviderBase
|
||||
{
|
||||
|
||||
private SemaphoreSlim _addSubscriberSemaphore = new SemaphoreSlim(
|
||||
1, 1 );
|
||||
private SemaphoreSlim _addSubscriberSemaphore = new SemaphoreSlim( 1, 1 );
|
||||
|
||||
protected SemaphoreSlim CheckSubscriberSemaphore
|
||||
{
|
||||
@@ -49,14 +48,17 @@ namespace StgSharp.Timing
|
||||
|
||||
public void AddSubscriber( TimeSpanProvider subscriber )
|
||||
{
|
||||
try {
|
||||
try
|
||||
{
|
||||
_addSubscriberSemaphore.Wait();
|
||||
AddSubscriberUnsynced( subscriber );
|
||||
}
|
||||
catch( Exception ) {
|
||||
catch( Exception )
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally {
|
||||
finally
|
||||
{
|
||||
_addSubscriberSemaphore.Release();
|
||||
}
|
||||
}
|
||||
@@ -64,13 +66,16 @@ namespace StgSharp.Timing
|
||||
public void RemoveSubscriber( TimeSpanProvider subscriber )
|
||||
{
|
||||
_addSubscriberSemaphore.Wait();
|
||||
try {
|
||||
try
|
||||
{
|
||||
RemoveSubscriberUnsynced( subscriber );
|
||||
}
|
||||
catch( Exception ) {
|
||||
catch( Exception )
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally {
|
||||
finally
|
||||
{
|
||||
_addSubscriberSemaphore.Release();
|
||||
}
|
||||
}
|
||||
@@ -79,11 +84,9 @@ namespace StgSharp.Timing
|
||||
|
||||
public abstract void StopProvidingTime();
|
||||
|
||||
protected abstract void AddSubscriberUnsynced(
|
||||
TimeSpanProvider subscriber );
|
||||
protected abstract void AddSubscriberUnsynced( TimeSpanProvider subscriber );
|
||||
|
||||
protected abstract void RemoveSubscriberUnsynced(
|
||||
TimeSpanProvider subscriber );
|
||||
protected abstract void RemoveSubscriberUnsynced( TimeSpanProvider subscriber );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +45,7 @@ namespace StgSharp.Timing
|
||||
public sealed class TimeSpanAwaitingToken : IDisposable
|
||||
{
|
||||
|
||||
private static readonly ConcurrentStack<int> s_unusedID = new ConcurrentStack<int>(
|
||||
[0] );
|
||||
private static readonly ConcurrentStack<int> s_unusedID = new ConcurrentStack<int>( [0] );
|
||||
private static int s_maxID;
|
||||
private static readonly object s_lock = new object();
|
||||
|
||||
@@ -60,13 +59,16 @@ namespace StgSharp.Timing
|
||||
|
||||
internal TimeSpanAwaitingToken( TimeSpanProvider provider )
|
||||
{
|
||||
if( s_unusedID.Count == 1 ) {
|
||||
lock( s_lock ) {
|
||||
if( s_unusedID.Count == 1 )
|
||||
{
|
||||
lock( s_lock )
|
||||
{
|
||||
s_unusedID.TryPop( out int id );
|
||||
TokenID = id;
|
||||
s_unusedID.Push( s_maxID++ );
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
s_unusedID.TryPop( out int id );
|
||||
TokenID = id;
|
||||
}
|
||||
@@ -77,13 +79,16 @@ namespace StgSharp.Timing
|
||||
|
||||
internal TimeSpanAwaitingToken( TimeSpanProvider provider, int count )
|
||||
{
|
||||
if( s_unusedID.Count == 1 ) {
|
||||
lock( s_lock ) {
|
||||
if( s_unusedID.Count == 1 )
|
||||
{
|
||||
lock( s_lock )
|
||||
{
|
||||
s_unusedID.TryPop( out int id );
|
||||
TokenID = id;
|
||||
s_unusedID.Push( s_maxID++ );
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
s_unusedID.TryPop( out int id );
|
||||
TokenID = id;
|
||||
}
|
||||
@@ -130,12 +135,12 @@ namespace StgSharp.Timing
|
||||
|
||||
public void Participant()
|
||||
{
|
||||
lock( _participantObject ) {
|
||||
lock( _participantObject )
|
||||
{
|
||||
_size++;
|
||||
SemaphoreSlim newSemaphore = new SemaphoreSlim(
|
||||
_awaitingSemaphore.CurrentCount, _size );
|
||||
Interlocked.Exchange( ref _awaitingSemaphore, newSemaphore )
|
||||
.Dispose();
|
||||
Interlocked.Exchange( ref _awaitingSemaphore, newSemaphore ).Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="TimeSpanProvider.cs"
|
||||
// Project: StgSharp
|
||||
// Project: StepVisualizer
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
@@ -28,7 +28,6 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
@@ -40,7 +39,7 @@ using System.Threading;
|
||||
namespace StgSharp.Timing
|
||||
{
|
||||
/// <summary>
|
||||
/// A microsecond-accuracy timer. It will refresh every a certain time span.
|
||||
/// A microsecond-accuracy timer. It will refresh every a certain time span.
|
||||
/// </summary>
|
||||
public class TimeSpanProvider
|
||||
{
|
||||
@@ -58,18 +57,18 @@ namespace StgSharp.Timing
|
||||
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 )
|
||||
/// <param _label="spanMicroSeconds">
|
||||
///
|
||||
/// </param>
|
||||
/// <param _label="provider">
|
||||
///
|
||||
/// </param>
|
||||
public TimeSpanProvider( long spanMicroSeconds, TimeSourceProviderBase provider )
|
||||
{
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull( provider );
|
||||
_timeSource = provider;
|
||||
_spanLength = spanMicroSeconds;
|
||||
_maxSpanCount = int.MaxValue;
|
||||
@@ -80,18 +79,18 @@ namespace StgSharp.Timing
|
||||
}
|
||||
|
||||
/// <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 )
|
||||
/// <param _label="spanSeconds">
|
||||
///
|
||||
/// </param>
|
||||
/// <param _label="provider">
|
||||
///
|
||||
/// </param>
|
||||
public TimeSpanProvider( double spanSeconds, TimeSourceProviderBase provider )
|
||||
{
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull( provider );
|
||||
_timeSource = provider;
|
||||
_spanLength = ( long )( spanSeconds * 1000L * 1000L );
|
||||
_spanBegin = -_spanLength;
|
||||
@@ -103,23 +102,23 @@ namespace StgSharp.Timing
|
||||
}
|
||||
|
||||
/// <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,
|
||||
long maxSpan,
|
||||
TimeSourceProviderBase provider )
|
||||
/// <param _label="spanLength">
|
||||
///
|
||||
/// </param>
|
||||
/// <param _label="maxSpan">
|
||||
///
|
||||
/// </param>
|
||||
/// <param _label="provider">
|
||||
///
|
||||
/// </param>
|
||||
public TimeSpanProvider( long spanLength, long maxSpan, TimeSourceProviderBase provider )
|
||||
{
|
||||
if( provider == null ) {
|
||||
throw new ArgumentNullException( nameof( provider ) );
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull( provider );
|
||||
if( maxSpan <= 0 ) {
|
||||
throw new ArgumentException(
|
||||
"Sapn count cannot be smaller than zero" );
|
||||
throw new ArgumentException( "Sapn count cannot be smaller than zero" );
|
||||
}
|
||||
_timeSource = provider;
|
||||
_spanLength = spanLength;
|
||||
@@ -151,14 +150,13 @@ namespace StgSharp.Timing
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Time source provider provided in <see cref="StgSharp" /> internal framework.
|
||||
/// Time source provider provided in <see cref="World" /> internal framework.
|
||||
/// </summary>
|
||||
public static TimeSourceProviderBase DefaultProvider => StgSharpTime.OnlyInstance;
|
||||
|
||||
public TimeSpanAwaitingToken ParticipantSpanAwaiting( int count )
|
||||
{
|
||||
TimeSpanAwaitingToken token = new TimeSpanAwaitingToken(
|
||||
this, count );
|
||||
TimeSpanAwaitingToken token = new TimeSpanAwaitingToken( this, count );
|
||||
_tokenToAdd.Enqueue( token );
|
||||
return token;
|
||||
}
|
||||
@@ -173,12 +171,9 @@ namespace StgSharp.Timing
|
||||
Interlocked.Exchange( ref _maxSpanCount, 0 );
|
||||
}
|
||||
|
||||
public void SubscribeToTimeSource(
|
||||
TimeSourceProviderBase serviceHandler )
|
||||
public void SubscribeToTimeSource( TimeSourceProviderBase serviceHandler )
|
||||
{
|
||||
if( serviceHandler == null ) {
|
||||
throw new ArgumentNullException( nameof( serviceHandler ) );
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull( serviceHandler );
|
||||
if( _subscribed ) {
|
||||
return;
|
||||
}
|
||||
@@ -196,26 +191,29 @@ namespace StgSharp.Timing
|
||||
if( microseconds - _spanBegin < _spanLength ) {
|
||||
return true;
|
||||
}
|
||||
while( !_tokenToAdd.IsEmpty ) {
|
||||
_tokenToAdd.TryDequeue( out TimeSpanAwaitingToken tokenAdd );
|
||||
foreach( TimeSpanAwaitingToken tokenAdd in _tokenToAdd ) {
|
||||
_awaitingTokens.Add( tokenAdd );
|
||||
}
|
||||
while( !_tokenToQuit.IsEmpty ) {
|
||||
_tokenToQuit.TryDequeue( out TimeSpanAwaitingToken tokenQuit );
|
||||
_tokenToAdd.Clear();
|
||||
foreach( TimeSpanAwaitingToken tokenQuit in _tokenToQuit )
|
||||
{
|
||||
_awaitingTokens.Remove( tokenQuit );
|
||||
InternalIO.InternalWriteLog(
|
||||
$"Awaiting token {tokenQuit!.TokenID} has been removed.",
|
||||
LogType.Info );
|
||||
$"Awaiting token {tokenQuit!.TokenID} has been removed.", LogType.Info );
|
||||
}
|
||||
_tokenToQuit.Clear();
|
||||
|
||||
_spanBegin = microseconds;
|
||||
timeSpanCount++;
|
||||
|
||||
foreach( TimeSpanAwaitingToken item in _awaitingTokens ) {
|
||||
if( item.AwaitingSemaphoreSlim.CurrentCount == 0 ) {
|
||||
foreach( TimeSpanAwaitingToken item in _awaitingTokens )
|
||||
{
|
||||
if( item.AwaitingSemaphoreSlim.CurrentCount == 0 )
|
||||
{
|
||||
item.AwaitingSemaphoreSlim.Release();
|
||||
item.Refresh();
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
item.MissRefresh();
|
||||
}
|
||||
}
|
||||
@@ -223,8 +221,7 @@ namespace StgSharp.Timing
|
||||
return timeSpanCount < _maxSpanCount;
|
||||
}
|
||||
|
||||
public static explicit operator int(
|
||||
[NotNull]TimeSpanProvider provider )
|
||||
public static explicit operator int( [NotNull]TimeSpanProvider provider )
|
||||
{
|
||||
return provider.ToInt32();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user