module Network.GnuTLS.RefCount
(RefCount, newRefCount, allocRef, freeRef, addRefFinalizer)
where
import Data.IORef
newtype RefCount = RC (IORef (Int, [IO ()]))
newRefCount :: IO () -> IO RefCount
newRefCount act = return . RC =<< newIORef (1, [act])
allocRef :: RefCount -> IO ()
allocRef (RC ref) = atomicModifyIORef ref $ \(iv,cs) -> ((iv+1,cs),())
freeRef :: RefCount -> IO ()
freeRef (RC ref) = atomicModifyIORef ref handler >>= sequence_
where handler (1,cs) = (error "Double free of RefCount",cs)
handler (k,cs) = ((k1,cs), [])
addRefFinalizer :: RefCount -> IO () -> IO ()
addRefFinalizer (RC ref) c = atomicModifyIORef ref $ \(iv,cs) -> ((iv,c:cs),())