
threadutil.pl -- Interactive thread utilities
This library provides utilities that are primarily intended for interactive usage in a threaded Prolog environment. It allows for inspecting threads, manage I/O of background threads (depending on the environment) and manipulating the debug status of threads.
threads- List currently known threads with their status. For each thread it lists the id, class, status, debug status, CPU time and current stack usage. If a thread is listed with Debug set to `X', it cannot be debugged. If the Debug is show as `V', it is running in debug mode (see debug/0) and responds to spy points and break points.
join_threads- Join all terminated threads. For normal applications, dealing with terminated threads must be part of the application logic, either detaching the thread before termination or making sure it will be joined. The predicate join_threads/0 is intended for interactive sessions to reclaim resources from threads that died unexpectedly during development.
with_stopped_threads(:Goal, Options) is det- Stop all threads except the caller while running
once(Goal). Note that this is in the thread user utilities as this is not something that should be used by normal applications. Notably, this may deadlock if the current thread requires input from some other thread to complete Goal or one of the stopped threads has a lock. Options:- stop_nodebug_threads(+Boolean)
- If
true(defaultfalse), also stop threads created with thedebug(false)option. - except(+List)
- Do not stop threads from this list.
thread_has_console is semidet- True when the calling thread has an attached console.
attach_console is det
attach_console(+Title) is det- Create a new console and make the standard Prolog streams point to it. If not provided, the title is built using the thread id. Does nothing if the current thread already has a console attached.
interactor is det[private]
interactor(+Title) is det[private]- Run a Prolog toplevel in another thread with a new console window.
If Title is given, this will be used as the window title. As of
SWI-Prolog version 10, the console is provided by the XPCE graphics
library using
library(epilog).
tspy(:Spec) is det
tspy(:Spec, +ThreadOrClass) is det- Trap the graphical debugger on reaching Spec. The predicate tspy/0 enabled debug mode in all threads using tdebug/0 while tspy/1 enables debug mode using tdebug/1.
tdebug is det
tdebug(+ThreadOrClass) is det
tnodebug is det
tnodebug(+ThreadOrClass) is det- Enable or disable a thread or group of threads for debugging using
the graphical tracer. A group of threads is addressed based on the
classproperty of a thread set by thread_create/3 or set_thread/2. This implies loading the graphical tracer and switching the thread to debug mode using debug/0. New threads created inherit their debug mode from the thread that created them.Thread classes have been introduced in SWI-Prolog 10.0.2/10.1.5. This allows for more selective debugging as well as ensuring debugging works in newly created threads. For example, the HTTP server creates all its worker threads in the class
http. Using query below, we reliable make sure spy points are trapped in HTTP handler threads, regardless of whether the worker existed or is lazily created and regardless of whether the user switched to nodebug mode while tracing a previous event (see debug_reset_from_class/0).?- tdebug(http).
tbacktrace(+Thread) is det
tbacktrace(+Thread, +Options) is det- Print a backtrace for Thread to the stream
user_errorof the calling thread. This is achieved by inserting an interrupt into Thread using call_in_thread/2. Options:- depth(+MaxFrames)
- Number of stack frames to show. Default is the current Prolog
flag
backtrace_depthor 20.
Other options are passed to get_prolog_backtrace/3.
thread_get_prolog_backtrace(+Depth, -Stack, +Options)[private]- As get_prolog_backtrace/3, but starts above the C callback, hiding the overhead inside call_in_thread/2.
tprofile(+Thread) is det- Profile the operation of Thread until the user hits a key.
init_pce[private]- Make sure XPCE is running if it is attached, so we can use the graphical display using in_pce_thread/1.
thread_alias(+Alias) is det- Set the alias for a thread.