Skip to content

Commit b52e282

Browse files
committed
Applied my locking rule of thumb: apply lock only in public methods
The only exception of this rule are private/protected methods running in a separate thread, which is not the case here.
1 parent 9df5c22 commit b52e282

1 file changed

Lines changed: 82 additions & 103 deletions

File tree

libraries/MTConnect.NET-Common/Buffers/MTConnectObservationBuffer.cs

Lines changed: 82 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public IDictionary<int, BufferObservation> CurrentObservations
7474
{
7575
get
7676
{
77-
lock (_lock) return _currentObservations;
77+
lock (_lock) return new Dictionary<int, BufferObservation>(_currentObservations);
7878
}
7979
}
8080

8181
public IDictionary<int, IEnumerable<BufferObservation>> CurrentConditions
8282
{
8383
get
8484
{
85-
lock (_lock) return _currentConditions;
85+
lock (_lock) return new Dictionary<int, IEnumerable<BufferObservation>>(_currentConditions);
8686
}
8787
}
8888

@@ -127,10 +127,7 @@ protected virtual void OnBufferObservationAdd(ref BufferObservation observation)
127127
/// </summary>
128128
protected void SetSequence(ulong sequence)
129129
{
130-
lock (_lock)
131-
{
132-
_sequence = sequence;
133-
}
130+
_sequence = sequence;
134131
}
135132

136133
/// <summary>
@@ -163,28 +160,17 @@ public void IncrementSequence(uint count)
163160

164161
protected IEnumerable<BufferObservation> GetCurrentObservations()
165162
{
166-
var x = new List<BufferObservation>();
167-
168-
lock (_lock)
169-
{
170-
var observations = _currentObservations.Values;
171-
if (!observations.IsNullOrEmpty()) x.AddRange(observations);
172-
}
173-
174-
return x;
163+
return new List<BufferObservation>(_currentObservations.Values);
175164
}
176165

177166
protected IEnumerable<BufferObservation> GetCurrentConditions()
178167
{
179168
var x = new List<BufferObservation>();
180169

181-
lock (_lock)
170+
var conditions = _currentConditions.Values;
171+
if (!conditions.IsNullOrEmpty())
182172
{
183-
var conditions = _currentConditions.Values;
184-
if (!conditions.IsNullOrEmpty())
185-
{
186-
foreach (var condition in conditions) x.AddRange(condition);
187-
}
173+
foreach (var condition in conditions) x.AddRange(condition);
188174
}
189175

190176
return x;
@@ -584,46 +570,42 @@ protected void AddCurrentObservation(BufferObservation observation)
584570
// Check for UNAVAILABLE
585571
var isUnavailable = observation.GetValue(ValueKeys.Result) == Observation.Unavailable;
586572

587-
BufferObservation existingObservation;
588-
lock (_lock)
573+
_currentObservations.TryGetValue(observation._key, out var existingObservation);
574+
_currentObservations.Remove(observation._key);
575+
576+
if (existingObservation.IsValid && !isUnavailable)
589577
{
590-
_currentObservations.TryGetValue(observation._key, out existingObservation);
591-
_currentObservations.Remove(observation._key);
592-
593-
if (existingObservation.IsValid && !isUnavailable)
578+
if (resetTriggered == ResetTriggered.NOT_SPECIFIED)
594579
{
595-
if (resetTriggered == ResetTriggered.NOT_SPECIFIED)
580+
// Update Observations based on Representation
581+
switch (observation.Representation)
596582
{
597-
// Update Observations based on Representation
598-
switch (observation.Representation)
599-
{
600-
case DataItemRepresentation.DATA_SET:
583+
case DataItemRepresentation.DATA_SET:
601584

602-
// Update DataSet Values
603-
var existingDataSetValues = GetDataSetValues(ref existingObservation);
604-
if (!existingDataSetValues.IsNullOrEmpty())
605-
{
606-
observation.Values = CombineDataSetValues(observation.Values, ref existingDataSetValues);
607-
}
585+
// Update DataSet Values
586+
var existingDataSetValues = GetDataSetValues(ref existingObservation);
587+
if (!existingDataSetValues.IsNullOrEmpty())
588+
{
589+
observation.Values = CombineDataSetValues(observation.Values, ref existingDataSetValues);
590+
}
608591

609-
break;
592+
break;
610593

611-
case DataItemRepresentation.TABLE:
594+
case DataItemRepresentation.TABLE:
612595

613-
// Update Table Values
614-
var existingTableValues = GetTableValues(ref existingObservation);
615-
if (!existingTableValues.IsNullOrEmpty())
616-
{
617-
observation.Values = CombineTableValues(observation.Values, ref existingTableValues);
618-
}
619-
break;
620-
}
596+
// Update Table Values
597+
var existingTableValues = GetTableValues(ref existingObservation);
598+
if (!existingTableValues.IsNullOrEmpty())
599+
{
600+
observation.Values = CombineTableValues(observation.Values, ref existingTableValues);
601+
}
602+
break;
621603
}
622604
}
623-
624-
_currentObservations.Add(observation._key, observation);
625605
}
626606

607+
_currentObservations.Add(observation._key, observation);
608+
627609
// Call Overridable Methods
628610
OnCurrentObservationAdd(ref observation);
629611
}
@@ -641,54 +623,49 @@ protected void AddCurrentCondition(BufferObservation observation)
641623
{
642624
var bufferObservations = new List<BufferObservation>();
643625

644-
IEnumerable<BufferObservation> existingObservations;
645-
IEnumerable<BufferObservation> iBufferObservations;
646-
lock (_lock)
626+
_currentConditions.TryGetValue(observation._key, out var existingObservations);
627+
_currentConditions.Remove(observation._key);
628+
629+
if (!existingObservations.IsNullOrEmpty())
647630
{
648-
_currentConditions.TryGetValue(observation._key, out existingObservations);
649-
_currentConditions.Remove(observation._key);
631+
var conditionLevel = observation.GetValue(ValueKeys.Level);
632+
var nativeCode = observation.GetValue(ValueKeys.NativeCode);
650633

651-
if (!existingObservations.IsNullOrEmpty())
634+
if (!(conditionLevel == ConditionLevel.NORMAL.ToString() && string.IsNullOrEmpty(nativeCode)) &&
635+
conditionLevel != ConditionLevel.UNAVAILABLE.ToString())
652636
{
653-
var conditionLevel = observation.GetValue(ValueKeys.Level);
654-
var nativeCode = observation.GetValue(ValueKeys.NativeCode);
655-
656-
if (!(conditionLevel == ConditionLevel.NORMAL.ToString() && string.IsNullOrEmpty(nativeCode)) &&
657-
conditionLevel != ConditionLevel.UNAVAILABLE.ToString())
637+
foreach (var existingObservation in existingObservations)
658638
{
659-
foreach (var existingObservation in existingObservations)
660-
{
661-
var existingLevel = existingObservation.GetValue(ValueKeys.Level);
662-
var existingNativeCode = existingObservation.GetValue(ValueKeys.NativeCode);
639+
var existingLevel = existingObservation.GetValue(ValueKeys.Level);
640+
var existingNativeCode = existingObservation.GetValue(ValueKeys.NativeCode);
663641

664-
if (existingNativeCode != nativeCode &&
665-
existingLevel != ConditionLevel.UNAVAILABLE.ToString() &&
666-
existingObservation.Sequence != observation.Sequence)
667-
{
668-
bufferObservations.Add(existingObservation);
669-
}
642+
if (existingNativeCode != nativeCode &&
643+
existingLevel != ConditionLevel.UNAVAILABLE.ToString() &&
644+
existingObservation.Sequence != observation.Sequence)
645+
{
646+
bufferObservations.Add(existingObservation);
670647
}
671648
}
672649
}
650+
}
673651

674-
// Add the new Observation
675-
bufferObservations.Add(observation);
652+
// Add the new Observation
653+
bufferObservations.Add(observation);
676654

677-
// If any WARNING or FAULT states present, then remove any NORMAL states
678-
// Current should only show the active states
679-
if (bufferObservations.Any(o =>
680-
o.GetValue(ValueKeys.Level) == ConditionLevel.WARNING.ToString() ||
681-
o.GetValue(ValueKeys.Level) == ConditionLevel.FAULT.ToString()))
682-
{
683-
bufferObservations.RemoveAll(o =>
684-
o.GetValue(ValueKeys.Level) == ConditionLevel.NORMAL.ToString());
685-
}
655+
// If any WARNING or FAULT states present, then remove any NORMAL states
656+
// Current should only show the active states
657+
if (bufferObservations.Any(o =>
658+
o.GetValue(ValueKeys.Level) == ConditionLevel.WARNING.ToString() ||
659+
o.GetValue(ValueKeys.Level) == ConditionLevel.FAULT.ToString()))
660+
{
661+
bufferObservations.RemoveAll(o =>
662+
o.GetValue(ValueKeys.Level) == ConditionLevel.NORMAL.ToString());
663+
}
686664

687-
iBufferObservations = bufferObservations;
665+
IEnumerable<BufferObservation> iBufferObservations = bufferObservations;
688666

689-
// Add to stored List
690-
_currentConditions.Add(observation._key, iBufferObservations);
691-
}
667+
// Add to stored List
668+
_currentConditions.Add(observation._key, iBufferObservations);
692669

693670
// Call Overridable Method
694671
OnCurrentConditionChange(GetCurrentConditions());
@@ -845,27 +822,29 @@ public virtual ulong AddObservation(ref BufferObservation observation)
845822
{
846823
if (observation._key >= 0 && !observation._values.IsNullOrEmpty() && observation._timestamp > 0)
847824
{
848-
// Get the Sequence to Add Observation at
849-
ulong sequence;
850-
lock (_lock) sequence = _sequence++;
825+
lock (_lock)
826+
{
827+
// Get the Sequence to Add Observation at
828+
ulong sequence = _sequence++;
851829

852-
observation._sequence = sequence;
830+
observation._sequence = sequence;
853831

854-
if (observation.Category == DataItemCategory.CONDITION)
855-
{
856-
// Add to Current Conditions
857-
AddCurrentCondition(observation);
858-
}
859-
else
860-
{
861-
// Add to Current Observations
862-
AddCurrentObservation(observation);
863-
}
832+
if (observation.Category == DataItemCategory.CONDITION)
833+
{
834+
// Add to Current Conditions
835+
AddCurrentCondition(observation);
836+
}
837+
else
838+
{
839+
// Add to Current Observations
840+
AddCurrentObservation(observation);
841+
}
864842

865-
// Add to Buffer
866-
AddBufferObservation(ref observation);
843+
// Add to Buffer
844+
AddBufferObservation(ref observation);
867845

868-
return sequence;
846+
return sequence;
847+
}
869848
}
870849

871850
return 0;

0 commit comments

Comments
 (0)