Fawkes API Fawkes Development Version
|
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) |
Add multiple threads. | |
virtual void | add (fawkes::Thread *t) |
Add single thread. | |
virtual void | remove (fawkes::ThreadList &tl) |
Remove multiple threads. | |
virtual void | remove (fawkes::Thread *t) |
Remove single thread. | |
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.
virtual void FawkesThreadManager::add | ( | fawkes::ThreadList & | tl | ) | [inline, virtual] |
Add multiple threads.
Adds all the threads in the list to the thread list. Implementations may throw an exception if this fails for whatever reason, read implementation documentation for details. The operation shall be atomic, either all threads are added successfully or none is added at all. If adding fails a CannotInitializeThreadException is thrown.
The thread is started if and only if initialization of all threads suceeds. A CannotInitializeThreadException is thrown if initialization failed for any thread.
tl | list of threads to add |
Implements fawkes::ThreadCollector.
Definition at line 53 of file thread_manager.h.
Referenced by FawkesNetworkManager::FawkesNetworkManager().
virtual void FawkesThreadManager::add | ( | fawkes::Thread * | t | ) | [inline, virtual] |
Add single thread.
Adds the single thread to the internal (implementation specific) thread list. The thread is started if and only if initialization suceeds. A CannotInitializeThreadException is thrown if initialization failed.
t | thread to add |
Implements fawkes::ThreadCollector.
Definition at line 58 of file thread_manager.h.
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). |
Implements fawkes::ThreadCollector.
Definition at line 422 of file thread_manager.cpp.
References fawkes::ThreadList::force_stop(), fawkes::LockList< Type >::lock(), 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). |
Implements fawkes::ThreadCollector.
Definition at line 458 of file thread_manager.cpp.
References fawkes::Thread::cancel(), fawkes::Thread::finalize(), fawkes::Thread::join(), 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.
virtual void FawkesThreadManager::remove | ( | fawkes::ThreadList & | tl | ) | [inline, virtual] |
Remove multiple threads.
Remove all threads in the thread list from this collector. If there is a thread in the supplied thread list that has never been collected no error shall be thrown but this just be silently ignored.
The threads are finalized, cancelled and joined. If the finalization fails for whatever reason the threads are NOT cancelled or stopped. In that case a CannotFinalizeThreadException is thrown.
tl | list of threads to remove |
Implements fawkes::ThreadCollector.
Definition at line 63 of file thread_manager.h.
virtual void FawkesThreadManager::remove | ( | fawkes::Thread * | t | ) | [inline, virtual] |
Remove single thread.
Remove the thread from the internal thread list. If the thread has never been collected no error shall be thrown but just be silently ignored. The thread is finalized, cancelled and joined. If the finalization fails for whatever reason the thread is NOT cancelled or stopped. In that case a CannotFinalizeThreadException is thrown.
t | Thread to remove. |
Implements fawkes::ThreadCollector.
Definition at line 68 of file thread_manager.h.
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.
Referenced by FawkesMainThread::loop().
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.
Referenced by FawkesMainThread::loop().
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.
Referenced by FawkesMainThread::loop().
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.
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.
Referenced by FawkesMainThread::loop().