Example #1
0
        public void MultipleSocketClientTest()
        {
            var         results    = new List <string>();
            int         numClients = 4;
            int         port       = 12345;
            TcpListener server     = new TcpListener(System.Net.IPAddress.Any, port);

            var a       = Scheduler.Start(ServerTask(server, numClients));
            var clients = new List <IFuture>();

            for (int i = 0; i < numClients; i++)
            {
                clients.Add(Scheduler.Start(ClientTask(results, "localhost", port)));
            }
            var b = Future.WaitForAll(clients);

            while (!b.Completed)
            {
                Scheduler.Step();
            }

            string helloWorld      = "Hello, world!";
            var    expectedResults = new List <string>();

            for (int i = 0; i < numClients; i++)
            {
                expectedResults.Add(helloWorld);
            }
            Assert.AreEqual(expectedResults.ToArray(), results.ToArray());
        }
Example #2
0
        void ISchedulable.Schedule(TaskScheduler scheduler, IFuture future)
        {
            if (_Schedulables == null)
            {
                throw new InvalidOperationException();
            }
            else if (_Schedulables.Length == 0)
            {
                future.Complete();
                return;
            }

            for (int i = 0; i < _Schedulables.Length; i++)
            {
                var s = _Schedulables[i];
                if (s != null)
                {
                    _Futures[i] = scheduler.Start(s, TaskExecutionPolicy.RunWhileFutureLives);
                }
            }

            _Schedulables = null;
            _ResultFuture = future;

            _WaitFuture = Future.WaitForAll(_Futures);
            _WaitFuture.RegisterOnComplete(HandleResult);
            _WaitFuture.RegisterOnDispose(HandleDisposed);
        }
Example #3
0
            public SequenceAwaiter(Future <T>[] futures)
            {
                Registration = new CancellationScope.Registration();

                Futures = futures;
                if (Futures.Length == 0)
                {
                    Ready = new SignalFuture(true);
                    return;
                }

                Ready = Future.WaitForAll(futures);
            }
Example #4
0
            public SequenceAwaiter(Future <T>[] futures)
            {
                Registration = new CancellationScope.Registration(TaskScheduler.Current);

                Futures = futures;
                if (Futures.Length == 0)
                {
                    Ready = SignalFuture.Signaled;
                    return;
                }

                Ready = Future.WaitForAll(futures);
            }
Example #5
0
            public SequenceAwaiter(ISchedulable <T>[] schedulables)
            {
                Registration = new CancellationScope.Registration();

                Futures = new Future <T> [schedulables.Length];
                if (Futures.Length == 0)
                {
                    Ready = new SignalFuture(true);
                    return;
                }

                for (var i = 0; i < Futures.Length; i++)
                {
                    Futures[i] = Registration.Scheduler.Start(schedulables[i]);
                }

                Ready = Future.WaitForAll(Futures);
            }
Example #6
0
            public SequenceAwaiter(ISchedulable <T>[] schedulables)
            {
                Registration = new CancellationScope.Registration(TaskScheduler.Current);

                Futures = new Future <T> [schedulables.Length];
                if (Futures.Length == 0)
                {
                    Ready = SignalFuture.Signaled;
                    return;
                }

                var ts = (TaskScheduler)Registration.Scheduler;

                for (var i = 0; i < Futures.Length; i++)
                {
                    Futures[i] = ts.Start(schedulables[i]);
                }

                Ready = Future.WaitForAll(Futures);
            }
Example #7
0
        public void MultipleThreadTest()
        {
            int numIterations = 1000000;
            int numWorkers    = 50;

            var buf = new ValueHolder();
            var fn  = new Func <ValueHolder, int, int>(WorkerThread);

            var futures = new List <IFuture>();

            for (int i = 0; i < numWorkers; i++)
            {
                futures.Add(Future.RunInThread(fn, buf, numIterations));
            }

            var f = Future.WaitForAll(futures);

            while (!f.Completed)
            {
                Scheduler.Step();
            }

            Assert.AreEqual(numIterations * numWorkers, buf.Value);
        }