Author(s): Manuel Carro.
Version: 1.10#7 (2006/4/26, 19:22:13 CEST)
Version of last change: 1.7#138 (2001/11/8, 19:50:32 CET)
This module provides basic mechanisms for using concurrency and implementing multi-goal applications. It provides a means for arbitrary goals to be specified to be run in a separate stack set; in that case, they are assigned a goal identifier with which further accesses (e.g., asking for more solutions) to the goal can be made. Additionally, in some architectures, these goals can be assigned an O.S. thread, separate from the one which made the initial call, thus providing concurrency and, in multiprocessors, parallelism capabilities.
As for now, the memory space of the threads (c.f., stack sets) is separate in the sense that goals are copied to the new stack set, and bindings of variables are not seen among stack sets which allows forward and backward execution to proceed independently in each stack set, at the cost of the initial goal copy. However, the program space (including, specially, the concurrent predicates) are shared and seen by all the goals and threads, and should be used as the primary means of communication and synchronization. Higer level libraries can be built using these basic blocks.
Additionally, a small set of lock primitives are provided. Locks are associated with atom names. Whereas the concurrent database facilities are enough to implement locks, semaphores, messages, etc., the predicates implementing atom-based locks are faster than the ones accessing the concurrent database (but they are less powerful).
concurrency
):- use_module(library(concurrency)).
concurrency
)
Meta-predicate with arguments: eng_call(goal,?,?,?)
.
Usage: eng_call(+Goal, +EngineCreation, +ThreadCreation, -GoalId)
Goal
in a new engine (stack set), possibly using a new thread, and returns a GoalId
to designate this new goal henceforth. EngineCreation
can be either wait
or create
; the distinction is not yet meaningful. ThreadCreation
can be one of self
, wait
, or create
. In the first case the creating thread is used to execute Goal
, and thus it has to wait until its first result or failure. The call will fail if Goal
fails, and succeed otherwise. However, the call will always suceed when a remote thread is started. The space and identifiers reclaimed for the thread must be explicitly deallocated by calling
eng_release/1
. GoalId
s are unique in each execution of a Ciao Prolog program.
+Goal
is a term which represents a goal, i.e., an atom or a structure.
(basic_props:callable/1
)
+EngineCreation
is an atom.
(basic_props:atm/1
)
+ThreadCreation
is an atom.
(basic_props:atm/1
)
-GoalId
is an integer.
(basic_props:int/1
)
Meta-predicate with arguments: eng_call(goal,?,?)
.
Usage: eng_call(+Goal, +EngineCreation, +ThreadCreation)
eng_call/4
, but the thread (if created) and stack areas are automatically released upon success or failure of the goal. No GoalId
is provided for further interaction with the goal.
+Goal
is a term which represents a goal, i.e., an atom or a structure.
(basic_props:callable/1
)
+EngineCreation
is an atom.
(basic_props:atm/1
)
+ThreadCreation
is an atom.
(basic_props:atm/1
)
Usage: eng_backtrack(+GoalId, +ThreadCreation)
GoalId
. A new thread can be used to perform backtracking, according to ThreadCreation
(same as in
eng_call/4
). Fails if the goal is backtracked over by the local thread, and there are no more solutions. Always succeeds if executed by a remote thread. The engine is not automatically released up upon failure:
eng_release/1
must be called to that end.
+GoalId
is an integer.
(basic_props:int/1
)
+ThreadCreation
is an atom.
(basic_props:atm/1
)
Usage: eng_cut(+GoalId)
Usage: eng_release(+GoalId)
Usage: eng_wait(+GoalId)
GoalId
to finish the computation (i.e., it has finished searching for a solution, either with success or failure).
+GoalId
is an integer.
(basic_props:int/1
)
Usage: eng_kill(+GoalId)
GoalId
(if any), and frees the memory used up by the stack set. Usually one should wait (
eng_wait/1
) for a goal, and then release it, but killing the thread explicitly allows recovering from error states. A goal cannot kill itself. This feature should be used with caution, because there are situations where killing a thread might render the system in an unstable state. Threads should cooperate in their killing, but if the killed thread is blocked in a I/O operation, or inside an internal critical region, this cooperation is not possible and the system, although stopped, might very well end up in a incosistent state.
+GoalId
is an integer.
(basic_props:int/1
)
Usage:
Usage: eng_self(?GoalId)
Usage: goal_id(?GoalId)
Usage: eng_goal_id(?GoalId)
Usage:
Usage: lock_atom(+Atom)
Atom
is accessed; if its value is nonzero, it is atomically decremented and the execution of this thread proceeds. Otherwise, the goal waits until a nonzero value is reached. The semaphore is then atomically decremented and the execution of this thread proceeds.
+Atom
is an atom.
(basic_props:atm/1
)
Usage: unlock_atom(+Atom)
Usage 1: atom_lock_state(+Atom, +Value)
Atom
to Value
. This is usually done at the beginning of the execution, but can be executed at any time. If not called, semaphore associated to atoms are by default inited to 1. It should be used with caution: arbitrary use can transform programs using locks in a mess of internal relations. The change of a semaphore value in a place other than the initialization stage of a program is not among the allowed operations as defined by Dijkstra [Dij65,BA82].
+Atom
is an atom.
(basic_props:atm/1
)
+Value
is an integer.
(basic_props:int/1
)
Usage 2: atom_lock_state(+Atom, -Value)
Value
of the semaphore associated to Atom
. Use sparingly and mainly as a medium to check state correctness. Not among the operations on semaphore by Djikstra.
+Atom
is an atom.
(basic_props:atm/1
)
-Value
is an integer.
(basic_props:int/1
)
concurrent F/A
The predicate named F
with arity A
is made
concurrent in the current module at runtime (useful for predicate names generated on-the-fly). This difficults a better compile-time analysis, but in turn offers more flexibility to applications. It is also faster for some applications: if several agents have to share data in a stuctured fashion (e.g., the generator knows and wants to restrict the data generated to a set of other threads), a possibility is to use the same concurrent fact and emply a field within the fact to distinguish the receiver/sender. This can cause many threads to access and wait on the same fact, which in turns can create contention problems. It is much better to create a new concurrent fact and to use that new name as a channel to communicate the different threads.
concurrent/1
can either be given a
predicate spec in the form Name/Arity
, with Name
and Arity
bound, or to give a value only to Arity
, and let the system choose a new, unused Name
for the fact.
concurrency
)Go to the first, previous, next, last section, table of contents.