summaryrefslogtreecommitdiffstats
path: root/zbar/processor/posix.c
blob: 06b8d9a45ab4b7b39a736b26730cdba0601be49d (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
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
/*------------------------------------------------------------------------
 *  Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader 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 Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "posix.h"
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include "processor.h"

static inline int proc_sleep(int timeout)
{
    assert(timeout > 0);
    struct timespec sleepns, remns;
    sleepns.tv_sec  = timeout / 1000;
    sleepns.tv_nsec = (timeout % 1000) * 1000000;
    while (nanosleep(&sleepns, &remns) && errno == EINTR)
	sleepns = remns;
    return (1);
}

int _zbar_event_init(zbar_event_t *event)
{
    event->state  = 0;
    event->pollfd = -1;
#ifdef HAVE_LIBPTHREAD
    pthread_cond_init(&event->cond, NULL);
#endif
    return (0);
}

void _zbar_event_destroy(zbar_event_t *event)
{
    event->state  = -1;
    event->pollfd = -1;
#ifdef HAVE_LIBPTHREAD
    pthread_cond_destroy(&event->cond);
#endif
}

/* lock must be held */
void _zbar_event_trigger(zbar_event_t *event)
{
    event->state = 1;
#ifdef HAVE_LIBPTHREAD
    pthread_cond_broadcast(&event->cond);
#endif
    if (event->pollfd >= 0) {
	unsigned i = 0; /* unused */
	if (write(event->pollfd, &i, sizeof(unsigned)) < 0)
	    perror("");
	event->pollfd = -1;
    }
}

#ifdef HAVE_LIBPTHREAD

/* lock must be held */
int _zbar_event_wait(zbar_event_t *event, zbar_mutex_t *lock,
		     zbar_timer_t *timeout)
{
    int rc = 0;
    while (!rc && !event->state) {
	if (!timeout)
	    rc = pthread_cond_wait(&event->cond, lock);
	else {
	    struct timespec *timer;
#if _POSIX_TIMERS > 0
	    timer = timeout;
#else
	    struct timespec tmp;
	    tmp.tv_sec	= timeout->tv_sec;
	    tmp.tv_nsec = timeout->tv_usec * 1000;
	    timer	= &tmp;
#endif
	    rc = pthread_cond_timedwait(&event->cond, lock, timer);
	}
    }

    /* consume/reset event */
    event->state = 0;

    if (!rc)
	return (1); /* got event */
    if (rc == ETIMEDOUT)
	return (0); /* timed out */
    return (-1);    /* error (FIXME save info) */
}

int _zbar_thread_start(zbar_thread_t *thr, zbar_thread_proc_t *proc, void *arg,
		       zbar_mutex_t *lock)
{
    if (thr->started || thr->running)
	return (-1 /*FIXME*/);
    thr->started = 1;
    _zbar_event_init(&thr->notify);
    _zbar_event_init(&thr->activity);

    int rc = 0;
    _zbar_mutex_lock(lock);
    if (pthread_create(&thr->tid, NULL, proc, arg) ||
	_zbar_event_wait(&thr->activity, lock, NULL) < 0 || !thr->running) {
	thr->started = 0;
	_zbar_event_destroy(&thr->notify);
	_zbar_event_destroy(&thr->activity);
	/*rc = err_capture_num(proc, SEV_ERROR, ZBAR_ERR_SYSTEM,
          __func__, "spawning thread", rc);*/
	rc = -1 /*FIXME*/;
    }
    _zbar_mutex_unlock(lock);
    return (rc);
}

int _zbar_thread_stop(zbar_thread_t *thr, zbar_mutex_t *lock)
{
    if (thr->started) {
	thr->started = 0;
	_zbar_event_trigger(&thr->notify);
	while (thr->running)
	    /* FIXME time out and abandon? */
	    _zbar_event_wait(&thr->activity, lock, NULL);
	pthread_join(thr->tid, NULL);
	_zbar_event_destroy(&thr->notify);
	_zbar_event_destroy(&thr->activity);
    }
    return (0);
}

#else

int _zbar_event_wait(zbar_event_t *event, zbar_mutex_t *lock,
		     zbar_timer_t *timeout)
{
    int rc = !event->state;
    if (rc) {
	if (!timeout)
	    /* FIXME was that error or hang? */
	    return (-1);

	int sleep = _zbar_timer_check(timeout);
	if (sleep)
	    proc_sleep(sleep);
    }

    rc = !event->state;

    /* consume/reset event */
    event->state = 0;

    return (rc);
}

#endif

/* used by poll interface.  lock is already held */
static int proc_video_handler(zbar_processor_t *proc, int i)
{
    _zbar_mutex_lock(&proc->mutex);
    _zbar_processor_lock(proc);
    _zbar_mutex_unlock(&proc->mutex);

    zbar_image_t *img = NULL;
    if (proc->streaming) {
	/* not expected to block */
	img = zbar_video_next_image(proc->video);
	if (img)
	    _zbar_process_image(proc, img);
    }

    _zbar_mutex_lock(&proc->mutex);
    _zbar_processor_unlock(proc, 0);
    _zbar_mutex_unlock(&proc->mutex);
    if (img)
	zbar_image_destroy(img);
    return (0);
}

static inline void proc_cache_polling(processor_state_t *state)
{
    /* make a thread-local copy of polling data */
    int n = state->thr_polling.num = state->polling.num;
    alloc_polls(&state->thr_polling);
    memcpy(state->thr_polling.fds, state->polling.fds,
	   n * sizeof(struct pollfd));
    memcpy(state->thr_polling.handlers, state->polling.handlers,
	   n * sizeof(poll_handler_t *));
}

static int proc_kick_handler(zbar_processor_t *proc, int i)
{
    processor_state_t *state = proc->state;
    zprintf(5, "kicking %d fds\n", state->polling.num);

    unsigned junk[2];
    int rc = read(state->kick_fds[0], junk, 2 * sizeof(unsigned));

    assert(proc->threaded);
    _zbar_mutex_lock(&proc->mutex);
    proc_cache_polling(proc->state);
    _zbar_mutex_unlock(&proc->mutex);
    return (rc);
}

static inline int proc_poll_inputs(zbar_processor_t *proc, int timeout)
{
    processor_state_t *state = proc->state;
    if (state->pre_poll_handler)
	state->pre_poll_handler(proc, -1);

    poll_desc_t *p = &state->thr_polling;
    assert(p->num);
    int rc = poll(p->fds, p->num, timeout);
    if (rc <= 0)
	/* FIXME detect and handle fatal errors (somehow) */
	return (rc);
    int i;
    for (i = p->num - 1; i >= 0; i--)
	if (p->fds[i].revents) {
	    if (p->handlers[i])
		p->handlers[i](proc, i);
	    p->fds[i].revents = 0; /* debug */
	    rc--;
	}
    assert(!rc);
    return (1);
}

int _zbar_processor_input_wait(zbar_processor_t *proc, zbar_event_t *event,
			       int timeout)
{
    processor_state_t *state = proc->state;
    if (state->thr_polling.num) {
	if (event) {
	    _zbar_mutex_lock(&proc->mutex);
	    event->pollfd = state->kick_fds[1];
	    _zbar_mutex_unlock(&proc->mutex);
	}
	return (proc_poll_inputs(proc, timeout));
    } else if (timeout)
	return (proc_sleep(timeout));
    return (-1);
}

int _zbar_processor_init(zbar_processor_t *proc)
{
    processor_state_t *state = proc->state =
	calloc(1, sizeof(processor_state_t));
    state->kick_fds[0] = state->kick_fds[1] = -1;

    if (proc->threaded) {
	/* FIXME check errors */
	if (pipe(state->kick_fds))
	    return (err_capture(proc, SEV_FATAL, ZBAR_ERR_SYSTEM, __func__,
				"failed to open pipe"));
	add_poll(proc, state->kick_fds[0], proc_kick_handler);
	proc_cache_polling(proc->state);
    }
    return (0);
}

int _zbar_processor_cleanup(zbar_processor_t *proc)
{
    processor_state_t *state = proc->state;
    if (proc->threaded) {
	close(state->kick_fds[0]);
	close(state->kick_fds[1]);
	state->kick_fds[0] = state->kick_fds[1] = -1;
    }
    if (state->polling.fds) {
	free(state->polling.fds);
	state->polling.fds = NULL;
	if (!proc->threaded)
	    state->thr_polling.fds = NULL;
    }
    if (state->polling.handlers) {
	free(state->polling.handlers);
	state->polling.handlers = NULL;
	if (!proc->threaded)
	    state->thr_polling.handlers = NULL;
    }
    if (state->thr_polling.fds) {
	free(state->thr_polling.fds);
	state->thr_polling.fds = NULL;
    }
    if (state->thr_polling.handlers) {
	free(state->thr_polling.handlers);
	state->thr_polling.handlers = NULL;
    }
    free(proc->state);
    proc->state = NULL;
    return (0);
}

int _zbar_processor_enable(zbar_processor_t *proc)
{
    int vid_fd = zbar_video_get_fd(proc->video);
    if (vid_fd < 0)
	return (0);

    if (proc->streaming)
	add_poll(proc, vid_fd, proc_video_handler);
    else
	remove_poll(proc, vid_fd);
    /* FIXME failure recovery? */
    return (0);
}