2 // System.Runtime.Remoting.Contexts.SynchronizationAttribute.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (C) Novell, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Threading;
31 using System.Runtime.Remoting.Messaging;
32 using System.Runtime.Remoting.Activation;
34 namespace System.Runtime.Remoting.Contexts
36 [AttributeUsage(AttributeTargets.Class)]
38 [System.Runtime.InteropServices.ComVisible (true)]
39 public class SynchronizationAttribute: ContextAttribute, IContributeClientContextSink, IContributeServerContextSink
41 public const int NOT_SUPPORTED = 1;
42 public const int SUPPORTED = 2;
43 public const int REQUIRED = 4;
44 public const int REQUIRES_NEW = 8;
53 Mutex _mutex = new Mutex (false);
57 public SynchronizationAttribute ()
58 : this (REQUIRES_NEW, false)
62 public SynchronizationAttribute (bool reEntrant)
63 : this (REQUIRES_NEW, reEntrant)
67 public SynchronizationAttribute (int flag)
72 public SynchronizationAttribute (int flag, bool reEntrant)
73 : base ("Synchronization")
75 if (flag != NOT_SUPPORTED && flag != REQUIRED && flag != REQUIRES_NEW && flag != SUPPORTED)
76 throw new ArgumentException ("flag");
78 _bReEntrant = reEntrant;
82 public virtual bool IsReEntrant
84 get { return _bReEntrant; }
87 public virtual bool Locked
91 return _lockCount > 0;
102 ReleaseLock (); // Thread already had the lock
109 while (_lockCount > 0 && _ownerThread == Thread.CurrentThread) {
117 internal void AcquireLock ()
123 _ownerThread = Thread.CurrentThread;
128 internal void ReleaseLock ()
132 if (_lockCount > 0 && _ownerThread == Thread.CurrentThread) {
134 _mutex.ReleaseMutex ();
135 if (_lockCount == 0) {
142 [System.Runtime.InteropServices.ComVisible (true)]
143 public override void GetPropertiesForNewContext (IConstructionCallMessage ctorMsg)
145 if (_flavor != NOT_SUPPORTED) {
146 ctorMsg.ContextProperties.Add (this);
150 public virtual IMessageSink GetClientContextSink (IMessageSink nextSink)
152 return new SynchronizedClientContextSink (nextSink, this);
155 public virtual IMessageSink GetServerContextSink (IMessageSink nextSink)
157 return new SynchronizedServerContextSink (nextSink, this);
160 [System.Runtime.InteropServices.ComVisible (true)]
161 public override bool IsContextOK (Context ctx, IConstructionCallMessage msg)
163 SynchronizationAttribute prop = ctx.GetProperty ("Synchronization") as SynchronizationAttribute;
166 case NOT_SUPPORTED: return (prop == null);
167 case REQUIRED: return (prop != null);
168 case REQUIRES_NEW: return false;
169 case SUPPORTED: return true;
174 internal static void ExitContext ()
176 if (Thread.CurrentContext.IsDefaultContext) return;
177 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
178 if (prop == null) return;
182 internal static void EnterContext ()
184 if (Thread.CurrentContext.IsDefaultContext) return;
185 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
186 if (prop == null) return;
191 internal class SynchronizedClientContextSink: IMessageSink
194 SynchronizationAttribute _att;
196 public SynchronizedClientContextSink (IMessageSink next, SynchronizationAttribute att)
202 public IMessageSink NextSink
204 get { return _next; }
207 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
209 if (_att.IsReEntrant)
211 _att.ReleaseLock(); // Unlock when leaving the context
212 replySink = new SynchronizedContextReplySink (replySink, _att, true);
214 return _next.AsyncProcessMessage (msg, replySink);
217 public IMessage SyncProcessMessage (IMessage msg)
219 if (_att.IsReEntrant)
220 _att.ReleaseLock (); // Unlock when leaving the context
224 return _next.SyncProcessMessage (msg);
228 if (_att.IsReEntrant)
234 internal class SynchronizedServerContextSink: IMessageSink
237 SynchronizationAttribute _att;
239 public SynchronizedServerContextSink (IMessageSink next, SynchronizationAttribute att)
245 public IMessageSink NextSink
247 get { return _next; }
250 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
253 replySink = new SynchronizedContextReplySink (replySink, _att, false);
254 return _next.AsyncProcessMessage (msg, replySink);
257 public IMessage SyncProcessMessage (IMessage msg)
262 return _next.SyncProcessMessage (msg);
271 internal class SynchronizedContextReplySink: IMessageSink
275 SynchronizationAttribute _att;
277 public SynchronizedContextReplySink (IMessageSink next, SynchronizationAttribute att, bool newLock)
284 public IMessageSink NextSink
286 get { return _next; }
289 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
292 throw new NotSupportedException ();
295 public IMessage SyncProcessMessage (IMessage msg)
297 if (_newLock) _att.AcquireLock ();
298 else _att.ReleaseLock ();
302 return _next.SyncProcessMessage (msg);