Skip to content

Commit aee8043

Browse files
committed
Fix concurrency bug.
1 parent abea17a commit aee8043

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/MsgPack/Serialization/DefaultSerializers/MultidimensionalArraySerializer`1.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region -- License Terms --
1+
#region -- License Terms --
22
//
33
// MessagePack for CLI
44
//
@@ -444,7 +444,7 @@ this.OwnerContext.ExtTypeCodeMapping[ KnownExtTypeName.MultidimensionalArray ]
444444
var totalLength = UnpackHelpers.GetItemsCount( arrayUnpacker );
445445
if ( totalLength > 0 )
446446
{
447-
ForEach(
447+
await ForEachAsync(
448448
result,
449449
totalLength,
450450
lengthsAndLowerBounds.Item2,
@@ -463,7 +463,7 @@ await this._itemSerializer.UnpackFromAsync( arrayUnpacker, cancellationToken ).C
463463
);
464464
// ReSharper restore AccessToDisposedClosure
465465
}
466-
);
466+
).ConfigureAwait( false );
467467
}
468468

469469
return ( TArray )( object )result;
@@ -536,5 +536,35 @@ private static void ForEach( Array array, int totalLength, int[] lowerBounds, in
536536
}
537537
}
538538
}
539+
540+
#if FEATURE_TAP
541+
542+
private static async Task ForEachAsync( Array array, int totalLength, int[] lowerBounds, int[] lengths, Func<int[], Task> action )
543+
{
544+
var indices = new int[ array.Rank ];
545+
for ( var dimension = 0; dimension < array.Rank; dimension++ )
546+
{
547+
indices[ dimension ] = lowerBounds[ dimension ];
548+
}
549+
550+
for ( var i = 0; i < totalLength; i++ )
551+
{
552+
await action( indices ).ConfigureAwait( false );
553+
// Canculate indices with carrying up.
554+
var dimension = indices.Length - 1;
555+
for ( ; dimension >= 0; dimension-- )
556+
{
557+
if ( ( indices[ dimension ] + 1 ) < lengths[ dimension ] + lowerBounds[ dimension ] )
558+
{
559+
indices[ dimension ]++;
560+
break;
561+
}
562+
563+
// Let's carry up, so set 0 to current dimension.
564+
indices[ dimension ] = lowerBounds[ dimension ];
565+
}
566+
}
567+
}
568+
#endif // FEATURE_TAP
539569
}
540570
}

0 commit comments

Comments
 (0)