SynchronizationAttribute.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // System.Runtime.Remoting.Contexts.SynchronizationAttribute.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) Novell, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Threading;
  32. using System.Runtime.Remoting.Messaging;
  33. using System.Runtime.Remoting.Activation;
  34. namespace System.Runtime.Remoting.Contexts
  35. {
  36. [AttributeUsage(AttributeTargets.Class)]
  37. [Serializable]
  38. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  39. public class SynchronizationAttribute: ContextAttribute, IContributeClientContextSink, IContributeServerContextSink
  40. {
  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;
  45. bool _isReentrant;
  46. bool _locked;
  47. int _flag;
  48. int _lockCount = 0;
  49. [NonSerialized]
  50. Mutex _mutex = new Mutex (false);
  51. [NonSerialized]
  52. Thread _ownerThread;
  53. public SynchronizationAttribute ()
  54. : this (REQUIRES_NEW, false)
  55. {
  56. }
  57. public SynchronizationAttribute (bool reEntrant)
  58. : this (REQUIRES_NEW, reEntrant)
  59. {
  60. }
  61. public SynchronizationAttribute (int flag)
  62. : this (flag, false)
  63. {
  64. }
  65. public SynchronizationAttribute (int flag, bool reEntrant)
  66. : base ("Synchronization")
  67. {
  68. if (flag != NOT_SUPPORTED && flag != REQUIRED && flag != REQUIRES_NEW && flag != SUPPORTED)
  69. throw new ArgumentException ("flag");
  70. _isReentrant = reEntrant;
  71. _flag = flag;
  72. }
  73. public virtual bool IsReEntrant
  74. {
  75. get { return _isReentrant; }
  76. }
  77. public virtual bool Locked
  78. {
  79. get
  80. {
  81. return _locked;
  82. }
  83. set
  84. {
  85. if (value)
  86. {
  87. _mutex.WaitOne ();
  88. lock (this)
  89. {
  90. _lockCount++;
  91. if (_lockCount > 1)
  92. ReleaseLock (); // Thread already had the lock
  93. _ownerThread = Thread.CurrentThread;
  94. }
  95. }
  96. else
  97. {
  98. lock (this)
  99. {
  100. while (_lockCount > 0 && _ownerThread == Thread.CurrentThread)
  101. {
  102. _lockCount--;
  103. _mutex.ReleaseMutex ();
  104. _ownerThread = null;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. internal void AcquireLock ()
  111. {
  112. _mutex.WaitOne ();
  113. lock (this)
  114. {
  115. _ownerThread = Thread.CurrentThread;
  116. _lockCount++;
  117. }
  118. }
  119. internal void ReleaseLock ()
  120. {
  121. lock (this)
  122. {
  123. if (_lockCount > 0 && _ownerThread == Thread.CurrentThread) {
  124. _lockCount--;
  125. _mutex.ReleaseMutex ();
  126. _ownerThread = null;
  127. }
  128. }
  129. }
  130. public override void GetPropertiesForNewContext (IConstructionCallMessage ctorMsg)
  131. {
  132. if (_flag != NOT_SUPPORTED) {
  133. ctorMsg.ContextProperties.Add (this);
  134. }
  135. }
  136. public virtual IMessageSink GetClientContextSink (IMessageSink nextSink)
  137. {
  138. return new SynchronizedClientContextSink (nextSink, this);
  139. }
  140. public virtual IMessageSink GetServerContextSink (IMessageSink nextSink)
  141. {
  142. return new SynchronizedServerContextSink (nextSink, this);
  143. }
  144. public override bool IsContextOK (Context ctx, IConstructionCallMessage msg)
  145. {
  146. SynchronizationAttribute prop = ctx.GetProperty ("Synchronization") as SynchronizationAttribute;
  147. switch (_flag)
  148. {
  149. case NOT_SUPPORTED: return (prop == null);
  150. case REQUIRED: return (prop != null);
  151. case REQUIRES_NEW: return false;
  152. case SUPPORTED: return true;
  153. }
  154. return false;
  155. }
  156. internal static void ExitContext ()
  157. {
  158. if (Thread.CurrentContext.IsDefaultContext) return;
  159. SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
  160. if (prop == null) return;
  161. prop.Locked = false;
  162. }
  163. internal static void EnterContext ()
  164. {
  165. if (Thread.CurrentContext.IsDefaultContext) return;
  166. SynchronizationAttribute prop = Thread.CurrentContext.GetProperty ("Synchronization") as SynchronizationAttribute;
  167. if (prop == null) return;
  168. prop.Locked = true;
  169. }
  170. }
  171. internal class SynchronizedClientContextSink: IMessageSink
  172. {
  173. IMessageSink _next;
  174. SynchronizationAttribute _att;
  175. public SynchronizedClientContextSink (IMessageSink next, SynchronizationAttribute att)
  176. {
  177. _att = att;
  178. _next = next;
  179. }
  180. public IMessageSink NextSink
  181. {
  182. get { return _next; }
  183. }
  184. public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
  185. {
  186. if (_att.IsReEntrant)
  187. {
  188. _att.ReleaseLock(); // Unlock when leaving the context
  189. replySink = new SynchronizedContextReplySink (replySink, _att, true);
  190. }
  191. return _next.AsyncProcessMessage (msg, replySink);
  192. }
  193. public IMessage SyncProcessMessage (IMessage msg)
  194. {
  195. if (_att.IsReEntrant)
  196. _att.ReleaseLock (); // Unlock when leaving the context
  197. try
  198. {
  199. return _next.SyncProcessMessage (msg);
  200. }
  201. finally
  202. {
  203. if (_att.IsReEntrant)
  204. _att.AcquireLock ();
  205. }
  206. }
  207. }
  208. internal class SynchronizedServerContextSink: IMessageSink
  209. {
  210. IMessageSink _next;
  211. SynchronizationAttribute _att;
  212. public SynchronizedServerContextSink (IMessageSink next, SynchronizationAttribute att)
  213. {
  214. _att = att;
  215. _next = next;
  216. }
  217. public IMessageSink NextSink
  218. {
  219. get { return _next; }
  220. }
  221. public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
  222. {
  223. _att.AcquireLock ();
  224. replySink = new SynchronizedContextReplySink (replySink, _att, false);
  225. return _next.AsyncProcessMessage (msg, replySink);
  226. }
  227. public IMessage SyncProcessMessage (IMessage msg)
  228. {
  229. _att.AcquireLock ();
  230. try
  231. {
  232. return _next.SyncProcessMessage (msg);
  233. }
  234. finally
  235. {
  236. _att.ReleaseLock ();
  237. }
  238. }
  239. }
  240. internal class SynchronizedContextReplySink: IMessageSink
  241. {
  242. IMessageSink _next;
  243. bool _newLock;
  244. SynchronizationAttribute _att;
  245. public SynchronizedContextReplySink (IMessageSink next, SynchronizationAttribute att, bool newLock)
  246. {
  247. _newLock = newLock;
  248. _next = next;
  249. _att = att;
  250. }
  251. public IMessageSink NextSink
  252. {
  253. get { return _next; }
  254. }
  255. public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
  256. {
  257. // Never called
  258. throw new NotSupportedException ();
  259. }
  260. public IMessage SyncProcessMessage (IMessage msg)
  261. {
  262. if (_newLock) _att.AcquireLock ();
  263. else _att.ReleaseLock ();
  264. try
  265. {
  266. return _next.SyncProcessMessage (msg);
  267. }
  268. finally
  269. {
  270. if (_newLock)
  271. _att.ReleaseLock ();
  272. }
  273. }
  274. }
  275. }