Open sandboxFocus

Class ActorAttribute

Custom attribute that, when applied to a class, applies the ActorThreadingModel to this class. In this model, calls to methods are processed asynchronously by a single thread, which suppresses concurrency inside each instance of the target class.

Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
Syntax
[ProvideAspectRole("ThreadingModel")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Tracing")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Caching")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "CacheInvalidation")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, "Validation")]
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.Actor")]
[IntroduceInterface(typeof(IActor), AncestorOverrideAction = InterfaceOverrideAction.Ignore, OverrideAction = InterfaceOverrideAction.Ignore)]
[Serializer(typeof(ActorAttribute.Serializer))]
public sealed class ActorAttribute : ThreadAwareAttribute, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, IAspectProvider, IService, IAdviceProvider, IAspect, IQueryInterface, IActor, IDispatcherObject, IThreadAware
Remarks

Calls to public methods of actors are packaged into messages and appended to a message queue. This message queue is processed by a single thread. Therefore, there is never more than one thread accessing the object, which makes the object thread-safe.

Public methods cannot have out or ref parameters. All method invocations are appended to the message queue. If the method has a return value and is not async, the calling thread waits synchronously for the actor to execute the method. Add the ExplicitlySynchronizedAttribute custom attribute to a method to prevent its execution from being dispatched to the actor queue. Note that using ExplicitlySynchronizedAttribute breaks the thread-safety guarantees of the ActorThreadingModel.

Several actors can share the same message queue by sharing the same instance of the IActorDispatcher interface, which the target class can provide by implementing the IDispatcherObject interface.

The message queue is processed in the ThreadPool, not in a thread dedicated to each instance, so an application can have a large number of actors.

The aspect emits a build-time error when the class has public or internal instance fields, or when an async method is not annotated with the ReentrantAttribute custom attribute. When run-time verification is enabled (see RuntimeVerificationEnabled), an access to a field from a context that does not have access to the actor results in a ThreadMismatchException. This typically happens in a private callback method, such as an event handler; add the EntryPointAttribute custom attribute to such methods.

To give an actor a state composed of several objects, declare the child objects with the ChildAttribute custom attribute and annotate their classes with the PrivateThreadAwareAttribute custom attribute.

Constructors

Name Description
ActorAttribute()
ActorAttribute(PortableFormatterConstructorContext)

Methods

Name Description
OnInstanceConstructed()
ProvideAspects(Type)

See Also