So I just coded up the approach. It compiles, so I assume it works as
intended. ;-)

Comments and clever obervations extremely welcome.

cheers,
T.
module W where

import Control.Concurrent.STM
import qualified Data.Map as M
import System.Mem.Weak
import GHC.Conc

makePtr tabPtr a
    = do
        tab <- readTVar tabPtr
        case M.lookup a tab of
            Nothing -> allocPtr tabPtr tab a
            Just wp -> do
                mp <- unsafeIOToSTM (deRefWeak wp)
                case mp of
                    Nothing -> allocPtr tabPtr tab a
                    Just ptr -> return ptr
    where
        allocPtr tabPtr tab a = do
            ptr <- newTVar (Just a, Nothing)
            wp <- unsafeIOToSTM (mkWeakPtr ptr Nothing)
            let tab' = M.insert a wp tab
            writeTVar tabPtr tab'
            return ptr
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to