Files
Dec2017-Script-Dump/mscorlib/System/Runtime/Remoting/Contexts/SynchronizationAttribute.cs
T
2026-06-04 11:42:34 +02:00

275 lines
12 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace System.Runtime.Remoting.Contexts
{
/// <summary>Enforces a synchronization domain for the current context and all contexts that share the same instance.</summary>
// Token: 0x020003CA RID: 970
[AttributeUsage(AttributeTargets.Class)]
[ComVisible(true)]
[Serializable]
public class SynchronizationAttribute : ContextAttribute, IContributeClientContextSink, IContributeServerContextSink
{
/// <summary>Initializes a new instance of the <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> class with default values.</summary>
// Token: 0x06002637 RID: 9783 RVA: 0x0007E06C File Offset: 0x0007C26C
public SynchronizationAttribute()
: this(8, false)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> class with a Boolean value indicating whether reentry is required.</summary>
/// <param name="reEntrant">A Boolean value indicating whether reentry is required. </param>
// Token: 0x06002638 RID: 9784 RVA: 0x0007E078 File Offset: 0x0007C278
public SynchronizationAttribute(bool reEntrant)
: this(8, reEntrant)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> class with a flag indicating the behavior of the object to which this attribute is applied.</summary>
/// <param name="flag">An integer value indicating the behavior of the object to which this attribute is applied. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="flag" /> parameter was not one of the defined flags. </exception>
// Token: 0x06002639 RID: 9785 RVA: 0x0007E084 File Offset: 0x0007C284
public SynchronizationAttribute(int flag)
: this(flag, false)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> class with a flag indicating the behavior of the object to which this attribute is applied, and a Boolean value indicating whether reentry is required.</summary>
/// <param name="flag">An integer value indicating the behavior of the object to which this attribute is applied. </param>
/// <param name="reEntrant">true if reentry is required, and callouts must be intercepted and serialized; otherwise, false. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="flag" /> parameter was not one of the defined flags. </exception>
// Token: 0x0600263A RID: 9786 RVA: 0x0007E090 File Offset: 0x0007C290
public SynchronizationAttribute(int flag, bool reEntrant)
: base("Synchronization")
{
if (flag != 1 && flag != 4 && flag != 8 && flag != 2)
{
throw new ArgumentException("flag");
}
this._bReEntrant = reEntrant;
this._flavor = flag;
}
/// <summary>Gets or sets a Boolean value indicating whether reentry is required.</summary>
/// <returns>A Boolean value indicating whether reentry is required.</returns>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x17000725 RID: 1829
// (get) Token: 0x0600263B RID: 9787 RVA: 0x0007E0EC File Offset: 0x0007C2EC
public virtual bool IsReEntrant
{
get
{
return this._bReEntrant;
}
}
/// <summary>Gets or sets a Boolean value indicating whether the <see cref="T:System.Runtime.Remoting.Contexts.Context" /> implementing this instance of <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> is locked.</summary>
/// <returns>A Boolean value indicating whether the <see cref="T:System.Runtime.Remoting.Contexts.Context" /> implementing this instance of <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> is locked.</returns>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x17000726 RID: 1830
// (get) Token: 0x0600263C RID: 9788 RVA: 0x0007E0F4 File Offset: 0x0007C2F4
// (set) Token: 0x0600263D RID: 9789 RVA: 0x0007E0FC File Offset: 0x0007C2FC
public virtual bool Locked
{
get
{
return this._locked;
}
set
{
if (value)
{
this._mutex.WaitOne();
lock (this)
{
this._lockCount++;
if (this._lockCount > 1)
{
this.ReleaseLock();
}
this._ownerThread = Thread.CurrentThread;
}
}
else
{
lock (this)
{
while (this._lockCount > 0 && this._ownerThread == Thread.CurrentThread)
{
this._lockCount--;
this._mutex.ReleaseMutex();
this._ownerThread = null;
}
}
}
}
}
// Token: 0x0600263E RID: 9790 RVA: 0x0007E1E8 File Offset: 0x0007C3E8
internal void AcquireLock()
{
this._mutex.WaitOne();
lock (this)
{
this._ownerThread = Thread.CurrentThread;
this._lockCount++;
}
}
// Token: 0x0600263F RID: 9791 RVA: 0x0007E24C File Offset: 0x0007C44C
internal void ReleaseLock()
{
lock (this)
{
if (this._lockCount > 0 && this._ownerThread == Thread.CurrentThread)
{
this._lockCount--;
this._mutex.ReleaseMutex();
this._ownerThread = null;
}
}
}
/// <summary>Adds the Synchronized context property to the specified <see cref="T:System.Runtime.Remoting.Activation.IConstructionCallMessage" />.</summary>
/// <param name="ctorMsg">The <see cref="T:System.Runtime.Remoting.Activation.IConstructionCallMessage" /> to which to add the property. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x06002640 RID: 9792 RVA: 0x0007E2C8 File Offset: 0x0007C4C8
[ComVisible(true)]
public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
if (this._flavor != 1)
{
ctorMsg.ContextProperties.Add(this);
}
}
/// <summary>Creates a CallOut sink and chains it in front of the provided chain of sinks at the context boundary on the client end of a remoting call.</summary>
/// <returns>The composite sink chain with the new CallOut sink.</returns>
/// <param name="nextSink">The chain of sinks composed so far. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x06002641 RID: 9793 RVA: 0x0007E2E4 File Offset: 0x0007C4E4
public virtual IMessageSink GetClientContextSink(IMessageSink nextSink)
{
return new SynchronizedClientContextSink(nextSink, this);
}
/// <summary>Creates a synchronized dispatch sink and chains it in front of the provided chain of sinks at the context boundary on the server end of a remoting call.</summary>
/// <returns>The composite sink chain with the new synchronized dispatch sink.</returns>
/// <param name="nextSink">The chain of sinks composed so far. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x06002642 RID: 9794 RVA: 0x0007E2F0 File Offset: 0x0007C4F0
public virtual IMessageSink GetServerContextSink(IMessageSink nextSink)
{
return new SynchronizedServerContextSink(nextSink, this);
}
/// <summary>Returns a Boolean value indicating whether the context parameter meets the context attribute's requirements.</summary>
/// <returns>true if the passed in context is OK; otherwise, false.</returns>
/// <param name="ctx">The context to check. </param>
/// <param name="msg">Information gathered at construction time of the context bound object marked by this attribute. The <see cref="T:System.Runtime.Remoting.Contexts.SynchronizationAttribute" /> can inspect, add to, and remove properties from the context while determining if the context is acceptable to it. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="ctx" /> or <paramref name="msg" /> parameter is null. </exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
/// </PermissionSet>
// Token: 0x06002643 RID: 9795 RVA: 0x0007E2FC File Offset: 0x0007C4FC
[ComVisible(true)]
public override bool IsContextOK(Context ctx, IConstructionCallMessage msg)
{
SynchronizationAttribute synchronizationAttribute = ctx.GetProperty("Synchronization") as SynchronizationAttribute;
switch (this._flavor)
{
case 1:
return synchronizationAttribute == null;
case 2:
return true;
case 4:
return synchronizationAttribute != null;
case 8:
return false;
}
return false;
}
// Token: 0x06002644 RID: 9796 RVA: 0x0007E360 File Offset: 0x0007C560
internal static void ExitContext()
{
if (Thread.CurrentContext.IsDefaultContext)
{
return;
}
SynchronizationAttribute synchronizationAttribute = Thread.CurrentContext.GetProperty("Synchronization") as SynchronizationAttribute;
if (synchronizationAttribute == null)
{
return;
}
synchronizationAttribute.Locked = false;
}
// Token: 0x06002645 RID: 9797 RVA: 0x0007E3A0 File Offset: 0x0007C5A0
internal static void EnterContext()
{
if (Thread.CurrentContext.IsDefaultContext)
{
return;
}
SynchronizationAttribute synchronizationAttribute = Thread.CurrentContext.GetProperty("Synchronization") as SynchronizationAttribute;
if (synchronizationAttribute == null)
{
return;
}
synchronizationAttribute.Locked = true;
}
/// <summary>Indicates that the class to which this attribute is applied cannot be created in a context that has synchronization. This field is constant.</summary>
// Token: 0x04000EE2 RID: 3810
public const int NOT_SUPPORTED = 1;
/// <summary>Indicates that the class to which this attribute is applied is not dependent on whether the context has synchronization. This field is constant.</summary>
// Token: 0x04000EE3 RID: 3811
public const int SUPPORTED = 2;
/// <summary>Indicates that the class to which this attribute is applied must be created in a context that has synchronization. This field is constant.</summary>
// Token: 0x04000EE4 RID: 3812
public const int REQUIRED = 4;
/// <summary>Indicates that the class to which this attribute is applied must be created in a context with a new instance of the synchronization property each time. This field is constant.</summary>
// Token: 0x04000EE5 RID: 3813
public const int REQUIRES_NEW = 8;
// Token: 0x04000EE6 RID: 3814
private bool _bReEntrant;
// Token: 0x04000EE7 RID: 3815
private int _flavor;
// Token: 0x04000EE8 RID: 3816
[NonSerialized]
private bool _locked;
// Token: 0x04000EE9 RID: 3817
[NonSerialized]
private int _lockCount;
// Token: 0x04000EEA RID: 3818
[NonSerialized]
private Mutex _mutex = new Mutex(false);
// Token: 0x04000EEB RID: 3819
[NonSerialized]
private Thread _ownerThread;
}
}