IEnumerator <TaskContract?> RunTasks(float timeout)
        {
            var enumerator = GetURLAsynchronously();

            //wait for one second (simulating async load)
            yield return(enumerator.Continue());

            string url = (string)enumerator.Current.Value.reference;

            var parallelTasks = new ParallelTaskCollection();

            //parallel tasks with race condition (timeout Breaks it)
            parallelTasks.Add(BreakOnTimeOut(timeout));
            parallelTasks.Add(new LoadSomething(new UnityWebRequest(url)).GetEnumerator());

            yield return(parallelTasks.Continue());

            if ((Break)parallelTasks.Current == Break.It)
            {
                yield return(Break.AndStop);

                throw new Exception("should never get here");
            }

            yield return(new WaitForSecondsEnumerator(2).Continue());
        }
예제 #2
0
        // Use this for initialization
        void Start()
        {
            Application.targetFrameRate = 60;

            Debug.Log("Set frame rate to 60fps");

            ParallelTaskCollection pt = new ParallelTaskCollection();
            SerialTaskCollection   st = new SerialTaskCollection();

            st.Add(Print("s1"));
            st.Add(DoSomethingAsynchonously());
            st.Add(Print("s3"));

            pt.Add(Print("1"));
            pt.Add(Print("2"));
            pt.Add(Print("3"));
            pt.Add(Print("4"));
            pt.Add(Print("5"));
            pt.Add(st);
            pt.Add(Print("6"));
            pt.Add(WWWTest());
            pt.Add(Print("7"));
            pt.Add(Print("8"));

            pt.onComplete += () =>
            {
                Application.targetFrameRate = -1;
                Debug.Log("Unlock framerate");
            };

            pt.Run();
        }
    // Use this for initialization
    void Start()
    {
        var someData = new SomeData();

        var pt = new ParallelTaskCollection <SomeData>(someData);
        var st = new SerialTaskCollection <SomeData>(someData);

        st.Add(Print("s1"));
        st.Add(Print("s2"));
        st.Add(pt);
        st.Add(Print("s3"));
        st.Add(Print("s4"));

        pt.Add(Print("1"));
        pt.Add(Print("2"));
        pt.Add(DoSomethingAsynchonously());
        pt.Add(new LoadSomething(new WWW("www.google.com"))); //obviously the token could be passed by constructor, but in some complicated situations, this is not possible (usually while exploiting continuation)
        pt.Add(new LoadSomething(new WWW("http://download.thinkbroadband.com/5MB.zip")));
        pt.Add(new LoadSomething(new WWW("www.ebay.com")));
        pt.Add(Print("3"));
        pt.Add(Print("4"));
        pt.Add(Print("5"));
        pt.Add(Print("6"));
        pt.Add(Print("7"));
        pt.Add(Print(someData.justForTest.ToString()));

        TaskRunner.Instance.Run(st);
    }
        // Use this for initialization
        void Start()
        {
            var pt = new ParallelTaskCollection();
            var st = new SerialTaskCollection();

            st.Add(Print("s1"));
            st.Add(Print("s2"));
            st.Add(pt);
            st.Add(Print("s3"));
            st.Add(Print("s4"));

            pt.Add(Print("1"));
            pt.Add(Print("2"));

            //only the task runner can actually handle parallel tasks
            //that return Unity operations (when unity compatible
            //schedulers are used)
            pt.Add(UnityAsyncOperationsMustNotBreakTheParallelism());
            pt.Add(UnityYieldInstructionsMustNotBreakTheParallelism());

            pt.Add(new LoadSomething(new UnityWebRequest("www.google.com")).GetEnumerator()); //obviously the token could be passed by constructor, but in some complicated situations, this is not possible (usually while exploiting continuation)
            pt.Add(new LoadSomething(new UnityWebRequest("http://download.thinkbroadband.com/5MB.zip")).GetEnumerator());
            pt.Add(new LoadSomething(new UnityWebRequest("www.ebay.com")).GetEnumerator());
            pt.Add(Print("3"));
            pt.Add(Print("4"));
            pt.Add(Print("5"));
            pt.Add(Print("6"));
            pt.Add(Print("7"));

            st.Start();
        }
예제 #5
0
        IEnumerator RunTasks(float timeout)
        {
            var enumerator = GetURLAsynchronously();

            //wait for one second (simulating async load)
            yield return(enumerator);

            string url = (enumerator.Current as ValueObject <string>).target as string;

            var parallelTasks = new ParallelTaskCollection();

            //parallel tasks with race condition (timeout Breaks it)
            parallelTasks.Add(BreakOnTimeOut(timeout));
            parallelTasks.Add(new LoadSomething(new WWW(url)).GetEnumerator());

            yield return(parallelTasks);

            if (parallelTasks.Current.current == Break.It)
            {
                yield return(Break.AndStop);

                throw new Exception("should never get here");
            }

            yield return(new WaitForSecondsEnumerator(2));
        }
        public void Setup()
        {
            _serialTasks1   = new SerialTaskCollection();
            _serialTasks2   = new SerialTaskCollection();
            _parallelTasks1 = new ParallelTaskCollection();
            _parallelTasks2 = new ParallelTaskCollection();

            _iterable1 = new Enumerator(10000);
            _iterable2 = new Enumerator(5000);

            _reusableTaskRoutine = TaskRunner.Instance.AllocateNewTaskRoutine(new SyncRunner());
        }
        /// <summary>
        /// Add can be called by another thread, so if the collection is already running
        /// I can't allow adding more tasks.
        /// </summary>
        /// <param name="enumerator"></param>
        /// <exception cref="MultiThreadedParallelTaskCollectionException"></exception>
        public void Add(IEnumerator enumerator)
        {
            if (isRunning == true)
            {
                throw new MultiThreadedParallelTaskCollectionException("can't add tasks on a started MultiThreadedParallelTaskCollection");
            }

            ParallelTaskCollection parallelTaskCollection = _parallelTasks[_numberOfTasksAdded++ % _parallelTasks.Length];

            parallelTaskCollection.Add(enumerator);

            _numberOfConcurrentOperationsToRun = Math.Min(_parallelTasks.Length, _numberOfTasksAdded);
        }
예제 #8
0
    public static ParallelTaskCollection Combine(this IEnumerator enumerator, IEnumerator enumerator2)
    {
        var parallel = enumerator as ParallelTaskCollection;

        if (parallel == null)
        {
            parallel = new ParallelTaskCollection();
            parallel.Add(enumerator);
        }

        parallel.Add(enumerator2);

        return(parallel);
    }
예제 #9
0
        IEnumerator InitialSpawning()
        {
            var loadTasks = new ParallelTaskCollection();

            loadTasks.Add(_shipDefinition.Prefab.LoadAssetTask <GameObject>());
            loadTasks.Add(_shipDefinition.MerchantSprite.LoadAssetTask <Sprite>());
            loadTasks.Add(_shipDefinition.PirateSprite.LoadAssetTask <Sprite>());
            foreach (var sprite in _shipDefinition.ShipSprites)
            {
                loadTasks.Add(sprite.LoadAssetTask <Sprite>());
            }

            yield return(loadTasks);

            for (var i = 0; i < _spawnPoints.Length; i++)
            {
                Spawn((uint)i, _spawnPoints[i], i == 0 ? ShipControl.Player : ShipControl.Ai);
            }
        }
        // Use this for initialization
        void Start()
        {
            ParallelTaskCollection pt = new ParallelTaskCollection();
            SerialTaskCollection   st = new SerialTaskCollection();

            st.Add(Print("s1"));
            st.Add(Print("s2"));
            st.Add(Print("s3"));
            st.Add(Print("s4"));

            pt.Add(Print("p1")).Add(Print("p2"));
            pt.Add(new LoadSomething(new WWWEnumerator(new WWW("www.google.com")))); //obviously the token could be passed by constructor, but in some complicated situations, this is not possible (usually while exploiting continuation)
            pt.Add(new LoadSomething(new WWWEnumerator(new WWW("http://download.thinkbroadband.com/5MB.zip"))));
            pt.Add(new LoadSomething(new WWWEnumerator(new WWW("www.ebay.com"))));

            pt.Add(Print("p3")).Add(Print("p4")).Add(st).Add(Print("p5")).Add(Print("p6")).Add(Print("p7"));

            TaskRunner.Instance.RunOnSchedule(MTRunner, pt); //running on another thread!
        }
예제 #11
0
    public IEnumerator TestUnityWaitInParallel()
    {
        var updateMonoRunner = new UpdateMonoRunner("test1");
        ITaskRoutine <IEnumerator> taskRoutine = TaskRunner.Instance.AllocateNewTaskRoutine(updateMonoRunner);
        ParallelTaskCollection     pt          = new ParallelTaskCollection();

        pt.Add(new WaitForSecondsUnity().GetEnumerator());
        pt.Add(new WaitForSecondsUnity().GetEnumerator());
        taskRoutine.SetEnumerator(pt);
        taskRoutine.Start();
        DateTime then = DateTime.Now;

        while (taskRoutine.isRunning == true)
        {
            yield return(null);
        }
        updateMonoRunner.Dispose();
        var totalSeconds = (DateTime.Now - then).TotalSeconds;

        Assert.That(totalSeconds, Is.InRange(0.9, 1.1));
    }
예제 #12
0
        public void Setup()
        {
            vo = new ValueObject();

            serialTasks1   = new SerialTaskCollection <ValueObject>(vo);
            parallelTasks1 = new ParallelTaskCollection <ValueObject>(vo);
            serialTasks2   = new SerialTaskCollection <ValueObject>(vo);
            parallelTasks2 = new ParallelTaskCollection <ValueObject>(vo);

            task1 = new Task();
            task2 = new Task();

            taskChain1 = new TaskChain();
            taskChain2 = new TaskChain();

            iterable1             = new Enumerable(10000);
            iterable2             = new Enumerable(10000);
            iterableWithException = new Enumerable(-5);

            _taskRunner          = TaskRunner.Instance;
            _reusableTaskRoutine = TaskRunner.Instance.AllocateNewTaskRoutine().SetScheduler(StandardSchedulers.syncScheduler); //the taskroutine will stall the thread because it runs on the SyncScheduler
        }
        void InitializeThreadsAndData(uint numberOfThreads, bool tightTasks)
        {
            _decrementRunningThread = DecrementRunningThread;

            _runners       = new MultiThreadRunner[numberOfThreads];
            _taskRoutines  = new ITaskRoutine <IEnumerator> [numberOfThreads];
            _parallelTasks = new ParallelTaskCollection[numberOfThreads];

            //prepare a single multithread runner for each group of fiber like task collections
            //number of threads can be less than the number of tasks to run
            for (int i = 0; i < numberOfThreads; i++)
            {
                _runners[i] = new MultiThreadRunner("MultiThreadedParallelRunner ".FastConcat(_name, " #").FastConcat(i),
                                                    false, tightTasks);
            }

            Func <Exception, bool> ptcOnOnException = (e) => {
                DecrementConcurrentOperationsCounter();
                return(false);
            };

            Action ptcOnOnComplete = DecrementConcurrentOperationsCounter;

            //prepare the fiber-like paralleltasks
            for (int i = 0; i < numberOfThreads; i++)
            {
                var ptask = TaskRunner.Instance.AllocateNewTaskRoutine(_runners[i]);
                var ptc   = new ParallelTaskCollection("MultiThreaded ParallelTaskCollection ".FastConcat(_name, " #").FastConcat(i));

                ptc.onComplete  += ptcOnOnComplete;
                ptc.onException += ptcOnOnException;

                ptask.SetEnumerator(ptc);

                _parallelTasks[i] = ptc;
                _taskRoutines[i]  = ptask;
            }
        }
예제 #14
0
        public void Setup()
        {
            _vo = new ValueObject();

            _serialTasks1   = new SerialTaskCollection <TestEnumerator>();
            _parallelTasks1 = new ParallelTaskCollection <IEnumerator>();
            _serialTasks2   = new SerialTaskCollection();
            _parallelTasks2 = new ParallelTaskCollection();

            _task1 = new Task();
            _task2 = new Task();

            _asyncTaskChain1 = new AsyncTask();
            _asyncTaskChain2 = new AsyncTask();

            _iterable1             = new TestEnumerator(10000);
            _iterable2             = new TestEnumerator(10000);
            _iterableWithException = new TestEnumerator(-5);

            _taskRunner = TaskRunner.Instance;
            //the taskroutine will stall the thread because it runs on the SyncScheduler
            _reusableTaskRoutine = TaskRunner.Instance.AllocateNewTaskRoutine(new SyncRunner());
        }
예제 #15
0
        void InitializeThreadsAndData(uint numberOfThreads, bool tightTasks)
        {
            _runners       = new MultiThreadRunner <TaskRoutine <ParallelTaskCollection <TTask> > > [numberOfThreads];
            _taskRoutines  = new TaskRoutine <ParallelTaskCollection <TTask> > [numberOfThreads];
            _parallelTasks = new ParallelTaskCollection <TTask> [numberOfThreads];

            //prepare a single multithread runner for each group of fiber like task collections
            //number of threads can be less than the number of tasks to run
            for (int i = 0; i < numberOfThreads; i++)
            {
                _runners[i] = new MultiThreadRunner <TaskRoutine <ParallelTaskCollection <TTask> > >
                                  ("MultiThreadedParallelRunner ".FastConcat(_name, " #").FastConcat(i),
                                  false, tightTasks);
            }

            Action ptcOnOnComplete = DecrementConcurrentOperationsCounter;
            Func <Exception, bool> ptcOnOnException = (e) =>
            {
                DecrementConcurrentOperationsCounter();
                return(false);
            };


            //prepare the fiber-like paralleltasks
            for (int i = 0; i < numberOfThreads; i++)
            {
                var ptc = new ParallelTaskCollection <TTask>("MultiThreaded ParallelTaskCollection ".
                                                             FastConcat(_name, " #").FastConcat(i));

                ptc.onComplete  += ptcOnOnComplete;
                ptc.onException += ptcOnOnException;

                _parallelTasks[i] = ptc;
                var taskRoutine = ptc.ToTaskRoutine(_runners[i]);
                _taskRoutines[i] = taskRoutine;
            }
        }
 public ParallelIEnumerator(int paralleNum = 3)
 {
     otc = new ParallelTaskCollection(paralleNum);
     otc.AddFinishAction(finish);
 }