For the scheduled goals, ECLiPSe uses an execution model which is based on goal priorities and which guarantees that a scheduled goal with a higher priority will be always executed before any goal with lower priority. Priority is a small integer number ranging from 1 to 12, 1 being the highest priority and 12 the lowest (cf. figure 17.1). All goals started from the ECLiPSe top-level loop or from the command line with the -e option have priority 12. Each suspension and each goal which is being executed therefore has an associated priority. The priority of the currently executing goal can be determined with get_priority/1.
Priority-based execution is driven by a scheduler: It picks up the scheduled suspension with the highest priority. If its priority is higher than the priority of the currently executing goal, then the execution of the current goal is interrupted and the new suspension is executed. This is repeated until there are no suspensions with priority higher than that of the current goal.
It is also possible to execute a goal with a given priority by means of call_priority(Goal, Prio) which calls Goal with the priority Prio. When a goal is called this way with high priority, it is effectively made atomic, ie. it will not be interrupted by goals with lower priority that wake up while it executes. Those goals will all be deferred until exit from call_priority/2. This technique can sometimes improve efficiency. Consider for example the following program:
p(1). report(Term) :- writeln(term=Term), suspend(report(Term),3,Term->inst).
and the execution
[eclipse 2]: report(f(X,Y,Z)), p(X),p(Y),p(Z). term = f(X, Y, Z) term = f(1, Y, Z) term = f(1, 1, Z) term = f(1, 1, 1)
report/1 is woken and executed three times, once for each variable binding. If instead we do the three bindings under high priority, it will only execute once after all bindings have already been done:
[eclipse 3]: report(f(X,Y,Z)), call_priority((p(X),p(Y),p(Z)), 2). term = f(X, Y, Z) term = f(1, 1, 1)
Although the programmer is more or less free to specify which priorities to use, we strongly recommend to stick to the following scheme (from urgent to less urgent):