示例#1
0
        internal override void DoJob(ref Worker wr, Cont <X> xK)
        {
Spin:
            var st = this.state;

            if (st > 0)
            {
                goto Done;
            }
            if (st < 0)
            {
                goto Spin;
            }
            if (0 != Interlocked.CompareExchange(ref this.state, 1, st))
            {
                goto Spin;
            }

            var x = this.value;

            this.value = default(X);
            Cont.Do(xK, ref wr, x);
Done:
            return;
        }
示例#2
0
        // Note that via selective communication it is possible for a job to offer
        // to both give and take on the same channel simultaneously.  So, both
        // Givers and Takers queues must be maintained even though in many cases
        // only one of them is non empty.

        internal override void DoJob(ref Worker wr, Cont <T> xK)
        {
TryNextGiver:
            this.Lock.Enter();
            var tail = this.Givers;

            if (null != tail)
            {
                goto TryGiver;
            }
            WaitQueue.AddTaker(ref this.Takers, xK);
            this.Lock.Exit();
            return;

TryGiver:
            var cursor = tail.Next;

            if (tail == cursor)
            {
                this.Givers = null;
            }
            else
            {
                tail.Next = cursor.Next;
            }
            this.Lock.Exit();

            var giver = cursor as Giver <T>;

            if (null == giver)
            {
                goto GotSend;
            }

            var pkOther = giver.Pick;

            if (null == pkOther)
            {
                goto GotGiver;
            }

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

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

            Pick.SetNacks(ref wr, giver.Me, pkOther);
GotGiver:
            Worker.Push(ref wr, giver.Cont);
GotSend:
            Cont.Do(xK, ref wr, cursor.Value);
            return;
        }
示例#3
0
        internal override void DoJob(ref Worker wr, Cont <T> aK)
        {
Spin:
            var state = this.State;

            if (state == Locked)
            {
                goto Spin;
            }
            if (state != Interlocked.CompareExchange(ref this.State, Locked, state))
            {
                goto Spin;
            }

            if (state <= Empty)
            {
                goto EmptyOrDemand;
            }

            T value = this.Value;

            this.Value = default(T); // Avoid memory leaks.
            this.State = Empty;
            Cont.Do(aK, ref wr, value);
            return;

EmptyOrDemand:
            WaitQueue.AddTaker(ref this.Takers, aK);
            this.State = Demand;
        }
示例#4
0
        internal override void DoJob(ref Worker wr, Cont <T> aK)
        {
Spin:
            var state = this.State;

Reconsider:
            if (state > Running)
            {
                goto Completed;
            }
            if (state < Delayed)
            {
                goto Spin;
            }
            var check = state;

            state = Interlocked.CompareExchange(ref this.State, state - MakeLocked, state);
            if (Delayed == state)
            {
                goto Delayed;
            }
            if (state != check)
            {
                goto Reconsider;
            }

            WaitQueue.AddTaker(ref this.Readers, aK);
            this.State = Running;
            return;

Delayed:
            var readers = this.Readers;

            this.Readers = null;
            this.State   = Running;

            var fulfill = readers as Fulfill;

            fulfill.tP     = this;
            fulfill.reader = aK;
            var tJ = fulfill.tJ;

            fulfill.tJ = null;
            Job.Do(tJ, ref wr, fulfill);
            return;

Completed:
            if (state == HasValue)
            {
                Cont.Do(aK, ref wr, this.Value);
            }
            else
            {
                aK.DoHandle(ref wr, (this.Readers as Fail <T>).exn);
            }
        }
示例#5
0
        internal override void TryAlt(ref Worker wr, int i, Cont <T> aK, Else aE)
        {
            var pkSelf = aE.pk;

Spin:
            var state = this.State;

            if (state == Locked)
            {
                goto Spin;
            }
            if (state != Interlocked.CompareExchange(ref this.State, Locked, state))
            {
                goto Spin;
            }

            if (state <= Empty)
            {
                goto EmptyOrDemand;
            }

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

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

            Pick.SetNacks(ref wr, i, pkSelf);

            T value = this.Value;

            this.Value = default(T);
            this.State = Empty;
            Cont.Do(aK, ref wr, value);
            return;

AlreadyPicked:
            this.State = Full;
            return;

EmptyOrDemand:
            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK);
            this.State = Demand;
            aE.TryElse(ref wr, i + 1);
            return;
        }
示例#6
0
        internal override void TryAlt(ref Worker wr, int i, Cont <X> xK, Else xE)
        {
Spin:
            var st = this.state;

            if (st > 0)
            {
                goto Done;
            }
            if (st < 0)
            {
                goto Spin;
            }
            if (0 != Interlocked.CompareExchange(ref this.state, -1, st))
            {
                goto Spin;
            }

            var pk = xE.pk;

TryPick:
            var stPk = Pick.TryPick(pk);

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

            this.state = 1;
            Pick.SetNacks(ref wr, i, pk);

            var x = this.value;

            this.value = default(X);
            Cont.Do(xK, ref wr, x);
            return;

AlreadyPicked:
            st = 0;
Done:
            xE.TryElse(ref wr, i);
        }
示例#7
0
文件: Mailbox.cs 项目: sjkdev21/Hopac
        internal override void DoJob(ref Worker wr, Cont <T> aK)
        {
            this.Lock.Enter();

            if (this.Values.Count > 0)
            {
                goto GotValue;
            }

            WaitQueue.AddTaker(ref this.Takers, aK);
            this.Lock.Exit();
            return;

GotValue:
            T value = this.Values.Dequeue();

            this.Lock.Exit();
            Cont.Do(aK, ref wr, value);
            return;
        }
示例#8
0
文件: Mailbox.cs 项目: sjkdev21/Hopac
        internal override void TryAlt(ref Worker wr, int i, Cont <T> aK, Else aE)
        {
            var pkSelf = aE.pk;

            this.Lock.Enter();

            if (this.Values.Count > 0)
            {
                goto GotValue;
            }

            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK);
            this.Lock.Exit();
            aE.TryElse(ref wr, i + 1);
            return;

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

            if (stSelf > 0)
            {
                goto AlreadyPicked;
            }
            if (stSelf < 0)
            {
                goto GotValue;
            }

            T value = this.Values.Dequeue();

            this.Lock.Exit();
            Pick.SetNacks(ref wr, i, pkSelf);
            Cont.Do(aK, ref wr, value);
            return;

AlreadyPicked:
            this.Lock.Exit();
            return;
        }
示例#9
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;
        }
示例#10
0
        internal override void TryAlt(ref Worker wr, int i, Cont <T> aK, Else aE)
        {
Spin:
            var state = this.State;

Reconsider:
            if (state > Running)
            {
                goto Completed;
            }
            if (state < Delayed)
            {
                goto Spin;
            }
            var check = state;

            state = Interlocked.CompareExchange(ref this.State, state - MakeLocked, state);
            if (Delayed == state)
            {
                goto Delayed;
            }
            if (state != check)
            {
                goto Reconsider;
            }

            WaitQueue.AddTaker(ref this.Readers, i, aE.pk, aK);
            this.State = Running;
            aE.TryElse(ref wr, i + 1);
            return;

Delayed:
            var readers = this.Readers;

            this.Readers = null;
            this.State   = Running;

            var fulfill = readers as Fulfill;

            fulfill.reader = aK;
            fulfill.me     = i;
            fulfill.pk     = aE.pk;

            Worker.PushNew(ref wr, fulfill);
            aE.TryElse(ref wr, i + i);
            return;

Completed:
            var pkSelf = aE.pk;

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

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

            Pick.SetNacks(ref wr, i, pkSelf);

            if (state == HasValue)
            {
                Cont.Do(aK, ref wr, this.Value);
            }
            else
            {
                aK.DoHandle(ref wr, (this.Readers as Fail <T>).exn);
            }
AlreadyPicked:
            return;
        }
示例#11
0
文件: Promise.cs 项目: kolektiv/Hopac
        internal override void TryAlt(ref Worker wr, int i, Cont <T> aK, Else aE)
        {
            var pkSelf = aE.pk;

Spin:
            var state = this.State;

            if (state > Running)
            {
                goto Completed;
            }
            if (state < Delayed)
            {
                goto Spin;
            }
            if (state != Interlocked.CompareExchange(ref this.State, ~state, state))
            {
                goto Spin;
            }

            if (Delayed == state)
            {
                goto Delayed;
            }

            WaitQueue.AddTaker(ref this.Readers, i, pkSelf, aK);
            this.State = Running;
            aE.TryElse(ref wr, i + 1);
            return;

Delayed:
            var taker = new Taker <T>();

            taker.Cont = aK;
            taker.Me   = i;
            taker.Pick = pkSelf;
            taker.Next = taker;

            var readers = this.Readers;

            this.Readers = taker;
            this.State   = Running;

            var fulfill = readers as Fulfill;
            var tJ      = fulfill.tP;

            fulfill.tP = this;
            Worker.PushNew(ref wr, new JobWork <T>(tJ, fulfill));
            aE.TryElse(ref wr, i + i);
            return;

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

            if (stSelf > 0)
            {
                goto AlreadyPicked;
            }
            if (stSelf < 0)
            {
                goto Completed;
            }

            Pick.SetNacks(ref wr, i, pkSelf);

            if (state == HasValue)
            {
                Cont.Do(aK, ref wr, this.Value);
            }
            else
            {
                aK.DoHandle(ref wr, (this.Readers as Fail <T>).exn);
            }
AlreadyPicked:
            return;
        }