/// <summary> /// An internal constructor that initializes a smart thread with a parameter /// indicating that this insance is started in a threadpool. /// </summary> /// <param name="isInitializedInPool">Indicates that this instance will live in a threadpool.</param> internal SmartThread(bool isInitializedInPool, int cpuId) { this.isInitializedInPool = isInitializedInPool; this.cpuId = cpuId; thread = new Thread(new ThreadStart(Process)); thread.IsBackground = true; thread.Priority = Configuration.SmartThreadDefaultPrority; waitSpinLmit = Configuration.SmartThreadWaitSpinLimit; waitSpinTime = Configuration.SmartThreadWaitSpinTime; stealCondition = Configuration.SmartThreadStealCondition; waitSpinLmitOne = waitSpinLmit + 1; waitSpinLmitTwo = waitSpinLmit + 2; wait = new ManualResetEvent(isSignalled); scheduler = new LockFreeDequeue <IComputation>(); id = thread.ManagedThreadId; }
/// <summary> /// Tries to perform a steal from the provided queue. /// </summary> /// <param name="queue">the queue type.</param> /// <param name="condition">the condition for the queue steal.</param> /// <returns>a value indicationg the sucess or failure of the operation.</returns> private bool TryStealFromQueue(IStealingQueue <IComputation> queue, int conditionThreshold) { // Try steal as much as possible from the selected thread as the selection // procedure to steal is expensive. while (!queue.IsEmpty && queue.UnsafeCount >= conditionThreshold) { IComputation localComputation = queue.DequeueLast(); if (localComputation != null) { stealCount++; InvokeAction(localComputation); return(true); } else { return(false); } } return(false); }