コード例 #1
0
ファイル: MVar.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        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;
            aK.DoCont(ref wr, value);
            return;

EmptyOrDemand:
            WaitQueue.AddTaker(ref this.Takers, aK);
            this.State = Demand;
        }
コード例 #2
0
ファイル: IVar.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void DoJob(ref Worker wr, Cont <T> aK)
        {
Spin:
            var state = this.State;

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

            WaitQueue.AddTaker(ref this.Takers, aK);
            this.State = Empty;
            return;

GotValue:
            aK.DoCont(ref wr, this.Value);
            return;
        }
コード例 #3
0
ファイル: Ch.cs プロジェクト: silky/Hopac
        // 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 implementation detail.
        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 pkOther = cursor.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, cursor.Me, pkOther);
GotGiver:
            var uK = cursor.Cont;

            uK.Next = null;
            Worker.Push(ref wr, uK);
            xK.DoCont(ref wr, cursor.Value);
            return;
        }
コード例 #4
0
ファイル: Promise.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void DoJob(ref Worker wr, Cont <T> aK)
        {
Spin:
            var state = this.State;

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

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

            var job = this.Readers as Cont;

            var taker = new Taker <T>();

            taker.Cont   = aK;
            taker.Next   = taker;
            this.Readers = taker;

            this.State = Running;

            job.DoWork(ref wr);
            return;

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

Completed:
            if (state == Completed)
            {
                aK.DoCont(ref wr, this.Value);
            }
            else
            {
                aK.DoHandle(ref wr, (this.Readers as Fail).exn);
            }
        }
コード例 #5
0
ファイル: MVar.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void TryAlt(ref Worker wr, int i, Pick pkSelf, 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;
            }

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

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

            T value = this.Value;

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

AlreadyPicked:
            this.State = Full;
            return;

EmptyOrDemand:
            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK);
            this.State = Demand;
            aK.TryNext(ref wr, i + 1, pkSelf);
            return;
        }
コード例 #6
0
        /// Internal implementation detail.
        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();
            aK.DoCont(ref wr, value);
            return;
        }
コード例 #7
0
ファイル: IVar.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void TryAlt(ref Worker wr, int i, Pick pkSelf, Cont <T> aK)
        {
Spin:
            var state = this.State;

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

            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK);
            this.State = Empty;
            aK.TryNext(ref wr, i + 1, pkSelf);
            return;

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

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

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

            aK.DoCont(ref wr, this.Value);
AlreadyPicked:
            return;
        }
コード例 #8
0
        /// Internal implementation detail.
        internal override void TryAlt(ref Worker wr, int i, Pick pkSelf, Cont <T> aK)
        {
            this.Lock.Enter();

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

            WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK);
            this.Lock.Exit();
            aK.TryNext(ref wr, i + 1, pkSelf);
            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);
            aK.DoCont(ref wr, value);
            return;

AlreadyPicked:
            this.Lock.Exit();
            return;
        }
コード例 #9
0
ファイル: Ch.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void TryAlt(ref Worker wr, int i, Pick pkSelf, Cont <T> xK)
        {
            this.Lock.Enter();
            var tail = this.Givers;

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

TryGiver:
            var giver = cursor;

            cursor = cursor.Next;
            var 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.ReplaceRange(ref this.Givers, giver, cache);
            this.Lock.Exit();
            if (null != pkOther)
            {
                Pick.PickClaimed(pkOther);
                Pick.SetNacks(ref wr, giver.Me, pkOther);
            }
            Pick.SetNacks(ref wr, i, pkSelf);
            var uK = giver.Cont;

            uK.Next = null;
            Worker.Push(ref wr, uK);
            xK.DoCont(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();
            xK.TryNext(ref wr, i + 1, pkSelf);
            return;

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

            WaitQueue.ReplaceRangeInclusive(this.Givers, giver, cache);
            this.Lock.Exit();
            return;
        }
コード例 #10
0
ファイル: Promise.cs プロジェクト: silky/Hopac
        /// Internal implementation detail.
        internal override void TryAlt(ref Worker wr, int i, Pick pkSelf, Cont <T> aK)
        {
Spin:
            var state = this.State;

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

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

            var job = this.Readers as Cont;

            var taker = new Taker <T>();

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

            this.State = Running;

            Worker.Push(ref wr, job);
            aK.TryNext(ref wr, i + 1, pkSelf);
            return;

Running:
            WaitQueue.AddTaker(ref this.Readers, i, pkSelf, aK);
            this.State = Running;
            aK.TryNext(ref wr, i + 1, pkSelf);
            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 == Completed)
            {
                aK.DoCont(ref wr, this.Value);
            }
            else
            {
                aK.DoHandle(ref wr, (this.Readers as Fail).exn);
            }
AlreadyPicked:
            return;
        }