[Haskell-cafe] Re: Abstraction leak

Thomas Conway drtomc at gmail.com
Wed Jul 4 22:39:02 EDT 2007


On 7/5/07, Donald Bruce Stewart <dons at cse.unsw.edu.au> wrote:
> Yep. The current impl is:
>
>     mmapFile :: FilePath -> IO ByteString
>     mmapFile f = mmap f >>= \(fp,l) -> return $! PS fp 0 l
>
>     mmap :: FilePath -> IO (ForeignPtr Word8, Int)
>     mmap = do
>          ...
>          p  <- mmap l fd
>          fp <- newForeignPtr p unmap -- attach unmap finaliser
>          return fp
>

Which, if I read it correctly is not safe in a concurrent/multitasking
environment, since it wraps the underlying mmapped region. In many
programs, I'm sure this won't be a problem. Unfortunately, the system
I'm working on is multi-threaded, and we definitely want to update
regions. Perhaps I'll have to bite the bullet and implement the
Mapping thing I described. The really unfortunate thing is that I'd
really like to be able to do it within the STM monad, with rollback,
etc - escaping to the IO monad is annoying.

FWIW, the technique I use to handle this kind of situation may be of
general interest. Consider a cache of structures reconstituted from an
external file.  If a requested item is not in the cache, then we throw
an exception which is caught in a wrapper function which is in the IO
monad, read the requested structure, stick it in the cache, then rerun
the transaction. There are a few details you have to get right,
including making sure none of the items you require to complete the
operation get evicted by another thread, but it works very nicely.

T.
-- 
Dr Thomas Conway
drtomc at gmail.com

Silence is the perfectest herald of joy:
I were but little happy, if I could say how much.


More information about the Haskell-Cafe mailing list