00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __FAWKES_THREAD_MANAGER_H_
00025 #define __FAWKES_THREAD_MANAGER_H_
00026
00027 #include <core/threading/thread_list.h>
00028 #include <core/threading/thread_collector.h>
00029 #include <core/exception.h>
00030 #include <aspect/blocked_timing.h>
00031 #include <aspect/blocked_timing/executor.h>
00032
00033 #include <core/utils/lock_map.h>
00034 #include <list>
00035
00036 namespace fawkes {
00037 class Mutex;
00038 class WaitCondition;
00039 class ThreadInitializer;
00040 class ThreadFinalizer;
00041 }
00042
00043 class FawkesThreadManager
00044 : public fawkes::ThreadCollector,
00045 public fawkes::BlockedTimingExecutor
00046 {
00047 public:
00048 FawkesThreadManager();
00049 virtual ~FawkesThreadManager();
00050
00051 void set_inifin(fawkes::ThreadInitializer *initializer, fawkes::ThreadFinalizer *finalizer);
00052
00053 virtual void add(fawkes::ThreadList &tl)
00054 {
00055 add_maybelocked(tl, true);
00056 }
00057
00058 virtual void add(fawkes::Thread *t)
00059 {
00060 add_maybelocked(t, true);
00061 }
00062
00063 virtual void remove(fawkes::ThreadList &tl)
00064 {
00065 remove_maybelocked(tl, true);
00066 }
00067
00068 virtual void remove(fawkes::Thread *t)
00069 {
00070 remove_maybelocked(t, true);
00071 }
00072
00073 virtual void force_remove(fawkes::ThreadList &tl);
00074 virtual void force_remove(fawkes::Thread *t);
00075
00076 virtual void wakeup_and_wait(fawkes::BlockedTimingAspect::WakeupHook hook,
00077 unsigned int timeout_usec = 0);
00078 virtual void wakeup(fawkes::BlockedTimingAspect::WakeupHook hook,
00079 fawkes::Barrier *barrier = 0);
00080 virtual void try_recover(std::list<std::string> &recovered_threads);
00081
00082 virtual bool timed_threads_exist();
00083 virtual void wait_for_timed_threads();
00084 virtual void interrupt_timed_thread_wait();
00085
00086 fawkes::ThreadCollector * aspect_collector() const;
00087
00088 private:
00089 void internal_add_thread(fawkes::Thread *t);
00090 void internal_remove_thread(fawkes::Thread *t);
00091 void add_maybelocked(fawkes::ThreadList &tl, bool lock);
00092 void add_maybelocked(fawkes::Thread *t, bool lock);
00093 void remove_maybelocked(fawkes::ThreadList &tl, bool lock);
00094 void remove_maybelocked(fawkes::Thread *t, bool lock);
00095
00096 class FawkesThreadManagerAspectCollector : public fawkes::ThreadCollector
00097 {
00098 public:
00099 FawkesThreadManagerAspectCollector(FawkesThreadManager *parent_manager);
00100
00101 virtual void add(fawkes::ThreadList &tl);
00102 virtual void add(fawkes::Thread *t);
00103
00104 virtual void remove(fawkes::ThreadList &tl);
00105 virtual void remove(fawkes::Thread *t);
00106
00107 virtual void force_remove(fawkes::ThreadList &tl);
00108 virtual void force_remove(fawkes::Thread *t);
00109
00110 private:
00111 FawkesThreadManager *__parent_manager;
00112 };
00113
00114 private:
00115 fawkes::ThreadInitializer *initializer;
00116 fawkes::ThreadFinalizer *finalizer;
00117
00118 fawkes::LockMap< fawkes::BlockedTimingAspect::WakeupHook, fawkes::ThreadList > threads;
00119 fawkes::LockMap< fawkes::BlockedTimingAspect::WakeupHook, fawkes::ThreadList >::iterator tit;
00120
00121 fawkes::ThreadList untimed_threads;
00122 fawkes::WaitCondition *waitcond_timedthreads;
00123
00124 FawkesThreadManagerAspectCollector *__aspect_collector;
00125 bool __interrupt_timed_thread_wait;
00126
00127 };
00128
00129 #endif