Skip to content

Programming Basie10

Jobs Come and Go ​

Chapters 5 and 6 worked with one or two jobs at a time, each owned by a local variable in main. In a real queue, jobs arrive, wait their turn, finish and leave. Later jobs take the slots they occupied. The program needs somewhere to keep the owners of all the waiting jobs. It also has to cope when more jobs arrive than it has room for.

Two kinds of room ​

The queue needs room for the job records themselves and room for the owners of the waiting jobs:

basie
pool HJob: Job[3]
basie
var queue: HJob?[3]

The pool reserves three slots for Job records. The local array queue holds three optional owners, which all start as none. Each one can later store a job's owning handle.

The pool limits how many job records can exist at once. The array limits how many positions the queue has. Here both are three, but they needn't be. A program might share one large pool among several small queues, each with its own array of owners.

Lending a place for an owner ​

Adding a job means allocating a record and putting its owner into a queue position. The routine has to store into the caller's array element, so it needs the place itself rather than a record:

basie
sub addJob(var place: HJob?, value: u16): boolean

place is a var parameter of an optional owner type. The caller lends the routine a place that holds an owner or none, such as a variable, a field or an array element. The call is written addJob(var queue[0], 7), with var before the argument. The routine can move an owner into the place, move one out of it or overwrite it.

In Chapter 6 a routine was lent a record, and it could read or change the record but never release it. Lending a place is different, because the routine changes what the place owns. Overwriting or emptying the place releases the record it owned.

Inside, addJob tries new? and selects the result with move. If the pool had room, it moves the new owner into place:

basie
select move new? HJob(value)
case some(allocated)
    place = move allocated
    return true
case none
    return false
end

After place = move allocated, the caller's queue position is the job's owner. The move emptied allocated, so nothing is released when the arm ends. If the pool was full, addJob returns false and leaves the place as it was.

Taking a job out ​

takeJob moves the owner out of the queue position and returns the job number:

basie
select move place
case some(taken)
    return taken.number
case none
    return 0
end

select move place empties the caller's queue position and moves its owner into taken. Returning from the arm ends taken, which releases the record. The job number survives because it's a copied scalar. The queue position is now none and ready for another job.

Returning zero for an empty position works only because this example never uses zero as a job number. Otherwise the routine would need another way to report an empty position, such as a Boolean returned alongside the number. Basie guarantees that the queue can't corrupt memory, but the meaning of its values is up to your program.

Tracing the queue ​

basie
type Job = record
    number: u16
end

pool HJob: Job[3]

sub addJob(var place: HJob?, value: u16): boolean
    select move new? HJob(value)
    case some(allocated)
        place = move allocated
        return true
    case none
        return false
    end
end

sub takeJob(var place: HJob?): u16
    select move place
    case some(taken)
        return taken.number
    case none
        return 0
    end
end

sub main()
    var queue: HJob?[3]
    var extra: HJob?

    assert addJob(var queue[0], 7)
    assert addJob(var queue[1], 9)
    assert addJob(var queue[2], 11)
    assert not addJob(var extra, 13)

    var completed = takeJob(var queue[0])
    assert completed = 7
    assert addJob(var queue[0], 13)

    assert takeJob(var queue[1]) = 9
    assert takeJob(var queue[2]) = 11
    assert takeJob(var queue[0]) = 13
    assert takeJob(var queue[0]) = 0
end

The trace follows the queue positions and the pool together:

Stepqueue[0]queue[1]queue[2]Live records
Add 7, 9 and 11job 7job 9job 113
Add 13 into extra failsjob 7job 9job 113
Take queue[0], returns 7nonejob 9job 112
Add 13 into queue[0]job 13job 9job 113
Take queue[1], returns 9job 13nonejob 112
Take queue[2], returns 11job 13nonenone1
Take queue[0], returns 13nonenonenone0
Take queue[0], returns 0nonenonenone0

The fourth add fails because the pool has no free slot, so extra stays none. Job 13 then takes the slot that job 7 released.

Three pool slots fill, one record ends its lifetime, and a later record begins in the available storage.

Two things are reused here: queue[0] as a position in the queue and the slot that job 7 used as storage in the pool. Job 13 is still a different record from job 7. A weak handle kept for job 7 would no longer match the slot's generation, so select would take its none arm.

Replacing a job ​

Assigning a new owner to a place that already owns a record releases the old record. addJob allocates the new job before storing it in place. With a full pool the allocation fails first, so the old job survives even though the store would have released it. The cost is that a successful replacement needs a spare slot for a moment, while the old and new records both exist.

To discard the old job first, assign place = none, which releases the record. The old job is then gone whether the new allocation succeeds or fails. Neither order is right in every case, so choose the one that fits what the program should do when it runs out of room.

Records that own records ​

The queue keeps all its owners in one array. Some structures, such as a linked list or a tree, are better built by letting each record own the next. Basie allows that through optional owning fields:

basie
forward pool HNode

type Node = record
    value: u16
    next: HNode?
end

pool HNode: Node[3]

The record needs the pool's handle type, and the pool needs the record. forward pool HNode introduces the pool's name early, so the record can use HNode? ahead of the full pool declaration.

Each node owns the node after it, so the owner of the first node owns the whole chain. Releasing the first node frees every node after it, down to the end of the chain. Basie does this without recursion, using a fixed amount of stack for a chain of any length.

STACK.BSI builds a stack this way. push adds a value at the front and pop removes the front value. total and contains walk along the stack without changing it:

basie
forward pool HNode

type Node = record
    value: u16
    next: HNode?
end

pool HNode: Node[3]

sub push(var list: HNode?, value: u16): boolean
    select move new? HNode(value, move list)
    case some(node)
        list = move node
        return true
    case none
        return false
    end
end

sub pop(var list: HNode?): u16
    select move list
    case some(top)
        list = move top.next
        return top.value
    case none
        return 0
    end
end

sub total(list: weak HNode?): u16
    var sum: u16 = 0
    var cursor: weak HNode? = list

    while true
        select cursor
        case some(node)
            sum = sum + node.value
            cursor = node.next
        case none
            exit
        end
    end

    return sum
end

sub contains(list: weak HNode?, value: u16): boolean
    var cursor: weak HNode? = list

    while true
        select cursor
        case some(node)
            if node.value = value
                return true
            end
            cursor = node.next
        case none
            exit
        end
    end

    return false
end

sub main()
    var stack: HNode?

    assert push(var stack, 7)
    assert push(var stack, 9)
    assert push(var stack, 11)
    assert not push(var stack, 13)
    assert total(stack) = 27

    assert pop(var stack) = 11
    assert push(var stack, 13)
    assert total(stack) = 29
    assert contains(stack, 9)
    assert not contains(stack, 11)


    stack = none
    assert total(stack) = 0

    assert push(var stack, 1)
    assert push(var stack, 2)
    assert push(var stack, 3)

    assert pop(var stack) = 3
    assert pop(var stack) = 2
    assert pop(var stack) = 1
    assert pop(var stack) = 0
end

push is lent the place that owns the top of the stack. new? HNode(value, move list) allocates a node whose next field takes over the old top. If the pool is full, new? returns none without evaluating either argument. The move list never happens, and the stack stays as it was. Chapter 7 described this property of new?. Here it stops a full pool from losing the stack.

pop moves the top node out of list into top, moves top.next back into list and returns the value. When the arm ends, top is released. The move already emptied its next field, so no other node goes with it.

main pushes 7, 9 and 11 to fill the pool, so the fourth attempt fails. Popping 11 frees a slot for 13. Then the single assignment stack = none releases the whole chain of three nodes. All three slots are free again, so the next three pushes succeed. The pops return 3, 2 and 1, then zero once the stack is empty. Along the way total returns 27 for the full stack, 29 after 13 replaces 11 and 0 for the empty stack. contains finds 9 but not the popped 11.

Walking a list ​

total adds up the values on the stack. Its parameter has the type weak HNode?, so it refers to the first node without owning it:

basie
sub total(list: weak HNode?): u16

The call total(stack) passes the owner stack. The parameter is weak, so total gets a weak handle to the first node, and stack keeps ownership. When the stack is empty, the weak handle is none.

Inside, cursor steps from node to node:

basie
var cursor: weak HNode? = list

while true
    select cursor
    case some(node)
        sum = sum + node.value
        cursor = node.next
    case none
        exit
    end
end

cursor is declared weak HNode?, so each assignment to it makes a weak handle. node.next is the owning field of the current node. Assigning it to cursor makes a weak handle to the next node and leaves ownership with the field. The select tests cursor each time round. The none arm runs at the end of the chain, and exit leaves the loop.

contains walks the same way and returns true as soon as it finds the value. Neither routine moves or releases anything, so walking the stack leaves every node with the owner it had.

A weak cursor doesn't keep its node alive. In this fragment the stack holds 9 above 7. top refers to the top node while pop releases it:

basie
select stack
case some(top)
    assert pop(var stack) = 9
    assert top.value = 9
case none
end

The arm leaves ownership with stack, which is an ordinary variable inside the arm. pop(var stack) moves the top node out of stack and releases it. top still refers to that node, so the next line traps with stale-handle. Read the value before you pop, or copy it into a local.

Guarding against cycles ​

A chain of owners must end somewhere. Suppose a node owned a node that owned it back. That loop would have no outside owner to release it. Storing a record into a field of something it owns would create such a loop. Basie checks for this before the store and stops the program with an ownership-cycle trap. A program reaches a record further down a chain through a weak handle, so Basie checks only a store through a weak handle. Every store in this example goes through an owner and needs no check.

A link that points back up a chain should be weak. In a list that runs in both directions, each node owns the next node and refers to the previous one:

basie
type Node = record
    value: u16
    next: HNode?
    prev: weak HNode?
end

prev owns nothing, so it can refer to any node without creating a cycle. When the first node is released, the whole chain still goes with it through next.

Things to try ​

In COLLECT.BSI, take the jobs in a different order, say queue[2] before queue[1], and adjust the assertions to match. Then add a second replacement after the first removal and count the live records after every step.

In STACK.BSI, remove the line stack = none and predict which assertion fails. The three nodes from the first half are still on the stack, so the pool is full and the next push returns false.

Add a walker count(list: weak HNode?): u16 that returns the number of nodes, and assert the count after each push and pop.

Summary ​

  • An array of optional owners, such as HJob?[3], holds the owners of a changing collection. Its capacity is separate from the pool's.
  • A var parameter of an optional owner type, such as var place: HJob?, is lent a place for an owner. The routine can fill, empty or replace it.
  • select move on such a place takes its owner out and leaves the place empty.
  • Positions and pool slots are both reused, but a new record is never the old one.
  • Allocating before replacing protects the old record when the pool is full, at the cost of needing a spare slot.
  • Optional owning fields let a record own another record. Releasing the first record frees the whole chain.
  • forward pool lets a record refer to its own pool's handles.
  • A walker takes a weak parameter and steps a weak cursor along the next fields. It changes no owner.
  • A weak cursor to a released node is stale, and the next access through it traps.
  • Storing an owner that would create a cycle traps with ownership-cycle. A link back up a chain should be a weak field.