示例#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);
            }
        }
示例#2
0
文件: Promise.cs 项目: hirous/test
        // 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;
            }
        }