File: infinite-loop.c

package info (click to toggle)
libdex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,136 kB
  • sloc: ansic: 13,114; asm: 226; makefile: 22; sh: 6; javascript: 5
file content (73 lines) | stat: -rw-r--r-- 2,099 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* infinite-loop.c
 *
 * Copyright 2022 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <libdex.h>

static DexFuture *
infinite_loop_cb (DexFuture *future,
                  gpointer   user_data)
{
  const GValue *value;
  GMainLoop *main_loop = user_data;
  GError *error = NULL;

  if ((value = dex_future_get_value (future, &error)))
    {
      g_print ("\nCaught signal %u, exiting.\n",
               g_value_get_int (value));
      g_main_loop_quit (main_loop);
      return NULL;
    }

  g_print ("Looping ...\n");

  return dex_future_first (dex_timeout_new_seconds (1),
#ifdef G_OS_UNIX
                           dex_unix_signal_new (SIGINT),
#endif
                           NULL);
}

int
main (int   argc,
      char *argv[])
{
  GMainLoop *main_loop;
  DexFuture *loop;

  dex_init ();

  main_loop = g_main_loop_new (NULL, FALSE);
  loop = dex_future_finally_loop (dex_future_first (dex_timeout_new_seconds (1),
#ifdef G_OS_UNIX
                                                    dex_unix_signal_new (SIGINT),
#endif
                                                    NULL),
                                  infinite_loop_cb,
                                  g_main_loop_ref (main_loop),
                                  (GDestroyNotify)g_main_loop_unref);

  g_main_loop_run (main_loop);

  dex_unref (loop);
  g_main_loop_unref (main_loop);

  return 0;
}