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 
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 /** 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  */
59 void
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  */
68 void
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  */
87 void
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  */
106 void
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  */
118 bool
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  */
127 bool
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  */
137 bool
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  */
147 bool
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  */
157 float
159 {
160  return clock->elapsed(&_empty_time);
161 }
162 
163 /** Wakeup and wait for all cyclic threads. */
164 void
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  */
181 void
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 }
fawkes::ThreadList::wakeup
void wakeup()
Wakeup all threads in list.
Definition: thread_list.cpp:163
FvAqtVisionThreads::has_waiting_thread
bool has_waiting_thread(fawkes::Thread *t)
Check if the given waiting thread is registered.
Definition: aqt_vision_threads.cpp:137
fawkes::VisionAspect::vision_thread_mode
VisionThreadMode vision_thread_mode()
Get the vision thread mode of this thread.
Definition: vision.cpp:85
FvAqtVisionThreads::~FvAqtVisionThreads
~FvAqtVisionThreads()
Destructor.
Definition: aqt_vision_threads.cpp:49
FvAqtVisionThreads::has_cont_thread
bool has_cont_thread()
Check if there is at least one continuous thread.
Definition: aqt_vision_threads.cpp:127
FvAqtVisionThreads::remove_waiting_thread
void remove_waiting_thread(fawkes::Thread *thread)
Remove waiting thread.
Definition: aqt_vision_threads.cpp:106
FvAqtVisionThreads::empty
bool empty()
Check if there is no thread at all.
Definition: aqt_vision_threads.cpp:147
fawkes::Clock::elapsed
float elapsed(Time *t) const
How much time has elapsed since t? Calculated as "now - t" in seconds.
Definition: clock.cpp:259
FvAqtVisionThreads::remove_thread
void remove_thread(fawkes::Thread *thread)
Remove a thread.
Definition: aqt_vision_threads.cpp:87
fawkes::ThreadList::set_prepfin_hold
void set_prepfin_hold(bool hold)
Set prepfin hold on all threads.
Definition: thread_list.cpp:636
FvAqtVisionThreads::FvAqtVisionThreads
FvAqtVisionThreads(fawkes::Clock *clock)
Constructor.
Definition: aqt_vision_threads.cpp:42
FvAqtVisionThreads::set_prepfin_hold
void set_prepfin_hold(bool hold)
Set prepfin hold fo cyclic threads.
Definition: aqt_vision_threads.cpp:181
FvAqtVisionThreads::add_waiting_thread
void add_waiting_thread(fawkes::Thread *thread)
Add a thread in waiting state.
Definition: aqt_vision_threads.cpp:59
fawkes
FvAqtVisionThreads::set_thread_running
void set_thread_running(fawkes::Thread *thread)
Mark the thread as running.
Definition: aqt_vision_threads.cpp:68
FvAqtVisionThreads::has_cyclic_thread
bool has_cyclic_thread()
Check if there is at least one cyclic thread.
Definition: aqt_vision_threads.cpp:118
fawkes::Clock::get_systime
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:220
fawkes::ThreadList::remove_locked
void remove_locked(Thread *thread)
Remove with lock protection.
Definition: thread_list.cpp:839
fawkes::Thread
Definition: thread.h:44
fawkes::Barrier
Definition: barrier.h:35
FvAqtVisionThreads::empty_time
float empty_time()
Get the empty time.
Definition: aqt_vision_threads.cpp:157
FvAqtVisionThreads::wakeup_and_wait_cyclic_threads
void wakeup_and_wait_cyclic_threads()
Wakeup and wait for all cyclic threads.
Definition: aqt_vision_threads.cpp:164
fawkes::ThreadList::push_back_locked
void push_back_locked(Thread *thread)
Add thread to the end with lock protection.
Definition: thread_list.cpp:796
fawkes::Clock
Definition: clock.h:39
fawkes::VisionAspect
Definition: vision.h:36
fawkes::Exception
Definition: exception.h:39