Section 8.8. Synchronizing Delegates


8.8. Synchronizing Delegates

As stated in Chapter 6, in .NET when a delegate has no targets, its value is set to null. Consequently, you must always check that the delegate is not null before invoking it. Otherwise, .NET will raise a null reference exception:

     public class MyPublisher     {        public event EventHandler MyEvent;        public void FireEvent(  )        {           if(MyEvent != null)              MyEvent(this,EventArgs.Empty);        }     }

Unfortunately, such a check is insufficient in a multithreaded environment because of the potential for a race condition to develop with regard to accessing the delegate. Because .NET applications execute on top of a preemptive operating system, it is possible that a thread context switch may take place in between comparing the delegate to null and invoking it. If this occurs, the thread that was switched in can remove the invocation targets from the delegate, so that when your thread is switched back in it will access a null delegate and will crash. There are a number of solutions to this race condition. You can ensure that the internal invocation list always has at least one member by initializing it with a do-nothing anonymous method. Because no external party can have a reference to the anonymous method, no external party can remove the method, so the delegate will never be null:

     public class MyPublisher     {        public event EventHandler MyEvent = delegate{};        public void FireEvent(  )        {           MyEvent(this,EventArgs.Empty);        }     }

However, I believe that initializing all delegates this way is impractical. Another option is to add event accessors and lock the object (or the delegate) in add and remove, as well as during invocation. The problem with this approach is that it may force you to maintain the lock for a long period of time, because the event publishing may be a lengthy operation. As long as you maintain the lock, nobody can add or remove subscriptions. Yet another solution is to copy the delegate to a temporary variable, and check and invoke that temporary variable instead of the original delegate:

     public class MyPublisher     {        public event EventHandler MyEvent;        public void FireEvent(  )        {           EventHandler temp = MyEvent;           if(temp != null)              temp(this,EventArgs.Empty);        }     }

Copying the delegate addresses the race condition, because delegates are immutable. Making any change to the state of the delegate (such as removing subscribers) creates a new delegate object on the heap and updates the reference to which the delegate points. By copying the delegate to a temporary variable, you keep a copy of the original state of the delegate, irrespective of any thread context switches. Unfortunately, however, the JIT compiler may optimize the code, eliminate the temporary variable, and use the original delegate directly. That puts you back where you started, susceptible to the race condition.

Instead of using a temporary variable, you could use a method that accepts the delegate as a parameter:

     public class MyPublisher     {        public event EventHandler MyEvent;        public void FireEvent(  )        {           FireEvent(MyEvent);        }        void FireEvent(EventHandler handler)        {           if(handler != null)              handler(this,EventArgs.Empty);        }     }

Passing the delegate as a parameter copies the reference to the invocation list. However, the JIT compiler's optimizations may still subvert your attempt: the JIT compiler is within its rights to inline the use of the helper method and use the delegate directly, leaving you exposed to the race condition yet again. To prevent the JIT compiler from inlining the helper method, you can use the MethodImpl attribute with the MethodImplOptions.NoInlining flag to instruct the JIT compiler not to inline the method under any circumstance:

     public class MyPublisher     {        public event EventHandler MyEvent;        public void FireEvent(  )        {           FireEvent(MyEvent);        }             [MethodImpl(MethodImplOptions.NoInlining)]        void FireEvent(EventHandler handler)        {           if(handler != null)              handler(this,EventArgs.Empty);        }     }

Chapter 6 introduced the EventsHelper static utility class, used to defensively publish events, while Chapter 7 added defensive asynchronous event publishing to it. You can retrofit EventsHelper to address the race condition and deal with the JIT compiler's optimizations, as shown in Example 8-16.

Example 8-16. The thread-safe EventsHelper
 public static class EventsHelper {    delegate void AsyncFire(Delegate del,object[] args);    static void InvokeDelegate(Delegate del,object[] args)    {       del.DynamicInvoke(args);    }     [MethodImpl(MethodImplOptions.NoInlining)]    public static void UnsafeFire(Delegate del,params object[] args)    {       if(del == null)       {          return;       }       Delegate[] delegates = del.GetInvocationList(  );       foreach(Delegate sink in delegates)       {          try          {             InvokeDelegate(sink,args);          }          catch          {}       }    }     [MethodImpl(MethodImplOptions.NoInlining)]    public static void UnsafeFireAsync(Delegate del,params object[] args)    {       if(del == null)       {          return;       }       Delegate[] delegates = del.GetInvocationList(  );       AsyncFire asyncFire = InvokeDelegate;       AsyncCallback cleanUp = delegate(IAsyncResult asyncResult)                               {                                 asyncResult.AsyncWaitHandle.Close();                               };       foreach(Delegate sink in delegates)       {         asyncFire.BeginInvoke(sink,args,cleanUp,null);       }    }    //Rest is same as Example 7-15. }

Note that when using EventsHelper, the race condition is hidden from the publishing componentas before, all the publisher has to do is use EventsHelper to publish the event in a defensive, thread-safe manner:

     public class MyPublisher     {        public event EventHandler MyEvent;        public void FireEvent(  )        {           EventsHelper.Fire(MyEvent,this,EventArgs.Empty);        }     }

The Visual Basic 2005 RaiseEvent statement has been upgraded to include copying of the delegate to a temporary variable. However, due to the JIT optimization just described, this solution is insufficient. In Visual Basic 2005, it is imperative that you use EventsHelper instead.




Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net