Пример #1
0
		public void SimpleExecutionTest ()
		{
			bool executed = false;
			Task t = new Task (() => executed = true);
			t.Execute (delegate {});

			Assert.IsTrue (executed);
		}
Пример #2
0
		public void ExecutionWithChildCreationTest ()
		{
			bool executed = false;
			bool childRetrieved = false;

			Task t = new Task (() => { Task.Factory.StartNew (() => Console.WriteLine ("execution")); executed = true; });
			t.Execute ((child) => childRetrieved = child != null);

			Assert.IsTrue (executed);
			Assert.IsTrue (childRetrieved);
		}
		protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
		{
			if (task.IsCompleted)
				return false;

			if (task.Status == TaskStatus.WaitingToRun) {
				task.Execute (null);
				return true;
			}

			return false;
		}
        protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
        {
            if (task.IsCompleted)
            {
                return(false);
            }

            if (task.Status == TaskStatus.WaitingToRun)
            {
                task.Execute(null);
                return(true);
            }

            return(false);
        }
        internal protected bool TryExecuteTask(Task task)
        {
            if (task.IsCompleted)
            {
                return(false);
            }

            if (task.Status == TaskStatus.WaitingToRun)
            {
                task.Execute();
                return(true);
            }

            return(false);
        }
Пример #6
0
        /// <summary>
        /// Attempts to execute the provided Task on this scheduler.
        /// </summary>
        protected bool TryExecuteTask(Task task)
        {
            if (task.IsCompleted)
            {
                return(false);
            }

            if (task.Status == TaskStatus.WaitingToRun)
            {
                task.Execute();
                //if (task.WaitOnChildren())
                task.Wait();

                return(true);
            }

            return(false);
        }
Пример #7
0
        public void ParticipateUntil(Task task)
        {
            if (participateUntil1 != null)
            {
                participateUntil1(task);
                return;
            }

            if (task.Status == TaskStatus.WaitingToRun)
            {
                task.Execute(null);
            }

            if (task.IsCompleted)
            {
                return;
            }

            ManualResetEventSlim evt = new ManualResetEventSlim(false);

            task.ContinueWith(_ => evt.Set(), TaskContinuationOptions.ExecuteSynchronously);

            ParticipateUntil(evt, -1);
        }
Пример #8
0
        private static void TaskExecuterCallback(object obj)
        {
            Task task = (Task)obj;

            task.Execute();
        }
Пример #9
0
		// Almost same as above but with an added predicate and treating one item at a time. 
		// It's used by Scheduler Participate(...) method for special waiting case like
		// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
		// Predicate should be really fast and not blocking as it is called a good deal of time
		// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
		public static void ParticipativeWorkerMethod (Task self,
		                                              ManualResetEventSlim predicateEvt,
		                                              int millisecondsTimeout,
		                                              IProducerConsumerCollection<Task> sharedWorkQueue,
		                                              ThreadWorker[] others,
		                                              ManualResetEvent evt,
		                                              Func<Task, Task, bool> checkTaskFitness)
		{
			const int stage1 = 5, stage2 = 0;
			int tries = 50;
			WaitHandle[] handles = null;
			Watch watch = Watch.StartNew ();
			if (millisecondsTimeout == -1)
				millisecondsTimeout = int.MaxValue;
			bool aggressive = false;
			bool hasAutoReference = autoReference != null;
			Action<Task> adder = null;

			while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout && !self.IsCompleted) {
				// We try to execute the self task as it may be the simplest way to unlock
				// the situation
				if (self.Status == TaskStatus.WaitingToRun) {
					self.Execute (hasAutoReference ? autoReference.adder : (Action<Task>)null);
					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (hasAutoReference) {
					var enumerable = autoReference.dDeque.GetEnumerable ();
					if (adder == null)
						adder = hasAutoReference ? autoReference.adder : (Action<Task>)null;

					if (enumerable != null) {
						foreach (var t in enumerable) {
							if (t == null)
								continue;

							if (checkTaskFitness (self, t))
								t.Execute (adder);

							if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
								return;
						}
					}
				}

				int count = sharedWorkQueue.Count;

				// Dequeue only one item as we have restriction
				while (--count >= 0 && sharedWorkQueue.TryTake (out value) && value != null) {
					evt.Set ();
					if (checkTaskFitness (self, value) || aggressive)
						value.Execute (null);
					else {
						if (autoReference == null)
							sharedWorkQueue.TryAdd (value);
						else
							autoReference.dDeque.PushBottom (value);
						evt.Set ();
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				// First check to see if we comply to predicate
				if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
					return;
				
				// Try to complete other work by stealing since our desired tasks may be in other worker
				ThreadWorker other;
				for (int i = 0; i < others.Length; i++) {
					if ((other = others [i]) == autoReference || other == null)
						continue;

					if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (checkTaskFitness (self, value) || aggressive)
							value.Execute (null);
						else {
							if (autoReference == null)
								sharedWorkQueue.TryAdd (value);
							else
								autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				/* Waiting is split in 4 phases
				 *   - until stage 1 we simply yield the thread to let others add data
				 *   - between stage 1 and stage2 we use ManualResetEventSlim light waiting mechanism
				 *   - after stage2 we fall back to the heavier WaitHandle waiting mechanism
				 *   - if really the situation isn't evolving after a couple of sleep, we disable
				 *     task fitness check altogether
				 */
				if (--tries > stage1)
					Thread.Yield ();
				else if (tries >= stage2)
					predicateEvt.Wait (ComputeTimeout (5, millisecondsTimeout, watch));
				else {
					if (tries == stage2 - 1)
						handles = new [] { predicateEvt.WaitHandle, evt };
					System.Threading.WaitHandle.WaitAny (handles, ComputeTimeout (1000, millisecondsTimeout, watch));
					if (tries == stage2 - 10)
						aggressive = true;
				}
			}
		}
Пример #10
0
		void ExecuteTask (Task value, ref bool result)
		{
			if (value == null)
				return;

			var saveCurrent = currentTask;
			currentTask = value;
			value.Execute (adder);
			result = true;
			currentTask = saveCurrent;
		}
		public void AddWork (Task t)
		{
			ThreadPool.QueueUserWorkItem (delegate {
				t.Execute (AddWork);
			});
		}
Пример #12
0
		internal protected bool TryExecuteTask (Task task)
		{
			if (task.IsCompleted)
				return false;

			if (task.Status == TaskStatus.WaitingToRun) {
				task.Execute (null);
				return true;
			}

			return false;
		}
Пример #13
0
        protected internal bool TryExecuteTask(Task task)
        {
            if (task.IsCompleted)
                return false;

            if (task.Status == TaskStatus.WaitingToRun) {
                task.Execute ();
                if (task.WaitOnChildren ())
                    task.Wait ();

                return true;
            }

            return false;
        }
Пример #14
0
        protected void TaskExecuterCallback(object obj)
        {
            Task task = (Task)obj;

            task.Execute();
        }
Пример #15
0
 protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
 {
     task.Execute(null);
     return(true);
 }
Пример #16
0
		protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
		{
			task.Execute (null);
			return true;
		}
Пример #17
0
        static void TaskExecuterCallback(object obj)
        {
            Task task = (Task)obj;

            task.Execute(null);
        }