Exemple #1
0
        /// <summary>
        /// Creates future from specified .Net task.
        /// Completion of this task will await with specified awaiter.
        /// </summary>
        /// <param name="awaiter">A strategy of await task completion.</param>
        /// <param name="task">A task which should be transformed to future.</param>
        /// <typeparam name="T">Type of task result.</typeparam>
        /// <returns>A future which wraps task computation results.</returns>
        public static Future <T> From <T>(IAwaiter awaiter, Task <T> task)
        {
            var promise       = new Promise <T>();
            var awaitableTask = AwaitableTask <T> .Acquire(task, promise);

            awaiter.Await(awaitableTask, awaitableTask);

            return(promise.Future);
        }
Exemple #2
0
        private static void Release(AwaitableTask <T> awaitableTask)
        {
            awaitableTask._task    = null;
            awaitableTask._promise = null;

            if (Pool.Count < PoolSize)
            {
                Pool.Push(awaitableTask);
            }
        }
Exemple #3
0
        public static AwaitableTask <T> Acquire(Task <T> task, Promise <T> promise)
        {
            if (!Pool.TryPop(out var awaitableTask))
            {
                awaitableTask = new AwaitableTask <T>();
            }

            awaitableTask._task    = task;
            awaitableTask._promise = promise;

            return(awaitableTask);
        }