STM patterns

retry waits for a change

retry abandons the current transaction and blocks until one of the variables it read changes. It is not a busy loop and not an exception: the runtime parks the thread and wakes it on the write.

retry :: STM a orElse :: STM a -> STM a -> STM a

A consumer that waits for a list to become non-empty writes the test and a retry in the empty branch. That is the whole condition variable, and the read that decided is the one the runtime watches.

orElse and a fallback

orElse runs the first transaction and, if it retrys, runs the second with the first one's writes rolled back. The subtlety is what does not count as a failure: a transaction that returns Nothing has committed, so orElse does not try the fallback. Only retry falls through.

A value that arrives late

module Main (main) where import Control.Concurrent (forkIO, threadDelay) import Control.Concurrent.STM main :: IO () main = do box <- newTVarIO ([] :: [String]) _ <- forkIO $ do threadDelay 50000 atomically (modifyTVar' box (++ ["late"])) item <- atomically $ do items <- readTVar box case items of [] -> retry first : _ -> pure first putStrLn item left <- newTVarIO (Nothing :: Maybe Int) right <- newTVarIO (Just 7) fallback <- atomically (readTVar left `orElse` readTVar right) print fallback chosen <- atomically $ (readTVar left >>= maybe retry pure) `orElse` (readTVar right >>= maybe retry pure) print chosen

ghc -Wall -O1 -threaded -o patterns patterns.hs
./patterns

late
Nothing
Just 7

The three lines are the three cases. The first transaction waited for the worker to add a value and then took it, which is why late appears even though the list was empty when the program started. The second line is the plain orElse: the left TVar held Nothing, which is a committed result, so the right side never ran and the answer is Nothing. The third is the same choice written to wait: Nothing into a retry, so the left branch falls through and the right one gives Just 7.

The bounded queue

The channel of the earlier lesson is unbounded. A bounded queue is the pair of a list and a size, and both ends wait on a condition:

data Queue a = Queue (TVar Int) (TVar [a]) (TVar Int) writeQ :: Int -> Queue a -> a -> STM () writeQ limit (Queue count items _) value = do n <- readTVar count if n >= limit then retry else writeTVar count (n + 1) >> modifyTVar' items (value :)

The writer retries while the queue is full and the reader retries while it is empty, so a producer that is faster than its consumer is slowed down instead of growing the heap. The capstone job runner is this queue with a Stop constructor, a result TVar and a counter of finished workers; every piece is one of the patterns above.

Exercises

  1. Write a transaction that waits until a counter reaches ten.
  2. Use orElse with two queues and take from whichever has a value.
  3. Add a timeout with registerDelay and orElse so the consumer gives up after a second.
  4. Explain why Nothing does not trigger the fallback.
  5. Write the bounded queue completely and test it with a slow consumer.
  6. Explain what the runtime watches when a transaction retries.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does retry do?

    Answer

    Abandons the transaction and blocks until a variable it read changes

    The thread parks and the write to a watched variable wakes it.

  2. When does orElse run its second transaction?

    Answer

    Only when the first one retries

    A committed Nothing is a result; only retry falls through.

  3. Why is readTVar left orElse readTVar right Nothing when left is Nothing?

    Answer

    The first transaction committed with Nothing

    orElse is about retry, not about the Maybe inside the value.

  4. What happens when a transaction retries and no other thread writes the TVar?

    Answer

    The runtime reports a thread blocked indefinitely in an STM transaction and ends the program

    A retry with no possible writer is a deadlock, and the RTS says so with that message.

  5. What is a bounded queue made of?

    Answer

    A count and a list in TVars, with retry at both ends

    The count enforces the limit and the retries make the writers wait.

  6. What does this program print? ``module Main (main) where import Control.Concurrent.STM main :: IO () main = do left <- newTVarIO (Nothing :: Maybe Int) right <- newTVarIO (Just 7) value <- atomically (readTVar left orElse readTVar right) print value``

    Answer

    Nothing

    The left transaction commits with Nothing, so the right side does not run.

  7. How does a consumer wait for a non-empty list?

    Answer

    Test the list in the transaction and retry on the empty branch

    The read of the TVar makes the retry wake on the write that fills it.

  8. What does this program print? module Main (main) where import Control.Concurrent.STM main :: IO () main = do box <- newTVarIO (1 :: Int) value <- atomically $ do n <- readTVar box if n > 0 then pure "yes" else retry putStrLn value

    Answer

    yes

    The value is positive, so the transaction commits with the string.

  9. What does the runtime watch when a transaction retries?

    Answer

    The TVars the transaction read

    A write to any of them makes the transaction eligible again.

  10. What does this program print? module Main (main) where import Control.Concurrent.STM main :: IO () main = do box <- newTVarIO (5 :: Int) atomically (writeTVar box 9) readTVarIO box >>= print

    Answer

    9

    The transaction replaced the value and the read shows 9.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev