merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / SafeHandle.cs
1 //
2 // System.Runtime.InteropServices.SafeHandle
3 //
4 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 // Notes:
26 //     This code is only API complete, but it lacks the runtime support
27 //     for CriticalFinalizerObject and any P/Invoke wrapping that might
28 //     happen.
29 //
30 //     For details, see:
31 //     http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx
32 //
33 // On implementing SafeHandles:
34 //     http://blogs.msdn.com/bclteam/archive/2005/03/15/396335.aspx
35 //
36 // Issues:
37 //     The System.Runtime.ConstrainedExecution.ReliabilityContractAttribute has
38 //     not been applied to any APIs here yet.
39 //
40 //     TODO: Although DangerousAddRef has been implemented, I need to
41 //     find out whether the runtime performs the P/Invoke if the
42 //     handle has been disposed already.
43 //
44 //
45
46 #if NET_2_0
47 using System;
48 using System.Runtime.InteropServices;
49 using System.Runtime.ConstrainedExecution;
50 using System.Threading;
51
52 namespace System.Runtime.InteropServices
53 {
54         public abstract class SafeHandle : CriticalFinalizerObject, IDisposable {
55                 //
56                 // Warning: the offset of handle is mapped inside the runtime
57                 // if you move this, you must updated the runtime definition of
58                 // MonoSafeHandle
59                 //
60                 protected IntPtr handle;
61                 IntPtr invalid_handle_value;
62                 int refcount = 0;
63                 bool owns_handle;
64                 
65                 protected SafeHandle (IntPtr invalidHandleValue, bool ownsHandle)
66                 {
67                         invalid_handle_value = invalidHandleValue;
68                         owns_handle = ownsHandle;
69                         refcount = 1;
70                 }
71
72                 public void Close ()
73                 {
74                         if (refcount == 0)
75                                 throw new ObjectDisposedException (GetType ().FullName);
76
77                         int newcount, current;
78                         do {
79                                 current = refcount;
80                                 newcount = current-1;
81                         } while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
82
83                         if (newcount == 0 && owns_handle){
84                                 ReleaseHandle ();
85                                 handle = invalid_handle_value;
86                         }
87                 }
88
89                 //
90                 // I do not know when we could not be able to increment the
91                 // reference count and set success to false.   It might just
92                 // be a convention used for the following code pattern:
93                 //
94                 // bool release = false
95                 // try { x.DangerousAddRef (ref release); ... }
96                 // finally { if (release) x.DangerousRelease (); }
97                 //
98                 public void DangerousAddRef (ref bool success)
99                 {
100                         if (refcount == 0)
101                                 throw new ObjectDisposedException (GetType ().FullName);
102
103                         int newcount, current;
104                         do {
105                                 current = refcount;
106                                 newcount = current + 1;
107                                 
108                                 if (handle == invalid_handle_value || current == 0){
109                                         //
110                                         // In MS, calling sf.Close () followed by a call
111                                         // to P/Invoke with SafeHandles throws this, but
112                                         // am left wondering: when would "success" be
113                                         // set to false?
114                                         //
115                                         throw new ObjectDisposedException (GetType ().FullName);
116                                 }
117                         } while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
118                         success = true;
119                 }
120
121                 public IntPtr DangerousGetHandle ()
122                 {
123                         if (refcount == 0){
124                                 throw new ObjectDisposedException (GetType ().FullName);
125                         }
126
127                         return handle;
128                 }
129
130                 public void DangerousRelease ()
131                 {
132                         if (refcount == 0)
133                                 throw new ObjectDisposedException (GetType ().FullName);
134
135                         int newcount, current;
136                         do {
137                                 current = refcount;
138                                 newcount = current-1;
139                         } while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
140
141                         if (newcount == 0 && owns_handle){
142                                 ReleaseHandle ();
143                                 handle = invalid_handle_value;
144                         }
145                 }
146
147                 public virtual void Dispose ()
148                 {
149                         Dispose (true);
150                         GC.SuppressFinalize (this);
151                 }
152
153                 //
154                 // See documentation, this invalidates the handle without
155                 // closing it.
156                 //
157                 public void SetHandleAsInvalid ()
158                 {
159                         handle = invalid_handle_value;
160                 }
161                 
162                 protected virtual void Dispose (bool disposing)
163                 {
164                         if (disposing)
165                                 Close ();
166                         else {
167                                 //
168                                 // The docs say `never call this with disposing=false',
169                                 // the question is whether:
170                                 //   * The runtime will ever call Dipose(false) for SafeHandles (special runtime case)
171                                 //   * Whether we should just call ReleaseHandle regardless?
172                                 //
173                         }
174                 }
175
176                 protected abstract bool ReleaseHandle ();
177
178                 protected void SetHandle (IntPtr handle)
179                 {
180                         this.handle = handle;
181                 }
182
183                 public bool IsClosed {
184                         get {
185                                 return refcount == 0;
186                         }
187                 }
188
189                 public abstract bool IsInvalid {
190                         get;
191                 }
192
193                 ~SafeHandle ()
194                 {
195                         if (owns_handle){
196                                 ReleaseHandle ();
197                                 handle = invalid_handle_value;
198                         }
199                 }
200         }
201 }
202 #endif