CompressedStack.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. [Serializable]
  38. public sealed class CompressedStack : ISerializable {
  39. private ArrayList _list;
  40. internal CompressedStack (int length)
  41. {
  42. if (length > 0)
  43. _list = new ArrayList (length);
  44. }
  45. internal CompressedStack (CompressedStack cs)
  46. {
  47. if ((cs != null) && (cs._list != null))
  48. _list = (ArrayList) cs._list.Clone ();
  49. }
  50. [ComVisibleAttribute (false)]
  51. public CompressedStack CreateCopy ()
  52. {
  53. return new CompressedStack (this);
  54. }
  55. public static CompressedStack Capture ()
  56. {
  57. CompressedStack cs = new CompressedStack (0);
  58. cs._list = SecurityFrame.GetStack (1);
  59. // include any current CompressedStack inside the new Capture
  60. CompressedStack currentCs = Thread.CurrentThread.ExecutionContext.SecurityContext.CompressedStack;
  61. if (currentCs != null) {
  62. for (int i=0; i < currentCs._list.Count; i++)
  63. cs._list.Add (currentCs._list [i]);
  64. }
  65. return cs;
  66. }
  67. // NOTE: This method doesn't show in the class library status page because
  68. // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
  69. // But it's there!
  70. [SecurityCritical]
  71. static public CompressedStack GetCompressedStack ()
  72. {
  73. // Note: CompressedStack.GetCompressedStack doesn't return null
  74. // like Thread.CurrentThread.GetCompressedStack if no compressed
  75. // stack is present.
  76. CompressedStack cs = Thread.CurrentThread.ExecutionContext.SecurityContext.CompressedStack;
  77. if (cs == null || cs.IsEmpty ()) {
  78. cs = CompressedStack.Capture ();
  79. } else {
  80. cs = cs.CreateCopy ();
  81. // merge the existing compressed stack (from a previous Thread) with the current
  82. // Thread stack so we can assign "all of it" to yet another Thread
  83. CompressedStack newstack = CompressedStack.Capture ();
  84. for (int i=0; i < newstack._list.Count; i++)
  85. cs._list.Add (newstack._list [i]);
  86. }
  87. return cs;
  88. }
  89. [MonoTODO ("incomplete")]
  90. [SecurityCritical]
  91. public void GetObjectData (SerializationInfo info, StreamingContext context)
  92. {
  93. if (info == null)
  94. throw new ArgumentNullException ("info");
  95. }
  96. [SecurityCritical]
  97. static public void Run (CompressedStack compressedStack, ContextCallback callback, object state)
  98. {
  99. if (compressedStack == null)
  100. throw new ArgumentException ("compressedStack");
  101. Thread t = Thread.CurrentThread;
  102. CompressedStack original = null;
  103. try {
  104. original = t.ExecutionContext.SecurityContext.CompressedStack;
  105. t.ExecutionContext.SecurityContext.CompressedStack = compressedStack;
  106. callback (state);
  107. }
  108. finally {
  109. if (original != null)
  110. t.ExecutionContext.SecurityContext.CompressedStack = original;
  111. }
  112. }
  113. // internal stuff
  114. internal bool Equals (CompressedStack cs)
  115. {
  116. if (IsEmpty ())
  117. return cs.IsEmpty ();
  118. if (cs.IsEmpty ())
  119. return false;
  120. if (_list.Count != cs._list.Count)
  121. return false;
  122. for (int i=0; i < _list.Count; i++) {
  123. SecurityFrame sf1 = (SecurityFrame) _list [i];
  124. SecurityFrame sf2 = (SecurityFrame) cs._list [i];
  125. if (!sf1.Equals (sf2))
  126. return false;
  127. }
  128. return true;
  129. }
  130. internal bool IsEmpty ()
  131. {
  132. return ((_list == null) || (_list.Count == 0));
  133. }
  134. internal IList List {
  135. get { return _list; }
  136. }
  137. }
  138. }