Debian Codegraph @

docs/schedulers.md

raw symbols SCIP
1 Title: Schedulers & Fibers
2
3 # Schedulers
4
5 The [[email protected]] is responsible for running work items on a thread.
6 This is performed by integrating with the threads [[email protected]].
7
8 The main thread of your application will have a [[email protected]] as the assigned scheduler.
9
10 The scheduler manages callbacks such as work items created with [[email protected]].
11
12 You can get the default scheduler for the application's main thread using [[email protected]_default].
13
14 The thread's scheduler can be retrieved with [[email protected]_thread_default].
15
16 # Thread Pool Scheduling
17
18 Libdex manages a thread pool which may be retrieved using [[email protected]_default].
19
20 The thread pool scheduler will manage a number of threads that is deemed useful based on the number of CPU available.
21
22 Work items created from outside of the thread pool are placed into a global queue.
23 Thread pool workers will take items from the global queue when they have no more items to process.
24
25 All thread pool workers have a local [[email protected]] so use of timeouts and other [[email protected]] features continue to work.
26
27 If you need to interact with long-blocking API calls it is better to use [[email protected]_spawn] rather than a thread pool thread.
28
29 Thread pool workers use a work-stealing wait-free queue which allows the worker to push work items onto one side of the queue quickly.
30 Doing so also helps improve cacheline effectiveness.
31
32 Workers sleep in their main context when they have no work. When a worker
33 publishes enough local work for another worker to help, it sends a directed
34 request to one peer. Requests to a peer are coalesced, and a successful steal
35 may wake one additional peer while useful backlog remains. This allows the pool
36 to increase parallelism gradually without waking every worker for one item.
37 An already-busy peer may retain a request until it drains its own local work,
38 and a thief continues while any victim observed later in its scan has backlog.
39
40 # Fibers
41
42 Fibers are a type of stackful co-routine.
43 A new stack is created and a trampoline is performed onto the stack from the current thread.
44
45 Use [[email protected]] to create a new fiber.
46
47 When a fiber calls a [[email protected]], similar function, or returns the fiber is suspended and execution returns to the scheduler.
48
49 By default, fibers have a 128-kb stack with a guard page at the end.
50 Fiber stacks are pooled so that they may be reused during heavy use.
51
52 Fibers are a [[email protected]] which means you can await the completion of a fiber just like any other future.
53
54 Note that fibers are pinned to a scheduler.
55 They will not be migrated between schedulers even when a thread pool is in use.
56
57 # Stackless Coroutines
58
59 Coroutines are a stackless alternative that are still futures managed by the same
60 [[email protected]]. Use [[email protected]_coroutine] to create them.
61
62 A coroutine function is given a [[email protected]] and is expected to
63 return a [[email protected]] or suspend by returning `NULL`.
64
65 Stackless coroutines generally have lower memory overhead than fibers when you are
66 mainly waiting on futures between steps.
67
68 Use `DEX_COROUTINE_BEGIN` and `DEX_COROUTINE_END` when writing
69 coroutines. Suspend with one of the `DEX_COROUTINE_SUSPEND_*` helpers.
70 See [Coroutines](coroutines.html) for more guidance.
71
72 ## Cancellation
73
74 Fibers may be cancelled if the fiber has been discarded by all futures awaiting completion.
75 Fibers will always exit through a natural exit point such as a pending "await".
76 All attempts to await will reject with error once a fiber has been cancelled.
77
78 If you want to ignore cancellation of fibers, use [[email protected]] on the fiber after creation.