Exemple #1
0
        public FiberThreadAwaiter <T> Set <T>(T data)
        {
            Type key = typeof(T);

            if (!receivers.ContainsKey(key) || receivers[key].Count == 0)
            {
                // Nobody receiving, let's wait until something comes up
                var GhostThread = Fiber.Current;

                if (GhostThread == null)
                {
                    GhostThread = this;
                }


                var waitingGhostThread = FiberThreadAwaiter <T> .New(GhostThread);

                waitingGhostThread.Result = data;

                if (senders.ContainsKey(key))
                {
                    senders[key].Enqueue(waitingGhostThread);
                    return(waitingGhostThread);
                }
                else
                {
                    ConcurrentQueue <FiberThreadAwaiterBase> table = new ConcurrentQueue <FiberThreadAwaiterBase>();
                    senders.AddOrUpdate(key, table, (a, b) => table);

                    senders[key].Enqueue(waitingGhostThread);
                    return(waitingGhostThread);
                }
            }



            FiberThreadAwaiterBase tmp;

            if (receivers[key].TryDequeue(out tmp))
            {
                var receiver = tmp as FiberThreadAwaiter <T>;

                receiver.Result      = data;
                receiver.IsCompleted = true;

                var previousSyncContext = SynchronizationContext.Current;
                SynchronizationContext.SetSynchronizationContext(_SynchronizationContext);
                if (receiver.Continuation != null)
                {
                    receiver.Continuation();
                }
                SynchronizationContext.SetSynchronizationContext(previousSyncContext);

                return(receiver);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        public FiberThreadAwaiter <T> Send <T>(T data)
        {
            Type key = typeof(T);


            // Nobody receiving, let's wait until something comes up
            var GhostThread = Fiber.Current;

            if (GhostThread == null)
            {
                GhostThread = this;
            }


            var waitingGhostThread = FiberThreadAwaiter <T> .New(GhostThread);

            waitingGhostThread.Result = data;

            if (senders.ContainsKey(key))
            {
                senders[key].Enqueue(waitingGhostThread);
            }
            else
            {
                ConcurrentQueue <FiberThreadAwaiterBase> table = new ConcurrentQueue <FiberThreadAwaiterBase>();
                senders.AddOrUpdate(key, table, (a, b) => table);
                senders[key].Enqueue(waitingGhostThread);
            }


            return(waitingGhostThread);
        }
Exemple #3
0
        public FiberThreadAwaiter <T> Back <T>()
        {
            Type key = typeof(T);

            if (!senders.ContainsKey(key) || senders[key].Count == 0)
            {
                var GhostThread = Fiber.Current;



                if (GhostThread == null)
                {
                    GhostThread = this;
                }

                var waitingGhostThread = FiberThreadAwaiter <T> .New(GhostThread);

                if (receivers.ContainsKey(key))
                {
                    receivers[key].Enqueue(waitingGhostThread);
                }
                else
                {
                    ConcurrentQueue <FiberThreadAwaiterBase> table = new ConcurrentQueue <FiberThreadAwaiterBase>();
                    receivers.AddOrUpdate(key, table, (a, b) => table);
                    receivers[key].Enqueue(waitingGhostThread);
                }
            }

            FiberThreadAwaiterBase sender;

            if (senders[key].TryDequeue(out sender))
            {
                sender.IsCompleted = true;

                var previousSyncContext = SynchronizationContext.Current;
                SynchronizationContext.SetSynchronizationContext(_SynchronizationContext);
                if (sender.Continuation != null)
                {
                    sender.Continuation();
                }
                SynchronizationContext.SetSynchronizationContext(previousSyncContext);

                var senderl = sender as FiberThreadAwaiter <T>;
                return(senderl);
            }
            else
            {
                return(null);
            }
        }
Exemple #4
0
        public FiberThreadAwaiter <T> Read <T>()
        {
            Type key = typeof(T);

            if (!senders.ContainsKey(key) || senders[key].Count == 0)
            {
                var GhostThread = Fiber.Current;



                if (GhostThread == null)
                {
                    GhostThread = this;
                }

                var waitingGhostThread = FiberThreadAwaiter <T> .New(GhostThread);

                if (receivers.ContainsKey(key))
                {
                    receivers[key].Enqueue(waitingGhostThread);
                    return(waitingGhostThread);
                }
                else
                {
                    ConcurrentQueue <FiberThreadAwaiterBase> table = new ConcurrentQueue <FiberThreadAwaiterBase>();
                    receivers.AddOrUpdate(key, table, (a, b) => table);
                    receivers[key].Enqueue(waitingGhostThread);
                    return(waitingGhostThread);
                }
            }

            FiberThreadAwaiterBase sender;

            if (senders[key].TryPeek(out sender))
            {
                sender.IsCompleted = true;
                var senderl = sender as FiberThreadAwaiter <T>;
                return(senderl);
            }
            else
            {
                return(null);
            }
        }