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 <core/threading/barrier.h>
26 #include <utils/time/clock.h>
27 #include <aspect/vision.h>
28 
29 #include <algorithm>
30 
31 using 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 
41 /** Constructor.
42  * @param clock Clock for timeout handling, system time is used only.
43  */
45 
46 {
47  this->clock = clock;
48 
49  _empty_time = new Time();
50  clock->get_systime(_empty_time);
51 
52  running_threads_cyclic = new ThreadList();
53  running_threads_cont = new ThreadList();
54  waiting_threads = new ThreadList();
55 
56  cyclic_barrier = new Barrier(1);
57 }
58 
59 
60 /** Destructor. */
62 {
63  delete _empty_time;
64  delete cyclic_barrier;
65 
66  delete running_threads_cyclic;
67  delete running_threads_cont;
68  delete waiting_threads;
69 
70 }
71 
72 
73 /** Add a thread in waiting state.
74  * The thread is marked as dependant but it is otherwise ignored.
75  * @param thread thread to add
76  * for access to the YUV422_PLANAR image.
77  */
78 void
80 {
81  waiting_threads->push_back_locked(thread);
82 }
83 
84 
85 /** Mark the thread as running.
86  * @param thread thread to mark as running
87  */
88 void
90 {
91  VisionAspect *vision_thread = dynamic_cast<VisionAspect *>(thread);
92  if ( find(waiting_threads->begin(), waiting_threads->end(), thread) != waiting_threads->end()) {
93  if ( vision_thread->vision_thread_mode() == VisionAspect::CYCLIC ) {
94  running_threads_cyclic->push_back_locked(thread);
95  delete cyclic_barrier;
96  cyclic_barrier = new Barrier( running_threads_cyclic->size() + 1 );
97  } else {
98  running_threads_cont->push_back_locked(thread);
99  }
100  waiting_threads->remove_locked(thread);
101  }
102 }
103 
104 
105 /** Remove a thread.
106  * The thread is removed from all internal structures.
107  * @param thread thread to remove
108  */
109 void
111 {
112  waiting_threads->remove_locked(thread);
113  if ( find(running_threads_cyclic->begin(), running_threads_cyclic->end(), thread) !=
114  running_threads_cyclic->end()) {
115 
116  running_threads_cyclic->remove_locked(thread);
117 
118  delete cyclic_barrier;
119  cyclic_barrier = new Barrier( running_threads_cyclic->size() + 1 );
120  }
121  running_threads_cont->remove_locked(thread);
122  if ( empty() ) {
123  clock->get_systime(_empty_time);
124  }
125 }
126 
127 
128 /** Remove waiting thread.
129  * @param thread thread to remove from waiting structures.
130  */
131 void
133 {
134  waiting_threads->remove_locked(thread);
135  if ( empty() ) {
136  clock->get_systime(_empty_time);
137  }
138 }
139 
140 
141 /** Check if there is at least one cyclic thread.
142  * @return true if there is at least one cyclic thread, false otherwise.
143  */
144 bool
146 {
147  return ( ! running_threads_cyclic->empty() );
148 }
149 
150 
151 /** Check if there is at least one continuous thread.
152  * @return true if there is at least one continuous thread, false otherwise.
153  */
154 bool
156 {
157  return ( ! running_threads_cont->empty() );
158 }
159 
160 
161 /** Check if the given waiting thread is registered.
162  * @param t thread to check for
163  * @return true if the given thread is marked as waiting, false otherwise
164  */
165 bool
167 {
168  return (find(waiting_threads->begin(), waiting_threads->end(), t) != waiting_threads->end());
169 }
170 
171 
172 /** Check if there is no thread at all.
173  * @return true if there is no thread in any of the internal running or waiting
174  * lists, false otherwise
175  */
176 bool
178 {
179  return ( waiting_threads->empty() &&
180  running_threads_cyclic->empty() &&
181  running_threads_cont->empty() );
182 }
183 
184 
185 /** Get the empty time.
186  * @return the time in seconds since the last thread has been removed.
187  */
188 float
190 {
191  return clock->elapsed(_empty_time);
192 }
193 
194 
195 /** Wakeup and wait for all cyclic threads. */
196 void
198 {
199  if ( has_cyclic_thread() ) {
200  running_threads_cyclic->wakeup(cyclic_barrier);
201  cyclic_barrier->wait();
202  }
203 }
204 
205 
206 /** Set prepfin hold fo cyclic threads.
207  * Sets prepfin hold for cyclice threads and rolls back if it cannot
208  * be set for any of the threads.
209  * @param hold prepfin hold value
210  * @exception Exception thrown if setting fails
211  * @see Thread::set_prepfin_hold()
212  * @see ThreadList::set_prepfin_hold()
213  */
214 void
216 {
217  try {
218  running_threads_cyclic->set_prepfin_hold(hold);
219  } catch (Exception &e) {
220  running_threads_cyclic->set_prepfin_hold(false);
221  throw;
222  }
223 }
void wakeup_and_wait_cyclic_threads()
Wakeup and wait for all cyclic threads.
bool has_cont_thread()
Check if there is at least one continuous thread.
Fawkes library namespace.
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:239
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
float elapsed(Time *t) const
How much time has elapsed since t? Calculated as "now - t" in seconds.
Definition: clock.cpp:282
bool has_waiting_thread(fawkes::Thread *t)
Check if the given waiting thread is registered.
FvAqtVisionThreads(fawkes::Clock *clock)
Constructor.
A class for handling time.
Definition: time.h:91
~FvAqtVisionThreads()
Destructor.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void set_prepfin_hold(bool hold)
Set prepfin hold fo cyclic threads.
void set_thread_running(fawkes::Thread *thread)
Mark the thread as running.
void remove_waiting_thread(fawkes::Thread *thread)
Remove waiting thread.
bool has_cyclic_thread()
Check if there is at least one cyclic thread.
bool empty()
Check if there is no thread at all.
List of threads.
Definition: thread_list.h:57
Base class for exceptions in Fawkes.
Definition: exception.h:36
Thread aspect to use in FireVision apps.
Definition: vision.h:35
VisionThreadMode vision_thread_mode()
Get the vision thread mode of this thread.
Definition: vision.cpp:85
void remove_thread(fawkes::Thread *thread)
Remove a thread.
float empty_time()
Get the empty time.
void add_waiting_thread(fawkes::Thread *thread)
Add a thread in waiting state.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32