2005-04-28 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Threading / CompressedStack.cs
1 //
2 // System.Threading.Thread.cs
3 //
4 // Authors:
5 //      Zoltan Varga (vargaz@freemail.hu)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
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;
37
38 namespace System.Threading {
39
40 #if NET_2_0
41         [Serializable]
42         public sealed class CompressedStack : ISerializable {
43 #else
44         public class CompressedStack {
45 #endif
46                 private ArrayList _list;
47
48                 internal CompressedStack (int length)
49                 {
50                         if (length > 0)
51                                 _list = new ArrayList (length);
52                 }
53
54                 internal CompressedStack (CompressedStack cs)
55                 {
56                         if ((cs != null) && (cs._list != null))
57                                 _list = (ArrayList) cs._list.Clone ();
58                 }
59
60 #if NET_2_0
61                 [ComVisibleAttribute (false)]
62                 public
63 #else
64                 internal
65 #endif
66                 CompressedStack CreateCopy ()
67                 {
68                         return new CompressedStack (this);
69                 }
70
71 #if NET_2_0
72                 public
73 #else
74                 internal
75 #endif
76                 static CompressedStack Capture ()
77                 {
78                         CompressedStack cs = new CompressedStack (0);
79                         cs._list = SecurityFrame.GetStack (1);
80                         return cs;
81                 }
82
83 #if NET_2_0
84                 [MonoTODO ("incomplete")]
85                 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
86                 public void GetObjectData (SerializationInfo info, StreamingContext context)
87                 {
88                         if (info == null)
89                                 throw new ArgumentNullException ("info");
90                 }
91
92                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
93                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
94                 static public CompressedStack GetCompressedStack ()
95                 {
96                         // Note: CompressedStack.GetCompressedStack doesn't return null
97                         // like Thread.CurrentThread.GetCompressedStack if no compressed
98                         // stack is present.
99                         return new CompressedStack (Thread.CurrentThread.GetCompressedStack ());
100                 }
101
102                 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
103                 static public void Run (CompressedStack compressedStack, ContextCallback callback, object state)
104                 {
105                         if (compressedStack == null)
106                                 throw new ArgumentException ("compressedStack");
107
108                         Thread t = Thread.CurrentThread;
109                         CompressedStack original = null;
110                         try {
111                                 original = t.GetCompressedStack (); 
112                                 t.SetCompressedStack (compressedStack);
113                                 callback (state);
114                         }
115                         finally {
116                                 if (original != null)
117                                         t.SetCompressedStack (original);
118                         }
119                 }
120 #endif
121                 // internal stuff
122
123                 internal bool Equals (CompressedStack cs)
124                 {
125                         if (IsEmpty ())
126                                 return cs.IsEmpty ();
127                         if (cs.IsEmpty ())
128                                 return false;
129                         if (_list.Count != cs._list.Count)
130                                 return false;
131
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
141                 internal bool IsEmpty ()
142                 {
143                         return ((_list == null) || (_list.Count == 0));
144                 }
145
146                 internal IList List {
147                         get { return _list; }
148                 }
149         }
150 }