Low-level concurrency/multithreading primitives

Author(s): Manuel Carro.

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.

Documentation on exports

PREDICATE

(True) Usage:eng_call(Goal,EngineCreation,ThreadCreation,GoalId)

Calls 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. GoalIds are unique in each execution of a Ciao Prolog program.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)Goal is currently a term which is not a free variable.
    (term_typing:nonvar/1)EngineCreation is currently a term which is not a free variable.
    (term_typing:nonvar/1)ThreadCreation is currently a term which is not a free variable.
    (term_typing:var/1)GoalId is a free variable.
    (basic_props:callable/1)Goal is a term which represents a goal, i.e., an atom or a structure.
    (basic_props:atm/1)EngineCreation is an atom.
    (basic_props:atm/1)ThreadCreation is an atom.
    (basic_props:int/1)GoalId is an integer.
Meta-predicate with arguments: eng_call(goal,?,?,?).

PREDICATE

Usage:eng_call(Goal,EngineCreation,ThreadCreation)

Similar to 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.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)Goal is currently a term which is not a free variable.
    (term_typing:nonvar/1)EngineCreation is currently a term which is not a free variable.
    (term_typing:nonvar/1)ThreadCreation is currently a term which is not a free variable.
    (basic_props:callable/1)Goal is a term which represents a goal, i.e., an atom or a structure.
    (basic_props:atm/1)EngineCreation is an atom.
    (basic_props:atm/1)ThreadCreation is an atom.
Meta-predicate with arguments: eng_call(goal,?,?).

PREDICATE

Usage:eng_backtrack(GoalId,ThreadCreation)

Performs backtracking on the goal designed by 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.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)GoalId is currently a term which is not a free variable.
    (term_typing:nonvar/1)ThreadCreation is currently a term which is not a free variable.
    (basic_props:int/1)GoalId is an integer.
    (basic_props:atm/1)ThreadCreation is an atom.

PREDICATE

Usage:eng_cut(GoalId)

Performs a cut in the execution of the goal GoalId. The next call to eng_backtrack/2 will therefore backtrack all the way and fail.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)GoalId is currently a term which is not a free variable.
    (basic_props:int/1)GoalId is an integer.

PREDICATE

Usage:eng_release(GoalId)

Cleans up and releases the engine executing the goal designed by GoalId. The engine must be idle, i.e., currently not executing any goal. eng_wait/1 can be used to ensure this.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)GoalId is currently a term which is not a free variable.
    (basic_props:int/1)GoalId is an integer.

PREDICATE

Usage:eng_wait(GoalId)

Waits for the engine executing the goal denoted by GoalId to finish the computation (i.e., it has finished searching for a solution, either with success or failure).

  • The following properties should hold at call time:
    (term_typing:nonvar/1)GoalId is currently a term which is not a free variable.
    (basic_props:int/1)GoalId is an integer.

PREDICATE

Usage:eng_kill(GoalId)

Kills the thread executing 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.

  • The following properties should hold at call time:
    (term_typing:nonvar/1)GoalId is currently a term which is not a free variable.
    (basic_props:int/1)GoalId is an integer.

PREDICATE

Usage:

Kills threads and releases stack sets of all active goals, but the one calling eng_killothers. Again, a safety measure. The same cautions as with eng_kill/1 should be taken.

    PREDICATE

    Usage:eng_self(GoalId)

    GoalId is unified with the identifier of the goal within which eng_self/1 is executed. eng_self/1 is deprecated, and eng_goal_id/1 should be used instead.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.

    PREDICATE

    Usage:goal_id(GoalId)

    GoalId is unified with the identifier of the goal within which goal_id/1 is executed. goal_id/1 is deprecated, and eng_goal_id/1 should be used instead.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.

    PREDICATE

    Usage:eng_goal_id(GoalId)

    GoalId is unified with the identifier of the goal within which eng_goal_id/1 is executed.

    • The following properties should hold at call time:
      (basic_props:int/1)GoalId is an integer.

    PREDICATE

    Usage:

    Prints to standard output the current status of the stack sets.

      PREDICATE

      (True) Usage 1:

      • The following properties should hold at call time:
        (basic_props:int/1)Arg1 is an integer.
      • The following properties hold globally:
        (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

      Usage 2:lock_atom(Atom)

      The semaphore associated to 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.

      • The following properties should hold at call time:
        (term_typing:nonvar/1)Atom is currently a term which is not a free variable.
        (basic_props:int/1)Atom is an integer.

      PREDICATE

      (True) Usage 1:

      • The following properties should hold at call time:
        (basic_props:int/1)Arg1 is an integer.
      • The following properties hold globally:
        (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

      Usage 2:unlock_atom(Atom)

      The semaphore associated to Atom is atomically incremented.

      • The following properties should hold at call time:
        (term_typing:nonvar/1)Atom is currently a term which is not a free variable.
        (basic_props:int/1)Atom is an integer.

      PREDICATE

      (True) Usage 1:

      • The following properties should hold at call time:
        (basic_props:int/1)Arg1 is an integer.
        (basic_props:int/1)Arg2 is an integer.
      • The following properties hold globally:
        (foreign_interface_properties:foreign_low/2)The Prolog predicate PrologName is implemented using the function ForeignName. The same considerations as above example are to be applied.

      Usage 2:atom_lock_state(Atom,Value)

      Sets the semaphore associated to 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].

      • The following properties should hold at call time:
        (term_typing:nonvar/1)Atom is currently a term which is not a free variable.
        (term_typing:nonvar/1)Value is currently a term which is not a free variable.
        (basic_props:int/1)Atom is an integer.
        (basic_props:int/1)Value is an integer.

      Usage 3:atom_lock_state(Atom,Value)

      Consults the 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.

      • The following properties should hold at call time:
        (term_typing:nonvar/1)Atom is currently a term which is not a free variable.
        (term_typing:var/1)Value is a free variable.
        (basic_props:atm/1)Atom is an atom.
        (basic_props:int/1)Value is an integer.

      PREDICATE
      concurrent PredName

      The predicate named PredName 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.

      Usage:

      • The following properties should hold at call time:
        (basic_props:predname/1)PredName is a Name/Arity structure denoting a predicate name:
        predname(P/A) :-
                atm(P),
                nnegint(A).
        

      Known bugs and planned improvements

      • Available only for Windows 32 environments and for architectures implementing POSIX threads.
      • Some implementation of threads have a limit on the total number of threads that can be created by a process. Thread creation, in this case, just hangs. A better solution is planned for the future.