Work off the interface's thread
The interface's thread makes no system call: no file read or written, no process started, no D-Bus call that waits,
not even a stat. Each of them can take a second (a disk asleep, a network share, polkit), and the window freezes for
it. Such work runs on another thread, and its result comes back to the interface's thread to be shown.
ui.task: the work there, the result here
Save :: struct { path: string, text: string, ok: bool }
save_work :: proc(d: rawptr) { // on a thread of the chores
s := (^Save)(d)
s.ok = os.write_entire_file(s.path, s.text) == nil
}
save_done :: proc(d: rawptr, word: u64) { // on the interface's thread, at the start of a build
s := (^Save)(d)
if !s.ok { ui.announce(&app.announcer, .Bad, "Not saved") }
free(s)
}
save_lost :: proc(d: rawptr) { free((^Save)(d)) } // the State was destroyed meanwhile: done does not run
ui.task(&app.ui, save_work, save_done, new_clone(Save{path = path, text = text}), save_lost)| Procedure | |
|---|---|
task(s, work, done, data, lost = nil) | work(data) on the chores, then done(data, 0) on s's thread at its next build (ui.post; the window wakes). Called on the interface's thread. When s is destroyed by then, done never runs and lost(data) does, on the thread that found it out. done nil: nothing comes back |
chores() | the program's pool for that: CHORES_THREADS threads, up to CHORES_GROW more while some hang; started at its first use, and again at the first use after chores_stop |
chores_stop(bound = CHORES_STOP_BOUND) | at the program's end, before ui.destroy: the work waiting runs (its results are posted), then the threads are joined, within bound (2 s; 0: no bound). False: a chore that hangs was left to the process's end. While the stop drains, an ask (a chore asking for one more) is refused, as after the end; once it is over, a later task starts the chores again: a second app in the same process (a test after a test) gets its answers |
The data of a task is the caller's until done or lost runs: allocate it with an allocator other threads may use
(the heap), not the frame's temporary memory.
A thread of the program's own (a reader that lives as long as a child, terminal.md) posts through a link:
| Procedure | |
|---|---|
task_link(s) -> ^Task_Box | on the interface's thread: a counted link to s, which outlives it |
task_link_post(link, done, data, lost = nil) | any thread: done(data, 0) at s's next build; lost(data) instead when s is destroyed, before or after the post |
task_link_release(link) | any thread: one per task_link |
The system's programs and dialogs
| Procedure | |
|---|---|
open(target, kind = .Path) | opens a file or folder (.Path), a web address (.Url) or shows a file in its folder (.Reveal) with the system's program, on the chores: a launch can wait on the disk, the shell or D-Bus. target is copied |
file_dialog(s, kind, d, done, user, source = nil) | a file dialog (.Open, .Save, .Folder; d a platform.File_Dialog, copied): the toolkit's browser, reading source (nil: the computer's files; docs/file-browser.md), built by the toolkit at each build; with set_file_dialogs(s, .System) and no source, the system's, on the chores: a modal loop (Windows) or another process (zenity, kdialog, the portal) that the interface's thread would wait for. done(result, user) comes at a next build of s; the result's paths are freed after it |
platform.open_url, open_path, reveal_in_folder and the platform.*_dialog procedures wait: they are for a
thread that may wait (a task's work).
Finding what blocks
In a debug build (TOOLKIT_BLOCKING_CHECK, on with -debug), every procedure of the toolkit that waits on the system
(platform dialogs, opening, notifications, the clipboard read at once; fsx reads and writes; config files;
desktop.theme_icon, desktop.open_with; setup.facts) called on a thread that builds an interface says so on the
standard error, once per call site of the program:
toolkit: fsx.write_atomic on the interface's thread (<program>/prefs.fm:88): it can freeze the window; make it a task (ui.task)base.blocking_count() counts them for the process, base.blocking_count_here() for this thread. ui.begin_build marks
its thread; a call before the first build (the start-up's reads) is not said.
A program's tests run with the check (toolbox.odin_command gives --define TOOLKIT_BLOCKING_CHECK=true to test builds),
and a testkit/session counts the calls made in its frames (Session.blocking): the test fails at its end when there
are any, so the CI finds a disk, process or D-Bus call in a build. What the test does between frames (its fixtures) is
not counted. A test of a step meant to wait (an install, a dialog of the system) says so with session.allow_blocking(s).
What is read once a process is read before the first frame, and the frames read the kept value: the system's light or
dark (platform.dark_mode_preferred, read by host_open, then told by the portal), the person's folders
(platform.known_folder_path: $HOME waits on nothing, user-dirs.dirs is read once).
trame:tasks: pools and jobs
#import "trame:tasks". A tasks.Pool is a few threads; ui.chores() is one. A program makes its own for work of its
own order (a pool per connection; the pictures last asked for first).
| Procedure | |
|---|---|
start(p, Pool_Options) | threads started now (0: none, see below), grow more while all are held for stuck (1 s), an added one ending after extra_idle (5 s) with nothing to do; newest_first; clock a test's clock |
add(p, work, data, Add_Options{after_ms, urgent}) | work for a thread: in the order it came (or the newest first), urgent before all that waits, after_ms from now with no thread asleep for it. False once stop began |
stop(p, lost = nil, bound = 0) | without lost, what waits runs first (timed work too); with it, what waits does not run and lost gets its data (as ui.task's lost). The threads are joined: work that hangs holds the stop, unless bound > 0: then it returns false after that long and leaves the pool to the process's end (a later stop joins it) |
job_ask(p, &job, Add_Options{}) | a run of a Job{work, data}: asked while one waits, nothing more; asked while it runs, one more run after it |
job_busy(p, &job) | a run waits or goes on |
counts(p) -> Counts, wait_rest(p, bound) | for tests: how much waits and runs, how many threads; until it rests |
A reader asked again and again (a folder read again each time it changes, a scan of every process) is a Job: its work
reads into a result of its own, then posts it (ui.post) or swaps it in under a lock of its own.
A pool of no thread (threads = 0) runs work at once on the thread that adds it, and timed work when pump(p) says,
by Pool_Options.clock (poke(p) after the test moves it): a test drives it in its own order and by its own clock.
Tests: src/tasks/tasks_test.fm (order, urgent and newest first, the test's clock, a job's merged asks, a held pool
that grows then shrinks, a stop that hands back what waits), tests/post/task_test.fm (a task's result on the
interface's thread; a task that ends after its State is destroyed; two apps in turn, each stops the chores, the
second's task answers; a chore that asks for more during the stop is refused).