forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairscan-eloop.c
281 lines (241 loc) · 6.23 KB
/
airscan-eloop.c
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
273
274
275
276
277
278
279
280
281
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Event loop (runs in separate thread)
*/
#include "airscan.h"
#include <glib-unix.h>
/* Static variables
*/
static GThread *eloop_thread;
static GMainContext *eloop_glib_main_context;
static GMainLoop *eloop_glib_main_loop;
static char *eloop_estring = NULL;
G_LOCK_DEFINE_STATIC(eloop_mutex);
/* Forward declarations
*/
static gint
glib_poll_hook (GPollFD *ufds, guint nfsd, gint timeout);
/* Initialize event loop
*/
SANE_Status
eloop_init (void)
{
eloop_glib_main_context = g_main_context_new();
eloop_glib_main_loop = g_main_loop_new(eloop_glib_main_context, FALSE);
g_main_context_set_poll_func(eloop_glib_main_context, glib_poll_hook);
return SANE_STATUS_GOOD;
}
/* Cleanup event loop
*/
void
eloop_cleanup (void)
{
if (eloop_glib_main_context != NULL) {
g_main_loop_unref(eloop_glib_main_loop);
eloop_glib_main_loop = NULL;
g_main_context_unref(eloop_glib_main_context);
eloop_glib_main_context = NULL;
g_free(eloop_estring);
eloop_estring = NULL;
}
}
/* Poll function hook
*/
static gint
glib_poll_hook (GPollFD *ufds, guint nfds, gint timeout)
{
G_UNLOCK(eloop_mutex);
gint ret = g_poll(ufds, nfds, timeout);
G_LOCK(eloop_mutex);
return ret;
}
/* Event loop thread main function
*/
static gpointer
eloop_thread_func (gpointer data)
{
void (*callback)(bool) = data;
G_LOCK(eloop_mutex);
g_main_context_push_thread_default(eloop_glib_main_context);
callback(true);
g_main_loop_run(eloop_glib_main_loop);
callback(false);
G_UNLOCK(eloop_mutex);
return NULL;
}
/* Start event loop thread.
*
* Callback is called from the thread context twice:
* callback(true) - when thread is started
* callback(false) - when thread is about to exit
*/
void
eloop_thread_start (void (*callback)(bool))
{
eloop_thread = g_thread_new("airscan", eloop_thread_func, callback);
/* Wait until thread is started. Otherwise, g_main_loop_quit()
* might not terminate the thread
*/
gulong usec = 100;
while (!g_main_loop_is_running(eloop_glib_main_loop)) {
g_usleep(usec);
usec += usec;
}
}
/* Stop event loop thread and wait until its termination
*/
void
eloop_thread_stop (void)
{
if (eloop_thread != NULL) {
g_main_loop_quit(eloop_glib_main_loop);
g_thread_join(eloop_thread);
eloop_thread = NULL;
}
}
/* Acquire event loop mutex
*/
void
eloop_mutex_lock (void)
{
G_LOCK(eloop_mutex);
}
/* Release event loop mutex
*/
void
eloop_mutex_unlock (void)
{
G_UNLOCK(eloop_mutex);
}
/* Wait on conditional variable under the event loop mutex
*/
void
eloop_cond_wait (GCond *cond)
{
return g_cond_wait(cond, &G_LOCK_NAME(eloop_mutex));
}
/* eloop_cond_wait() with timeout
*/
bool
eloop_cond_wait_until (GCond *cond, gint64 timeout)
{
return g_cond_wait_until(cond, &G_LOCK_NAME(eloop_mutex), timeout);
}
/* Create AvahiGLibPoll that runs in context of the event loop
*/
AvahiGLibPoll*
eloop_new_avahi_poll (void)
{
return avahi_glib_poll_new(eloop_glib_main_context, G_PRIORITY_DEFAULT);
}
/* Call function on a context of event loop thread
*/
void
eloop_call (GSourceFunc func, gpointer data)
{
GSource *source = g_idle_source_new ();
g_source_set_priority(source, G_PRIORITY_DEFAULT);
g_source_set_callback(source, func, data, NULL);
g_source_attach(source, eloop_glib_main_context);
g_source_unref(source);
}
/* Event notifier. Calls user-defined function on a context
* of event loop thread, when event is triggered. This is
* safe to trigger the event from a context of any thread
* or even from a signal handler
*/
struct eloop_event {
GSource source; /* Underlying GSource */
pollable *p; /* Underlying pollable event */
void (*callback)(void*); /* user-defined callback */
void *data; /* callback's argument */
};
/* eloop_event GSource callback
*/
gboolean
eloop_event_callback (gpointer data)
{
eloop_event *event = data;
pollable_reset(event->p);
event->callback(event->data);
return G_SOURCE_CONTINUE;
}
/* eloop_event source dispatch function
*/
static gboolean
eloop_event_source_dispatch (GSource *source, GSourceFunc callback,
gpointer data)
{
(void) source;
return callback(data);
}
/* Create new event notifier. May return NULL
*/
eloop_event*
eloop_event_new (void (*callback)(void *), void *data)
{
eloop_event *event;
pollable *p;
static GSourceFuncs funcs = {
.dispatch = eloop_event_source_dispatch,
};
p = pollable_new();
if (p == NULL) {
return NULL;
}
event = (eloop_event*) g_source_new(&funcs, sizeof(eloop_event));
event->p = p;
event->callback = callback;
event->data = data;
g_source_add_unix_fd(&event->source, pollable_get_fd(event->p), G_IO_IN);
g_source_set_callback(&event->source, eloop_event_callback, event, NULL);
g_source_attach(&event->source, eloop_glib_main_context);
return event;
}
/* Destroy event notifier
*/
void
eloop_event_free (eloop_event *event)
{
g_source_destroy(&event->source);
pollable_free(event->p);
g_source_unref(&event->source);
}
/* Trigger an event
*/
void
eloop_event_trigger (eloop_event *event)
{
pollable_signal(event->p);
}
/* Format error string, as printf() does and save result
* in the memory, owned by the event loop
*
* Caller should not free returned string. This is safe
* to use the returned string as an argument to the
* subsequent eloop_eprintf() call.
*
* The returned string remains valid until next call
* to eloop_eprintf(), which makes it usable to
* report errors up by the stack. However, it should
* not be assumed, that the string will remain valid
* on a next eloop roll, so don't save this string
* anywhere, if you need to do so, create a copy!
*/
const char*
eloop_eprintf(const char *fmt, ...)
{
gchar *estring;
va_list ap;
va_start(ap, fmt);
estring = g_strdup_vprintf(fmt, ap);
va_end(ap);
g_free(eloop_estring);
eloop_estring = estring;
return estring;
}
/* vim:ts=8:sw=4:et
*/