performance and safety tweaks; safe/unsafe FFI calls and adding an MVar to ensure only one Haskell writer at a time