2 // System.Threading.Thread.cs
5 // Zoltan Varga (vargaz@freemail.hu)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Diagnostics;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Permissions;
38 namespace System.Threading {
42 public sealed class CompressedStack : ISerializable {
44 public class CompressedStack {
46 private ArrayList _list;
48 internal CompressedStack (int length)
51 _list = new ArrayList (length);
54 internal CompressedStack (CompressedStack cs)
56 if ((cs != null) && (cs._list != null))
57 _list = (ArrayList) cs._list.Clone ();
61 [ComVisibleAttribute (false)]
66 CompressedStack CreateCopy ()
68 return new CompressedStack (this);
76 static CompressedStack Capture ()
78 CompressedStack cs = new CompressedStack (0);
79 cs._list = SecurityFrame.GetStack (1);
81 // include any current CompressedStack inside the new Capture
82 CompressedStack currentCs = Thread.CurrentThread.GetCompressedStack ();
83 if (currentCs != null) {
84 for (int i=0; i < currentCs._list.Count; i++)
85 cs._list.Add (currentCs._list [i]);
90 // NOTE: This method doesn't show in the class library status page because
91 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
93 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
94 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
95 static public CompressedStack GetCompressedStack ()
97 // Note: CompressedStack.GetCompressedStack doesn't return null
98 // like Thread.CurrentThread.GetCompressedStack if no compressed
100 CompressedStack cs = Thread.CurrentThread.GetCompressedStack ();
102 cs = CompressedStack.Capture ();
104 // merge the existing compressed stack (from a previous Thread) with the current
105 // Thread stack so we can assign "all of it" to yet another Thread
106 CompressedStack newstack = CompressedStack.Capture ();
107 for (int i=0; i < newstack._list.Count; i++)
108 cs._list.Add (newstack._list [i]);
114 [MonoTODO ("incomplete")]
115 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
116 public void GetObjectData (SerializationInfo info, StreamingContext context)
119 throw new ArgumentNullException ("info");
122 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
123 static public void Run (CompressedStack compressedStack, ContextCallback callback, object state)
125 if (compressedStack == null)
126 throw new ArgumentException ("compressedStack");
128 Thread t = Thread.CurrentThread;
129 CompressedStack original = null;
131 original = t.GetCompressedStack ();
132 t.SetCompressedStack (compressedStack);
136 if (original != null)
137 t.SetCompressedStack (original);
143 internal bool Equals (CompressedStack cs)
146 return cs.IsEmpty ();
149 if (_list.Count != cs._list.Count)
152 for (int i=0; i < _list.Count; i++) {
153 SecurityFrame sf1 = (SecurityFrame) _list [i];
154 SecurityFrame sf2 = (SecurityFrame) cs._list [i];
155 if (!sf1.Equals (sf2))
161 internal bool IsEmpty ()
163 return ((_list == null) || (_list.Count == 0));
166 internal IList List {
167 get { return _list; }