-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathReaderWriterLock.cs
51 lines (40 loc) · 1.09 KB
/
ReaderWriterLock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Threading.Tasks;
using Orleankka;
using Orleankka.Meta;
using Orleans;
using Orleans.Concurrency;
namespace Example
{
using Orleans.Serialization.Invocation;
[Serializable, GenerateSerializer]
public class Write : Command
{
[Id(0)] public int Value;
[Id(1)] public TimeSpan Delay;
}
[Serializable, GenerateSerializer]
public class Read : Query<int>
{}
public interface IReaderWriterLock : IActorGrain, IGrainWithStringKey
{}
[MayInterleave(nameof(Interleave))]
public class ReaderWriterLock : DispatchActorGrain, IReaderWriterLock
{
public static bool Interleave(IInvokable req) => req.Message() is Read;
int value;
ConsolePosition indicator;
void On(Activate _)
{
Console.Write("\nWrites: ");
indicator = ConsolePosition.Current();
}
async Task On(Write req)
{
value = req.Value;
indicator.Write(value);
await Task.Delay(req.Delay);
}
int On(Read req) => value;
}
}