Fawkes API
Fawkes Development Version
|
Base application thread manager. More...
#include <>>
Public Member Functions | |
ThreadManager () | |
Constructor. More... | |
ThreadManager (ThreadInitializer *initializer, ThreadFinalizer *finalizer) | |
Constructor. More... | |
virtual | ~ThreadManager () |
Destructor. More... | |
void | set_inifin (ThreadInitializer *initializer, ThreadFinalizer *finalizer) |
Set initializer/finalizer. More... | |
virtual void | add (ThreadList &tl) |
Add multiple threads. More... | |
virtual void | add (Thread *t) |
Add single thread. More... | |
virtual void | remove (ThreadList &tl) |
Remove multiple threads. More... | |
virtual void | remove (Thread *t) |
Remove single thread. More... | |
virtual void | force_remove (ThreadList &tl) |
Force removal of the given threads. More... | |
virtual void | force_remove (Thread *t) |
Force removal of the given thread. More... | |
virtual void | wakeup_and_wait (BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0) |
Wakeup thread for given hook and wait for completion. More... | |
virtual void | wakeup (BlockedTimingAspect::WakeupHook hook, Barrier *barrier=0) |
Wakeup thread for given hook. More... | |
virtual void | try_recover (std::list< std::string > &recovered_threads) |
Try to recover threads. More... | |
virtual bool | timed_threads_exist () |
Check if any timed threads exist. More... | |
virtual void | wait_for_timed_threads () |
Wait for timed threads. More... | |
virtual void | interrupt_timed_thread_wait () |
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedException. More... | |
ThreadCollector * | aspect_collector () const |
Get a thread collector to be used for an aspect initializer. More... | |
![]() | |
virtual | ~ThreadCollector () |
Empty virtual destructor. More... | |
![]() | |
virtual | ~BlockedTimingExecutor () |
Virtual empty destructor. More... | |
Base application 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 45 of file thread_manager.h.
fawkes::ThreadManager::ThreadManager | ( | ) |
Constructor.
When using this constructor you need to make sure to call set_inifin() before any thread is added.
Definition at line 139 of file thread_manager.cpp.
fawkes::ThreadManager::ThreadManager | ( | ThreadInitializer * | initializer, |
ThreadFinalizer * | finalizer | ||
) |
Constructor.
This contsructor is equivalent to the one without parameters followed by a call to set_inifins().
initializer | thread initializer |
finalizer | thread finalizer |
Definition at line 155 of file thread_manager.cpp.
References set_inifin().
|
virtual |
Destructor.
Definition at line 169 of file thread_manager.cpp.
References fawkes::ThreadList::force_stop().
|
inlinevirtual |
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 57 of file thread_manager.h.
Referenced by remove().
|
inlinevirtual |
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 62 of file thread_manager.h.
ThreadCollector * fawkes::ThreadManager::aspect_collector | ( | ) | const |
Get a thread collector to be used for an aspect initializer.
Definition at line 634 of file thread_manager.cpp.
Referenced by remove().
|
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 490 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().
Referenced by remove().
|
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 537 of file thread_manager.cpp.
References fawkes::Thread::cancel(), fawkes::ThreadFinalizer::finalize(), fawkes::Thread::finalize(), fawkes::Thread::join(), and fawkes::Thread::prepare_finalize().
|
virtual |
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedException.
Implements fawkes::BlockedTimingExecutor.
Definition at line 622 of file thread_manager.cpp.
References fawkes::WaitCondition::wake_all().
Referenced by remove().
|
inlinevirtual |
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 67 of file thread_manager.h.
|
inlinevirtual |
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 72 of file thread_manager.h.
References add(), aspect_collector(), force_remove(), interrupt_timed_thread_wait(), timed_threads_exist(), try_recover(), wait_for_timed_threads(), wakeup(), and wakeup_and_wait().
void fawkes::ThreadManager::set_inifin | ( | ThreadInitializer * | initializer, |
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 194 of file thread_manager.cpp.
References fawkes::Exception::append(), fawkes::BlockedTimingAspect::blockedTimingAspectHook(), fawkes::Thread::cancel(), fawkes::Thread::cancel_finalize(), fawkes::ThreadList::cancel_finalize(), fawkes::ThreadFinalizer::finalize(), fawkes::Thread::finalize(), fawkes::ThreadList::finalize(), fawkes::ThreadInitializer::init(), fawkes::Thread::init(), fawkes::ThreadList::init(), fawkes::Thread::join(), fawkes::LockList< Type >::lock(), fawkes::ThreadList::name(), fawkes::Thread::name(), fawkes::Thread::notify_of_failed_init(), fawkes::Thread::prepare_finalize(), fawkes::ThreadList::prepare_finalize(), fawkes::ThreadList::push_back_locked(), fawkes::ThreadList::remove_locked(), fawkes::ThreadList::seal(), fawkes::ThreadList::sealed(), fawkes::Thread::start(), fawkes::ThreadList::start(), fawkes::ThreadList::stop(), fawkes::LockList< Type >::unlock(), and fawkes::WaitCondition::wake_all().
Referenced by ThreadManager().
|
virtual |
Check if any timed threads exist.
Implements fawkes::BlockedTimingExecutor.
Definition at line 604 of file thread_manager.cpp.
Referenced by fawkes::FawkesMainThread::loop(), and remove().
|
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 593 of file thread_manager.cpp.
Referenced by fawkes::FawkesMainThread::loop(), and remove().
|
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 611 of file thread_manager.cpp.
References fawkes::WaitCondition::wait().
Referenced by fawkes::FawkesMainThread::loop(), and remove().
|
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 575 of file thread_manager.cpp.
Referenced by remove().
|
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 556 of file thread_manager.cpp.
Referenced by remove().