+ //
+ // I do not know when we could not be able to increment the
+ // reference count and set success to false. It might just
+ // be a convention used for the following code pattern:
+ //
+ // bool release = false
+ // try { x.DangerousAddRef (ref release); ... }
+ // finally { if (release) x.DangerousRelease (); }
+ //
+ public void DangerousAddRef (ref bool success)
+ {
+ if (refcount == 0)
+ throw new ObjectDisposedException (GetType ().FullName);
+
+ int newcount, current;
+ do {
+ current = refcount;
+ newcount = current + 1;
+
+ if (handle == invalid_handle_value || current == 0){
+ //
+ // In MS, calling sf.Close () followed by a call
+ // to P/Invoke with SafeHandles throws this, but
+ // am left wondering: when would "success" be
+ // set to false?
+ //
+ throw new ObjectDisposedException (GetType ().FullName);
+ }
+ } while (Interlocked.CompareExchange (ref refcount, newcount, current) != current);
+ success = true;