Create generic ThreadSafeValue

This commit is contained in:
Sebastian Godelet
2021-06-23 16:21:08 +10:00
parent 6dff32505e
commit 6c27499769
11 changed files with 35 additions and 102 deletions
+1 -4
View File
@@ -146,10 +146,7 @@
<Compile Include="ProfileProperties.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TelescopeList.cs" />
<Compile Include="ThreadSafeBool.cs" />
<Compile Include="ThreadSafeDateTime.cs" />
<Compile Include="ThreadSafeEnum.cs" />
<Compile Include="ThreadSafeNullableDouble.cs" />
<Compile Include="ThreadSafeValue.cs" />
<Compile Include="Win32Utilities.cs" />
<Compile Include="Wrapper\IProfileWrapper.cs" />
<Compile Include="Wrapper\SharedResourcesWrapper.cs" />
+8 -8
View File
@@ -502,7 +502,7 @@ namespace ASCOM.Meade.net
ParkedPosition = parkedPosition;
}
private static readonly ThreadSafeBool _isParked = false;
private static readonly ThreadSafeValue<bool> _isParked = false;
public static bool IsParked
{
get => _isParked;
@@ -516,7 +516,7 @@ namespace ASCOM.Meade.net
private set => Interlocked.Exchange(ref _parkedPosition, value);
}
private static readonly ThreadSafeEnum<PierSide> _sideOfPier = PierSide.pierUnknown;
private static readonly ThreadSafeValue<PierSide> _sideOfPier = PierSide.pierUnknown;
/// <summary>
/// Start with <see cref="PierSide.pierUnknown"/>.
/// As we do not know the physical declination axis position, we have to keep track manually.
@@ -527,14 +527,14 @@ namespace ASCOM.Meade.net
internal set => _sideOfPier.Set(value);
}
private static readonly ThreadSafeNullableDouble _targetRightAscension = null as double?;
private static readonly ThreadSafeValue<double?> _targetRightAscension = null as double?;
public static double? TargetRightAscension
{
get => _targetRightAscension;
internal set => _targetRightAscension.Set(value);
}
private static readonly ThreadSafeNullableDouble _targetDeclination = null as double?;
private static readonly ThreadSafeValue<double?> _targetDeclination = null as double?;
public static double? TargetDeclination
{
get => _targetDeclination;
@@ -548,28 +548,28 @@ namespace ASCOM.Meade.net
internal set => Interlocked.Exchange(ref _slewSettleTime, value);
}
private static readonly ThreadSafeBool _isLongFormat = false;
private static readonly ThreadSafeValue<bool> _isLongFormat = false;
public static bool IsLongFormat
{
get => _isLongFormat;
internal set => _isLongFormat.Set(value);
}
private static readonly ThreadSafeBool _movingPrimary = false;
private static readonly ThreadSafeValue<bool> _movingPrimary = false;
public static bool MovingPrimary
{
get => _movingPrimary;
internal set => _movingPrimary.Set(value);
}
private static readonly ThreadSafeBool _movingSecondary = false;
private static readonly ThreadSafeValue<bool> _movingSecondary = false;
public static bool MovingSecondary
{
get => _movingSecondary;
internal set => _movingSecondary.Set(value);
}
private static readonly ThreadSafeDateTime _earliestNonSlewingTime = DateTime.MinValue;
private static readonly ThreadSafeValue<DateTime> _earliestNonSlewingTime = DateTime.MinValue;
public static DateTime EarliestNonSlewingTime
{
get => _earliestNonSlewingTime;
-17
View File
@@ -1,17 +0,0 @@
using System.Threading;
namespace ASCOM.Meade.net
{
public class ThreadSafeBool
{
private object _value;
public ThreadSafeBool(in bool value) => _value = value;
public void Set(in bool value) => Interlocked.Exchange(ref _value, value);
public static implicit operator ThreadSafeBool(in bool value) => new ThreadSafeBool(value);
public static implicit operator bool(ThreadSafeBool @this) => (bool)@this._value;
}
}
-20
View File
@@ -1,20 +0,0 @@
using System;
using System.Threading;
namespace ASCOM.Meade.net
{
public class ThreadSafeDateTime
{
private long _value;
public ThreadSafeDateTime(in DateTime value) => _value = DateTimeToLong(value);
public void Set(in DateTime value) => Interlocked.Exchange(ref _value, DateTimeToLong(value));
private static long DateTimeToLong(in DateTime value) => value.ToUniversalTime().Ticks;
public static implicit operator ThreadSafeDateTime(in DateTime value) => new ThreadSafeDateTime(value);
public static implicit operator DateTime(ThreadSafeDateTime @this) => new DateTime(Interlocked.Read(ref @this._value), DateTimeKind.Utc);
}
}
-21
View File
@@ -1,21 +0,0 @@
using System;
using System.Threading;
namespace ASCOM.Meade.net
{
public class ThreadSafeEnum<T>
where T: struct, Enum
{
private long _value;
public ThreadSafeEnum(T value) => _value = EnumToLong(value);
public void Set(T value) => Interlocked.Exchange(ref _value, EnumToLong(value));
private static long EnumToLong(T value) => Convert.ToInt64(value);
public static implicit operator ThreadSafeEnum<T>(T value) => new ThreadSafeEnum<T>(value);
public static implicit operator T(ThreadSafeEnum<T> @this) => (T) Enum.ToObject(typeof(T), Interlocked.Read(ref @this._value));
}
}
-24
View File
@@ -1,24 +0,0 @@
using System;
using System.Threading;
namespace ASCOM.Meade.net
{
public class ThreadSafeNullableDouble
{
private long _value;
public ThreadSafeNullableDouble(in double? value) => _value = NullableDoubleToLong(value);
public void Set(in double? value) => Interlocked.Exchange(ref _value, NullableDoubleToLong(value));
private static long NullableDoubleToLong(in double? value) => BitConverter.DoubleToInt64Bits(value ?? double.NaN);
public static implicit operator ThreadSafeNullableDouble(in double? value) => new ThreadSafeNullableDouble(value);
public static implicit operator double?(ThreadSafeNullableDouble @this)
{
var doubleValue = BitConverter.Int64BitsToDouble(Interlocked.Read(ref @this._value));
return double.IsNaN(doubleValue) ? null as double? : doubleValue;
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using JetBrains.Annotations;
using System.Threading;
namespace ASCOM.Meade.net
{
public class ThreadSafeValue<T>
{
private object _value;
public ThreadSafeValue(in T value) => _value = value;
public void Set(in T value) => Interlocked.Exchange(ref _value, value);
public static implicit operator ThreadSafeValue<T>(in T value) => new ThreadSafeValue<T>(value);
public static implicit operator T([NotNull] ThreadSafeValue<T> @this) => (T)(@this?._value ?? default);
}
}