Skip to content

Commit 3920619

Browse files
committed
1. Merging "Buffer read" methods of TdsParserStateObject, port dotnet#667 to netfx. Replaced Thread.MemoryBarrier usage of netfx with Interlocked.MemoryBarrier.
1 parent 6beab6a commit 3920619

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ public bool TryReadByteArray(Span<byte> buff, int len)
310310
// Every time you call this method increment the offset and decrease len by the value of totalRead
311311
public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
312312
{
313+
#if NETFRAMEWORK
314+
TdsParser.ReliabilitySection.Assert("unreliable call to ReadByteArray"); // you need to setup for a thread abort somewhere before you call this method
315+
#endif
313316
totalRead = 0;
314317

315318
#if DEBUG
@@ -331,7 +334,7 @@ public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
331334
}
332335
#endif
333336

334-
Debug.Assert(buff == null || buff.Length >= len, "Invalid length sent to ReadByteArray()!");
337+
Debug.Assert(buff.IsEmpty || buff.Length >= len, "Invalid length sent to ReadByteArray()!");
335338

336339
// loop through and read up to array length
337340
while (len > 0)
@@ -368,6 +371,9 @@ public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
368371
// before the byte is returned.
369372
internal bool TryReadByte(out byte value)
370373
{
374+
#if NETFRAMEWORK
375+
TdsParser.ReliabilitySection.Assert("unreliable call to ReadByte"); // you need to setup for a thread abort somewhere before you call this method
376+
#endif
371377
Debug.Assert(_inBytesUsed >= 0 && _inBytesUsed <= _inBytesRead, "ERROR - TDSParser: _inBytesUsed < 0 or _inBytesUsed > _inBytesRead");
372378
value = 0;
373379

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,16 @@ public bool TryReadByteArray(Span<byte> buff, int len)
404404
// Every time you call this method increment the offset and decrease len by the value of totalRead
405405
public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
406406
{
407+
#if NETFRAMEWORK
407408
TdsParser.ReliabilitySection.Assert("unreliable call to ReadByteArray"); // you need to setup for a thread abort somewhere before you call this method
409+
#endif
408410
totalRead = 0;
409411

410412
#if DEBUG
411413
if (_snapshot != null && _snapshot.DoPend())
412414
{
413415
_networkPacketTaskSource = new TaskCompletionSource<object>();
414-
Thread.MemoryBarrier();
416+
Interlocked.MemoryBarrier();
415417

416418
if (s_forcePendingReadsToWaitForUser)
417419
{
@@ -463,15 +465,17 @@ public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
463465
// before the byte is returned.
464466
internal bool TryReadByte(out byte value)
465467
{
468+
#if NETFRAMEWORK
466469
TdsParser.ReliabilitySection.Assert("unreliable call to ReadByte"); // you need to setup for a thread abort somewhere before you call this method
470+
#endif
467471
Debug.Assert(_inBytesUsed >= 0 && _inBytesUsed <= _inBytesRead, "ERROR - TDSParser: _inBytesUsed < 0 or _inBytesUsed > _inBytesRead");
468472
value = 0;
469473

470474
#if DEBUG
471475
if (_snapshot != null && _snapshot.DoPend())
472476
{
473477
_networkPacketTaskSource = new TaskCompletionSource<object>();
474-
Thread.MemoryBarrier();
478+
Interlocked.MemoryBarrier();
475479

476480
if (s_forcePendingReadsToWaitForUser)
477481
{
@@ -510,35 +514,28 @@ internal bool TryReadChar(out char value)
510514
{
511515
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
512516

513-
byte[] buffer;
514-
int offset;
517+
Span<byte> buffer = stackalloc byte[2];
515518
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
516519
{
517520
// If the char isn't fully in the buffer, or if it isn't fully in the packet,
518521
// then use ReadByteArray since the logic is there to take care of that.
519-
if (!TryReadByteArray(_bTmp, 2))
522+
if (!TryReadByteArray(buffer, 2))
520523
{
521524
value = '\0';
522525
return false;
523526
}
524-
525-
buffer = _bTmp;
526-
offset = 0;
527527
}
528528
else
529529
{
530530
// The entire char is in the packet and in the buffer, so just return it
531531
// and take care of the counters.
532-
533-
buffer = _inBuff;
534-
offset = _inBytesUsed;
535-
532+
buffer = _inBuff.AsSpan(_inBytesUsed, 2);
536533
_inBytesUsed += 2;
537534
_inBytesPacket -= 2;
538535
}
539536

540537
AssertValidState();
541-
value = (char)((buffer[offset + 1] << 8) + buffer[offset]);
538+
value = (char)((buffer[1] << 8) + buffer[0]);
542539

543540
return true;
544541
}

0 commit comments

Comments
 (0)