A background task is some CPU or I/O intensive operation that needs to be split up in small chunks of processing because it would block the process for too long if executed atomically.
#include "common.h"
#include "bg.h"
#include "misc.h"
#include "tm.h"
#include "walloc.h"
#include "override.h"
Data Structures | |
| struct | bgtask |
| Internal representation of a user-defined task. More... | |
Defines | |
| #define | MAX_LIFE 350000 |
| In useconds, MUST be << 1 sec. | |
| #define | MIN_LIFE 40000 |
| Min lifetime per task, in usecs. | |
| #define | DELTA_FACTOR 4 |
| Max variations are 400%. | |
Enumerations | |
| enum | bgtask_magic { BGTASK_MAGIC = 0xbacc931dU, BGTASK_DEAD_MAGIC = 0x6f5c8a03U } |
| enum | { TASK_F_EXITED = 1 << 0, TASK_F_SIGNAL = 1 << 1, TASK_F_RUNNING = 1 << 2, TASK_F_ZOMBIE = 1 << 3, TASK_F_NOTICK = 1 << 4, TASK_F_SLEEPING = 1 << 5, TASK_F_RUNNABLE = 1 << 6, TASK_F_DAEMON = 1 << 7 } |
Functions | |
| void | bg_task_check (const struct bgtask *const bt) |
| gint | bg_task_seqno (const struct bgtask *bt) |
| gpointer | bg_task_context (const struct bgtask *bt) |
| void | bg_sched_add (struct bgtask *bt) |
| Add new task to the scheduler (run queue). | |
| void | bg_sched_remove (struct bgtask *bt) |
| Remove task from the scheduler (run queue). | |
| bgtask * | bg_sched_pick (void) |
| Pick next task to schedule. | |
| void | bg_task_suspend (struct bgtask *bt) |
| Suspend task. | |
| void | bg_task_resume (struct bgtask *bt) |
| Resume task execution. | |
| void | bg_sched_sleep (struct bgtask *bt) |
| Add task to the sleep queue. | |
| void | bg_sched_wakeup (struct bgtask *bt) |
| Remove task from the sleep queue and insert it to the runqueue. | |
| bgtask * | bg_task_switch (struct bgtask *bt) |
| Switch to new task `bt'. | |
| bgtask * | bg_task_alloc (void) |
| bgtask * | bg_task_create (const gchar *name, const bgstep_cb_t *steps, gint stepcnt, gpointer ucontext, bgclean_cb_t ucontext_free, bgdone_cb_t done_cb, gpointer done_arg) |
| Create a new background task. | |
| bgtask * | bg_daemon_create (const gchar *name, const bgstep_cb_t *steps, gint stepcnt, gpointer ucontext, bgclean_cb_t ucontext_free, bgstart_cb_t start_cb, bgend_cb_t end_cb, bgclean_cb_t item_free, bgnotify_cb_t notify) |
| A "daemon" is a task equipped with a work queue. | |
| void | bg_daemon_enqueue (struct bgtask *bt, gpointer item) |
| Enqueue work item to the daemon task. | |
| void | bg_task_free (struct bgtask *bt) |
| Free task structure. | |
| void | bg_task_terminate (struct bgtask *bt) |
| Terminate the task, invoking the completion callback if defined. | |
| void | bg_task_exit (struct bgtask *bt, gint code) |
| Called by user code to "exit" the task. | |
| void | bg_task_sendsig (struct bgtask *bt, bgsig_t sig, bgsig_cb_t handler) |
| Deliver signal via the user's signal handler. | |
| gint | bg_task_kill (struct bgtask *bt, bgsig_t sig) |
| Send a signal to the given task. | |
| bgsig_cb_t | bg_task_signal (struct bgtask *bt, bgsig_t sig, bgsig_cb_t handler) |
| Install user-level signal handler for a task signal. | |
| void | bg_task_deliver_signals (struct bgtask *bt) |
| Deliver all the signals queued so far for the task. | |
| void | bg_task_cancel (struct bgtask *bt) |
| Cancel a given task. | |
| void | bg_task_ticks_used (struct bgtask *bt, gint used) |
| This routine can be called by the task when a single step is not using all its ticks and it matters for the computation of the cost per tick. | |
| void | bg_reclaim_dead (void) |
| Reclaim all dead tasks. | |
| void | bg_task_ended (struct bgtask *bt) |
| Called when a task has ended its processing. | |
| void | bg_sched_timer (gboolean overloaded) |
| Main task scheduling timer, called once per second. | |
| guint | bg_task_terminate_all (GSList **ptr) |
| void | bg_close (void) |
| Called at shutdown time. | |
Variables | |
| guint32 | common_dbg = 0 |
| gint | runcount |
| GSList * | runq |
| GSList * | sleepq |
| GSList * | dead_tasks |
| bgtask * | current_task |
|
|
Max variations are 400%.
|
|
|
In useconds, MUST be << 1 sec.
|
|
|
Min lifetime per task, in usecs.
|
|
|
|
|
|
|
|
|
Called at shutdown time.
|
|
||||||||||||||||||||||||||||||||||||||||
|
A "daemon" is a task equipped with a work queue. When the daemon is initially created, it has an empty work queue and it is put in the "sleeping" state where it is not scheduled. As long as there is work in the work queue, the task is scheduled. It goes back to sleep when the work queue becomes empty. The `steps' given represent the processing to be done on each item of the work queue. The `start_cb' callback is invoked before working on a new item, so that the context can be initialized. The `end+cb' callback is invoked when the item has been processed (successfully or not). Since a daemon is not supposed to exit (although it can), there is no `done' callback. Use bg_daemon_enqueue() to enqueue more work to the daemon.
|
|
||||||||||||
|
Enqueue work item to the daemon task. If task was sleeping, wake it up. |
|
|
Reclaim all dead tasks.
|
|
|
Add new task to the scheduler (run queue).
|
|
|
Pick next task to schedule.
|
|
|
Remove task from the scheduler (run queue).
|
|
|
Add task to the sleep queue.
|
|
|
Main task scheduling timer, called once per second.
|
|
|
Remove task from the sleep queue and insert it to the runqueue.
|
|
|
|
|
|
Cancel a given task.
|
|
|
|
|
|
|
|
||||||||||||||||||||||||||||||||
|
Create a new background task. The `steps' array is cloned, so it can be built on the caller's stack. Each time the task is scheduled, the current processing step is ran. Each step should perform a small amount of work, as determined by the number of ticks it is allowed to process. When a step is done, we move to the next step. When the task is done, the `done_cb' callback is called, if supplied. The user-supplied argument `done_arg' will also be given to that callback. Note that "done" does not necessarily mean success.
|
|
|
Deliver all the signals queued so far for the task.
|
|
|
Called when a task has ended its processing.
|
|
||||||||||||
|
Called by user code to "exit" the task. We exit immediately, not returning to the user code. |
|
|
Free task structure.
|
|
||||||||||||
|
Send a signal to the given task.
|
|
|
Resume task execution.
|
|
||||||||||||||||
|
Deliver signal via the user's signal handler.
|
|
|
|
|
||||||||||||||||
|
Install user-level signal handler for a task signal.
|
|
|
Suspend task.
|
|
|
Switch to new task `bt'. If argument is NULL, suspends current task.
|
|
|
Terminate the task, invoking the completion callback if defined.
|
|
|
|
|
||||||||||||
|
This routine can be called by the task when a single step is not using all its ticks and it matters for the computation of the cost per tick.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.3.9.1