summaryrefslogtreecommitdiff
path: root/gtk/gio-coroutine.c
blob: 355c75c359023ea612cbc2d2c3161a4795fef237 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2010 Red Hat, Inc.
   Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
   Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.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.0 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 Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
*/

#include "gio-coroutine.h"

typedef struct _GConditionWaitSource
{
    GCoroutine *self;
    GSource src;
    GConditionWaitFunc func;
    gpointer data;
} GConditionWaitSource;

GCoroutine* g_coroutine_self(void)
{
    return (GCoroutine*)coroutine_self();
}

/* Main loop helper functions */
static gboolean g_io_wait_helper(GSocket *sock G_GNUC_UNUSED,
				 GIOCondition cond,
				 gpointer data)
{
    struct coroutine *to = data;
    coroutine_yieldto(to, &cond);
    return FALSE;
}

GIOCondition g_coroutine_socket_wait(GCoroutine *self,
                                     GSocket *sock,
                                     GIOCondition cond)
{
    GIOCondition *ret, val = 0;
    GSource *src;

    g_return_val_if_fail(self != NULL, 0);
    g_return_val_if_fail(self->wait_id == 0, 0);
    g_return_val_if_fail(sock != NULL, 0);

    src = g_socket_create_source(sock, cond | G_IO_HUP | G_IO_ERR | G_IO_NVAL, NULL);
    g_source_set_callback(src, (GSourceFunc)g_io_wait_helper, self, NULL);
    self->wait_id = g_source_attach(src, NULL);
    ret = coroutine_yield(NULL);
    g_source_unref(src);

    if (ret != NULL)
        val = *ret;
    else
        g_source_remove(self->wait_id);

    self->wait_id = 0;
    return val;
}

void g_coroutine_condition_cancel(GCoroutine *coroutine)
{
    g_return_if_fail(coroutine != NULL);

    if (coroutine->condition_id == 0)
        return;

    g_source_remove(coroutine->condition_id);
    coroutine->condition_id = 0;
}

void g_coroutine_wakeup(GCoroutine *coroutine)
{
    g_return_if_fail(coroutine != NULL);
    g_return_if_fail(coroutine != g_coroutine_self());

    if (coroutine->wait_id)
        coroutine_yieldto(&coroutine->coroutine, NULL);
}

/*
 * Call immediately before the main loop does an iteration. Returns
 * true if the condition we're checking is ready for dispatch
 */
static gboolean g_condition_wait_prepare(GSource *src,
					 int *timeout) {
    GConditionWaitSource *vsrc = (GConditionWaitSource *)src;
    *timeout = -1;
    return vsrc->func(vsrc->data);
}

/*
 * Call immediately after the main loop does an iteration. Returns
 * true if the condition we're checking is ready for dispatch
 */
static gboolean g_condition_wait_check(GSource *src)
{
    GConditionWaitSource *vsrc = (GConditionWaitSource *)src;
    return vsrc->func(vsrc->data);
}

static gboolean g_condition_wait_dispatch(GSource *src G_GNUC_UNUSED,
					  GSourceFunc cb,
					  gpointer data) {
    return cb(data);
}

GSourceFuncs waitFuncs = {
    .prepare = g_condition_wait_prepare,
    .check = g_condition_wait_check,
    .dispatch = g_condition_wait_dispatch,
};

static gboolean g_condition_wait_helper(gpointer data)
{
    GCoroutine *self = (GCoroutine *)data;
    coroutine_yieldto(&self->coroutine, NULL);
    return FALSE;
}

/*
 * g_coroutine_condition_wait:
 * @coroutine: the coroutine to wait on
 * @func: the condition callback
 * @data: the user data passed to @func callback
 *
 * This function will wait on caller coroutine until @func returns %TRUE.
 *
 * @func is called when entering the main loop from the main context (coroutine).
 *
 * The condition can be cancelled by calling g_coroutine_wakeup()
 *
 * Returns: %TRUE if condition reached, %FALSE if not and cancelled
 */
gboolean g_coroutine_condition_wait(GCoroutine *self, GConditionWaitFunc func, gpointer data)
{
    GSource *src;
    GConditionWaitSource *vsrc;

    g_return_val_if_fail(self != NULL, FALSE);
    g_return_val_if_fail(self->condition_id == 0, FALSE);
    g_return_val_if_fail(func != NULL, FALSE);

    /* Short-circuit check in case we've got it ahead of time */
    if (func(data))
        return TRUE;

    /*
     * Don't have it, so yield to the main loop, checking the condition
     * on each iteration of the main loop
     */
    src = g_source_new(&waitFuncs, sizeof(GConditionWaitSource));
    vsrc = (GConditionWaitSource *)src;

    vsrc->func = func;
    vsrc->data = data;
    vsrc->self = self;

    self->condition_id = g_source_attach(src, NULL);
    g_source_set_callback(src, g_condition_wait_helper, self, NULL);
    coroutine_yield(NULL);
    g_source_unref(src);

    /* it got woked up / cancelled? */
    if (self->condition_id == 0)
        return func(data);

    self->condition_id = 0;
    return TRUE;
}

struct signal_data
{
    GObject *object;
    struct coroutine *caller;
    int signum;
    gpointer params;
    GSignalEmitMainFunc func;
    const char *debug_info;
    gboolean notified;
};

static gboolean emit_main_context(gpointer opaque)
{
    struct signal_data *signal = opaque;

    signal->func(signal->object, signal->signum, signal->params);
    signal->notified = TRUE;

    coroutine_yieldto(signal->caller, NULL);

    return FALSE;
}

/* coroutine -> main context */
void g_signal_emit_main_context(GObject *object,
                                GSignalEmitMainFunc emit_main_func,
                                int signum,
                                gpointer params,
                                const char *debug_info)
{
    struct signal_data data;

    g_return_if_fail(coroutine_self()->caller);

    data.object = object;
    data.caller = coroutine_self();
    data.signum = signum;
    data.params = params;
    data.func = emit_main_func;
    data.debug_info = debug_info;
    data.notified = FALSE;
    g_idle_add(emit_main_context, &data);

    /* This switches to the system coroutine context, lets
     * the idle function run to dispatch the signal, and
     * finally returns once complete. ie this is synchronous
     * from the POV of the coroutine despite there being
     * an idle function involved
     */
    coroutine_yield(NULL);
    g_warn_if_fail(data.notified);
}

static gboolean notify_main_context(gpointer opaque)
{
    struct signal_data *signal = opaque;

    g_object_notify(signal->object, signal->params);
    signal->notified = TRUE;

    coroutine_yieldto(signal->caller, NULL);

    return FALSE;
}

/* coroutine -> main context */
void g_object_notify_main_context(GObject *object,
                                  const gchar *property_name)
{
    struct signal_data data;

    g_return_if_fail(coroutine_self()->caller);

    data.object = object;
    data.caller = coroutine_self();
    data.params = (gpointer)property_name;
    data.notified = FALSE;

    g_idle_add(notify_main_context, &data);

    /* This switches to the system coroutine context, lets
     * the idle function run to dispatch the signal, and
     * finally returns once complete. ie this is synchronous
     * from the POV of the coroutine despite there being
     * an idle function involved
     */
    coroutine_yield(NULL);
    g_warn_if_fail(data.notified);
}