Created
December 13, 2011 11:34
-
-
Save gfraiteur/1471809 to your computer and use it in GitHub Desktop.
A debugging aspect that throws an exception is an object is used by several threads simultaneously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Threading; | |
using PostSharp.Aspects; | |
using PostSharp.Aspects.Advices; | |
using PostSharp.Aspects.Configuration; | |
using PostSharp.Aspects.Serialization; | |
using PostSharp.Extensibility; | |
namespace Resonance.Messenging.Threading | |
{ | |
[Conditional("DEBUG")] | |
[MulticastAttributeUsage(MulticastTargets.Class, TargetMemberAttributes = MulticastAttributes.Instance | MulticastAttributes.Public, | |
Inheritance = MulticastInheritance.Strict)] | |
[AspectConfiguration(SerializerType = typeof(MsilAspectSerializer))] | |
internal sealed class SinglethreadedAttribute : InstanceLevelAspect | |
{ | |
[OnMethodEntryAdvice, MulticastPointcut(Attributes = MulticastAttributes.Instance | MulticastAttributes.Public)] | |
public void OnEntry(MethodExecutionArgs args) | |
{ | |
if (!Monitor.TryEnter(this)) throw new ThreadAffinityException(); | |
} | |
[OnMethodExitAdvice(Master = "OnEntry")] | |
public void OnExit(MethodExecutionArgs args) | |
{ | |
Monitor.Exit(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment