-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocklessLazyWriteOnceValue.cs
127 lines (109 loc) · 3.78 KB
/
LocklessLazyWriteOnceValue.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace HpTimeStamps
{
[DataContract]
internal sealed class LocklessWriteOnceValue<TValue> where TValue : struct
{
public bool IsSet => _threeStapFlag.Code == ThreeStepFlagCode.Complete;
public ref readonly TValue Value
{
get
{
if (_threeStapFlag.Code != ThreeStepFlagCode.Complete)
{
throw new InvalidOperationException("The value has not yet been set.");
}
return ref _value;
}
}
public bool TrySetValue(in TValue value)
{
if (_threeStapFlag.TryBegin())
{
_value = value;
_threeStapFlag.CompleteOrThrow();
return true;
}
return false;
}
public bool TrySetValue(TValue value) => TrySetValue(in value);
public void SetValueOrThrow(in TValue value)
{
if (!TrySetValue(in value)) throw new InvalidOperationException("Value has already been set.");
}
public void SetValueOrThrow(TValue value) => SetValueOrThrow(in value);
public override string ToString() =>
IsSet ? $"LocklessWriteOnceValue. Value: [{_value}]" : $"LocklessWriteOnceValue. Value NOT SET.";
private TValue _value;
private ReadOnlyThreeStepFlag _threeStapFlag = default;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TValue"></typeparam>
internal sealed class LocklessLazyWriteOnceValue<TValue> where TValue : struct
{
/// <summary>
///
/// </summary>
public bool IsSet => _threeStepFlag.Code == ThreeStepFlagCode.Complete;
public ref readonly TValue Value
{
get
{
EnsureAvailability();
return ref _value;
}
}
/// <summary>
///
/// </summary>
/// <param name="ctor"></param>
public LocklessLazyWriteOnceValue(Func<TValue> ctor) =>
_ctor = ctor ?? throw new ArgumentNullException(nameof(ctor));
public void SupplyNonDefaultValueOrThrow(in TValue nonDefaultVal)
{
if (!TrySupplyNonDefaultValue(in nonDefaultVal))
{
throw new InvalidOperationException(
"The value has already been set either threw default initialization or by a prior explicit setting.");
}
}
public bool TrySupplyNonDefaultValue(in TValue nonDefaultVal)
{
if (_threeStepFlag.TryBegin())
{
_value = nonDefaultVal;
_threeStepFlag.CompleteOrThrow();
return true;
}
return false;
}
private void EnsureAvailability()
{
while (_threeStepFlag.Code != ThreeStepFlagCode.Complete)
{
if (_threeStepFlag.TryBegin())
{
try
{
_value = _ctor();
}
catch (Exception ex)
{
_threeStepFlag.ErrorOutOrThrow();
throw new InvalidOperationException(
"Initialization delegate threw exception. Consult inner exception for details.", ex);
}
_threeStepFlag.CompleteOrThrow();
}
}
Debug.Assert(_threeStepFlag.Code == ThreeStepFlagCode.Complete);
}
private TValue _value;
private ReadOnlyThreeStepFlag _threeStepFlag;
private readonly Func<TValue> _ctor;
}
}