User guide
90 CHAPTER 3. THE LIBRARY
AND downheap(event, i) BE
{ LET j, min = 2*i, ? // j is left child, if present
IF j > priqn DO
{ upheap(event, i)
RETURN
}
min := priq!j!0
// Look at other child, if it exists
IF j<priqn & min>priq!(j+1)!0 DO j := j+1
// promote earlier child
priq!i := priq!j
i := j
} REPEAT
AND getevent() = VALOF
{ LET event = priq!1 // Get the earliest event
LET last = priq!priqn // Get the event at the end of the heap
UNLESS priqn>0 RESULTIS 0 // No events in the priority queue
priqn := priqn-1 // Decrement the heap size
downheap(last, 1) // Re-insert last event
RESULTIS event
}
AND waitfor(ticks) BE
{ // Make an event item into the priority queue
LET eventtime, co = simtime+ticks, currco
insertevent(@eventtime) // Insert into the priority queue
cowait() // Wait for the specified number of ticks
}
// ###################### Queueing functions #########################
AND prwaitq(node) BE
{ LET p = wkqv!node
IF -1 <= p <= 0 DO { writef("wkq for node %n: %n*n", node, p); RETURN }
writef("wkq for node %n:", node)
WHILE p DO
{ writef(" %n", p!1)
p := !p
}
newline()
}