Debian Codegraph @

src/Data/Cache/LRU/IO/Internal.hs

raw
1 {-# OPTIONS_HADDOCK not-home #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3
4 -- | This module contains a mutable wrapping of an LRU in the IO
5 -- monad, providing atomic access in a concurrent environment. All
6 -- calls preserve the same semantics as those in "Data.Cache.LRU", but
7 -- perform updates in place.
8 --
9 -- This module contains the internal implementation details. It's
10 -- possible to put an 'AtomicLRU' into a bad state with this module.
11 -- It is highly recommended that the external interface,
12 -- "Data.Cache.LRU.IO", be used instead.
13 module Data.Cache.LRU.IO.Internal where
14
15 import Prelude hiding ( lookup, mod, take )
16
17 import Control.Applicative ( (<$>) )
18 import Control.Concurrent.MVar ( MVar )
19 import qualified Control.Concurrent.MVar as MV
20
21 import Control.Exception ( bracketOnError )
22
23 import Data.Cache.LRU ( LRU )
24 import qualified Data.Cache.LRU as LRU
25
26 import Data.Typeable (Typeable)
27
28 -- | The opaque wrapper type
29 newtype AtomicLRU key val = C (MVar (LRU key val)) deriving Typeable
30
31 -- | Make a new AtomicLRU that will not grow beyond the optional
32 -- maximum size, if specified.
33 newAtomicLRU :: Ord key => Maybe Integer -- ^ the optional maximum size
34 -> IO (AtomicLRU key val)
35 newAtomicLRU = fmap C . MV.newMVar . LRU.newLRU
36
37 -- | Build a new LRU from the optional maximum size and list of
38 -- contents. See 'LRU.fromList' for the semantics.
39 fromList :: Ord key => Maybe Integer -- ^ the optional maximum size
40 -> [(key, val)] -> IO (AtomicLRU key val)
41 fromList s l = fmap C . MV.newMVar $ LRU.fromList s l
42
43 -- | Retrieve a list view of an AtomicLRU. See 'LRU.toList' for the
44 -- semantics.
45 toList :: Ord key => AtomicLRU key val -> IO [(key, val)]
46 toList (C mvar) = LRU.toList <$> MV.readMVar mvar
47
48 maxSize :: AtomicLRU key val -> IO (Maybe Integer)
49 maxSize (C mvar) = LRU.maxSize <$> MV.readMVar mvar
50
51 -- | Insert a key/value pair into an AtomicLRU. See 'LRU.insert' for
52 -- the semantics.
53 insert :: Ord key => key -> val -> AtomicLRU key val -> IO ()
54 insert key val (C mvar) = modifyMVar_' mvar $ return . LRU.insert key val
55
56 -- | Look up a key in an AtomicLRU. See 'LRU.lookup' for the
57 -- semantics.
58 lookup :: Ord key => key -> AtomicLRU key val -> IO (Maybe val)
59 lookup key (C mvar) = modifyMVar' mvar $ return . LRU.lookup key
60
61 -- | Remove an item from an AtomicLRU. Returns the value for the
62 -- removed key, if it was present
63 delete :: Ord key => key -> AtomicLRU key val -> IO (Maybe val)
64 delete key (C mvar) = modifyMVar' mvar $ return . LRU.delete key
65
66 -- | Remove the least-recently accessed item from an AtomicLRU.
67 -- Returns the (key, val) pair removed, if one was present.
68 pop :: Ord key => AtomicLRU key val -> IO (Maybe (key, val))
69 pop (C mvar) = modifyMVar' mvar $ return . LRU.pop
70
71 -- | Returns the number of elements the AtomicLRU currently contains.
72 size :: AtomicLRU key val -> IO Int
73 size (C mvar) = LRU.size <$> MV.readMVar mvar
74
75 -- | Given a function that takes an 'LRU.LRU' and returns one of the
76 -- same type, use it to modify the contents of this AtomicLRU.
77 modifyAtomicLRU :: (LRU.LRU key val -> LRU.LRU key val)
78 -> AtomicLRU key val
79 -> IO ()
80 modifyAtomicLRU f = modifyAtomicLRU' $ return . f
81
82 -- | Given a function that takes an 'LRU.LRU' and returns an IO action
83 -- producting one of the same type, use it to modify the contents of
84 -- this AtomicLRU.
85 modifyAtomicLRU' :: (LRU.LRU key val -> IO (LRU.LRU key val))
86 -> AtomicLRU key val
87 -> IO ()
88 modifyAtomicLRU' f (C mvar) = modifyMVar_' mvar f
89
90 -- | A version of 'MV.modifyMVar_' that forces the result of the
91 -- function application to WHNF.
92 modifyMVar_' :: MVar a -> (a -> IO a) -> IO ()
93 modifyMVar_' mvar f = do
94 let take = MV.takeMVar mvar
95 replace = MV.putMVar mvar
96 mod x = do
97 x' <- f x
98 MV.putMVar mvar $! x'
99
100 bracketOnError take replace mod
101
102 -- | A version of 'MV.modifyMVar' that forces the result of the
103 -- function application to WHNF.
104 modifyMVar' :: MVar a -> (a -> IO (a, b)) -> IO b
105 modifyMVar' mvar f = do
106 let take = MV.takeMVar mvar
107 replace = MV.putMVar mvar
108 mod x = do
109 (x', result) <- f x
110 MV.putMVar mvar $! x'
111 return result
112
113 bracketOnError take replace mod