Example #1
0
        // allocates a slot; p can be null; returns slot index
        protected int alloc(Promise p, EvictHandler h)
        {
            // using promise to alloc?
            if (p != null)
            {
                // yes: clear out contents if there are any
                if (slots[p.slot].used)
                {
                    // shouldn't already have promised this slot
                    if (slots[p.slot].promise)
                        throw new Exception("attempting to use promise on already-promised slot!");

                    // evict speculative contents...
                    slots[p.slot].handler();
                    slots[p.slot].used = false;
                }

                slots[p.slot].used = true;
                slots[p.slot].promise = true;
                slots[p.slot].handler = null;

                return p.slot;
            }
            else
            {
                // no: attempt to grab a free slot if there is one
                for (int i = 0; i < slots.Length; i++)
                {
                    if (!slots[i].used)
                    {
                        slots[i].used = true;
                        slots[i].promise = false;
                        slots[i].handler = h;
                        return i;
                    }
                }

                // no free slots
                return -1;
            }
        }
Example #2
0
        public void putPromise(Promise p)
        {
#if DEBUG
            Console.WriteLine("putting promise {0} back, count was {1}", p, promises.Count);
#endif
            if (!p.active)
                throw new Exception("returning inactive promise!");

            p.active = false;
            promises.Enqueue(p);
        }