static ZTHREAD proc_input_thread (void *arg) { zbar_processor_t *proc = arg; zbar_thread_t *thread = &proc->input_thread; int rc; _zbar_mutex_lock(&proc->mutex); thread->running = 1; _zbar_event_trigger(&thread->activity); zprintf(4, "spawned input thread\n"); rc = 0; while(thread->started && rc >= 0) { _zbar_mutex_unlock(&proc->mutex); rc = _zbar_processor_input_wait(proc, &thread->notify, -1); _zbar_mutex_lock(&proc->mutex); } _zbar_mutex_unlock(&proc->mutex); _zbar_processor_close(proc); _zbar_mutex_lock(&proc->mutex); thread->running = 0; _zbar_event_trigger(&thread->activity); _zbar_mutex_unlock(&proc->mutex); return(0); }
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); _zbar_event_destroy(&thr->notify); _zbar_event_destroy(&thr->activity); } return(0); }
void _zbar_processor_notify (zbar_processor_t *proc, unsigned events) { proc->wait_next = NULL; proc_waiter_t *waiter; for(waiter = proc->wait_head; waiter; waiter = waiter->next) waiter->events = ((waiter->events & ~events) | (events & EVENT_CANCELED)); if(!proc->lock_level) { waiter = proc_waiter_dequeue(proc); if(waiter) _zbar_event_trigger(&waiter->notify); } }
static ZTHREAD proc_video_thread (void *arg) { zbar_processor_t *proc = arg; zbar_thread_t *thread = &proc->video_thread; _zbar_mutex_lock(&proc->mutex); _zbar_thread_init(thread); zprintf(4, "spawned video thread\n"); while(thread->started) { /* wait for video stream to be active */ while(thread->started && !proc->streaming) _zbar_event_wait(&thread->notify, &proc->mutex, NULL); if(!thread->started) break; /* blocking capture image from video */ _zbar_mutex_unlock(&proc->mutex); zbar_image_t *img = zbar_video_next_image(proc->video); _zbar_mutex_lock(&proc->mutex); if(!img && !proc->streaming) continue; else if(!img) /* FIXME could abort streaming and keep running? */ break; /* acquire API lock */ _zbar_processor_lock(proc); _zbar_mutex_unlock(&proc->mutex); if(thread->started && proc->streaming) _zbar_process_image(proc, img); zbar_image_destroy(img); _zbar_mutex_lock(&proc->mutex); /* release API lock */ _zbar_processor_unlock(proc, 0); } thread->running = 0; _zbar_event_trigger(&thread->activity); _zbar_mutex_unlock(&proc->mutex); return(0); }
int _zbar_processor_unlock (zbar_processor_t *proc, int all) { assert(proc->lock_level > 0); assert(_zbar_thread_is_self(proc->lock_owner)); if(all) proc->lock_level = 0; else proc->lock_level--; if(!proc->lock_level) { proc_waiter_t *waiter = proc_waiter_dequeue(proc); if(waiter) _zbar_event_trigger(&waiter->notify); } return(0); }
int zbar_processor_set_active (zbar_processor_t *proc, int active) { proc_enter(proc); int rc; if(!proc->video) { rc = err_capture(proc, SEV_ERROR, ZBAR_ERR_INVALID, __func__, "video input not initialized"); goto done; } _zbar_mutex_unlock(&proc->mutex); zbar_image_scanner_enable_cache(proc->scanner, active); rc = zbar_video_enable(proc->video, active); if(!rc) { _zbar_mutex_lock(&proc->mutex); proc->streaming = active; _zbar_mutex_unlock(&proc->mutex); rc = _zbar_processor_enable(proc); } else err_copy(proc, proc->video); if(!proc->streaming && proc->window) { if(zbar_window_draw(proc->window, NULL) && !rc) rc = err_copy(proc, proc->window); _zbar_processor_invalidate(proc); } _zbar_mutex_lock(&proc->mutex); if(proc->video_thread.started) _zbar_event_trigger(&proc->video_thread.notify); done: proc_leave(proc); return(rc); }
static ZTHREAD vfw_capture_thread (void *arg) { zbar_video_t *vdo = arg; video_state_t *state = vdo->state; zbar_thread_t *thr = &state->thread; state->hwnd = capCreateCaptureWindow(NULL, WS_POPUP, 0, 0, 1, 1, NULL, 0); if(!state->hwnd) goto done; _zbar_mutex_lock(&vdo->qlock); _zbar_thread_init(thr); zprintf(4, "spawned vfw capture thread (thr=%04lx)\n", _zbar_thread_self()); MSG msg; int rc = 0; while(thr->started && rc >= 0 && rc <= 1) { _zbar_mutex_unlock(&vdo->qlock); rc = MsgWaitForMultipleObjects(1, &thr->notify, 0, INFINITE, QS_ALLINPUT); if(rc == 1) while(PeekMessage(&msg, NULL, 0, 0, PM_NOYIELD | PM_REMOVE)) if(rc > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } _zbar_mutex_lock(&vdo->qlock); } done: thr->running = 0; _zbar_event_trigger(&thr->activity); _zbar_mutex_unlock(&vdo->qlock); return(0); }