2 // System.Runtime.Remoting.Contexts.SynchronizationAttribute.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (C) Novell, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Remoting.Activation;
37 namespace System.Runtime.Remoting.Contexts
39 [AttributeUsage(AttributeTargets.Class)]
41 [MonoTODO ("Fix serialization compatibility with MS.NET")]
42 public class SynchronizationAttribute: ContextAttribute, IContributeClientContextSink, IContributeServerContextSink
44 public const int NOT_SUPPORTED = 1;
45 public const int SUPPORTED = 2;
46 public const int REQUIRED = 4;
47 public const int REQUIRES_NEW = 8;
54 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 _isReentrant = reEntrant;
82 public virtual bool IsReEntrant
84 get { return _isReentrant; }
87 public virtual bool Locked
103 ReleaseLock (); // Thread already had the lock
105 _ownerThread = Thread.CurrentThread;
112 while (_lockCount > 0 && _ownerThread == Thread.CurrentThread)
115 _mutex.ReleaseMutex ();
123 internal void AcquireLock ()
129 _ownerThread = Thread.CurrentThread;
134 internal void ReleaseLock ()
138 if (_lockCount > 0 && _ownerThread == Thread.CurrentThread) {
140 _mutex.ReleaseMutex ();
146 public override void GetPropertiesForNewContext (IConstructionCallMessage ctorMsg)
148 if (_flag != NOT_SUPPORTED) {
149 ctorMsg.ContextProperties.Add (this);
153 public virtual IMessageSink GetClientContextSink (IMessageSink nextSink)
155 return new SynchronizedClientContextSink (nextSink, this);
158 public virtual IMessageSink GetServerContextSink (IMessageSink nextSink)
160 return new SynchronizedServerContextSink (nextSink, this);
163 public override bool IsContextOK (Context ctx, IConstructionCallMessage msg)
165 SynchronizationAttribute prop = ctx.GetProperty ("Synchronization") as SynchronizationAttribute;
168 case NOT_SUPPORTED: return (prop == null);
169 case REQUIRED: return (prop != null);
170 case REQUIRES_NEW: return false;
171 case SUPPORTED: return true;
176 internal static void ExitContext ()
178 if (Thread.CurrentContext.IsDefaultContext) return;
179 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
180 if (prop == null) return;
184 internal static void EnterContext ()
186 if (Thread.CurrentContext.IsDefaultContext) return;
187 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
188 if (prop == null) return;
193 internal class SynchronizedClientContextSink: IMessageSink
196 SynchronizationAttribute _att;
198 public SynchronizedClientContextSink (IMessageSink next, SynchronizationAttribute att)
204 public IMessageSink NextSink
206 get { return _next; }
209 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
211 if (_att.IsReEntrant)
213 _att.ReleaseLock(); // Unlock when leaving the context
214 replySink = new SynchronizedContextReplySink (replySink, _att, true);
216 return _next.AsyncProcessMessage (msg, replySink);
219 public IMessage SyncProcessMessage (IMessage msg)
221 if (_att.IsReEntrant)
222 _att.ReleaseLock (); // Unlock when leaving the context
226 return _next.SyncProcessMessage (msg);
230 if (_att.IsReEntrant)
236 internal class SynchronizedServerContextSink: IMessageSink
239 SynchronizationAttribute _att;
241 public SynchronizedServerContextSink (IMessageSink next, SynchronizationAttribute att)
247 public IMessageSink NextSink
249 get { return _next; }
252 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
255 replySink = new SynchronizedContextReplySink (replySink, _att, false);
256 return _next.AsyncProcessMessage (msg, replySink);
259 public IMessage SyncProcessMessage (IMessage msg)
264 return _next.SyncProcessMessage (msg);
273 internal class SynchronizedContextReplySink: IMessageSink
277 SynchronizationAttribute _att;
279 public SynchronizedContextReplySink (IMessageSink next, SynchronizationAttribute att, bool newLock)
286 public IMessageSink NextSink
288 get { return _next; }
291 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
294 throw new NotSupportedException ();
297 public IMessage SyncProcessMessage (IMessage msg)
299 if (_newLock) _att.AcquireLock ();
300 else _att.ReleaseLock ();
304 return _next.SyncProcessMessage (msg);