Example #1
0
        internal override void TryAlt(ref Worker wr, int i, Cont <T> xK, Else xE)
        {
            var pkSelf = xE.pk;

            this.Lock.Enter();
            var tail = this.Givers;

            if (null == tail)
            {
                goto TryTaker;
            }
            Send <T> cache  = null;
            var      cursor = tail.Next;

TryGiver:
            var giver = cursor as Giver <T>;
            Pick pkOther = null;

            if (null != giver)
            {
                goto Giver;
            }

TryPick:
            var st = Pick.TryPick(pkSelf);

            if (st > 0)
            {
                goto AlreadyPicked;
            }
            if (st < 0)
            {
                goto TryPick;
            }

            WaitQueue.RemoveRange(ref this.Givers, cursor);
            this.Lock.Exit();

            Pick.SetNacks(ref wr, i, pkSelf);
            Cont.Do(xK, ref wr, cursor.Value);
            return;

AlreadyPicked:
            WaitQueue.RemoveRangeInclusive(this.Givers, cursor);
            this.Lock.Exit();
            return;

Giver:
            cursor  = cursor.Next;
            pkOther = giver.Pick;

            if (null == pkOther)
            {
                goto TryPickSelf;
            }
            if (pkOther == pkSelf)
            {
                goto OtherIsSelf;
            }

TryPickOther:
            var stOther = Pick.TryClaim(pkOther);

            if (stOther > 0)
            {
                goto TryNextGiver;
            }
            if (stOther < 0)
            {
                goto TryPickOther;
            }

TryPickSelf:
            var stSelf = Pick.TryPick(pkSelf);

            if (stSelf > 0)
            {
                goto SelfAlreadyPicked;
            }
            if (stSelf < 0)
            {
                goto BackOff;
            }

            //GotGiver:
            WaitQueue.RemoveRange(ref this.Givers, giver);
            this.Lock.Exit();
            if (null != pkOther)
            {
                Pick.PickClaimedAndSetNacks(ref wr, giver.Me, pkOther);
            }
            Pick.SetNacks(ref wr, i, pkSelf);
            Worker.Push(ref wr, giver.Cont);
            Cont.Do(xK, ref wr, giver.Value);
            return;

BackOff:
            if (null == pkOther)
            {
                goto TryPickSelf;
            }
            Pick.Unclaim(pkOther);
            goto TryPickOther;

OtherIsSelf:
            WaitQueue.Enqueue(ref cache, giver);
            if (giver != tail)
            {
                goto TryGiver;
            }

            this.Givers = cache;
            goto TryTaker;

TryNextGiver:
            if (giver != tail)
            {
                goto TryGiver;
            }

            this.Givers = cache;
TryTaker:
            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, xK);
            this.Lock.Exit();
            xE.TryElse(ref wr, i + 1);
            return;

SelfAlreadyPicked:
            if (null != pkOther)
            {
                Pick.Unclaim(pkOther);
            }

            WaitQueue.RemoveRangeInclusive(this.Givers, giver);
            this.Lock.Exit();
            return;
        }
Example #2
0
            internal override void TryAlt(ref Worker wr, int i, Cont <Unit> uK, Else uE)
            {
                var pkSelf = uE.pk;
                var ch     = this.Ch;

                ch.Lock.Enter();
                var tail = ch.Takers;

                if (null == tail)
                {
                    goto TryGiver;
                }
                Cont <T> cache  = null;
                var      cursor = tail.Next;

TryTaker:
                var taker = cursor as Cont <T>;

                cursor = cursor.Next;

                int me      = 0;
                var pkOther = taker.GetPick(ref me);

                if (null == pkOther)
                {
                    goto TryPickSelf;
                }
                if (pkOther == pkSelf)
                {
                    goto OtherIsSelf;
                }

TryPickOther:
                var stOther = Pick.TryClaim(pkOther);

                if (stOther > 0)
                {
                    goto TryNextTaker;
                }
                if (stOther < 0)
                {
                    goto TryPickOther;
                }

TryPickSelf:
                var stSelf = Pick.TryPick(pkSelf);

                if (stSelf > 0)
                {
                    goto SelfAlreadyPicked;
                }
                if (stSelf < 0)
                {
                    goto BackOff;
                }

                WaitQueue.RemoveRange(ref ch.Takers, taker);
                ch.Lock.Exit();
                if (null != pkOther)
                {
                    Pick.PickClaimedAndSetNacks(ref wr, me, pkOther);
                }
                Pick.SetNacks(ref wr, i, pkSelf);
                taker.Value = this.X;
                Worker.Push(ref wr, taker);
                Work.Do(uK, ref wr);
                return;

BackOff:
                if (null == pkOther)
                {
                    goto TryPickSelf;
                }
                Pick.Unclaim(pkOther);
                goto TryPickOther;

OtherIsSelf:
                WaitQueue.Enqueue(ref cache, taker);
                if (taker != tail)
                {
                    goto TryTaker;
                }

                ch.Takers = cache;
                goto TryGiver;

TryNextTaker:
                if (taker != tail)
                {
                    goto TryTaker;
                }

                ch.Takers = cache;
TryGiver:
                WaitQueue.AddGiver(ref ch.Givers, this.X, i, pkSelf, uK);
                ch.Lock.Exit();
                uE.TryElse(ref wr, i + 1);
                return;

SelfAlreadyPicked:
                if (null != pkOther)
                {
                    Pick.Unclaim(pkOther);
                }

                WaitQueue.RemoveRangeInclusive(ch.Takers, taker);
                ch.Lock.Exit();
                return;
            }