CompressedStack.cs 4.7 KB

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