forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-eloop.c
470 lines (397 loc) · 11 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* 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>
/* Limits */
#define ELOOP_START_STOP_CALLBACKS_MAX 8
/* Static variables
*/
static GThread *eloop_thread;
static GMainContext *eloop_glib_main_context;
static GMainLoop *eloop_glib_main_loop;
static char *eloop_estring = NULL;
static void (*eloop_start_stop_callbacks[ELOOP_START_STOP_CALLBACKS_MAX]) (bool);
static int eloop_start_stop_callbacks_count;
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);
eloop_start_stop_callbacks_count = 0;
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;
}
}
/* Add start/stop callback. This callback is called
* on a event loop thread context, once when event
* loop is started, and second time when it is stopped
*
* Start callbacks are called in the same order as
* they were added. Stop callbacks are called in a
* reverse order
*/
void
eloop_add_start_stop_callback (void (*callback) (bool start))
{
log_assert(NULL,
eloop_start_stop_callbacks_count < ELOOP_START_STOP_CALLBACKS_MAX);
eloop_start_stop_callbacks[eloop_start_stop_callbacks_count] = callback;
eloop_start_stop_callbacks_count ++;
}
/* 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)
{
int i;
(void) data;
G_LOCK(eloop_mutex);
g_main_context_push_thread_default(eloop_glib_main_context);
for (i = 0; i < eloop_start_stop_callbacks_count; i ++) {
eloop_start_stop_callbacks[i](true);
}
g_main_loop_run(eloop_glib_main_loop);
for (i = eloop_start_stop_callbacks_count - 1; i >= 0; i --) {
eloop_start_stop_callbacks[i](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)
{
eloop_thread = g_thread_new("airscan", eloop_thread_func, NULL);
/* 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);
}
/* Timer. Calls user-defined function after a specified
* interval
*/
struct eloop_timer {
GSource *source; /* Underlying GSource */
void (*callback)(void *); /* User callback */
void *data; /* User data */
};
/* eloop_timer callback for GSource
*/
static gboolean
eloop_timer_callback (gpointer data)
{
eloop_timer *timer = data;
timer->callback(timer->data);
eloop_timer_cancel(timer);
return G_SOURCE_REMOVE;
}
/* Create new timer. Timeout is in milliseconds
*/
eloop_timer*
eloop_timer_new (int timeout, void (*callback)(void *), void *data)
{
eloop_timer *timer = g_new0(eloop_timer, 1);
timer->source = g_timeout_source_new(timeout);
timer->callback = callback;
timer->data = data;
g_source_set_priority(timer->source, G_PRIORITY_DEFAULT);
g_source_set_callback(timer->source, eloop_timer_callback, timer, NULL);
g_source_attach(timer->source, eloop_glib_main_context);
return timer;
}
/* Cancel a timer
*
* Caller SHOULD NOT cancel expired timer (timer with called
* callback) -- this is done automatically
*/
void
eloop_timer_cancel (eloop_timer *timer)
{
g_source_destroy(timer->source);
g_source_unref(timer->source);
g_free(timer);
}
/* eloop_fdpoll notifies user when file becomes
* readable, writable or both, depending on its
* event mask
*/
struct eloop_fdpoll {
GSource source; /* Underlying GSource */
int fd; /* Underlying file descriptor */
gpointer fd_tag; /* Returned by g_source_add_unix_fd() */
ELOOP_FDPOLL_MASK mask; /* Mask of active events */
void (*callback)( /* User-defined callback */
int, void*, ELOOP_FDPOLL_MASK);
void *data; /* Callback's data */
};
/* eloop_fdpoll GSource dispatch function
*/
static gboolean
eloop_fdpoll_source_dispatch (GSource *source, GSourceFunc callback,
gpointer data)
{
eloop_fdpoll *fdpoll = (eloop_fdpoll*) source;
guint events = g_source_query_unix_fd(source, fdpoll->fd_tag);
ELOOP_FDPOLL_MASK mask = 0;
(void) callback;
(void) data;
if ((events & G_IO_IN) != 0) {
mask |= ELOOP_FDPOLL_READ;
}
if ((events & G_IO_OUT) != 0) {
mask |= ELOOP_FDPOLL_WRITE;
}
mask &= fdpoll->mask;
if (mask != 0) {
fdpoll->callback(fdpoll->fd, fdpoll->data, mask);
}
return G_SOURCE_CONTINUE;
}
/* Create eloop_fdpoll
*
* Callback will be called, when file will be ready for read/write/both,
* depending on mask
*
* Initial mask value is 0, and it can be changed, using
* eloop_fdpoll_set_mask() function
*/
eloop_fdpoll*
eloop_fdpoll_new (int fd,
void (*callback) (int, void*, ELOOP_FDPOLL_MASK), void *data)
{
eloop_fdpoll *fdpoll;
static GSourceFuncs funcs = {
.dispatch = eloop_fdpoll_source_dispatch
};
fdpoll = (eloop_fdpoll*) g_source_new(&funcs, sizeof(eloop_fdpoll));
fdpoll->fd = fd;
fdpoll->callback = callback;
fdpoll->data = data;
fdpoll->fd_tag = g_source_add_unix_fd(&fdpoll->source, fd, 0);
g_source_attach(&fdpoll->source, eloop_glib_main_context);
return fdpoll;
}
/* Destroy eloop_fdpoll
*/
void
eloop_fdpoll_free (eloop_fdpoll *fdpoll)
{
g_source_destroy(&fdpoll->source);
g_source_unref(&fdpoll->source);
}
/* Set eloop_fdpoll event mask
*/
void
eloop_fdpoll_set_mask (eloop_fdpoll *fdpoll, ELOOP_FDPOLL_MASK mask)
{
if (fdpoll->mask != mask) {
guint events = 0;
if ((mask & ELOOP_FDPOLL_READ) != 0) {
events |= G_IO_IN;
}
if ((mask & ELOOP_FDPOLL_WRITE) != 0) {
events |= G_IO_OUT;
}
fdpoll->mask = mask;
g_source_modify_unix_fd(&fdpoll->source, fdpoll->fd_tag, events);
}
}
/* 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!
*/
error
eloop_eprintf(const char *fmt, ...)
{
gchar *estring;
va_list ap;
log_assert(NULL, g_thread_self() == eloop_thread);
va_start(ap, fmt);
estring = g_strdup_vprintf(fmt, ap);
va_end(ap);
g_free(eloop_estring);
eloop_estring = estring;
return ERROR(estring);
}
/* vim:ts=8:sw=4:et
*/