| 1 | Title: Terminology |
| 2 | |
| 3 | # Futures |
| 4 | |
| 5 | A future is a container that will eventually contain a result or an error. |
| 6 | |
| 7 | Programmers often use the words "future" and "promise" interchangeably. |
| 8 | Libdex tries, when possible, to follow the academic nomenclature for futures. |
| 9 | That is to say that a future is the interface and promise is a type of future. |
| 10 | |
| 11 | Futures exist in one of three states. |
| 12 | The first state is [[email protected]]. |
| 13 | A future exists in this state until it has either rejected or resolved. |
| 14 | |
| 15 | The second state is [[email protected]]. |
| 16 | A future reaches this state when it has successfully obtained a value. |
| 17 | |
| 18 | The last third state is [[email protected]]. |
| 19 | If there was a failure to obtain a value a future will be in this state and contain a [[email protected]] representing such failure. |
| 20 | |
| 21 | # Promises and More |
| 22 | |
| 23 | A promise is a type of future that allows the creator to set the resolved value or error. |
| 24 | This is a common type of future to use when you are integrating with things that are not yet integrated with Libdex. |
| 25 | |
| 26 | Other types of futures also exist. |
| 27 | |
| 28 | ```c |
| 29 | /* resolve to "true" */ |
| 30 | DexPromise *good = dex_promise_new (); |
| 31 | dex_promise_resolve_boolean (good, TRUE); |
| 32 | |
| 33 | /* reject with error */ |
| 34 | DexPromise *bad = dex_promise_new (); |
| 35 | dex_promise_reject (good, |
| 36 | g_error_new (G_IO_ERROR, |
| 37 | G_IO_ERROR_FAILED, |
| 38 | "Failed")); |
| 39 | ``` |
| 40 | |
| 41 | ## Static Futures |
| 42 | |
| 43 | Sometimes you already know the result of a future upfront. |
| 44 | The [[email protected]] is used in this case. |
| 45 | Various constructors for [[email protected]] will help you create one. |
| 46 | |
| 47 | For example, [[email protected]_take_object] will create a static future for a [[email protected]]-derived instance. |
| 48 | |
| 49 | ```c |
| 50 | DexFuture *future = dex_future_new_for_int (123); |
| 51 | ``` |
| 52 | |
| 53 | ## Blocks |
| 54 | |
| 55 | One of the most commonly used types of futures in Libdex is a [[email protected]]. |
| 56 | |
| 57 | a [[email protected]] is a callback that is run to process the result of a future. |
| 58 | The block itself is also a future meaning that you can chain these blocks together into robust processing groups. |
| 59 | |
| 60 | ### "Then" Blocks |
| 61 | |
| 62 | The first type of block is a "then" block which is created using [[email protected]]. |
| 63 | These blocks will only be run if the dependent future resolves with a value. |
| 64 | Otherwise, the rejection of the dependent future is propagated to the block. |
| 65 | |
| 66 | ```c |
| 67 | static DexFuture * |
| 68 | further_processing (DexFuture *future, |
| 69 | gpointer user_data) |
| 70 | { |
| 71 | const GValue *result = dex_promise_get_value (future, NULL); |
| 72 | |
| 73 | return dex_ref (future); |
| 74 | } |
| 75 | ``` |
| 76 | |
| 77 | ### "Catch" Blocks |
| 78 | |
| 79 | Since some futures may fail, there is value in being able to "catch" the failure and resolve it. |
| 80 | Use [[email protected]] to handle the result of a rejected future and resolve or reject it further. |
| 81 | |
| 82 | ```c |
| 83 | static DexFuture * |
| 84 | catch_rejection (DexFuture *future, |
| 85 | gpointer user_data) |
| 86 | { |
| 87 | g_autoptr(GError) error = NULL; |
| 88 | |
| 89 | dex_future_get_value (future, &error); |
| 90 | |
| 91 | if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
| 92 | return dex_future_new_true (); |
| 93 | |
| 94 | return dex_ref (future); |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | ### "Finally" Blocks |
| 99 | |
| 100 | There may be times when you want to handle completion of a future whether it resolved or rejected. |
| 101 | For this case, use a "finally" block by calling [[email protected]]. |
| 102 | |
| 103 | ### Infinite Loops |
| 104 | |
| 105 | If you find you have a case where you want a [[email protected]] to loop indefinitely, you can use the "_loop" variants of the block APIs. |
| 106 | |
| 107 | See [[email protected]_loop], [[email protected]_loop], or [[email protected]_loop]. |
| 108 | This is generally useful when your block's callback will begin the next stage of work as the result of the callback. |
| 109 | |
| 110 | ## Future Sets |
| 111 | |
| 112 | A [[email protected]] is a type of future that contains the result of multiple futures. |
| 113 | This is an extremely useful construct because it allows you to do work concurrently and then process the results in a sort of "reduce" phase. |
| 114 | |
| 115 | For example, you could make a request to a database, cache server, and a timeout and process the first that completes. |
| 116 | |
| 117 | There are multiple types of future sets based on the type of problem you want to solve. |
| 118 | |
| 119 | [[email protected]] can be used to resolve when all dependent futures have resolved, otherwise it will reject with error once they are complete. |
| 120 | If you want to reject as soon as the first item rejects, [[email protected]_race] will get you that behavior. |
| 121 | |
| 122 | Other useful [[email protected]] constructors include [[email protected]] and [[email protected]]. |
| 123 | |
| 124 | ```c |
| 125 | /* Either timeout or propagate result of cache/db query */ |
| 126 | return dex_future_first (dex_timeout_new_seconds (60), |
| 127 | dex_future_any (query_db_server (), |
| 128 | query_cache_server (), |
| 129 | NULL), |
| 130 | NULL); |
| 131 | ``` |
| 132 | |
| 133 | ## Cancellable |
| 134 | |
| 135 | Many programmers who use GTK and GIO are familiar with [[email protected]]. |
| 136 | Libdex has something similar in the form of [[email protected]]. |
| 137 | However, in the Libdex case, [[email protected]] is a future. |
| 138 | |
| 139 | It allows for convenient grouping with other futures to perform cancellation when the [[email protected]] method is called. |
| 140 | |
| 141 | It can also integrate with [[email protected]] when created using [[email protected]_from_cancellable]. |
| 142 | |
| 143 | A [[email protected]] will only ever reject. |
| 144 | |
| 145 | ```c |
| 146 | DexFuture *future = dex_cancellable_new (); |
| 147 | |
| 148 | dex_cancellable_cancel (DEX_CANCELLABLE (future)); |
| 149 | ``` |
| 150 | |
| 151 | ## Timeouts |
| 152 | |
| 153 | A timeout may be represented as a future. |
| 154 | In this case, the timeout will reject after a time period has passed. |
| 155 | |
| 156 | A [[email protected]] will only ever reject. |
| 157 | |
| 158 | This future is implemented on top of [[email protected]] via API like `g_timeout_add()`. |
| 159 | |
| 160 | ```c |
| 161 | DexFuture *future = dex_timeout_new_seconds (60); |
| 162 | ``` |
| 163 | |
| 164 | If you want to bound another future with a timeout, prefer the timeout wrapper |
| 165 | constructors. They pass through the wrapped future's resolved value or |
| 166 | rejection when it completes first, and reject with |
| 167 | [[email protected]_OUT] when the timeout wins. |
| 168 | |
| 169 | ```c |
| 170 | DexFuture *future = dex_future_with_timeout_seconds (query_db_server (), 60); |
| 171 | ``` |
| 172 | |
| 173 | The wrapped future is discarded when the timeout wins. If the underlying work |
| 174 | must continue independently, keep and disown a separate reference before |
| 175 | wrapping or keep that work separate from the timeout-bound result. |
| 176 | |
| 177 | Since: 1.2 |
| 178 | |
| 179 | ## Unix Signals |
| 180 | |
| 181 | Libdex can represent unix signals as a future. |
| 182 | That is to say that the future will resolve to an integer of the signal number when that signal has been raised. |
| 183 | |
| 184 | This is implemented using [func@GLibUnix@signal_source_new] and comes with all the same restrictions. |
| 185 | |
| 186 | ## Delayed |
| 187 | |
| 188 | Sometimes you may run into a case where you want to gate the result of a future until a specific moment. |
| 189 | |
| 190 | For this case, [[email protected]] allows you to wrap another future and decide when to "uncork" the result. |
| 191 | |
| 192 | ```c |
| 193 | DexFuture *delayed = dex_delayed_new (dex_future_new_true ()); |
| 194 | |
| 195 | dex_delayed_release (DEX_DELAYED (delayed)); |
| 196 | ``` |
| 197 | |
| 198 | ## Fibers |
| 199 | |
| 200 | Another type of future is a "fiber". |
| 201 | |
| 202 | More care will be spent on fibers later on but suffice to say that the result of a fiber is easily consumable as a future via [[email protected]]. |
| 203 | |
| 204 | ```c |
| 205 | DexFuture *future = dex_scheduler_spawn (NULL, 0, my_fiber, state, state_free); |
| 206 | ``` |
| 207 | |
| 208 | ## Coroutines |
| 209 | |
| 210 | Another type of future is a stackless "coroutine", [[email protected]]. |
| 211 | |
| 212 | Use [[email protected]_coroutine] to create a coroutine for work that is |
| 213 | mostly waiting on other futures. |
| 214 | |
| 215 | The coroutine function receives a [[email protected]] and must use |
| 216 | the `DEX_COROUTINE_BEGIN` / `DEX_COROUTINE_END` pair together with |
| 217 | `DEX_COROUTINE_SUSPEND_*` macros to yield and resume across waits. |
| 218 | |
| 219 | Coroutines are often useful for lower-overhead state-machine style async code. |
| 220 | See [Coroutines](coroutines.html) for practical guidance. |
| 221 | |
| 222 | # Cancellation Propagation |
| 223 | |
| 224 | Futures within your application will inevitably depend on other futures. |
| 225 | |
| 226 | If all of the futures depending on a future have been released, the dependent future will have the opportunity to cancel itself. |
| 227 | This allows for cascading cancellation so that unnecessary work may be elided. |
| 228 | |
| 229 | You can use [[email protected]] to ensure that a future will continue to be run even if the dependent futures are released. |
| 230 | |
| 231 | # Schedulers |
| 232 | |
| 233 | Libdex requires much processing that needs to be done on the main loop of a thread. |
| 234 | This is generally handled by a [[email protected]]. |
| 235 | |
| 236 | The main thread of an application has the default scheduler which is a [[email protected]]. |
| 237 | |
| 238 | Libdex also has a managed thread pool of schedulers via the [[email protected]]. |
| 239 | |
| 240 | Schedulers manage short tasks, executing [[email protected]] when they are ready, finalizing objects on their owning thread, and running fibers. |
| 241 | |
| 242 | Schedulers integrate with the current threads [[email protected]] via `GSource` making it easy to use Libdex with GTK and Clutter-based applications. |
| 243 | |
| 244 | # Channels |
| 245 | |
| 246 | Channels are a higher-level construct built on futures that allow passing work between producers and consumers. |
| 247 | They are akin to Go channels in that they have a read and a write side. |
| 248 | However, they are much more focused on integrating well with [[email protected]]. |