takeTMVar :: TMVar a -> STM a
takeTMVar (TMVar t) = do
  m <- readTVar t                       -- <1>
  case m of
    Nothing -> retry                    -- <2>
    Just a  -> do
      writeTVar t Nothing               -- <3>
      return a
-- >>

-- <<putTMVar
takeTMVar :: TMVar a -> STM a
takeTMVar (TMVar t) = do
  m <- readTVar t                       -- <1>
  case m of
    Nothing -> retry                    -- <2>
    Just a  -> do
      writeTVar t Nothing               -- <3>
      return a
-- >>

-- <<putTMVar
takeTMVar (TMVar t) = do
  m <- readTVar t                       -- <1>
  case m of
    Nothing -> retry                    -- <2>
    Just a  -> do
      writeTVar t Nothing               -- <3>
      return a
-- >>

-- <<putTMVar