Ejemplo n.º 1
0
		public ThreadWorker (IScheduler sched, ThreadWorker[] others, IProducerConsumerCollection<Task> sharedWorkQueue,
		                     bool createThread, int maxStackSize, ThreadPriority priority)
		{
			this.others          = others;

//			if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("USE_CYCLIC"))) {
//				Console.WriteLine ("Using cyclic deque");
//				this.dDeque = new CyclicDeque<Task> ();
//			} else {
//				this.dDeque = new DynamicDeque<Task> ();
//			}
			this.dDeque = new CyclicDeque<Task> ();
			
			this.sharedWorkQueue = sharedWorkQueue;
			this.workerLength    = others.Length;
			this.isLocal         = !createThread;
			
			this.childWorkAdder = delegate (Task t) { 
				dDeque.PushBottom (t);
				sched.PulseAll ();
			};
			
			// Find the stealing start index randomly (then the traversal
			// will be done in Round-Robin fashion)
			do {
				this.stealingStart = r.Next(0, workerLength);
			} while (others[stealingStart] == this);
			
			InitializeUnderlyingThread (maxStackSize, priority);
		}
        public static void AddStressTest(IProducerConsumerCollection<int> coll)
        {
            ParallelTestHelper.Repeat(delegate
            {
                int amount = -1;
                const int count = 10;
                const int threads = 5;

                ParallelTestHelper.ParallelStressTest(coll, (q) =>
                {
                    int t = Interlocked.Increment(ref amount);
                    for (int i = 0; i < count; i++)
                        coll.TryAdd(t);
                }, threads);

                Assert.AreEqual(threads * count, coll.Count, "#-1");
                int[] values = new int[threads];
                int temp;
                while (coll.TryTake(out temp))
                {
                    values[temp]++;
                }

                for (int i = 0; i < threads; i++)
                    Assert.AreEqual(count, values[i], "#" + i);
            });
        }
Ejemplo n.º 3
0
 public SlackSendQueueService(IProducerConsumerCollection<OutputMessage> collection)
 {
     if (collection == null)
         this.outputMessageQueue = new BlockingCollection<OutputMessage>();
     else
         this.outputMessageQueue = new BlockingCollection<OutputMessage>(collection);
 }
Ejemplo n.º 4
0
		public ThreadWorker (IScheduler sched, ThreadWorker[] others, IProducerConsumerCollection<Task> sharedWorkQueue,
		                     bool createThread, int maxStackSize, ThreadPriority priority, EventWaitHandle handle)
		{
			this.others          = others;

			this.dDeque = new CyclicDeque<Task> ();
			
			this.sharedWorkQueue = sharedWorkQueue;
			this.workerLength    = others.Length;
			this.isLocal         = !createThread;
			this.waitHandle      = handle;
			
			this.childWorkAdder = delegate (Task t) { 
				dDeque.PushBottom (t);
				sched.PulseAll ();
			};
			
			// Find the stealing start index randomly (then the traversal
			// will be done in Round-Robin fashion)
			do {
				this.stealingStart = r.Next(0, workerLength);
			} while (others[stealingStart] == this);
			
			InitializeUnderlyingThread (maxStackSize, priority);
		}
        public static void RemoveStressTest(IProducerConsumerCollection<int> coll, CheckOrderingType order)
        {
            ParallelTestHelper.Repeat(delegate
            {

                const int count = 10;
                const int threads = 5;
                const int delta = 5;

                for (int i = 0; i < (count + delta) * threads; i++)
                    while (!coll.TryAdd(i)) ;

                bool state = true;

                Assert.AreEqual((count + delta) * threads, coll.Count, "#0");

                ParallelTestHelper.ParallelStressTest(coll, (q) =>
                {
                    bool s = true;
                    int t;

                    for (int i = 0; i < count; i++)
                    {
                        s &= coll.TryTake(out t);
                        // try again in case it was a transient failure
                        if (!s && coll.TryTake(out t))
                            s = true;
                    }

                    if (!s)
                        state = false;
                }, threads);

                Assert.IsTrue(state, "#1");
                Assert.AreEqual(delta * threads, coll.Count, "#2");

                string actual = string.Empty;
                int temp;
                while (coll.TryTake(out temp))
                {
                    actual += temp.ToString(); ;
                }

                IEnumerable<int> range = Enumerable.Range(order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
                if (order == CheckOrderingType.Reversed)
                    range = range.Reverse();

                string expected = range.Aggregate(string.Empty, (acc, v) => acc + v);

                if (order == CheckOrderingType.DontCare)
                {
                    actual.AreCollectionEquivalent(expected, "#3");
                    //--TODO Assert.That (actual, new CollectionEquivalentConstraint (expected), "#3");
                }
                else
                    Assert.AreEqual(expected, actual, "#3");
            }, 10);
        }
Ejemplo n.º 6
0
		public Scheduler (int maxWorker, int maxStackSize, ThreadPriority priority)
		{
			workQueue = new ConcurrentQueue<Task> ();
			workers = new ThreadWorker [maxWorker];
			
			for (int i = 0; i < maxWorker; i++) {
				workers [i] = new ThreadWorker (this, workers, workQueue, maxStackSize, priority);
			}
		}
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisCommandQueue"/> class.
        /// </summary>
        /// <param name="configuration">The configuration string.</param>
        /// <param name="collection">The collection to use as the underlying data store.</param>
        public RedisCommandQueue(string configuration, IProducerConsumerCollection<ICommand> collection)
        {
            this.queue = new BlockingCollection<ICommand>(collection);
            this.multiplexer = ConnectionMultiplexer.Connect(configuration);
            this.database = this.multiplexer.GetDatabase();
            this.pubsub = this.multiplexer.GetSubscriber();
            this.pubsub.Subscribe(ChannelKey, this.HandleMessage);

            this.timer = new Timer(this.FlushQueue, null, EmptyQueueInterval, EmptyQueueInterval);
        }
Ejemplo n.º 8
0
		public Scheduler (int maxWorker, ThreadPriority priority)
		{
			workQueue = new ConcurrentQueue<Task> ();
			workers = new ThreadWorker [maxWorker];
			
			for (int i = 0; i < maxWorker; i++) {
				workers [i] = new ThreadWorker (workers, i, workQueue, new CyclicDeque<Task> (), priority, pulseHandle);
				workers [i].Pulse ();
			}
		}
		private IMessagingChannel TryConnect(string channelGroup, IProducerConsumerCollection<IMessagingChannel> items)
		{
			IMessagingChannel channel;
			if (!items.TryTake(out channel))
			{
				Log.Debug("No available channel in the pool for '{0}', establishing a new channel.", channelGroup);
				channel = this.connector.Connect(channelGroup);
			}

			Log.Verbose("Resolving channel for '{0}' from the pool of available channels.", channelGroup);
			this.open.TryAdd(channel, true);
			return new PooledDispatchChannel(this, channel, this.currentToken);
		}
Ejemplo n.º 10
0
        public static IObservable<Tuple<IDisposable<byte[]>, int>> ReadAsync(this Stream stream, int bufferSize, bool waitForObserver, bool refCount, IProducerConsumerCollection<IDisposable<byte[]>> cache)
        {
            var read = Observable.FromAsyncPattern<byte[], int, int, int> (stream.BeginRead, stream.EndRead);

            return Observable.Create<Tuple<IDisposable<byte[]>, int>> (observer => {
                var subscription = new BooleanDisposable ();
                Action<Exception> onError = error => {
                    observer.OnError (error);
                    subscription.Dispose ();
                };

                Action loop = null;
                Action<IDisposable<byte[]>, int> engine;
                if (waitForObserver)
                    engine = (buffer, bytesRead) => {
                        // OnNext is called first
                        observer.OnNext (new Tuple<IDisposable<byte[]>, int> (buffer, bytesRead));
                        // then we begin async read
                        if (!subscription.IsDisposed)
                            loop ();
                    };
                else
                    engine = (buffer, bytesRead) => {
                        // begin async read first
                        if (!subscription.IsDisposed)
                            loop ();
                        //then call OnNext
                        observer.OnNext (new Tuple<IDisposable<byte[]>, int> (buffer, bytesRead));
                    };

                loop = () => {
                    var buffer = cache.TakeOrCreate (refCount, () =>
                        new byte[bufferSize]);
                    read (buffer.Value, 0, bufferSize).Subscribe (bytesRead => {
                        if (0 == bytesRead)
                            observer.OnCompleted ();
                        else
                            engine (buffer, bytesRead);
                    }, onError);
                };

                loop ();

                return subscription;
            });
        }
Ejemplo n.º 11
0
		public ThreadWorker (ThreadWorker[] others,
		                     int workerPosition,
		                     IProducerConsumerCollection<Task> sharedWorkQueue,
		                     IConcurrentDeque<Task> dDeque,
		                     ThreadPriority priority,
		                     ManualResetEvent handle)
		{
			this.others          = others;
			this.dDeque          = dDeque;
			this.sharedWorkQueue = sharedWorkQueue;
			this.workerLength    = others.Length;
			this.workerPosition  = workerPosition;
			this.waitHandle      = handle;
			this.threadPriority  = priority;

			InitializeUnderlyingThread ();
		}
Ejemplo n.º 12
0
		public ThreadWorker (IScheduler sched,
		                     ThreadWorker[] others,
		                     int workerPosition,
		                     IProducerConsumerCollection<Task> sharedWorkQueue,
		                     ThreadPriority priority,
		                     EventWaitHandle handle)
		{
			this.others          = others;
			this.dDeque          = new CyclicDeque<Task> ();
			this.sched           = sched;
			this.sharedWorkQueue = sharedWorkQueue;
			this.workerLength    = others.Length;
			this.workerPosition  = workerPosition;
			this.waitHandle      = handle;
			this.threadPriority  = priority;

			InitializeUnderlyingThread ();
		}
Ejemplo n.º 13
0
		static async Task RunProgram(IProducerConsumerCollection<CustomTask> collection = null)
		{
			var taskCollection = new BlockingCollection<CustomTask>();
			if(collection != null)
				taskCollection= new BlockingCollection<CustomTask>(collection);

			var taskSource = Task.Run(() => TaskProducer(taskCollection));

			Task[] processors = new Task[4];
			for (int i = 1; i <= 4; i++)
			{
				string processorId = "Processor " + i;
				processors[i - 1] = Task.Run(
					() => TaskProcessor(taskCollection, processorId));
			}

			await taskSource;

			await Task.WhenAll(processors);
		}
		public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
		{
			ParallelTestHelper.Repeat (delegate {
				
				const int count = 10;
				const int threads = 5;
				const int delta = 5;
				
				for (int i = 0; i < (count + delta) * threads; i++)
					coll.TryAdd (i);
				
				bool state = true;
				
				ParallelTestHelper.ParallelStressTest (coll, (q) => {
					int t;
					for (int i = 0; i < count; i++)
						state &= coll.TryTake (out t);
				}, threads);
				
				Assert.IsTrue (state, "#1");
				Assert.AreEqual (delta * threads, coll.Count, "#2");
				
				string actual = string.Empty;
				int temp;
				while (coll.TryTake (out temp)) {
					actual += temp.ToString ();;
				}
				
				IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
				if (order == CheckOrderingType.Reversed)
					range = range.Reverse ();
				
				string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
				
				if (order == CheckOrderingType.DontCare)
					CollectionAssert.AreEquivalent (expected, actual, "#3");
				else 
					Assert.AreEqual (expected, actual, "#3");
			});
		}
Ejemplo n.º 15
0
 public IndexModificationBatch(params Action<TransactionalIndexWorker>[] work)
 {
     _batch = new ConcurrentQueue<Action<TransactionalIndexWorker>>(work);
 }
Ejemplo n.º 16
0
 protected override bool IsEmpty(IProducerConsumerCollection <int> pcc) => ((ConcurrentStack <int>)pcc).IsEmpty;
Ejemplo n.º 17
0
 public FlowInputJunction(params IFlowConsumer <T>[] consumers)
 {
     Consumers = new ConcurrentQueue <IFlowConsumer <T> >(consumers);
 }
Ejemplo n.º 18
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)
		{
			const int stage1 = 5, stage2 = 0;
			int tries = 8;
			WaitHandle[] handles = null;
			Watch watch = Watch.StartNew ();
			if (millisecondsTimeout == -1)
				millisecondsTimeout = int.MaxValue;

			while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout) {
				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (autoReference != null) {
					while (autoReference.dDeque.PopBottom (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (self, value))
							value.Execute (autoReference.ChildWorkAdder);
						else {
							sharedWorkQueue.TryAdd (value);
							evt.Set ();
						}

						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))
						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))
							value.Execute (null);
						else {
							if (autoReference == null)
								sharedWorkQueue.TryAdd (value);
							else
								autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}
					}

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

				if (--tries > stage1)
					Thread.Yield ();
				else if (tries >= stage2)
					predicateEvt.Wait (ComputeTimeout (100, millisecondsTimeout, watch));
				else {
					if (tries == stage2 - 1)
						handles = new [] { predicateEvt.WaitHandle, evt };
					WaitHandle.WaitAny (handles, ComputeTimeout (1000, millisecondsTimeout, watch));
				}
			}
		}
Ejemplo n.º 19
0
 protected ProcessorBase(int maxThreads, IProducerConsumerCollection <T> baseCollection, CancellationToken cancelToken)
 {
     items       = baseCollection;
     MaxThreads  = maxThreads;
     CancelToken = cancelToken;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes the asynchronous producer/consumer collection.
 /// </summary>
 /// <param name="collection">The underlying collection to use to store
 /// data.</param>
 public AsyncQueue(IProducerConsumerCollection <T> collection)
 {
     _collection = collection ??
                   throw new ArgumentNullException(nameof(collection));
 }
 protected abstract bool IsEmpty(IProducerConsumerCollection <int> pcc);
Ejemplo n.º 22
0
 public ResourceSearchStrategy(ApiVisibility apiVisibility, SearchRequest request, IProducerConsumerCollection <SearchResult> resultQueue)
     : base(request, resultQueue)
 {
     this.treeNodeFactory = request.TreeNodeFactory;
     this.apiVisibility   = apiVisibility;
     this.searchInside    = true;
 }
Ejemplo n.º 23
0
 /// <summary>
 ///     Creates a new Transformer instance.
 /// </summary>
 public Transformer(IProducerConsumerCollection <TConsume> consume, Transform <TProduce, TConsume> transform, ITransformer dependentTransformer)
     : this(consume, transform, dependentTransformer, dependentTransformer.ThreadCount)
 {
 }
Ejemplo n.º 24
0
 /// <summary>
 ///     Creates a new Transformer instance.
 /// </summary>
 public Transformer(IProducerConsumerCollection <TConsume> consume, Transform <TProduce, TConsume> transform, ITransformer dependentTransformer, int threads)
     : base(consume, dependentTransformer, threads)
 {
     _transform = transform;
 }
Ejemplo n.º 25
0
 /// <summary>
 ///     Creates a new Transformer instance.
 /// </summary>
 public Transformer(IProducerConsumerCollection <TConsume> consume, Transform <TProduce, TConsume> transform, int threads)
     : this(consume, transform, null, threads)
 {
 }
Ejemplo n.º 26
0
 /// <summary>
 ///     Creates a new Transformer instance.
 /// </summary>
 public Transformer(IProducerConsumerCollection <TConsume> consume, Transform <TProduce, TConsume> transform)
     : this(consume, transform, null, DefaultThreadCount)
 {
 }
 public TimedTokenProvider(T authConstant, int maxQueuedAuthTokens, IProducerConsumerCollection <TaskCompletionSource <T> > waitingCollection)
 {
     _authorizationConstant = authConstant;
     _maxQueuedAuthTokens   = maxQueuedAuthTokens;
     _waiting = waitingCollection;
 }
Ejemplo n.º 28
0
 public CommandBus(IProducerConsumerCollection<Envelope<ICommand>> commandCollection)
 {
     if (commandCollection == null) throw new ArgumentNullException(nameof(commandCollection));
     _commandCollection = commandCollection;
 }
 protected abstract bool TryPeek <T>(IProducerConsumerCollection <T> pcc, out T result);
Ejemplo n.º 30
0
 /// <summary>
 /// Initializes a new instance of <see cref="AsyncCollection{T}"/> with a specified <see cref="IProducerConsumerCollection{T}"/> as an underlying item storage.
 /// </summary>
 /// <param name="itemQueue">The collection to use as an underlying item storage. MUST NOT be accessed elsewhere.</param>
 public AsyncCollection(IProducerConsumerCollection <T> itemQueue)
 {
     _itemQueue    = itemQueue;
     _queueBalance = _itemQueue.Count;
 }
 /// <summary>
 /// Initializes an instance of the ObservableConcurrentCollection class with the specified
 /// collection as the underlying data structure.
 /// </summary>
 public ObservableConcurrentCollection(IProducerConsumerCollection <T> collection) : base(collection)
 {
     _context = AsyncOperationManager.SynchronizationContext;
 }
Ejemplo n.º 32
0
 protected ProcessorBase(int maxThreads, IProducerConsumerCollection <T> baseCollection)
     : this(maxThreads, baseCollection, CancellationToken.None)
 {
 }
Ejemplo n.º 33
0
 /// <summary>Creates a new <see cref="Pool&lt;T>"/> object.</summary>
 /// <param name="store">The <see cref="IProducerConsumerCollection&lt;T>"/> to use as a backing store to the pool.</param>
 /// <exception cref="ArgumentNullException"><paramref name="store"/> was null.</exception>
 public Pool(IProducerConsumerCollection <T> store)
     : this(store, null, int.MaxValue)
 {
 }
Ejemplo n.º 34
0
 public MemoryQueue(IProducerConsumerCollection <T> collection)
 {
     _Collection = collection;
 }
Ejemplo n.º 35
0
 /// <summary>Creates a new <see cref="Pool&lt;T>"/> object.</summary>
 /// <param name="store">The <see cref="IProducerConsumerCollection&lt;T>"/> to use as a backing store to the pool.</param>
 /// <param name="factory">The default factory to create new items as needed. It can be null, but in this case
 /// the overload of <see cref="Get()"/> that doesn’t take a factory as a parameter will throw <see cref="InvalidOperationException"/>.</param>
 /// <param name="max">A maximum size for the pool. If <see cref="int.MaxValue"/> is passed, the
 /// maximum is ignored.</param>
 /// <exception cref="ArgumentNullException"><paramref name="store"/> was null.</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="max"/> was less than one.</exception>
 public Pool(IProducerConsumerCollection <T> store, Func <T> factory, int max)
     : this(store, factory, max, 0)
 {
 }
Ejemplo n.º 36
0
 public BlockingCollection(IProducerConsumerCollection <T> underlyingColl)
     : this(underlyingColl, -1)
 {
 }
Ejemplo n.º 37
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 WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
		                                 ThreadWorker[] others, ManualResetEvent evt)
		{
			while (!predicate ()) {
				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (autoReference != null) {
					while (autoReference.dDeque.PopBottom (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (value))
							value.Execute (autoReference.ChildWorkAdder);
						else {
							autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}

						if (predicate ())
							return;
					}
				}

				// Dequeue only one item as we have restriction
				while (sharedWorkQueue.TryTake (out value) && value != null) {
					evt.Set ();
					if (CheckTaskFitness (value))
						value.Execute (null);
					else {
						sharedWorkQueue.TryAdd (value);
						evt.Set ();
					}

					if (predicate ())
						return;
				}

				// First check to see if we comply to predicate
				if (predicate ())
					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]) == null)
						continue;
					
					if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (value))
							value.Execute (null);
						else {
							sharedWorkQueue.TryAdd (value);
							evt.Set ();
						}
					}
					
					if (predicate ())
						return;
				}

				Thread.Yield ();
			}
		}
Ejemplo n.º 38
0
		private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count )
		{
			using ( var clientTransportManager = new UdpClientTransportManager( new RpcClientConfiguration() { PreferIPv4 = true } ) )
			using ( var connectTask = clientTransportManager.ConnectAsync( endPoint ) )
			{
				if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
				{
					throw new TimeoutException();
				}

				using ( var clientTransport = connectTask.Result )
				{
					for ( int i = 0; i < count; i++ )
					{
						if ( arrivalLatch != null )
						{
							arrivalLatch.Reset();
						}

						var args = Enumerable.Repeat( 0, arrivalLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray();
						var exceptions = new ConcurrentBag<Exception>();

						if ( !Task.Factory.ContinueWhenAll(
							args.Select(
								arg =>
									Task.Factory.StartNew(
										() =>
										{
											var requestContext = clientTransport.GetClientRequestContext();
											requestContext.SetNotification(
												"Dummy",
												( exception, completedSynchronously ) =>
												{
													if ( exception != null )
													{
														exceptions.Add( exception );
													}

													arrivalLatch.Signal();
												}
											);
											requestContext.ArgumentsPacker.PackArrayHeader( 1 );
											requestContext.ArgumentsPacker.Pack( arg );

											return requestContext;
										}
									)
							).ToArray(),
							previouses =>
							{
								var contexts = previouses.Select( previous => previous.Result ).ToArray();
								foreach ( var context in contexts )
								{
									clientTransport.Send( context );
								}
							}
						).ContinueWith(
							previous =>
							{
								if ( previous.IsFaulted )
								{
									throw previous.Exception;
								}

								// receive
								if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
								{
									throw new TimeoutException( "Receive" );
								}

								if ( exceptions.Any() )
								{
									throw new AggregateException( exceptions );
								}
							}
						).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
						{
							throw new TimeoutException();
						}
					}
				}
			}
		}
Ejemplo n.º 39
0
		public ThreadWorker (IScheduler sched, ThreadWorker[] others, IProducerConsumerCollection<Task> sharedWorkQueue,
		                     int maxStackSize, ThreadPriority priority)
			: this (sched, others, sharedWorkQueue, true, maxStackSize, priority)
		{
		}
Ejemplo n.º 40
0
 public PushQueue(IProducerConsumerCollection <T> source, int capacity) : this(
         new BlockingCollection <T>(source, capacity), true)
 {
 }
Ejemplo n.º 41
0
        public static void RemoveStressTest(IProducerConsumerCollection <int> coll, CheckOrderingType order)
        {
            ParallelTestHelper.Repeat(delegate {
                const int count   = 10;
                const int threads = 5;
                const int delta   = 5;

                for (int i = 0; i < (count + delta) * threads; i++)
                {
                    while (!coll.TryAdd(i))
                    {
                        ;
                    }
                }

                bool state = true;

                Assert.AreEqual((count + delta) * threads, coll.Count, "#0");

                ParallelTestHelper.ParallelStressTest(coll, (q) => {
                    bool s = true;
                    int t;

                    for (int i = 0; i < count; i++)
                    {
                        s &= coll.TryTake(out t);
                        // try again in case it was a transient failure
                        if (!s && coll.TryTake(out t))
                        {
                            s = true;
                        }
                    }

                    if (!s)
                    {
                        state = false;
                    }
                }, threads);

                Assert.IsTrue(state, "#1");
                Assert.AreEqual(delta * threads, coll.Count, "#2");

                string actual = string.Empty;
                int temp;
                while (coll.TryTake(out temp))
                {
                    actual += temp.ToString();;
                }

                IEnumerable <int> range = Enumerable.Range(order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
                if (order == CheckOrderingType.Reversed)
                {
                    range = range.Reverse();
                }

                string expected = range.Aggregate(string.Empty, (acc, v) => acc + v);

                if (order == CheckOrderingType.DontCare)
                {
                    CollectionAssert.AreEquivalent(expected, actual, "#3");
                }
                else
                {
                    Assert.AreEqual(expected, actual, "#3");
                }
            }, 1000);
        }
Ejemplo n.º 42
0
 /// <summary>
 /// Custom constructor using JobExecution.
 /// </summary>
 /// <param name="original"></param>
 public JobExecution(JobExecution original)
 {
     JobParameters = original.JobParameters;
     JobInstance = original.JobInstance;
     _stepExecutions = original._stepExecutions;
     _status = original.Status;
     StartTime = original.StartTime;
     CreateTime = original.CreateTime;
     EndTime = original.EndTime;
     LastUpdated = original.LastUpdated;
     _exitStatus = original.ExitStatus;
     _executionContext = original.ExecutionContext;
     _failureExceptions = original.FailureExceptions;
     _jobConfigurationName = original.JobConfigurationName;
     Id = original.Id;
     Version = original.Version;
 }
Ejemplo n.º 43
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;
				}
			}
		}
Ejemplo n.º 44
0
 public PushQueue(IProducerConsumerCollection <T> source) : this(new BlockingCollection <T>(source),
                                                                 true)
 {
 }
Ejemplo n.º 45
0
 public RateLimiter(IProducerConsumerCollection<Task> Queue)
 {
 }
Ejemplo n.º 46
0
 public EventBus(IProducerConsumerCollection<Envelope<IEvent>> eventCollection)
 {
     if (eventCollection == null) throw new ArgumentNullException(nameof(eventCollection));
     _eventCollection = eventCollection;
 }
Ejemplo n.º 47
0
 public static IProducerConsumerCollection <T> ToConsumerOnlyCollection <T>(
     this IProducerConsumerCollection <T> collection)
 {
     return((IProducerConsumerCollection <T>) new ProducerConsumerCollectionExtensions.ProduceOrConsumeOnlyCollection <T>(collection, false));
 }
Ejemplo n.º 48
0
 public WorkQueue(IProducerConsumerCollection<WorkTask> workTaskCollection)
 {
     workQueue = new BlockingCollection<WorkTask>(workTaskCollection);
 }
Ejemplo n.º 49
0
 protected override bool TryPeek <T>(IProducerConsumerCollection <T> pcc, out T result) => ((ConcurrentStack <T>)pcc).TryPeek(out result);
Ejemplo n.º 50
0
 public BlockingCollection(IProducerConsumerCollection <T> collection, int boundedCapacity)
 {
     this.underlyingColl = collection;
     this.upperBound     = boundedCapacity;
     this.isComplete     = new AtomicBoolean();
 }
Ejemplo n.º 51
0
        protected AbstractSearchStrategy(Language language, ApiVisibility apiVisibility, IProducerConsumerCollection <SearchResult> resultQueue, params string[] terms)
        {
            this.language      = language;
            this.apiVisibility = apiVisibility;
            this.resultQueue   = resultQueue;

            if (terms.Length == 1 && terms[0].Length > 2)
            {
                string search = terms[0];
                if (search.StartsWith("/", StringComparison.Ordinal) && search.Length > 4)
                {
                    var regexString = search.Substring(1, search.Length - 1);
                    fullNameSearch = search.Contains("\\.");
                    omitGenerics   = !search.Contains("<");
                    if (regexString.EndsWith("/", StringComparison.Ordinal))
                    {
                        regexString = regexString.Substring(0, regexString.Length - 1);
                    }
                    regex = SafeNewRegex(regexString);
                }
                else
                {
                    fullNameSearch = search.Contains(".");
                    omitGenerics   = !search.Contains("<");
                }
            }
            searchTerm = terms;
        }
Ejemplo n.º 52
0
 /// <summary>Creates a new <see cref="Pool&lt;T>"/> object.</summary>
 /// <param name="store">The <see cref="IProducerConsumerCollection&lt;T>"/> to use as a backing store to the pool.</param>
 /// <param name="factory">The default factory to create new items as needed. It can be null, but in this case
 /// the overload of <see cref="Get()"/> that doesn’t take a factory as a parameter will throw <see cref="InvalidOperationException"/>.</param>
 /// <exception cref="ArgumentNullException"><paramref name="store"/> was null.</exception>
 public Pool(IProducerConsumerCollection <T> store, Func <T> factory)
     : this(store, factory, int.MaxValue)
 {
 }
Ejemplo n.º 53
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryCommandQueue"/> class.
 /// </summary>
 /// <param name="collection">The collection to use as the underlying data store.</param>
 public InMemoryCommandQueue(IProducerConsumerCollection <ICommand> collection)
 {
     this.queue = new BlockingCollection <ICommand>(collection);
 }
Ejemplo n.º 54
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 WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
		                                 ThreadWorker[] others)
		{
			while (!predicate ()) {
				Task value;
				
				// Dequeue only one item as we have restriction
				if (sharedWorkQueue.TryTake (out value)) {
					if (value != null) {
						if (CheckTaskFitness (value))
							value.Execute (null);
						else
							sharedWorkQueue.TryAdd (value);
					}
				}
				
				// First check to see if we comply to predicate
				if (predicate ()) {
					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]) == null)
						continue;
					
					if (other.dDeque.PopTop (out value) == PopResult.Succeed) {
						if (value != null) {
							if (CheckTaskFitness (value))
								value.Execute (null);
							else
								sharedWorkQueue.TryAdd (value);
						}
					}
					
					if (predicate ()) {
						return;
					}
				}
			}
		}
Ejemplo n.º 55
0
 /// <summary>Creates a new <see cref="Pool&lt;T>"/> object.</summary>
 /// <param name="store">The <see cref="IProducerConsumerCollection&lt;T>"/> to use as a backing store to the pool.</param>
 /// <param name="max">A maximum size for the pool. If <see cref="int.MaxValue"/> is passed, the
 /// maximum is ignored.</param>
 /// <exception cref="ArgumentNullException"><paramref name="store"/> was null.</exception>
 public Pool(IProducerConsumerCollection <T> store, int max)
     : this(store, null, max)
 {
 }
		private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count )
		{
			using ( var udpClient = new UdpClient( AddressFamily.InterNetwork ) )
			using ( var concurrencyLatch = new CountdownEvent( arrivalLatch.InitialCount ) )
			{
				udpClient.Connect( endPoint );

				for ( int i = 0; i < count; i++ )
				{
					if ( concurrencyLatch != null )
					{
						concurrencyLatch.Reset();
					}

					arrivalLatch.Reset();

					// Clear ids.
					string dummy;
					while ( arrivedIds.TryTake( out dummy ) ) { }

					var ids = Enumerable.Repeat( 0, concurrencyLatch == null ? 1 : concurrencyLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray();

					if ( !Task.WaitAll(
						ids.Select(
							id =>
								Task.Factory.StartNew(
									_ =>
									{
										using ( var buffer = new MemoryStream() )
										{
											using ( var packer = Packer.Create( buffer, false ) )
											{
												PackRequest( packer, id );
											}

											buffer.Position = 0;

											if ( concurrencyLatch != null )
											{
												concurrencyLatch.Signal();
												if ( !concurrencyLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
												{
													throw new TimeoutException();
												}
											}

											// send
											udpClient.Send( buffer.ToArray(), ( int )buffer.Length );
										}
									},
									id
								)
						).ToArray(),
						Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds
					) )
					{
						throw new TimeoutException();
					}

					// wait
					if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
					{
						throw new TimeoutException();
					}

					Assert.That( arrivedIds, Is.EquivalentTo( ids ) );
				}
			}
		}
Ejemplo n.º 57
0
 public BlockingCollection(IProducerConsumerCollection <T> collection)
     : this(collection, -1)
 {
 }
Ejemplo n.º 58
-1
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryCommandQueue"/> class.
 /// </summary> 
 /// <param name="collection">The collection to use as the underlying data store.</param>
 public InMemoryCommandQueue(IProducerConsumerCollection<ICommand> collection)
 {
     this.queue = new BlockingCollection<ICommand>(collection);
 }