Open sandboxFocus

Class ThreadSafetyPolicy

When applied to an assembly, this policy will check for possible concurrency issues.

Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
Syntax
[MulticastAttributeUsage(MulticastTargets.Assembly)]
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class ThreadSafetyPolicy : ScalarConstraint, IScalarConstraint, IConstraint
Remarks

This policy recognizes the following concurrency issues:

  • Type is not thread-safe.
  • Static field is not read-only.
  • Type of static field is not thread-safe.

A type is considered thread-safe if it has no instance fields, if it has a threading model (an aspect derived from ThreadAwareAttribute), if it is annotated with ExplicitlySynchronizedAttribute, or if the type analyzer of the threading library recognizes it as thread-safe. Value types, custom attribute classes and compiler-generated types are not checked. Static fields annotated with ExplicitlySynchronizedAttribute or ThreadStaticAttribute are not checked.

A type declared in a referenced assembly has a threading model only if PostSharp processed that assembly. The policy does not see ExplicitlySynchronizedAttribute on a type of a referenced assembly, because this attribute is not kept in the compiled assembly. Annotate the static field with ExplicitlySynchronizedAttribute instead.

The policy only emits warnings: it reminds you to assign a threading model to each class, but does not make the code thread-safe by itself. To apply the policy to all projects of a solution, add it as a multicast attribute to the pssln file.

Constructors

Name Description
ThreadSafetyPolicy()

Methods

Name Description
ValidateCode(object)

Validates the element of code to which the constraint is applied.

See Also