| 1 | Title: Overview |
| 2 | |
| 3 | # Overview |
| 4 | |
| 5 | Libdex is a [GNOME](https://www.gnome.org/) library that provides support for |
| 6 | futures and fibers in GObject-based applications. |
| 7 | |
| 8 | The library attempts to follow well established patterns and terminology around |
| 9 | programming with futures. |
| 10 | |
| 11 | Libdex depends on GLib 2.68 or newer. |
| 12 | |
| 13 | ## Building |
| 14 | |
| 15 | To build a program that uses `libdex`, you can use the following command to get |
| 16 | the `CFLAGS` and libraries necessary to compile and link. |
| 17 | |
| 18 | ```sh |
| 19 | cc hello.c `pkg-config --cflags --libs libdex-1` -o hello |
| 20 | ``` |
| 21 | |
| 22 | Use `#include <libdex.h>` to include support for libdex. |
| 23 | |
| 24 | ## Platform Support |
| 25 | |
| 26 | Libdex, including Fiber support, has been known to work on Linux, FreeBSD, |
| 27 | Solaris, Illumos, macOS, and Windows. |
| 28 | |
| 29 | Support for `io_uring` is limited to Linux. |
| 30 | |
| 31 | ## Futures and Promises |
| 32 | |
| 33 | A [[email protected]] is the primary object representing work that will |
| 34 | complete now or at some point in the future. There are various types of |
| 35 | futures, such as a [[email protected]] or [[email protected]]. |
| 36 | |
| 37 | See the class hierarchy for other types of futures. |
| 38 | |
| 39 | ## Task Groups |
| 40 | |
| 41 | Use [[email protected]] to manage a structured scope of related futures. |
| 42 | Task groups let you close a scope, cancel every tracked child, and await the |
| 43 | group as a single future. See [Task Groups](task-groups.html) for details. |
| 44 | |
| 45 | ## Fibers |
| 46 | |
| 47 | Libdex has support for fibers which run on a separate stack from your thread |
| 48 | stack. They are represented with the [[email protected]] type which is also a |
| 49 | [[email protected]]. |
| 50 | |
| 51 | To create a fiber, use [[email protected]] with the appropriate |
| 52 | scheduler. For thread pool usage, [[email protected]_default] |
| 53 | will get you the default thread pool. |
| 54 | |
| 55 | Fibers are scheduled from a [[email protected]] in a [[email protected]] |
| 56 | which allows them to run cooperatively with other work. |
| 57 | |
| 58 | For lower-overhead async control flow, use a [[email protected]], which is a |
| 59 | stackless alternative to fibers. See [Coroutines](coroutines.html) for the usage |
| 60 | pattern. |
| 61 | |
| 62 | ## Awaiting Futures |
| 63 | |
| 64 | If you are running on a fiber, you may await completion of a future. This will |
| 65 | suspend your fiber stack and yield back to the main thread until that future |
| 66 | has completed, rejected, or the fiber has been canceled. |
| 67 | |
| 68 | Use [[email protected]] or it's variants such as |
| 69 | [[email protected]_object] to await completion of any future. If you are |
| 70 | not on a fiber, then the await will fail and the `error` argument will be set. |
| 71 | |
| 72 | ## Reference Counting |
| 73 | |
| 74 | Everything that is a [[email protected]], include [[email protected]] may be |
| 75 | referenced using `dex_ref()` and unref'd using `dex_unref()` or `dex_clear()`. |
| 76 | |
| 77 | You may also use `g_autoptr(DexFuture)` to automate this. |
| 78 | |
| 79 | ## Callbacks |
| 80 | |
| 81 | When using fibers is not desired you can manually use callbacks based on the |
| 82 | completion or rejection of futures. This is faster than using fibers and does |
| 83 | not require the use of a special stack. However, it can be more cumbersome to |
| 84 | write when in C. |
| 85 | |
| 86 | Use [[email protected]], [[email protected]], and |
| 87 | [[email protected]] to do typical try/catch/finally semantics. Each |
| 88 | of these return a new [[email protected]] that may be further acted upon. |
| 89 | |
| 90 | ## Disowning Futures |
| 91 | |
| 92 | When a future is no longer needed and unref'd, it will notify the futures it |
| 93 | was depending on that they are no longer requiring their completion. Some |
| 94 | future types may use this to cascade work cancellation. |
| 95 | |
| 96 | If this is not a behavior you want (e.g. you want the dependent task to always |
| 97 | complete), then you may use [[email protected]] to disown a future |
| 98 | ensuring it will always complete or reject without cancellation. |
| 99 | |
| 100 | ## Futures as Sets of Work |
| 101 | |
| 102 | A common use of libdex is to await for multiple futures to complete before |
| 103 | taking further action. Use [[email protected]], [[email protected]_race], |
| 104 | [[email protected]], and [[email protected]] to await multiple futures |
| 105 | with different semantics about when the new future will complete. |
| 106 | |
| 107 | ## Limiters |
| 108 | |
| 109 | Use [[email protected]] when a workload should run with bounded concurrency. |
| 110 | [[email protected]] is the preferred API for spawning fiber work because |
| 111 | it acquires a permit before starting and releases it when the fiber completes. |
| 112 | |
| 113 | For custom scopes, use [[email protected]] and |
| 114 | [[email protected]], releasing exactly once for every successful |
| 115 | acquisition. |
| 116 | |
| 117 | See [Limiters](limiters.html) for guidance on choosing limits and shutdown. |
| 118 | |
| 119 | ## State Machines |
| 120 | |
| 121 | Use [[email protected]] when an object has a finite set of modes and |
| 122 | transitions may need asynchronous work. Transition requests are serialized and |
| 123 | callbacks run from a fiber, so callbacks can use `dex_await()` while preserving |
| 124 | a declared transition graph. |
| 125 | |
| 126 | See [State Machines](state-machines.html) for table setup, state updates, and |
| 127 | error handling. |
| 128 | |
| 129 | ## Deadlines and Timeouts |
| 130 | |
| 131 | Use [[email protected]_timeout], [[email protected]_timeout_msec], |
| 132 | [[email protected]_timeout_seconds], or [[email protected]_deadline] |
| 133 | when an operation should either produce its normal result or reject after a |
| 134 | time limit. |
| 135 | |
| 136 | The returned future resolves or rejects with the wrapped future's result when |
| 137 | the wrapped future completes first. If the timeout wins, the returned future |
| 138 | rejects with [[email protected]_OUT] and the wrapped future is discarded. |
| 139 | Keep and [[email protected]] a separate reference to work that must |
| 140 | continue after the timeout. |
| 141 | |
| 142 | ```c |
| 143 | DexFuture *future = dex_future_with_timeout_seconds (load_project (), 10); |
| 144 | ``` |
| 145 | |
| 146 | ## Work Queues |
| 147 | |
| 148 | In addition to fibers, [[email protected]] can also schedule general work |
| 149 | items to be run. Use [[email protected]] to push a work item onto that |
| 150 | scheduler. |
| 151 | |
| 152 | On systems that support double-wide compare-and-swap, this will avoid an |
| 153 | allocation for the work item and data pair. They will be placed into the |
| 154 | wait-free work queue atomically. |
| 155 | |
| 156 | ## Thread Pools and Schedulers |
| 157 | |
| 158 | In many thread pool implementations, the worker thread is only executing |
| 159 | work items. In libdex, thread pools can both service short work items and |
| 160 | fibers along with longer running timeouts and generalized `GSource` on the |
| 161 | `GMainContext`. This allows a great deal of flexibility in using futures on |
| 162 | the thread pool. |
| 163 | |
| 164 | The thread pool is implemented as a global work queue and a series of lock-free |
| 165 | work queues per worker thread. Work stealing is also used between thread pools |
| 166 | for general purpose work items. However, fibers will never be migrated between |
| 167 | threads in order to ensure runtime safety. |
| 168 | |
| 169 | Because of the expectation that you will await futures on the thread pool |
| 170 | threads, should you need to do long blocking work you may consider using |
| 171 | [[email protected]_spawn]. This thread may not await like fibers as it is a |
| 172 | real operating system thread. You may however block on a future using |
| 173 | [[email protected]_wait_for]. |