public void TestPriorities() { var log = new List <int>(); var normal = SingleThreadScheduler.RunOnSeparateThread(TestLifetime, "s", s => { s.Queue(() => { log.Add(0); log.Add(1); log.Add(2); log.Add(3); log.Add(4); }); var hi = SingleThreadScheduler.CreateOverExisting(s, "Hi", PrioritizedAction.HighPriority); hi.Queue(() => log.Add(10)); hi.Queue(() => log.Add(11)); hi.Queue(() => log.Add(12)); hi.Queue(() => log.Add(13)); hi.Queue(() => log.Add(14)); var lo = SingleThreadScheduler.CreateOverExisting(s, "Lo", PrioritizedAction.LowPriority); lo.Queue(() => log.Add(-10)); lo.Queue(() => log.Add(-11)); lo.Queue(() => log.Add(-12)); lo.Queue(() => log.Add(-13)); lo.Queue(() => log.Add(-14)); }); normal.PumpAndWaitFor(() => normal.IsIdle); Assert.AreEqual(new List <int> { 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, -10, -11, -12, -13, -14 }, log); }
public void TestAwaitDoesntChangeScheduler() { var scheduler = SingleThreadScheduler.RunOnSeparateThread(TestLifetime, "TestScheduler"); var channel = new AsyncChannel <Unit>(TestLifetime); var actor = new Actor <int>("TestActor", TestLifetime, async x => { Assert.AreEqual(scheduler, TaskScheduler.Current); var task = channel.ReceiveAsync(); await task; Assert.AreEqual(scheduler, TaskScheduler.Current); } , scheduler); //first one - immediate actor.SendAsync(1); channel.SendAsync(Unit.Instance); //second one - await after timeout Thread.Sleep(50); channel.SendAsync(Unit.Instance); Thread.Sleep(50); channel.SendAsync(Unit.Instance); actor.WaitForEmpty(); }
public void TestCurrentThread() { int x = 0; Action a = () => { x++; }; var ld = Lifetime.Define(); SingleThreadScheduler.RunInCurrentStackframe(ld.Lifetime, "s", s => { Task.Run(() => { s.Queue(a); s.Queue(a); s.Queue(() => { Assert.False(s.IsIdle); s.PumpAndWaitFor(ld.Lifetime, TimeSpan.FromSeconds(5), () => x == 3); }); s.Queue(a); s.PumpAndWaitFor(ld.Lifetime, TimeSpan.FromSeconds(5), () => x == 3); ld.Terminate(); }); }); }
void OnEnable() { if (_taskScheduler != null) { return; } _taskScheduler = new SingleThreadScheduler(Thread.CurrentThread, _jobQueue); }
void OnEnable() { if (_taskScheduler != null) { return; } _taskScheduler = new SingleThreadScheduler(Thread.CurrentThread, _jobQueue); PersistentDataPath = Application.persistentDataPath; }
public void SimpleTest() { var scheduler = SingleThreadScheduler.RunOnSeparateThread(TestLifetime, "Test Scheduler"); DoTest(TestLifetime, scheduler); // IScheduler, but not TaskScheduler var schedulerWrapper = new MyTestSchedulerWrapper(scheduler); DoTest(TestLifetime, schedulerWrapper);
protected void Queue(Action action) { SingleThreadScheduler.RunOnSeparateThread(SocketLifetime, "Worker", scheduler => { var client = new SocketWire.Client(ModelLifetime, scheduler, myPort, "DemoClient"); var serializers = new Serializers(); Protocol = new Protocol("Server", serializers, new Identities(IdKind.Client), scheduler, client, SocketLifetime); scheduler.Queue(action); }); }
protected void Queue(Action action) { SingleThreadScheduler.RunOnSeparateThread(SocketLifetime, "Worker", scheduler => { var client = new SocketWire.Server(ModelLifetime, scheduler, new IPEndPoint(IPAddress.Loopback, Port), "DemoServer"); var serializers = new Serializers(); Protocol = new Protocol("Server", serializers, new Identities(IdKind.Server), scheduler, client, SocketLifetime); scheduler.Queue(action); }); }
private static void MainLifetime(string[] args, LifetimeDefinition lifetimeDefinition) { var lifetime = lifetimeDefinition.Lifetime; var reflectionSerializers = new ReflectionSerializersFacade(); var serializers = new Serializers(reflectionSerializers.Registrar); var scheduler = SingleThreadScheduler.RunOnSeparateThread(lifetime, "Scheduler"); Protocol protocol; SocketWire.Base wire; var isServer = args.Length == 0 ? Util.Fork(args) : args[0] == "server"; if (isServer) { Console.Title = "Server"; wire = new SocketWire.Server(lifetime, scheduler, ourIpEndPoint); protocol = new Protocol("Server", serializers, new Identities(IdKind.Server), scheduler, wire, lifetime); } else { Console.Title = "Client"; wire = new SocketWire.Client(lifetime, scheduler, ourIpEndPoint); protocol = new Protocol("Client", serializers, new Identities(IdKind.Client), scheduler, wire, lifetime); } scheduler.Queue(() => RunApplication(isServer, reflectionSerializers, lifetime, protocol)); wire.Connected.Change.Advise(lifetime, value => { if (value) { Console.Title += ": connected"; } else { lifetimeDefinition.Terminate(); } }); while (lifetime.IsAlive) { if (Console.KeyAvailable && OnChar != null) { scheduler.Queue(() => OnChar?.Invoke(Console.ReadKey(true).KeyChar)); } Thread.Sleep(100); } }
protected override IScheduler CreateScheduler(bool isServer) { string name = (isServer ? "server" : "client") + " scheduler"; IScheduler result = null; var thread = new Thread(() => SingleThreadScheduler.RunInCurrentStackframe(TestLifetime, name, s => result = s)) { Name = name }; thread.Start(); SpinWaitEx.SpinUntil(() => result != null); return(result); }
private void SetupModel(Lifetime lifetime, ISolution solution) { var scheduler = SingleThreadScheduler.RunOnSeparateThread(lifetime, Host); var server = new SocketWire.Server(lifetime, scheduler); Port = server.Port; var serializers = new Serializers(); var identities = new Identities(IdKind.Server); scheduler.Queue(() => { var protocol = new Protocol(Protocol, serializers, identities, scheduler, server, lifetime); var model = new DteProtocolModel(lifetime, protocol); RegisterCallbacks(model, solution); }); }
public void TestOverriddenHandlerScheduler() { ClientWire.AutoTransmitMode = true; ServerWire.AutoTransmitMode = true; var scheduler = SingleThreadScheduler.RunOnSeparateThread(TestLifetime, "Background scheduler"); var callsite = BindToServer(LifetimeDefinition.Lifetime, new RdCall <int, string>(), ourKey); var endpoint = BindToClient(LifetimeDefinition.Lifetime, new RdCall <int, string>(), ourKey); Assert.IsFalse(scheduler.IsActive); var point1 = false; var point2 = false; var thread = Thread.CurrentThread; endpoint.Set(i => { Assert.IsTrue(scheduler.IsActive); Assert.AreNotEqual(thread, Thread.CurrentThread); Volatile.Write(ref point1, true); SpinWaitEx.SpinUntil(TestLifetime, TimeSpan.FromSeconds(10), () => point2); Assert.IsTrue(point2); return(i.ToString()); }, handlerScheduler: scheduler); Assert.IsFalse(scheduler.IsActive); var task = callsite.Start(0); var result = task.Result; Assert.IsFalse(result.Maybe.HasValue); SpinWaitEx.SpinUntil(TestLifetime, TimeSpan.FromSeconds(10), () => point1); Assert.IsTrue(point1); Assert.IsFalse(result.Maybe.HasValue); Volatile.Write(ref point2, true); SpinWaitEx.SpinUntil(TestLifetime, () => result.Maybe.HasValue); Assert.AreEqual("0", result.Value.Result); }
public void TestSeparateThread() { var sc = SingleThreadScheduler.RunOnSeparateThread(TestLifetime, "s"); int x = 0; Action a = () => { x++; }; Assert.True(sc.IsIdle); sc.Queue(a); sc.Queue(a); sc.Queue(a); sc.Queue(() => { if (sc.IsIdle) { x--; //should never happed } }); Assert.True(sc.PumpAndWaitFor(Lifetime.Eternal, TimeSpan.FromSeconds(5), () => x == 3)); Assert.True(sc.PumpAndWaitFor(() => sc.IsIdle)); }
public MultiThreadDispatcher() : base(new Rule(x => true)) { _preserveOrderScheduler = new SingleThreadScheduler(); }
protected override IScheduler CreateScheduler(bool isServer) { return(SingleThreadScheduler.RunOnSeparateThread(LifetimeDefinition.Lifetime, isServer ? "Server" : "Client")); }