Debian Codegraph @

src/dex-scheduler.h

raw symbols SCIP
1 /*
2 * dex-scheduler.h
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #pragma once
23
24 #if !defined (DEX_INSIDE) && !defined (DEX_COMPILATION)
25 # error "Only <libdex.h> can be included directly."
26 #endif
27
28 #include "dex-future.h"
29
30 G_BEGIN_DECLS
31
32 #define DEX_TYPE_SCHEDULER (dex_scheduler_get_type())
33 #define DEX_SCHEDULER(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, DEX_TYPE_SCHEDULER, DexScheduler))
34 #define DEX_IS_SCHEDULER(obj) (G_TYPE_CHECK_INSTANCE_TYPE(obj, DEX_TYPE_SCHEDULER))
35
36 typedef struct _DexScheduler DexScheduler;
37 typedef struct _DexCoroutineContext DexCoroutineContext;
38
39 typedef void (*DexSchedulerFunc) (gpointer user_data);
40
41 /**
42 * DexFiberFunc:
43 *
44 * This function prototype is used for spawning fibers. A fiber
45 * is a lightweight, cooperative-multitasking feature where the
46 * fiber is given its own stack. The fiber runs until it reaches
47 * a point of suspension (using [[email protected]] or
48 * similar) or exits the fiber.
49 *
50 * When suspended, the fiber is placed onto a queue until it is
51 * runnable again. Once runnable, the fiber is scheduled to run
52 * from within whatever scheduler it was created with.
53 *
54 * See [[email protected]]
55 *
56 * Returns: (transfer full) (nullable): a [[email protected]] or %NULL
57 */
58 typedef DexFuture *(*DexFiberFunc) (gpointer user_data);
59
60 /**
61 * DexCoroutineFunc:
62 *
63 * This function prototype is used for spawning stackless coroutines.
64 *
65 * A coroutine function receives a [[email protected]] and optional user
66 * data and returns the next future for completion.
67 *
68 * If the function returns %NULL to indicate suspension, it must have stashed the
69 * currently awaited future in `coroutine` using the
70 * `DEX_COROUTINE_SUSPEND_*` helpers.
71 *
72 * Returns: (transfer full) (nullable): a [[email protected]]
73 */
74 typedef DexFuture *(*DexCoroutineFunc) (DexCoroutineContext *context,
75 gpointer user_data);
76
77 DEX_AVAILABLE_IN_ALL
78 GType dex_scheduler_get_type (void);
79 DEX_AVAILABLE_IN_ALL
80 DexScheduler *dex_scheduler_get_thread_default (void);
81 DEX_AVAILABLE_IN_ALL
82 DexScheduler *dex_scheduler_ref_thread_default (void) G_GNUC_WARN_UNUSED_RESULT;
83 DEX_AVAILABLE_IN_ALL
84 DexScheduler *dex_scheduler_get_default (void);
85 DEX_AVAILABLE_IN_ALL
86 GMainContext *dex_scheduler_get_main_context (DexScheduler *scheduler);
87 DEX_AVAILABLE_IN_ALL
88 void dex_scheduler_push (DexScheduler *scheduler,
89 DexSchedulerFunc func,
90 gpointer func_data);
91 DEX_AVAILABLE_IN_ALL
92 DexFuture *dex_scheduler_spawn (DexScheduler *scheduler,
93 gsize stack_size,
94 DexFiberFunc func,
95 gpointer func_data,
96 GDestroyNotify func_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
97 DEX_AVAILABLE_IN_ALL
98 DexFuture *dex_scheduler_spawn_coroutine (DexScheduler *scheduler,
99 DexCoroutineFunc func,
100 gpointer user_data,
101 GDestroyNotify user_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
102 DEX_AVAILABLE_IN_1_2
103 DexFuture *dex_scheduler_spawnv (DexScheduler *scheduler,
104 gsize stack_size,
105 GCallback callback,
106 guint n_params,
107 ...) G_GNUC_WARN_UNUSED_RESULT;
108
109 #if !defined(DEX_DISABLE_STATIC_NAME_MACROS)
110 # define _DEX_FIBER_NEW_(counter, scheduler, stack_size, func, func_data, func_data_destroy) \
111 ({ DexFuture *G_PASTE(__f, counter) = dex_scheduler_spawn ((scheduler), \
112 (stack_size), \
113 (func), \
114 (func_data), \
115 (func_data_destroy)); \
116 dex_future_set_static_name (DEX_FUTURE (G_PASTE (__f, counter)), G_STRINGIFY (func)); \
117 G_PASTE (__f, counter); })
118 # define _DEX_FIBER_NEW(...) _DEX_FIBER_NEW_(__COUNTER__, __VA_ARGS__)
119 # define dex_scheduler_spawn(...) _DEX_FIBER_NEW(__VA_ARGS__)
120
121 # define _DEX_COROUTINE_NEW_(counter, scheduler, func, user_data, user_data_destroy) \
122 ({ DexFuture *G_PASTE(__f, counter) = dex_scheduler_spawn_coroutine ((scheduler), \
123 (func), \
124 (user_data), \
125 (user_data_destroy)); \
126 dex_future_set_static_name (DEX_FUTURE (G_PASTE (__f, counter)), G_STRINGIFY (func)); \
127 G_PASTE (__f, counter); })
128 # define _DEX_COROUTINE_NEW(...) _DEX_COROUTINE_NEW_(__COUNTER__, __VA_ARGS__)
129 # define dex_scheduler_spawn_coroutine(...) _DEX_COROUTINE_NEW(__VA_ARGS__)
130 #endif
131
132 G_DEFINE_AUTOPTR_CLEANUP_FUNC (DexScheduler, dex_unref)
133
134 G_END_DECLS