Chapter 33: the pair no lock can make atomic, built

The compiler stops a race on a value, a lock taken in the wrong order, a lock handed to another thread. What it does not try to do is make a pair of operations atomic: a check under one lock followed by an act under another is two operations, and nothing is held between them. This Account makes the mistake and the fix side by side. The IDE writes the constant, the class with its lock and each method's shape; you type the four keys and the program that shows the difference. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-check-then-act.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.concurrency.checkthenact

  defines constant
    withdrawal <- 80

  defines class

    Account
      lockedBalance <- MutexLock(100)

      hasFunds()
        -> amount as Integer
        <- rtn as Boolean: false
        checker <- (amount, rtn) is MutexKey of Integer as function
          rtn :=: lockedItem >= amount
        require lockedBalance.enter(checker)

      take()
        -> amount as Integer
        taker <- (amount) is MutexKey of Integer as function
          lockedItem :=: lockedItem - amount
        require lockedBalance.enter(taker)

      takeIfFunds()
        -> amount as Integer
        <- rtn as Boolean: false
        taker <- (amount, rtn) is MutexKey of Integer as function
          if lockedItem >= amount
            lockedItem :=: lockedItem - amount
            rtn :=: true
        require lockedBalance.enter(taker)

      balance()
        <- rtn as Integer: Integer()
        reader <- (rtn) is MutexKey of Integer as function
          rtn :=: lockedItem
        require lockedBalance.enter(reader)

      default operator ?

  defines program

    CheckThenAct()
      stdout <- Stdout()

      split <- Account()
      firstSeesFunds <- split.hasFunds(withdrawal)
      secondSeesFunds <- split.hasFunds(withdrawal)
      stdout.println(`both saw enough: ${firstSeesFunds and secondSeesFunds}`)
      split.take(withdrawal)
      split.take(withdrawal)
      stdout.println(`check and take apart: ${split.balance()}`)

      together <- Account()
      firstTaken <- together.takeIfFunds(withdrawal)
      secondTaken <- together.takeIfFunds(withdrawal)
      stdout.println(`check and take together: ${together.balance()}, taken ${firstTaken} then ${secondTaken}`)

Step 1: A constant

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

withdrawal is the constant's name and 80 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: withdrawal is the constant's name and 80 its value - a literal in the Value field, from which the type is inferred

The preview shows withdrawal about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows withdrawal about to be written

Step 2: Account — a balance under a lock

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

Account is the class's name. Choose Preview.

The EK9 IDE just before choosing Preview: Account is the class's name

The preview shows Account about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Account about to be written

Select the Account line and, above it, type the blank line the book opens its first class with. Saves when typed.

The EK9 IDE once this is done: Select the Account line and, above it, type the blank line the book opens its first class with

Right-click the Account line and choose Add Field…

The EK9 IDE once this is done: Right-click the Account line and choose Add Field…

Type lockedBalance <- MutexLock(100) - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type lockedBalance <- MutexLock(100) - the field and the value it starts with

The preview shows lockedBalance added - and default operator ? with it, Account's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows lockedBalance added - and default operator ? with it, Account's first field

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

hasFunds is the method's name. Add one parameter, amount of type Integer, set Return type to Boolean. Choose Apply.

The EK9 IDE just before choosing Apply: hasFunds is the method's name

Select the stub and type hasFunds' key: it answers through rtn, which the key captures and changes with :=: - the verb that changes the one object both names share. Saves when typed.

The EK9 IDE once this is done: Select the stub and type hasFunds' key

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

take is the method's name. Add one parameter, amount of type Integer. Choose Apply.

The EK9 IDE just before choosing Apply: take is the method's name

Select the stub and type take's key: the balance reduced under the lock. Saves when typed.

The EK9 IDE once this is done: Select the stub and type take's key

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

takeIfFunds is the method's name. Add one parameter, amount of type Integer, set Return type to Boolean. Choose Apply.

The EK9 IDE just before choosing Apply: takeIfFunds is the method's name

Select the stub and type takeIfFunds' key: the check and the take in ONE key, so no one can slip between them. Saves when typed.

The EK9 IDE once this is done: Select the stub and type takeIfFunds' key

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

balance is the method's name. Set Return type to Integer. Choose Apply.

The EK9 IDE just before choosing Apply: balance is the method's name

Select the stub and type balance's key. Saves when typed.

The EK9 IDE once this is done: Select the stub and type balance's key

Step 3: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

CheckThenAct is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: CheckThenAct is the program's name

The preview shows CheckThenAct about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows CheckThenAct about to be written

Select the CheckThenAct() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the CheckThenAct() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type CheckThenAct's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type CheckThenAct's body exactly as the chapter shows it

Step 4: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

both saw enough: true, then check and take apart a balance of -60, then together a balance of 20, with taken true then false. Choose Close.

The EK9 IDE just before choosing Close: both saw enough

The result

CheckThenAct.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. hasFunds and take, apart, let two withdrawals both see enough and both go through; takeIfFunds does the check and the take under one enter, so the second finds nothing left. Every operation was correct - the pair was not.

The EK9 IDE when the lesson has finished

Where this lesson comes from