| 1 | Title: Threads for Blocking APIs |
| 2 | |
| 3 | There are often cases where you need to integrate with blocking APIs that do |
| 4 | not fit well into the async or future-based models of the GNOME ecosystem. |
| 5 | In those cases, you may want to use a dedicated thread for blocking calls so |
| 6 | that you do not disrupt main loops, timeouts, or fiber scheduling. |
| 7 | |
| 8 | # Creating a Dedicated Thread |
| 9 | |
| 10 | Use the [[email protected]_spawn] function to spawn a new thread. |
| 11 | When the thread completes the resulting future will either resolve or reject. |
| 12 | |
| 13 | ```c |
| 14 | typedef DexFuture *(*DexThreadFunc) (gpointer user_data); |
| 15 | |
| 16 | DexFuture *future = dex_thread_spawn ("[my-thread]", thread_func, thread_data, |
| 17 | (GDestroyNotify)thread_data_free); |
| 18 | ``` |
| 19 | |
| 20 | # Waiting for Future Completion |
| 21 | |
| 22 | Since dedicated threads do not have a [[email protected]] on them and are not a fiber, you may not await futures. |
| 23 | Awaiting would suspend a fiber stack but there is no such fiber to suspend. |
| 24 | |
| 25 | To make integration easier, you may use [[email protected]_wait_for] to wait for a future to complete. |
| 26 | The mechanism used in this case is a mutex and condition variable which will be signaled when the dependent future completes. |
| 27 | |
| 28 | # Managing a Fixed Thread Pool |
| 29 | |
| 30 | Use [[email protected]] when you need bounded concurrency for blocking or |
| 31 | foreign work. It is useful when you have a set of operations that can run in |
| 32 | parallel, but you do not want to create an unbounded number of native threads |
| 33 | with [[email protected]_spawn]. |
| 34 | |
| 35 | ```c |
| 36 | DexThreadPool *pool = dex_thread_pool_new (4); |
| 37 | |
| 38 | DexFuture *future = dex_thread_pool_submit (pool, |
| 39 | "[thumbnailer]", |
| 40 | run_blocking_job, |
| 41 | job_data, |
| 42 | job_data_free); |
| 43 | ``` |
| 44 | |
| 45 | The pool creates a fixed number of OS threads up front. Each submission is |
| 46 | queued until a worker becomes available. The returned future resolves when the |
| 47 | job completes, or rejects if the job returns an error future. |
| 48 | |
| 49 | The optional `thread_name` passed to `dex_thread_pool_submit()` is applied to |
| 50 | the returned future, not to the underlying OS thread. Use it to make the |
| 51 | queued work visible in tracing and debugging output without paying the cost of |
| 52 | renaming the worker itself. |
| 53 | |
| 54 | If the pool is shared by multiple workloads and one workload needs a smaller |
| 55 | concurrency budget, submit it through [[email protected]_on_pool]. That |
| 56 | keeps the pool reusable while limiting how many jobs from that workload may |
| 57 | run at once. |
| 58 | |
| 59 | This is a good fit for: |
| 60 | |
| 61 | * blocking filesystem or database work; |
| 62 | * wrapping legacy C libraries that do not integrate with GMainContext; |
| 63 | * limiting concurrency when the external dependency is expensive or not |
| 64 | thread-safe at high fanout. |
| 65 | |
| 66 | Important caveats: |
| 67 | |
| 68 | * Pool workers do not have a [[email protected]], so you cannot `await()` |
| 69 | other Dex futures from inside a worker function. |
| 70 | * The pool is for blocking work only. Do not use it for normal async work that |
| 71 | already integrates with the main loop. |
| 72 | * `dex_thread_pool_close()` is asynchronous. Call it once, keep the returned |
| 73 | future, and await that future to know shutdown is complete. |
| 74 | * Use [[email protected]] to let queued work finish, or |
| 75 | [[email protected]_QUEUED] to reject anything still |
| 76 | waiting in the queue. |
| 77 | * Call `close()` before dropping your last reference if there is any chance the |
| 78 | pool is still active. That avoids finalization waiting for worker threads in |
| 79 | an unexpected place. |
| 80 | |
| 81 | In practice, the pattern is: |
| 82 | |
| 83 | 1. Create the pool with the number of concurrent workers you want. |
| 84 | 2. Submit each blocking task with `dex_thread_pool_submit()`. |
| 85 | 3. Hold onto the returned future if you need the result. |
| 86 | 4. Close the pool with `dex_thread_pool_close()`. |
| 87 | 5. Wait for the close future before letting the pool go out of scope. |