Thread Manager. More...
#include <mainapp/thread_manager.h>
Classes | |
class | FawkesThreadManagerAspectCollector |
Public Member Functions | |
FawkesThreadManager () | |
Constructor. | |
virtual | ~FawkesThreadManager () |
Destructor. | |
void | set_inifin (fawkes::ThreadInitializer *initializer, fawkes::ThreadFinalizer *finalizer) |
Set initializer/finalizer. | |
virtual void | add (fawkes::ThreadList &tl) |
virtual void | add (fawkes::Thread *t) |
virtual void | remove (fawkes::ThreadList &tl) |
virtual void | remove (fawkes::Thread *t) |
virtual void | force_remove (fawkes::ThreadList &tl) |
Force removal of the given threads. | |
virtual void | force_remove (fawkes::Thread *t) |
Force removal of the given thread. | |
virtual void | wakeup_and_wait (fawkes::BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0) |
Wakeup thread for given hook and wait for completion. | |
virtual void | wakeup (fawkes::BlockedTimingAspect::WakeupHook hook, fawkes::Barrier *barrier=0) |
Wakeup thread for given hook. | |
virtual void | try_recover (std::list< std::string > &recovered_threads) |
Try to recover threads. | |
virtual bool | timed_threads_exist () |
Check if any timed threads exist. | |
virtual void | wait_for_timed_threads () |
Wait for timed threads. | |
virtual void | interrupt_timed_thread_wait () |
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedException. | |
fawkes::ThreadCollector * | aspect_collector () const |
Get a thread collector to be used for an aspect initializer. |
Thread Manager.
This class provides a manager for the threads. Threads are memorized by their wakeup hook. When the thread manager is deleted, all threads are appropriately cancelled, joined and deleted. Thus the thread manager can be used for "garbage collection" of threads.
The thread manager allows easy wakeup of threads of a given wakeup hook.
The thread manager needs a thread initializer. Each thread that is added to the thread manager is initialized with this. The runtime type information (RTTI) supplied by C++ can be used to initialize threads if appropriate (if the thread has certain aspects that need special treatment).
Definition at line 43 of file thread_manager.h.
FawkesThreadManager::FawkesThreadManager | ( | ) |
Constructor.
Definition at line 131 of file thread_manager.cpp.
FawkesThreadManager::~FawkesThreadManager | ( | ) | [virtual] |
Destructor.
Definition at line 143 of file thread_manager.cpp.
References fawkes::ThreadList::force_stop().
ThreadCollector * FawkesThreadManager::aspect_collector | ( | ) | const |
Get a thread collector to be used for an aspect initializer.
Definition at line 555 of file thread_manager.cpp.
Referenced by FawkesMainThread::FawkesMainThread().
void FawkesThreadManager::force_remove | ( | fawkes::ThreadList & | tl | ) | [virtual] |
Force removal of the given threads.
The thread manager tries to finalize and stop the threads and then removes the threads from the internal structures.
This will succeed even if a thread of the given list cannot be finalized, for example if prepare_finalize() returns false or if the thread finalizer cannot finalize the thread.
Caution, using this function may damage your robot.
tl | threads to remove. |
ThreadListNotSealedException | if the given thread lits tl is not sealed the thread manager will refuse to remove it The threads are removed from thread manager control. The threads will be stopped before they are removed (may cause unpredictable results otherwise). |
Definition at line 422 of file thread_manager.cpp.
References fawkes::ThreadList::force_stop(), fawkes::LockList< Type >::lock(), fawkes::LockMap< KeyType, ValueType, LessKey >::mutex(), fawkes::ThreadList::name(), fawkes::ThreadList::sealed(), and fawkes::LockList< Type >::unlock().
void FawkesThreadManager::force_remove | ( | fawkes::Thread * | thread | ) | [virtual] |
Force removal of the given thread.
The thread manager tries to finalize and stop the thread and then removes the thread from the internal structures.
This will succeed even if the thread cannot be finalized, for example if prepare_finalize() returns false or if the thread finalizer cannot finalize the thread.
Caution, using this function may damage your robot.
thread | thread to remove. |
ThreadListNotSealedException | if the given thread lits tl is not sealed the thread manager will refuse to remove it The threads are removed from thread manager control. The threads will be stopped before they are removed (may cause unpredictable results otherwise). |
Definition at line 458 of file thread_manager.cpp.
References fawkes::Thread::cancel(), fawkes::Thread::finalize(), fawkes::ThreadFinalizer::finalize(), fawkes::Thread::join(), fawkes::LockMap< KeyType, ValueType, LessKey >::mutex(), and fawkes::Thread::prepare_finalize().
void FawkesThreadManager::interrupt_timed_thread_wait | ( | ) | [virtual] |
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedException.
Implements fawkes::BlockedTimingExecutor.
Definition at line 543 of file thread_manager.cpp.
References fawkes::WaitCondition::wake_all().
void FawkesThreadManager::set_inifin | ( | fawkes::ThreadInitializer * | initializer, | |
fawkes::ThreadFinalizer * | finalizer | |||
) |
Set initializer/finalizer.
This method has to be called before any thread is added/removed.
initializer | thread initializer | |
finalizer | thread finalizer |
Definition at line 164 of file thread_manager.cpp.
Referenced by FawkesMainThread::FawkesMainThread().
bool FawkesThreadManager::timed_threads_exist | ( | ) | [virtual] |
Check if any timed threads exist.
Implements fawkes::BlockedTimingExecutor.
Definition at line 525 of file thread_manager.cpp.
void FawkesThreadManager::try_recover | ( | std::list< std::string > & | recovered_threads | ) | [virtual] |
Try to recover threads.
An advanced BlockedTimingExecutor might be able to detect deadlocked threads. In this case this function should be called regularly to allow for recovering threads that are back to normal operation.
recovered_threads | upon return the names of any threads that could be recovered from a bad state have been added to the list. |
Implements fawkes::BlockedTimingExecutor.
Definition at line 514 of file thread_manager.cpp.
References fawkes::LockMap< KeyType, ValueType, LessKey >::lock(), and fawkes::LockMap< KeyType, ValueType, LessKey >::unlock().
void FawkesThreadManager::wait_for_timed_threads | ( | ) | [virtual] |
Wait for timed threads.
Use this method to wait until a timed that is added to the thread manager. Note that an InterruptedException is thrown if interrup_timed_thread_wait() is called from another thread. You should catch this exception and stop the loop execution.
InterruptedException | thrown if another thread calls interrupt_timed_thread_wait() |
Implements fawkes::BlockedTimingExecutor.
Definition at line 532 of file thread_manager.cpp.
References fawkes::WaitCondition::wait().
void FawkesThreadManager::wakeup | ( | fawkes::BlockedTimingAspect::WakeupHook | hook, | |
fawkes::Barrier * | barrier = 0 | |||
) | [virtual] |
Wakeup thread for given hook.
This will wakeup all threads registered for the given hook. With the optional barrier you can synchronize to the end of the loop execution of the threads of the given hook.
hook | hook for which to wait for | |
barrier | optional barrier that can be used to synchronize to the end of the loop execution of the threads. |
Implements fawkes::BlockedTimingExecutor.
Definition at line 496 of file thread_manager.cpp.
References fawkes::LockMap< KeyType, ValueType, LessKey >::mutex().
void FawkesThreadManager::wakeup_and_wait | ( | fawkes::BlockedTimingAspect::WakeupHook | hook, | |
unsigned int | timeout_usec = 0 | |||
) | [virtual] |
Wakeup thread for given hook and wait for completion.
This will wakeup all threads registered for the given hook. Afterwards this method will block until all threads finished their loop.
hook | hook for which to wait for | |
timeout_usec | timeout for thread execution in usec. 0 disables the timeout and makes this function to wait forever. |
Exception | thrown if the timeout was exceeded (at least one of the threads for the given hook took longer than the desired time. |
Implements fawkes::BlockedTimingExecutor.
Definition at line 477 of file thread_manager.cpp.
References fawkes::LockMap< KeyType, ValueType, LessKey >::mutex().