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