Commits

Jean-Marie Gaillourdet committed 7b6ba4b

new function: declareEmptyMVar

Comments (0)

Files changed (2)

src/Data/Global.hs

   -- * Reference of Variable Declaration Functions
     declareIORef
   , declareMVar
+  , declareEmptyMVar
   , declareTVar
 
 ) where

src/Data/Global/Registry.hs

 -----------------------------------------------------------------------------------------------
 module Data.Global.Registry (
   -- * Public Interface
-    declareIORef, declareMVar, declareTVar
+    declareIORef, declareMVar, declareEmptyMVar, declareTVar
 
   -- * Private Testing Interface
   , lookupOrInsert
     -> String
     -> a
     -> IO (ref a)
-lookupOrInsert registry new name val
-    | registry `pseq` new `pseq` name `pseq` val `pseq` False = undefined
+lookupOrInsert registry new name _
+    | registry `pseq` new `pseq` name `pseq` False = undefined
 lookupOrInsert registry new name val = modifyMVar registry lkup
   where
     err ex got = error $ "Data.Global.Registry: Invariant violation\n"
 
 
 
+lookupOrInsertEmptyMVar
+    :: forall a. Typeable a
+    => String
+    -> IO (MVar a)
+lookupOrInsertEmptyMVar name = lookupOrInsert globalRegistry newEmptyMVar' name (undefined :: a)
+  where
+    newEmptyMVar' :: a -> IO (MVar a)
+    newEmptyMVar' _ = newEmptyMVar
+{-# NOINLINE lookupOrInsertEmptyMVar #-}
+
+
+
 lookupOrInsertTVar
     :: Typeable a
     => String
 -- pragma in order to define top-level 'IORef's.
 declareIORef
     :: Typeable a
-    => String     -- ^ The identifying name
-    -> a          -- ^ The initial value of the 'IORef', it may or may not be used.
-    -> (IORef a)  -- ^ A unique 'IORef' determined by @(name, typeOf val)@. Whether it refers
-                  -- to the given initial value or not is unspecified.
+    => String   -- ^ The identifying name
+    -> a        -- ^ The initial value of the 'IORef', it may or may not be used.
+    -> IORef a  -- ^ A unique 'IORef' determined by @(name, typeOf val)@. Whether it refers to
+                -- the given initial value or not is unspecified.
 declareIORef name val = unsafePerformIO $ lookupOrInsertIORef name val
 {-# NOINLINE declareIORef #-}
 
 -- pragma in order to define top-level 'MVar's.
 declareMVar
     :: Typeable a
-    => String    -- ^ The identifying name
-    -> a         -- ^ The initial value of the 'MVar', it may or may not be used.
-    -> (MVar a)  -- ^ A unique 'MVar' determined by @(name, typeOf val)@. Whether it refers to
-                 -- the given initial value or not is unspecified.
+    => String  -- ^ The identifying name
+    -> a       -- ^ The initial value of the 'MVar', it may or may not be used.
+    -> MVar a  -- ^ A unique 'MVar' determined by @(name, typeOf val)@. Whether it refers to
+               -- the given initial value or not is unspecified.
 declareMVar name val = unsafePerformIO $ lookupOrInsertMVar name val
 {-# NOINLINE declareMVar #-}
 
 
+-- | @declareEmptyMVar name@ maps a variable name to an 'MVar'. Calling it multiple times with
+-- the same @name@ and type of the expected 'MVar' will always return the same 'MVar'.
+--
+-- @
+-- someVar :: MVar Int
+-- someVar = declareEmptyMVar \"my-global-some-var\"
+-- @
+--
+-- Note, there is /no/ need to use 'unsafePerformIO' or to add a @{-\# NOINLINE someVar \#-}@
+-- pragma in order to define top-level 'MVar's.
+declareEmptyMVar
+    :: Typeable a
+    => String  -- ^ The identifying name
+    -> MVar a  -- ^ A unique 'MVar' determined by @(name, typeOf val)@. Whether it is still
+               -- empty depends on the rest of the program.
+declareEmptyMVar name = unsafePerformIO $ lookupOrInsertEmptyMVar name
+{-# NOINLINE declareEmptyMVar #-}
+
 
 -- | @declareTVar name val@ maps a variable name to an 'TVar'. Calling it multiple times with the same
 -- @name@ and type of 'val' will always return the same 'TVar'.
 -- pragma in order to define top-level 'TVar's.
 declareTVar
     :: Typeable a
-    => String    -- ^ The identifying name
-    -> a         -- ^ The initial value of the 'TVar', it may or may not be used.
-    -> (TVar a)  -- ^ A unique 'TVar' determined by @(name, typeOf val)@. Whether it refers to
-                 -- the given initial value or not is unspecified.
+    => String  -- ^ The identifying name
+    -> a       -- ^ The initial value of the 'TVar', it may or may not be used.
+    -> TVar a  -- ^ A unique 'TVar' determined by @(name, typeOf val)@. Whether it refers to
+               -- the given initial value or not is unspecified.
 declareTVar name val = unsafePerformIO $ lookupOrInsertTVar name val
 {-# NOINLINE declareTVar #-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.