Debian Codegraph @

src/dex-cancellable.c

raw symbols SCIP
1 /*
2 * dex-cancellable.c
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 #include "config.h"
23
24 #include <gio/gio.h>
25
26 #include "dex-cancellable.h"
27 #include "dex-future-private.h"
28
29 /**
30 * DexCancellable:
31 *
32 * `DexCancellable` is a simple cancellation primitive which allows
33 * for you to create [[email protected]] that will reject upon cancellation.
34 *
35 * Use this combined with other futures using [[email protected]_race]
36 * to create a future that resolves when all other futures complete
37 * or [[email protected]] is called to reject.
38 */
39
40 typedef struct _DexCancellable
41 {
42 DexFuture parent_instance;
43 GCancellable *cancellable;
44 gulong handler;
45 } DexCancellable;
46
47 typedef struct _DexCancellableClass
48 {
49 DexFutureClass parent_class;
50 } DexCancellableClass;
51
52 DEX_DEFINE_FINAL_TYPE (DexCancellable, dex_cancellable, DEX_TYPE_FUTURE)
53
54 static void
55 dex_cancellable_finalize (DexObject *object)
56 {
57 DexCancellable *self = (DexCancellable *)object;
58
59 if (self->handler)
60 g_cancellable_disconnect (self->cancellable, self->handler);
61
62 g_clear_object (&self->cancellable);
63
64 DEX_OBJECT_CLASS (dex_cancellable_parent_class)->finalize (object);
65 }
66
67 static void
68 dex_cancellable_class_init (DexCancellableClass *cancellable_class)
69 {
70 DexObjectClass *object_class = DEX_OBJECT_CLASS (cancellable_class);
71
72 object_class->finalize = dex_cancellable_finalize;
73 }
74
75 static void
76 dex_cancellable_init (DexCancellable *cancellable)
77 {
78 }
79
80 /**
81 * dex_cancellable_new:
82 *
83 * Create a new cancellable
84 *
85 * Returns: (transfer full):
86 */
87 DexCancellable *
88 dex_cancellable_new (void)
89 {
90 return (DexCancellable *)dex_object_create_instance (DEX_TYPE_CANCELLABLE);
91 }
92
93 static void
94 dex_cancellable_cancelled_cb (GCancellable *cancellable,
95 DexWeakRef *wr)
96 {
97 DexCancellable *self;
98
99 g_assert (G_IS_CANCELLABLE (cancellable));
100 g_assert (wr != NULL);
101
102 if ((self = dex_weak_ref_get (wr)))
103 {
104 self->handler = 0;
105 dex_future_complete (DEX_FUTURE (self),
106 NULL,
107 g_error_new_literal (G_IO_ERROR,
108 G_IO_ERROR_CANCELLED,
109 "Operation cancelled"));
110 dex_clear (&self);
111 }
112 }
113
114 static void
115 weak_ref_free (gpointer data)
116 {
117 dex_weak_ref_clear (data);
118 g_free (data);
119 }
120
121 /**
122 * dex_cancellable_new_from_cancellable:
123 * @cancellable: a [[email protected]]
124 *
125 * Creates a new [[email protected]] that will reject
126 * when @cancellable is cancelled or
127 * [[email protected]] is called.
128 *
129 * Returns: (transfer full):
130 */
131 DexFuture *
132 dex_cancellable_new_from_cancellable (GCancellable *cancellable)
133 {
134 DexWeakRef *wr;
135 DexCancellable *ret;
136
137 g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), NULL);
138
139 ret = dex_cancellable_new ();
140
141 wr = g_new0 (DexWeakRef, 1);
142 dex_weak_ref_init (wr, ret);
143
144 ret->cancellable = g_object_ref (cancellable);
145 ret->handler = g_cancellable_connect (cancellable,
146 G_CALLBACK (dex_cancellable_cancelled_cb),
147 wr, weak_ref_free);
148
149 return DEX_FUTURE (ret);
150 }
151
152 /**
153 * dex_cancellable_cancel:
154 * @cancellable: a [[email protected]]
155 *
156 * Rejects @cancellable.
157 *
158 * Any future that is dependent on this cancellable will be notified
159 * of the rejection. For some futures, that may cause them to also
160 * reject or resolve.
161 */
162 void
163 dex_cancellable_cancel (DexCancellable *cancellable)
164 {
165 g_return_if_fail (DEX_IS_CANCELLABLE (cancellable));
166
167 dex_future_complete (DEX_FUTURE (cancellable),
168 NULL,
169 g_error_new_literal (G_IO_ERROR,
170 G_IO_ERROR_CANCELLED,
171 "Operation cancelled"));
172 }