Debian Codegraph @

examples/infinite-loop.c

raw symbols SCIP
1 /* infinite-loop.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include <libdex.h>
22
23 static DexFuture *
24 infinite_loop_cb (DexFuture *future,
25 gpointer user_data)
26 {
27 const GValue *value;
28 GMainLoop *main_loop = user_data;
29 GError *error = NULL;
30
31 if ((value = dex_future_get_value (future, &error)))
32 {
33 g_print ("\nCaught signal %u, exiting.\n",
34 g_value_get_int (value));
35 g_main_loop_quit (main_loop);
36 return NULL;
37 }
38
39 g_print ("Looping ...\n");
40
41 return dex_future_first (dex_timeout_new_seconds (1),
42 #ifdef G_OS_UNIX
43 dex_unix_signal_new (SIGINT),
44 #endif
45 NULL);
46 }
47
48 int
49 main (int argc,
50 char *argv[])
51 {
52 GMainLoop *main_loop;
53 DexFuture *loop;
54
55 dex_init ();
56
57 main_loop = g_main_loop_new (NULL, FALSE);
58 loop = dex_future_finally_loop (dex_future_first (dex_timeout_new_seconds (1),
59 #ifdef G_OS_UNIX
60 dex_unix_signal_new (SIGINT),
61 #endif
62 NULL),
63 infinite_loop_cb,
64 g_main_loop_ref (main_loop),
65 (GDestroyNotify)g_main_loop_unref);
66
67 g_main_loop_run (main_loop);
68
69 dex_unref (loop);
70 g_main_loop_unref (main_loop);
71
72 return 0;
73 }