Chapter 33: one lock over one record, built

In Java the shared tally is a synchronized method or an AtomicLong. Here the tally is a record, the record sits under one lock inside a class, and the only way in is a key - a function that runs while the lock is held. Eight workers each make 250 deposits. The IDE writes the constants, the record and its defaults, the Ledger with its lock and the two methods' shapes, and the abstract work function; you type the keys and the workers. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-shared-tally.

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.tally

  defines constant
    workerCount <- 8

    depositsPerWorker <- 250

  defines record

    Tally
      deposits <- 0
      total <- 0

      default operator

  defines class

    Ledger
      lockedTally <- MutexLock(Tally())

      deposit()
        -> amount as Integer
        depositor <- (amount) is MutexKey of Tally as function
          lockedItem.deposits += 1
          lockedItem.total += amount
        require lockedTally.enter(depositor)

      snapshot()
        <- rtn as Tally: Tally()
        reader <- (rtn) is MutexKey of Tally as function
          rtn :=: lockedItem
        require lockedTally.enter(reader)

      default operator ?

  defines function

    work() as abstract
      <- rtn as Integer?

  defines program

    SharedTally()
      stdout <- Stdout()
      ledger <- Ledger()

      workers <- List() of work
      for id in 1 ... workerCount
        worker <- (ledger, id) is work as function
          for n in 1 ... depositsPerWorker
            ledger.deposit(id)
          rtn: id
        workers += worker

      finished <- cat workers | async | sort | collect as List of Integer
      stdout.println(`workers finished: ${finished}`)
      stdout.println(`tally: ${ledger.snapshot()}`)

Step 1: Constants

Choose Code > New Construct > Constant…

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

workerCount is the constant's name and 8 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: workerCount is the constant's name and 8 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: Constants

Choose Code > New Construct > Constant…

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

depositsPerWorker is the constant's name and 250 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: depositsPerWorker is the constant's name and 250 its value - a literal in the Value field, from which the type is inferred

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

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

Step 3: Tally — the record the lock protects

Choose Code > New Construct > Record…

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

Tally is the record's name. The Fields table holds deposits of type Integer, starting at 0; total of type Integer, starting at 0. Choose Preview.

The EK9 IDE just before choosing Preview: Tally is the record's name

The preview shows Tally about to be written - and default operator ? with them: a record that holds fields must say when it counts as set. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Tally about to be written - and default operator ? with them

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

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

Select the two fields and type them the way the book does: EK9 infers each type from its value, so deposits <- 0 is an Integer. Saves when typed.

The EK9 IDE once this is done: Select the two fields and type them the way the book does

Right-click the Tally line and choose Add Operator > default operator (all missing).

The EK9 IDE once this is done: Right-click the Tally line and choose Add Operator > default operator (all missing)

Select the line the first field brought, default operator ?: the bare default operator below it covers every operator, ? included, so it goes. Saves when typed.

The EK9 IDE once this is done: Select the line the first field brought, default operator ?

Set the operators apart from the fields: above default operator, type a blank line. Saves when typed.

The EK9 IDE once this is done: Set the operators apart from the fields

Step 4: Ledger — one lock over one record

Choose Code > New Construct > Class…

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

Ledger is the class's name. Choose Preview.

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

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

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

Select the Ledger 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 Ledger line and, above it, type the blank line the book opens its first class with

Right-click the Ledger line and choose Add Field…

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

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

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

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

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

Right-click the Ledger line and choose Add Method…

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

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

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

Select the stub and type deposit's key: a dynamic function that implements MutexKey of Tally and captures amount, handed to enter. While enter runs it the lock is held, the key sees the record as lockedItem, and when it ends - however it ends - enter releases. There is no unlock to forget. Saves when typed.

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

Right-click the Ledger line and choose Add Method…

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

snapshot is the method's name. Set Return type to Tally. Choose Apply.

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

Select the stub and type snapshot's key: it copies the locked record out with :=: - a copy is a new object, so nothing outside the lock holds a reference to the locked one. Saves when typed.

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

Step 5: work — what a worker is

Choose Code > New Construct > Function…

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

work is the function's name. Set Returns to Integer and tick as abstract - a function type: a contract with no body. Choose Preview.

The EK9 IDE just before choosing Preview: work is the function's name

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

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

Select the work() as abstract line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the work() as abstract line and, above it, type the blank line the book opens its first function with

Step 6: The program

Choose Code > New Construct > Program…

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

SharedTally is the program's name. Choose Preview.

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

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

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

Select the SharedTally() 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 SharedTally() 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 SharedTally'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 SharedTally's body exactly as the chapter shows it

Step 7: 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.

workers finished: [1, 2, 3, 4, 5, 6, 7, 8], then tally: Tally(deposits=2000, total=9000). Choose Close.

The EK9 IDE just before choosing Close: workers finished

The result

SharedTally.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. lockedTally is a field with an initialiser - the only place a MutexLock may be built - and the only way to the Tally inside is a key. A race would have shown as a smaller total.

The EK9 IDE when the lesson has finished

Where this lesson comes from