2
0

CompressedStack.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return cs;
  74. }
  75. // NOTE: This method doesn't show in the class library status page because
  76. // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
  77. // But it's there!
  78. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  79. [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
  80. static public CompressedStack GetCompressedStack ()
  81. {
  82. // Note: CompressedStack.GetCompressedStack doesn't return null
  83. // like Thread.CurrentThread.GetCompressedStack if no compressed
  84. // stack is present.
  85. CompressedStack cs = Thread.CurrentThread.GetCompressedStack ();
  86. if (cs == null) {
  87. cs = CompressedStack.Capture ();
  88. } else {
  89. // merge the existing compressed stack (from a previous Thread) with the current
  90. // Thread stack so we can assign "all of it" to yet another Thread
  91. CompressedStack newstack = CompressedStack.Capture ();
  92. for (int i=0; i < newstack._list.Count; i++)
  93. cs._list.Add (newstack._list [i]);
  94. }
  95. return cs;
  96. }
  97. #if NET_2_0
  98. [MonoTODO ("incomplete")]
  99. [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
  100. public void GetObjectData (SerializationInfo info, StreamingContext context)
  101. {
  102. if (info == null)
  103. throw new ArgumentNullException ("info");
  104. }
  105. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
  106. static public void Run (CompressedStack compressedStack, ContextCallback callback, object state)
  107. {
  108. if (compressedStack == null)
  109. throw new ArgumentException ("compressedStack");
  110. Thread t = Thread.CurrentThread;
  111. CompressedStack original = null;
  112. try {
  113. original = t.GetCompressedStack ();
  114. t.SetCompressedStack (compressedStack);
  115. callback (state);
  116. }
  117. finally {
  118. if (original != null)
  119. t.SetCompressedStack (original);
  120. }
  121. }
  122. #endif
  123. // internal stuff
  124. internal bool Equals (CompressedStack cs)
  125. {
  126. if (IsEmpty ())
  127. return cs.IsEmpty ();
  128. if (cs.IsEmpty ())
  129. return false;
  130. if (_list.Count != cs._list.Count)
  131. return false;
  132. for (int i=0; i < _list.Count; i++) {
  133. SecurityFrame sf1 = (SecurityFrame) _list [i];
  134. SecurityFrame sf2 = (SecurityFrame) cs._list [i];
  135. if (!sf1.Equals (sf2))
  136. return false;
  137. }
  138. return true;
  139. }
  140. internal bool IsEmpty ()
  141. {
  142. return ((_list == null) || (_list.Count == 0));
  143. }
  144. internal IList List {
  145. get { return _list; }
  146. }
  147. }
  148. }