Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace ThreadStatic with ThreadLocal for Thread Safety #2

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions CodeGenerator/ProtocolParser/ProtocolParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public interface IProto

public static partial class ProtocolParser
{
// Seperate copy of buffer per thread
[ThreadStatic]
private static byte[] staticBuffer = new byte[ 1024 * 128 ];
// Thread static does not support intializers use threadlocal and access using staticBuffer.Value
private static ThreadLocal<byte[]> staticBuffer = new ThreadLocal<byte[]>(() => new byte[ 1024 * 128 ]);

public static float ReadSingle( Stream stream )
{
Expand All @@ -28,8 +27,8 @@ public static float ReadSingle( Stream stream )
return reader.Float();
}

stream.Read( staticBuffer, 0, 4 );
return staticBuffer.ReadUnsafe<float>();
stream.Read( staticBuffer.Value, 0, 4 );
return staticBuffer.Value.ReadUnsafe<float>();
}

public static void WriteSingle( Stream stream, float f )
Expand All @@ -40,8 +39,8 @@ public static void WriteSingle( Stream stream, float f )
return;
}

staticBuffer.WriteUnsafe( f );
stream.Write( staticBuffer, 0, 4 );
staticBuffer.Value.WriteUnsafe( f );
stream.Write( staticBuffer.Value, 0, 4 );
}

public static double ReadDouble( Stream stream )
Expand All @@ -51,8 +50,8 @@ public static double ReadDouble( Stream stream )
return reader.Double();
}

stream.Read( staticBuffer, 0, 8 );
return staticBuffer.ReadUnsafe<double>();
stream.Read( staticBuffer.Value, 0, 8 );
return staticBuffer.Value.ReadUnsafe<double>();
}

public static void WriteDouble( Stream stream, double f )
Expand All @@ -63,8 +62,8 @@ public static void WriteDouble( Stream stream, double f )
return;
}

staticBuffer.WriteUnsafe( f );
stream.Write( staticBuffer, 0, 8 );
staticBuffer.Value.WriteUnsafe( f );
stream.Write( staticBuffer.Value, 0, 8 );
}

public static string ReadString( Stream stream )
Expand All @@ -81,7 +80,7 @@ public static string ReadString( Stream stream )

string str;

if (length >= staticBuffer.Length)
if (length >= staticBuffer.Value.Length)
{
Profiler.BeginSample( "new Buffer" );
byte[] buffer = new byte[ length ];
Expand All @@ -91,8 +90,8 @@ public static string ReadString( Stream stream )
}
else
{
stream.Read( staticBuffer, 0, length );
str = Encoding.UTF8.GetString( staticBuffer, 0, length );
stream.Read( staticBuffer.Value, 0, length );
str = Encoding.UTF8.GetString( staticBuffer.Value, 0, length );
}

Profiler.EndSample();
Expand All @@ -109,10 +108,10 @@ public static void WriteString( Stream stream, string val )
}

Profiler.BeginSample( "ProtoParser.WriteString" );
var len = Encoding.UTF8.GetBytes( val, 0, val.Length, staticBuffer, 0 );
var len = Encoding.UTF8.GetBytes( val, 0, val.Length, staticBuffer.Value, 0 );

WriteUInt32( stream, (uint)len );
stream.Write( staticBuffer, 0, len );
stream.Write( staticBuffer.Value, 0, len );
Profiler.EndSample();
}

Expand Down Expand Up @@ -212,4 +211,4 @@ public static void WritePooledBytes( Stream stream, ArraySegment<byte> segment )
stream.Write( segment.Array, segment.Offset, segment.Count );
}
}
}
}