Fawkes API Fawkes Development Version
aqt_vision_threads.cpp
1
2/***************************************************************************
3 * aqt_vision_threads.cpp - FireVision Base Vision Camera Data
4 *
5 * Created: Mon Sep 24 16:16:25 2007
6 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include "aqt_vision_threads.h"
24
25#include <aspect/vision.h>
26#include <core/threading/barrier.h>
27#include <utils/time/clock.h>
28
29#include <algorithm>
30
31using namespace fawkes;
32
33/** @class FvAqtVisionThreads "aqt_vision_threads.h"
34 * Aquisition-dependant threads.
35 * This class is used for managing the vision threads that depend on a
36 * given aquisition thread. Used internally in base vision.
37 * @author Tim Niemueller
38 */
39
40/** Constructor.
41 * @param clock Clock for timeout handling, system time is used only.
42 */
44: cyclic_barrier(new Barrier(1)), clock(clock)
45{
46 clock->get_systime(_empty_time);
47}
48
49/** Destructor. */
51{
52}
53
54/** Add a thread in waiting state.
55 * The thread is marked as dependant but it is otherwise ignored.
56 * @param thread thread to add
57 * for access to the YUV422_PLANAR image.
58 */
59void
61{
62 waiting_threads.push_back_locked(thread);
63}
64
65/** Mark the thread as running.
66 * @param thread thread to mark as running
67 */
68void
70{
71 VisionAspect *vision_thread = dynamic_cast<VisionAspect *>(thread);
72 if (find(waiting_threads.begin(), waiting_threads.end(), thread) != waiting_threads.end()) {
73 if (vision_thread->vision_thread_mode() == VisionAspect::CYCLIC) {
74 running_threads_cyclic.push_back_locked(thread);
75 cyclic_barrier.reset(new Barrier(running_threads_cyclic.size() + 1));
76 } else {
77 running_threads_cont.push_back_locked(thread);
78 }
79 waiting_threads.remove_locked(thread);
80 }
81}
82
83/** Remove a thread.
84 * The thread is removed from all internal structures.
85 * @param thread thread to remove
86 */
87void
89{
90 waiting_threads.remove_locked(thread);
91 if (find(running_threads_cyclic.begin(), running_threads_cyclic.end(), thread)
92 != running_threads_cyclic.end()) {
93 running_threads_cyclic.remove_locked(thread);
94
95 cyclic_barrier.reset(new Barrier(running_threads_cyclic.size() + 1));
96 }
97 running_threads_cont.remove_locked(thread);
98 if (empty()) {
99 clock->get_systime(_empty_time);
100 }
101}
102
103/** Remove waiting thread.
104 * @param thread thread to remove from waiting structures.
105 */
106void
108{
109 waiting_threads.remove_locked(thread);
110 if (empty()) {
111 clock->get_systime(_empty_time);
112 }
113}
114
115/** Check if there is at least one cyclic thread.
116 * @return true if there is at least one cyclic thread, false otherwise.
117 */
118bool
120{
121 return (!running_threads_cyclic.empty());
122}
123
124/** Check if there is at least one continuous thread.
125 * @return true if there is at least one continuous thread, false otherwise.
126 */
127bool
129{
130 return (!running_threads_cont.empty());
131}
132
133/** Check if the given waiting thread is registered.
134 * @param t thread to check for
135 * @return true if the given thread is marked as waiting, false otherwise
136 */
137bool
139{
140 return (find(waiting_threads.begin(), waiting_threads.end(), t) != waiting_threads.end());
141}
142
143/** Check if there is no thread at all.
144 * @return true if there is no thread in any of the internal running or waiting
145 * lists, false otherwise
146 */
147bool
149{
150 return (waiting_threads.empty() && running_threads_cyclic.empty()
151 && running_threads_cont.empty());
152}
153
154/** Get the empty time.
155 * @return the time in seconds since the last thread has been removed.
156 */
157float
159{
160 return clock->elapsed(&_empty_time);
161}
162
163/** Wakeup and wait for all cyclic threads. */
164void
166{
167 if (has_cyclic_thread()) {
168 running_threads_cyclic.wakeup(&*cyclic_barrier);
169 cyclic_barrier->wait();
170 }
171}
172
173/** Set prepfin hold fo cyclic threads.
174 * Sets prepfin hold for cyclice threads and rolls back if it cannot
175 * be set for any of the threads.
176 * @param hold prepfin hold value
177 * @exception Exception thrown if setting fails
178 * @see Thread::set_prepfin_hold()
179 * @see ThreadList::set_prepfin_hold()
180 */
181void
183{
184 try {
185 running_threads_cyclic.set_prepfin_hold(hold);
186 } catch (Exception &e) {
187 running_threads_cyclic.set_prepfin_hold(false);
188 throw;
189 }
190}
~FvAqtVisionThreads()
Destructor.
void add_waiting_thread(fawkes::Thread *thread)
Add a thread in waiting state.
bool has_waiting_thread(fawkes::Thread *t)
Check if the given waiting thread is registered.
void wakeup_and_wait_cyclic_threads()
Wakeup and wait for all cyclic threads.
float empty_time()
Get the empty time.
void remove_waiting_thread(fawkes::Thread *thread)
Remove waiting thread.
void set_prepfin_hold(bool hold)
Set prepfin hold fo cyclic threads.
bool empty()
Check if there is no thread at all.
bool has_cont_thread()
Check if there is at least one continuous thread.
void remove_thread(fawkes::Thread *thread)
Remove a thread.
void set_thread_running(fawkes::Thread *thread)
Mark the thread as running.
FvAqtVisionThreads(fawkes::Clock *clock)
Constructor.
bool has_cyclic_thread()
Check if there is at least one cyclic thread.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:215
float elapsed(Time *t) const
How much time has elapsed since t? Calculated as "now - t" in seconds.
Definition: clock.cpp:254
Base class for exceptions in Fawkes.
Definition: exception.h:36
void remove_locked(Thread *thread)
Remove with lock protection.
void set_prepfin_hold(bool hold)
Set prepfin hold on all threads.
void wakeup()
Wakeup all threads in list.
void push_back_locked(Thread *thread)
Add thread to the end with lock protection.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Thread aspect to use in FireVision apps.
Definition: vision.h:33
VisionThreadMode vision_thread_mode()
Get the vision thread mode of this thread.
Definition: vision.cpp:81
Fawkes library namespace.