Beispiel #1
0
        //TODO:  SPEED UP THESE ENQUEUES!
        public void Enqueue(T item)
        {
            //bool taken = false;
            //locker.Enter(ref taken);
            locker.Enter();
            try
            {
                //Enqueues go to the tail only; it's like a queue.
                //head ----> tail

                if (count == array.Length)
                {
                    //Resize
                    //TODO: Better shift-resize
                    T[] oldArray = array;
                    array = new T[Math.Max(4, oldArray.Length * 2)];
                    //Copy the old first-end to the first part of the new array.
                    Array.Copy(oldArray, firstIndex, array, 0, oldArray.Length - firstIndex);
                    //Copy the old begin-first to the second part of the new array.
                    Array.Copy(oldArray, 0, array, oldArray.Length - firstIndex, firstIndex);
                    firstIndex = 0;
                    lastIndex  = count - 1;
                }


                lastIndex++;
                if (lastIndex == array.Length)
                {
                    lastIndex = 0;
                }
                array[lastIndex] = item;
                count++;
            }
            finally
            {
                locker.Exit();
                //locker.Exit();
            }
        }
Beispiel #2
0
        public static void Enter(this SpinLock spinLock)
        {
            bool token = false;

            spinLock.Enter(ref token);
        }