singrdk/base/Services/Tests/Replace/ReplaceWorker.sg

93 lines
3.2 KiB
Plaintext

///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Service\Test\Replace\ReplaceWorker.sg
//
// Note:
//
using System;
using System.Threading;
using Microsoft.SingSharp;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.Resiliency;
using Microsoft.Singularity.ServiceManager;
using Microsoft.Singularity.Services;
namespace Microsoft.Singularity.Services.Replace
{
internal sealed class ReplaceWorker : IRunnable
{
private TRef<ThreadTerminationContract.Exp:Start> signalRef;
private TRef<GamePlayerContract.Exp:Start>! gameRef;
private GameTable! table;
internal ReplaceWorker([Claims]GamePlayerContract.Exp:Start! ep,
GameTable! table)
{
this.gameRef = new TRef<GamePlayerContract.Exp:Start>(ep);
this.table = table;
}
public void Signal([Claims]ThreadTerminationContract.Exp:Start! ep)
{
signalRef = new TRef<ThreadTerminationContract.Exp:Start>(ep);
}
public void Run()
{
GamePlayerContract.Exp:Start! ep;
ThreadTerminationContract.Exp:Start! signal;
ep = gameRef.Acquire();
ep.SendSuccess();
assert signalRef != null;
signal = signalRef.Acquire();
for (;;) {
switch receive {
case ep.Search(query):
int current = table.Lookup(Bitter.ToString2(query));
ep.SendAckSearch(current);
delete query;
break;
case ep.Overwrite(replacement, position):
table.Replace(Bitter.ToString2(replacement), position);
ep.SendAckOverwrite();
//Checkpoint();
table.Commit();
delete replacement;
break;
case ep.Protect(position):
table.Lock(position);
ep.SendAckProtect();
break;
case ep.Score(query):
int score = table.Score(Bitter.ToString2(query));
ep.SendAckScore(score);
delete query;
break;
case ep.ChannelClosed():
goto exit;
break;
case signal.Stop():
signal.SendAckStop();
goto exit;
break;
case signal.ChannelClosed():
goto exit;
break;
}
} // End of loop
exit:
gameRef.Release(ep);
assert signalRef != null;
signalRef.Release(signal);
}
}
}