 extension Atomic {
     
     public mutating func isLockFree() -> Bool {
-        @_transparent
-        func _isLockFree(_ theVal: UnsafeMutablePointer<AtomicBase<Instance>>) -> Bool {
-            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicPtrIsLockFree($0) }
-        }
-        return _isLockFree(&base)
-    }
-    
+        return withUnsafeMutablePointer(to: &base) { $0.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicPtrIsLockFree($0) } }
+    }
+    
+    @_transparent
     private mutating func _fetch() -> AtomicBase<Instance> {
-        @_transparent
-        func load(theVal: UnsafeMutablePointer<AtomicBase<Instance>>) -> UnsafeMutableRawPointer {
-            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicLoadPtrBarrier($0) }
-        }
-        let _old = Unmanaged<AtomicBase<Instance>>.fromOpaque(UnsafeRawPointer(load(theVal: &base)))
+        let result = withUnsafeMutablePointer(to: &base) { $0.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicLoadPtrBarrier($0) } }
+        let _old = Unmanaged<AtomicBase<Instance>>.fromOpaque(UnsafeRawPointer(result!))
         return _old.takeUnretainedValue()
     }
     
+    @_transparent
     private mutating func _compareSet(old: AtomicBase<Instance>, new: AtomicBase<Instance>) -> Bool {
         let _old = Unmanaged.passUnretained(old)
         let _new = Unmanaged.passRetained(new)
-        @_transparent
-        func cas(theVal: UnsafeMutablePointer<AtomicBase<Instance>>) -> Bool {
-            return theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicCompareAndSwapPtrBarrier(_old.toOpaque(), _new.toOpaque(), $0) }
-        }
-        let result = cas(theVal: &base)
+        let result = withUnsafeMutablePointer(to: &base) { $0.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicCompareAndSwapPtrBarrier(_old.toOpaque(), _new.toOpaque(), $0) } }
         if result {
             _old.release()
         } else {

    
    @_inlineable
    public mutating func isLockFree() -> Bool {
        @_transparent
        func _isLockFree(_ theVal: UnsafeMutablePointer<UnsafeMutablePointer<Pointee>>) -> Bool {
            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicPtrIsLockFree($0) }
        }
        return _isLockFree(&self)
    }
    
    /// Atomic fetch the current value with barrier.
    @_inlineable
    public mutating func fetchSelf() -> (current: UnsafeMutablePointer, value: UnsafeMutablePointer) {
        @_transparent
        func load(_ theVal: UnsafeMutablePointer<UnsafeMutablePointer<Pointee>>) -> UnsafeMutableRawPointer {
            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicLoadPtrBarrier($0) }
        }
        let val = load(&self).assumingMemoryBound(to: Pointee.self)
        return (val, val)
    }
    
    /// Atomic sets the value with barrier.
    @_inlineable
    public mutating func store(_ new: UnsafeMutablePointer) {
        @_transparent
        func store(_ theVal: UnsafeMutablePointer<UnsafeMutablePointer<Pointee>>) {
            theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicStorePtrBarrier(UnsafeMutableRawPointer(new), $0) }
        }
        store(&self)
    }
    
    /// Compare and set pointers with barrier.
    @_inlineable
    public mutating func compareSet(old: UnsafeMutablePointer, new: UnsafeMutablePointer) -> Bool {
        @_transparent
        func cas(_ theVal: UnsafeMutablePointer<UnsafeMutablePointer<Pointee>>) -> Bool {
            return theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicCompareAndSwapPtrBarrier(UnsafeMutableRawPointer(old), UnsafeMutableRawPointer(new), $0) }
        }
        return cas(&self)
    }
    
    /// Set pointers with barrier.
    @_inlineable
    public mutating func fetchStore(_ new: UnsafeMutablePointer) -> UnsafeMutablePointer {
        @_transparent
        func exchange(_ theVal: UnsafeMutablePointer<UnsafeMutablePointer<Pointee>>) -> UnsafeMutableRawPointer {
            return theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicExchangePtrBarrier(UnsafeMutableRawPointer(new), $0) }
        }
        return exchange(&self).assumingMemoryBound(to: Pointee.self)
    }
}
extension UnsafeRawPointer : SDAtomicProtocol {
    
    @_inlineable
    public mutating func isLockFree() -> Bool {
        @_transparent
        func _isLockFree(_ theVal: UnsafeMutablePointer<UnsafeRawPointer>) -> Bool {
            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicPtrIsLockFree($0) }
        }
        return _isLockFree(&self)
    }
    
    /// Atomic fetch the current value with barrier.
    @_inlineable
    public mutating func fetchSelf() -> (current: UnsafeRawPointer, value: UnsafeRawPointer) {
        @_transparent
        func load(_ theVal: UnsafeMutablePointer<UnsafeRawPointer>) -> UnsafeMutableRawPointer {
            return theVal.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicLoadPtrBarrier($0) }
        }
        let val = UnsafeRawPointer(load(&self))
        return (val, val)
    }
    
    /// Atomic sets the value with barrier.
    @_inlineable
    public mutating func store(_ new: UnsafeRawPointer) {
        @_transparent
        func store(_ theVal: UnsafeMutablePointer<UnsafeRawPointer>) {
            theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicStorePtrBarrier(UnsafeMutableRawPointer(mutating: new), $0) }
        }
        store(&self)
    }
    
    /// Compare and set pointers with barrier.
    @_inlineable
    public mutating func compareSet(old: UnsafeRawPointer, new: UnsafeRawPointer) -> Bool {
        @_transparent
        func cas(_ theVal: UnsafeMutablePointer<UnsafeRawPointer>) -> Bool {
            return theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicCompareAndSwapPtrBarrier(UnsafeMutableRawPointer(mutating: old), UnsafeMutableRawPointer(mutating: new), $0) }
        }
        return cas(&self)
    }
    
    /// Set pointers with barrier.
    @_inlineable
    public mutating func fetchStore(_ new: UnsafeRawPointer) -> UnsafeRawPointer {
        @_transparent
        func exchange(_ theVal: UnsafeMutablePointer<UnsafeRawPointer>) -> UnsafeMutableRawPointer {
            return theVal.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicExchangePtrBarrier(UnsafeMutableRawPointer(mutating: new), $0) }
        }
        return UnsafeRawPointer(exchange(&self))
    }
}


    
    @_inlineable
    public mutating func isLockFree() -> Bool {
        return withUnsafeMutablePointer(to: &self) { $0.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicPtrIsLockFree($0) } }
    }
    
    /// Atomic fetch the current value with barrier.
    @_inlineable
    public mutating func fetchSelf() -> (current: UnsafePointer, value: UnsafePointer) {
        let result = withUnsafeMutablePointer(to: &self) { $0.withMemoryRebound(to: Optional<UnsafeRawPointer>.self, capacity: 1) { _AtomicLoadPtrBarrier($0) } }
        let val = UnsafePointer(result!.assumingMemoryBound(to: Pointee.self))
        return (val, val)
    }
    
    /// Atomic sets the value with barrier.
    @_inlineable
    public mutating func store(_ new: UnsafePointer) {
        withUnsafeMutablePointer(to: &self) { $0.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicStorePtrBarrier(UnsafeMutableRawPointer(mutating: new), $0) } }
    }
    
    /// Compare and set pointers with barrier.
    @_inlineable
    public mutating func compareSet(old: UnsafePointer, new: UnsafePointer) -> Bool {
        return withUnsafeMutablePointer(to: &self) { $0.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicCompareAndSwapPtrBarrier(UnsafeMutableRawPointer(mutating: old), UnsafeMutableRawPointer(mutating: new), $0) } }
    }
    
    /// Set pointers with barrier.
    @_inlineable
    public mutating func fetchStore(_ new: UnsafePointer) -> UnsafePointer {
        let result = withUnsafeMutablePointer(to: &self) { $0.withMemoryRebound(to: Optional<UnsafeMutableRawPointer>.self, capacity: 1) { _AtomicExchangePtrBarrier(UnsafeMutableRawPointer(mutating: new), $0) } }
        return UnsafePointer(result!.assumingMemoryBound(to: Pointee.self))
    }
}

