     }
 }
 
+/// Follows the routines in Apple's libc, defined by:
+/// http://llvm.org/docs/Atomics.html#libcalls-atomic
 private struct DarwinAtomics {
-    let load: @convention(c) (CInt, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t) -> Void
-    let exchange: @convention(c) (CInt, UnsafeMutableRawPointer, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t) -> Void
-    let compareExchange: @convention(c) (CInt, UnsafeMutableRawPointer, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t, bnr_atomic_memory_order_t) -> CInt
+    let load: @convention(c) (Int, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t) -> Void
+    let store: @convention(c) (Int, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t) -> Void
+    let exchange: @convention(c) (Int, UnsafeMutableRawPointer, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t) -> Void
+    let compareExchange: @convention(c) (Int, UnsafeMutableRawPointer, UnsafeMutableRawPointer, UnsafeMutableRawPointer, bnr_atomic_memory_order_t, bnr_atomic_memory_order_t) -> Bool
     let or8: @convention(c) (UnsafeMutablePointer<UInt8>, UInt8, bnr_atomic_memory_order_t) -> UInt8
     let and8: @convention(c) (UnsafeMutablePointer<UInt8>, UInt8, bnr_atomic_memory_order_t) -> UInt8
 
     static let shared: DarwinAtomics = {
-        let library = UnsafeMutableRawPointer(bitPattern: -2)
+        let library = UnsafeMutableRawPointer(bitPattern: -2) // RTLD_DEFAULT
         return DarwinAtomics(
             load: library.symbol(named: "__atomic_load"),
+            store: library.symbol(named: "__atomic_store"),
             exchange: library.symbol(named: "__atomic_exchange"),
             compareExchange: library.symbol(named: "__atomic_compare_exchange"),
             or8: library.symbol(named: "__atomic_fetch_or_1"),


func bnr_atomic_flag_load(_ target: bnr_atomic_flag_t, _ order: bnr_atomic_memory_order_t) -> Bool {
    var result: Bool = false
    DarwinAtomics.shared.load(CInt(MemoryLayout<Bool>.size), target, &result, order)
    return result
}

func bnr_atomic_flag_test_and_set(_ target: bnr_atomic_flag_t, _ order: bnr_atomic_memory_order_t) -> Bool {
    var new = true
    var old = false
    DarwinAtomics.shared.exchange(CInt(MemoryLayout<Bool>.size), target, &new, &old, order)
    return old
}



func bnr_atomic_bitmask_test(_ target: bnr_atomic_bitmask_t, _ mask: UInt8, _ order: bnr_atomic_memory_order_t) -> Bool {
    var result: UInt8 = 0
    DarwinAtomics.shared.load(MemoryLayout<UInt8>.size, target, &result, order)
    return (result & mask) != 0
}
#endif
