-setNameCache :: NameCache -> TcRnIf a b ()
-setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
- writeMutVar nc_var nc }
+updNameCache :: (NameCache -> (NameCache, c)) -> TcRnIf a b c
+updNameCache upd_fn = do
+ HscEnv { hsc_NC = nc_var } <- getTopEnv
+ atomicUpdMutVar' nc_var upd_fn
+
+-- | A function that atomically updates the name cache given a modifier
+-- function. The second result of the modifier function will be the result
+-- of the IO action.
+type NameCacheUpdater c = (NameCache -> (NameCache, c)) -> IO c
+
+-- | Return a function to atomically update the name cache.
+mkNameCacheUpdater :: TcRnIf a b (NameCacheUpdater c)
+mkNameCacheUpdater = do
+ nc_var <- hsc_NC `fmap` getTopEnv
+ let update_nc f = do r <- atomicModifyIORef nc_var f
+ _ <- evaluate =<< readIORef nc_var
+ return r
+ return update_nc