Skip to content

Commit c020670

Browse files
committed
Remove race on date update
1 parent cfe0190 commit c020670

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

experimental/ManagedRIOHttpServer/Program.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public sealed class Program
2424
"Date:DDD, dd mmm yyyy hh:mm:ss GMT" +
2525
"\r\n\r\n";
2626

27-
private static byte[] _headersBytes = Encoding.ASCII.GetBytes(headersKeepAliveStr);
27+
private static byte[][] _headersBytesBuffers = new byte[][] {
28+
Encoding.ASCII.GetBytes(headersKeepAliveStr),
29+
Encoding.ASCII.GetBytes(headersKeepAliveStr)
30+
};
31+
private static byte[] _headersBytes;
2832

2933
private static readonly string bodyStr = "Hello, World!";
3034

@@ -33,11 +37,14 @@ public sealed class Program
3337

3438
static Program()
3539
{
36-
var start = _headersBytes.Length - 33;
40+
var start = _headersBytesBuffers[0].Length - 33;
41+
var loop = 0u;
3742
UpdateDateTimer = new Timer((obj) =>
3843
{
39-
var date = DateTime.UtcNow.ToString("r");
40-
Encoding.ASCII.GetBytes(date, 0, date.Length, _headersBytes, start);
44+
var date = DateTime.UtcNow.ToString("r");
45+
var index = (++loop) & 1;
46+
Encoding.ASCII.GetBytes(date, 0, date.Length, _headersBytesBuffers[index], start);
47+
_headersBytes = _headersBytesBuffers[index];
4148
}, null, 0, 1000);
4249
}
4350

@@ -46,7 +53,7 @@ static void Main(string[] args)
4653
Console.WriteLine("Starting Managed Registered IO Server");
4754
Console.WriteLine("* Hardware Accelerated SIMD: {0}", Vector.IsHardwareAccelerated);
4855
Console.WriteLine("* Vector<byte>.Count: {0}", Vector<byte>.Count);
49-
56+
5057
if (IntPtr.Size != 8)
5158
{
5259
Console.WriteLine("ManagedRIOHttpServer needs to be run in x64 mode");
@@ -186,7 +193,7 @@ static async Task ServeSocket(RIOTcpConnection socket)
186193

187194
var ul = r - 3;
188195
var hasStart = false;
189-
196+
190197

191198
for (var i = start; i < buffer.Length - Vector<byte>.Count; i += Vector<byte>.Count)
192199
{
@@ -284,8 +291,8 @@ static async Task ServeSocket(RIOTcpConnection socket)
284291
socket.SendCachedBad();
285292
break;
286293
}
287-
288-
294+
295+
289296
for (var i = 1; i < count; i++)
290297
{
291298
socket.QueueSend(headerBuffer, false);
@@ -317,7 +324,7 @@ static async Task ServeSocket(RIOTcpConnection socket)
317324
// }
318325
// var A = new Vector<byte>(65); // A
319326
// var Z = new Vector<byte>(90); // Z
320-
327+
321328
// for (var o = data.Offset; o < data.Count - Vector<byte>.Count; o += Vector<byte>.Count)
322329
// {
323330
// var v = new Vector<byte>(data.Array, o);

0 commit comments

Comments
 (0)