CompressedStack.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.Threading.Thread.cs
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System.Collections;
  30. using System.Diagnostics;
  31. using System.Reflection;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.Serialization;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. namespace System.Threading {
  37. #if NET_2_0
  38. [Serializable]
  39. public sealed class CompressedStack : ISerializable {
  40. #else
  41. public class CompressedStack {
  42. #endif
  43. private ArrayList _list;
  44. internal CompressedStack (int length)
  45. {
  46. if (length > 0)
  47. _list = new ArrayList (length);
  48. }
  49. internal CompressedStack (CompressedStack cs)
  50. {
  51. if ((cs != null) && (cs._list != null))
  52. _list = (ArrayList) cs._list.Clone ();
  53. }
  54. #if NET_2_0
  55. [ComVisibleAttribute (false)]
  56. public
  57. #else
  58. internal
  59. #endif
  60. CompressedStack CreateCopy ()
  61. {
  62. return new CompressedStack (this);
  63. }
  64. #if NET_2_0
  65. public
  66. #else
  67. internal
  68. #endif
  69. static CompressedStack Capture ()
  70. {
  71. CompressedStack cs = new CompressedStack (0);
  72. cs._list = SecurityFrame.GetStack (1);
  73. // include any current CompressedStack inside the new Capture
  74. CompressedStack currentCs = Thread.CurrentThread.GetCompressedStack ();
  75. if (currentCs != null) {
  76. for (int i=0; i < currentCs._list.Count; i++)
  77. cs._list.Add (currentCs._list [i]);
  78. }
  79. return cs;
  80. }
  81. // NOTE: This method doesn't show in the class library status page because
  82. // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
  83. // But it's there!
  84. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  85. [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
  86. static public CompressedStack GetCompressedStack ()
  87. {
  88. // Note: CompressedStack.GetCompressedStack doesn't return null
  89. // like Thread.CurrentThread.GetCompressedStack if no compressed
  90. // stack is present.
  91. CompressedStack cs = Thread.CurrentThread.GetCompressedStack ();
  92. if (cs == null) {
  93. cs = CompressedStack.Capture ();
  94. } else {
  95. // merge the existing compressed stack (from a previous Thread) with the current
  96. // Thread stack so we can assign "all of it" to yet another Thread
  97. CompressedStack newstack = CompressedStack.Capture ();
  98. for (int i=0; i < newstack._list.Count; i++)
  99. cs._list.Add (newstack._list [i]);
  100. }
  101. return cs;
  102. }
  103. #if NET_2_0
  104. [MonoTODO ("incomplete")]
  105. [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
  106. public void GetObjectData (SerializationInfo info, StreamingContext context)
  107. {
  108. if (info == null)
  109. throw new ArgumentNullException ("info");
  110. }
  111. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
  112. static public void Run (CompressedStack compressedStack, ContextCallback callback, object state)
  113. {
  114. if (compressedStack == null)
  115. throw new ArgumentException ("compressedStack");
  116. Thread t = Thread.CurrentThread;
  117. CompressedStack original = null;
  118. try {
  119. original = t.GetCompressedStack ();
  120. t.SetCompressedStack (compressedStack);
  121. callback (state);
  122. }
  123. finally {
  124. if (original != null)
  125. t.SetCompressedStack (original);
  126. }
  127. }
  128. #endif
  129. // internal stuff
  130. internal bool Equals (CompressedStack cs)
  131. {
  132. if (IsEmpty ())
  133. return cs.IsEmpty ();
  134. if (cs.IsEmpty ())
  135. return false;
  136. if (_list.Count != cs._list.Count)
  137. return false;
  138. for (int i=0; i < _list.Count; i++) {
  139. SecurityFrame sf1 = (SecurityFrame) _list [i];
  140. SecurityFrame sf2 = (SecurityFrame) cs._list [i];
  141. if (!sf1.Equals (sf2))
  142. return false;
  143. }
  144. return true;
  145. }
  146. internal bool IsEmpty ()
  147. {
  148. return ((_list == null) || (_list.Count == 0));
  149. }
  150. internal IList List {
  151. get { return _list; }
  152. }
  153. }
  154. }