Click here to Skip to main content
11,500,106 members (61,532 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Async/Await Could Be Better

, 11 Apr 2012 CPOL 70.5K 814 58
This article will explain how the async/await pair really works and why it could be better if real cooperative threading was used instead.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;

namespace Pfz.Threading.Cooperative
{
	/// <summary>
	/// Class that simulates a stack-saving mechanism (which does not exist in .Net).
	/// If in the future such mechanism exists, this class may be killed or simple redirect to it.
	/// At this moment, the "saved stack" runs in another thread completely synchronized with
	/// the main one. But this can cause dead-locks if the caller holds a lock that the calle
	/// should also use.
	/// Aside from that, you can YieldReturn at any moment, even inside sub-methods that are
	/// not enumerable.
	/// </summary>
	public sealed class StackSaver:
		IDisposable,
		IEnumerable<bool>,
		IEnumerator<bool>
	{
		internal static readonly object _syncAbortToken = new object();

		#if DEBUG
			private Thread _thisThread;
		#endif

		private readonly object _executePulser = new object();
		private Action _action;
		private bool _wasDisposed;
		private bool _executing;
		
		/// <summary>
		/// Creates a new StackSaver that will run the given action, allowing it to
		/// YieldReturn at any moment. It will not generate values, so it is used as
		/// a synchronization mechanism.
		/// </summary>
		public StackSaver(Action action)
		{
			_action = action;

			// The real implementation uses my UnlimitedThreadPool class.
			// I removed such class in this version to give a smaller download
			// and make the right classes easier to find.
			var thread = new Thread(_Start);
			thread.Start();
		}
		
		/// <summary>
		/// Releases the stack-saver. It will kill the executing method the same way a thread is aborted
		/// (with a ThreadAbortException, calling only catches and finallys).
		/// </summary>
		public void Dispose()
		{
			lock(_executePulser)
			{
				_wasDisposed = true;
				Monitor.Pulse(_executePulser);
				
				while(_action != null)
					Monitor.Wait(_executePulser);
			}
		}
		
		public bool WasDisposed
		{
			get
			{
				return _wasDisposed;
			}
		}
		
		/// <summary>
		/// Gets the exception that finished this stack-saver.
		/// </summary>
		public Exception Exception { get; private set; }

		[ThreadStatic]
		private static StackSaver _current;
		/// <summary>
		/// When inside the action of a stack-saver, the stack-saver can be discovered by this.
		/// If the return is null, then this method was called directly or by a value-producing stack-saver.
		/// </summary>
		public static StackSaver Current
		{
			get
			{
				return _current;
			}
		}
		
		private void _Start()
		{
			try
			{
				var thisThread = Thread.CurrentThread;
				thisThread.IsBackground = true;

				#if DEBUG
					_thisThread = thisThread;
				#endif

				_current = this;
		
				lock(_executePulser)
					_Wait();
					
				_action();
			}
			catch(ThreadAbortException abortException)
			{
				// if it is not our abort, rethrow.
				if (abortException.ExceptionState != _syncAbortToken)
				{
					Exception = abortException;
					throw;
				}

				// if it is our abort, it was used only to arrive here, so
				// now we can resetabort and let the thread return to the pool.
				Thread.ResetAbort();
			}
			catch(Exception exception)
			{
				Exception = exception;
			}
			finally
			{
				#if DEBUG
					_thisThread = null;
				#endif

				_current = null;
				_action = null;
				_executing = false;
				
				lock(_executePulser)
					Monitor.PulseAll(_executePulser);
			}
		}

		/// <summary>
		/// Yield returns to a stack-saver that does not produce values, considering there is one.
		/// If a stack-saver does not exist, this method throws an InvalidOperationException.
		/// </summary>
		public static void StaticYieldReturn()
		{
			var stackSaver = _current;
			if (stackSaver == null)
				throw new InvalidOperationException("This method is not managed by a non-returning stack-saver.");
				
			stackSaver.YieldReturn();
		}

		/// <summary>
		/// YieldReturns to the caller of this stack-saver.
		/// </summary>
		public void YieldReturn()
		{
			#if DEBUG
				if (Thread.CurrentThread != _thisThread)
					throw new InvalidOperationException("Yield return may only be called by the action thread when it executes.");
			#endif
				
			lock(_executePulser)
			{
				_executing = false;
				Monitor.Pulse(_executePulser);

				_Wait();
			}
		}

		private void _Wait()
		{
			while(true)
			{
				if (_wasDisposed)
				{
					Thread.CurrentThread.Abort(_syncAbortToken);
					// if the stacksaver was disposed, we must kill
					// the actual thread. Calling abort on the actual thread
					// does not cause an asynchronous exception, so it is safe.
				}
				
				if (_executing)
					return;
					
				Monitor.Wait(_executePulser);
			}
		}

		/// <summary>
		/// Executes the method of this stack-saver until it ends or until it calls
		/// a YieldReturn.
		/// Returns true if it YieldReturned, false if the action ended.
		/// If an exception is thrown on the called method, it is rethrown as a 
		/// TargetInvocationException.
		/// </summary>
		public bool Execute()
		{
			BeginExecute();
			return EndExecute();
		}

		/// <summary>
		/// Executes the method of this stack-saver until it ends or until it calls
		/// a YieldReturn.
		/// Returns true if it YieldReturned, false if the action ended.
		/// If an exception is thrown, it is stored in the Exception property.
		/// </summary>
		public bool ExecuteNoException()
		{
			BeginExecute();
			return EndExecuteNoException();
		}

		/// <summary>
		/// Starts the execution of the method of this StackSaver in parallel.
		/// </summary>
		public void BeginExecute()
		{
			lock(_executePulser)
			{
				if (_executing)
					throw new InvalidOperationException("This object is already executing.");
					
				if (_action == null)
					throw new InvalidOperationException("The stacksaver already finished its execution.");
					
				if (_wasDisposed)
					throw new ObjectDisposedException(GetType().FullName);
					
				_executing = true;
				Monitor.Pulse(_executePulser);
			}
		}

		/// <summary>
		/// Waits until the action executed by the BeginExecute finishes.
		/// If the action throws an exception, it is rethrown in this thread as a TargetInvocationException.
		/// </summary>
		/// <returns>true if the action yield returned, false if it ended.</returns>
		public bool EndExecute()
		{
			bool result = EndExecuteNoException();

			var exception = Exception;
			if (exception != null)
				throw new TargetInvocationException(exception);

			return result;
		}

		/// <summary>
		/// Waits until the action executed by the BeginExecute finishes.
		/// If an exception is thrown on the other thread, it is rethrown here as a TargetInvocationException.
		/// </summary>
		/// <returns>true if the action yield returned, false if it ended.</returns>
		public bool EndExecuteNoException()
		{
			lock(_executePulser)
			{
				while(_executing)
					Monitor.Wait(_executePulser);
					
				return _action != null;
			}
		}

		#region IEnumerable<bool> Membres
			IEnumerator<bool> IEnumerable<bool>.GetEnumerator()
			{
				return this;
			}
		#endregion
		#region IEnumerable Membres
			IEnumerator IEnumerable.GetEnumerator()
			{
				return this;
			}
		#endregion

		#region IEnumerator<bool> Membres
			bool IEnumerator<bool>.Current
			{
				get
				{
					return true;
				}
			}
		#endregion
		#region IEnumerator Membres
			internal static readonly object BoxedTrue = true;
			object IEnumerator.Current
			{
				get
				{
					return BoxedTrue;
				}
			}
			bool IEnumerator.MoveNext()
			{
				return Execute();
			}
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Zemek
Engineer Microsoft Corporation
United States United States
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.

Now I just started working as a Senior Software Engineer at Microsoft.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015
Microsoft MVP 2013-2014

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 11 Apr 2012
Article Copyright 2012 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid