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;
55 Mutex _mutex = new Mutex (false);
59 public SynchronizationAttribute ()
60 : this (REQUIRES_NEW, false)
64 public SynchronizationAttribute (bool reEntrant)
65 : this (REQUIRES_NEW, reEntrant)
69 public SynchronizationAttribute (int flag)
74 public SynchronizationAttribute (int flag, bool reEntrant)
75 : base ("Synchronization")
77 if (flag != NOT_SUPPORTED && flag != REQUIRED && flag != REQUIRES_NEW && flag != SUPPORTED)
78 throw new ArgumentException ("flag");
80 _bReEntrant = reEntrant;
84 public virtual bool IsReEntrant
86 get { return _bReEntrant; }
89 public virtual bool Locked
105 ReleaseLock (); // Thread already had the lock
107 _ownerThread = Thread.CurrentThread;
114 while (_lockCount > 0 && _ownerThread == Thread.CurrentThread)
117 _mutex.ReleaseMutex ();
125 internal void AcquireLock ()
131 _ownerThread = Thread.CurrentThread;
136 internal void ReleaseLock ()
140 if (_lockCount > 0 && _ownerThread == Thread.CurrentThread) {
142 _mutex.ReleaseMutex ();
148 [System.Runtime.InteropServices.ComVisible (true)]
149 public override void GetPropertiesForNewContext (IConstructionCallMessage ctorMsg)
151 if (_flavor != NOT_SUPPORTED) {
152 ctorMsg.ContextProperties.Add (this);
156 public virtual IMessageSink GetClientContextSink (IMessageSink nextSink)
158 return new SynchronizedClientContextSink (nextSink, this);
161 public virtual IMessageSink GetServerContextSink (IMessageSink nextSink)
163 return new SynchronizedServerContextSink (nextSink, this);
166 [System.Runtime.InteropServices.ComVisible (true)]
167 public override bool IsContextOK (Context ctx, IConstructionCallMessage msg)
169 SynchronizationAttribute prop = ctx.GetProperty ("Synchronization") as SynchronizationAttribute;
172 case NOT_SUPPORTED: return (prop == null);
173 case REQUIRED: return (prop != null);
174 case REQUIRES_NEW: return false;
175 case SUPPORTED: return true;
180 internal static void ExitContext ()
182 if (Thread.CurrentContext.IsDefaultContext) return;
183 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
184 if (prop == null) return;
188 internal static void EnterContext ()
190 if (Thread.CurrentContext.IsDefaultContext) return;
191 SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
192 if (prop == null) return;
197 internal class SynchronizedClientContextSink: IMessageSink
200 SynchronizationAttribute _att;
202 public SynchronizedClientContextSink (IMessageSink next, SynchronizationAttribute att)
208 public IMessageSink NextSink
210 get { return _next; }
213 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
215 if (_att.IsReEntrant)
217 _att.ReleaseLock(); // Unlock when leaving the context
218 replySink = new SynchronizedContextReplySink (replySink, _att, true);
220 return _next.AsyncProcessMessage (msg, replySink);
223 public IMessage SyncProcessMessage (IMessage msg)
225 if (_att.IsReEntrant)
226 _att.ReleaseLock (); // Unlock when leaving the context
230 return _next.SyncProcessMessage (msg);
234 if (_att.IsReEntrant)
240 internal class SynchronizedServerContextSink: IMessageSink
243 SynchronizationAttribute _att;
245 public SynchronizedServerContextSink (IMessageSink next, SynchronizationAttribute att)
251 public IMessageSink NextSink
253 get { return _next; }
256 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
259 replySink = new SynchronizedContextReplySink (replySink, _att, false);
260 return _next.AsyncProcessMessage (msg, replySink);
263 public IMessage SyncProcessMessage (IMessage msg)
268 return _next.SyncProcessMessage (msg);
277 internal class SynchronizedContextReplySink: IMessageSink
281 SynchronizationAttribute _att;
283 public SynchronizedContextReplySink (IMessageSink next, SynchronizationAttribute att, bool newLock)
290 public IMessageSink NextSink
292 get { return _next; }
295 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
298 throw new NotSupportedException ();
301 public IMessage SyncProcessMessage (IMessage msg)
303 if (_newLock) _att.AcquireLock ();
304 else _att.ReleaseLock ();
308 return _next.SyncProcessMessage (msg);