Fawkes API  Fawkes Development Version
thread_manager.h
1 
2 /***************************************************************************
3  * thread_manager.h - Fawkes thread manager
4  *
5  * Created: Thu Nov 3 19:08:23 2006 (on train to Cologne)
6  * Copyright 2006-2009 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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _LIBS_BASEAPP_THREAD_MANAGER_H_
25 #define _LIBS_BASEAPP_THREAD_MANAGER_H_
26 
27 #include <aspect/blocked_timing.h>
28 #include <aspect/blocked_timing/executor.h>
29 #include <core/exception.h>
30 #include <core/threading/thread_collector.h>
31 #include <core/threading/thread_list.h>
32 #include <core/utils/lock_map.h>
33 
34 #include <list>
35 
36 namespace fawkes {
37 class Mutex;
38 class WaitCondition;
39 class ThreadInitializer;
40 class ThreadFinalizer;
41 
42 class ThreadManager : public ThreadCollector, public BlockedTimingExecutor
43 {
44 public:
45  ThreadManager();
46  ThreadManager(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
47  virtual ~ThreadManager();
48 
49  void set_inifin(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
50 
51  virtual void
52  add(ThreadList &tl)
53  {
54  add_maybelocked(tl, /* lock */ true);
55  }
56 
57  virtual void
58  add(Thread *t)
59  {
60  add_maybelocked(t, /* lock */ true);
61  }
62 
63  virtual void
64  remove(ThreadList &tl)
65  {
66  remove_maybelocked(tl, /* lock */ true);
67  }
68 
69  virtual void
70  remove(Thread *t)
71  {
72  remove_maybelocked(t, /* lock */ true);
73  }
74 
75  virtual void force_remove(ThreadList &tl);
76  virtual void force_remove(Thread *t);
77 
78  virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec = 0);
79  virtual void wakeup(BlockedTimingAspect::WakeupHook hook, Barrier *barrier = 0);
80  virtual void try_recover(std::list<std::string> &recovered_threads);
81 
82  virtual bool timed_threads_exist();
83  virtual void wait_for_timed_threads();
84  virtual void interrupt_timed_thread_wait();
85 
87 
88 private:
89  void internal_add_thread(Thread *t);
90  void internal_remove_thread(Thread *t);
91  void add_maybelocked(ThreadList &tl, bool lock);
92  void add_maybelocked(Thread *t, bool lock);
93  void remove_maybelocked(ThreadList &tl, bool lock);
94  void remove_maybelocked(Thread *t, bool lock);
95 
96  class ThreadManagerAspectCollector : public ThreadCollector
97  {
98  public:
99  ThreadManagerAspectCollector(ThreadManager *parent_manager);
100 
101  virtual void add(ThreadList &tl);
102  virtual void add(Thread *t);
103 
104  virtual void remove(ThreadList &tl);
105  virtual void remove(Thread *t);
106 
107  virtual void force_remove(ThreadList &tl);
108  virtual void force_remove(Thread *t);
109 
110  private:
111  ThreadManager *parent_manager_;
112  };
113 
114 private:
115  ThreadInitializer *initializer_;
116  ThreadFinalizer * finalizer_;
117 
120 
121  ThreadList untimed_threads_;
122  WaitCondition *waitcond_timedthreads_;
123 
124  ThreadManagerAspectCollector *aspect_collector_;
125  bool interrupt_timed_thread_wait_;
126 };
127 
128 } // end namespace fawkes
129 
130 #endif
fawkes::ThreadCollector
Definition: thread_collector.h:38
fawkes::LockMap
Definition: lock_map.h:39
fawkes::ThreadManager::try_recover
virtual void try_recover(std::list< std::string > &recovered_threads)
Definition: thread_manager.cpp:579
fawkes::ThreadManager::timed_threads_exist
virtual bool timed_threads_exist()
Definition: thread_manager.cpp:589
fawkes::WaitCondition
Definition: wait_condition.h:40
fawkes::ThreadManager::set_inifin
void set_inifin(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Set initializer/finalizer.
Definition: thread_manager.cpp:192
fawkes::ThreadManager::wait_for_timed_threads
virtual void wait_for_timed_threads()
Definition: thread_manager.cpp:595
fawkes::ThreadManager::~ThreadManager
virtual ~ThreadManager()
Destructor.
Definition: thread_manager.cpp:166
fawkes::ThreadManager::wakeup_and_wait
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)
Definition: thread_manager.cpp:545
fawkes::ThreadManager::force_remove
virtual void force_remove(ThreadList &tl)
Force removal of the given threads.
Definition: thread_manager.cpp:481
fawkes::ThreadManager::ThreadManager
ThreadManager()
Constructor.
Definition: thread_manager.cpp:138
fawkes
fawkes::ThreadManager
Definition: thread_manager.h:46
fawkes::ThreadManager::aspect_collector
ThreadCollector * aspect_collector() const
Get a thread collector to be used for an aspect initializer.
Definition: thread_manager.cpp:616
fawkes::BlockedTimingAspect::WakeupHook
WakeupHook
Type to define at which hook the thread is woken up.
Definition: blocked_timing.h:62
fawkes::ThreadFinalizer
Definition: thread_finalizer.h:44
fawkes::Thread
Definition: thread.h:44
fawkes::ThreadManager::wakeup
virtual void wakeup(BlockedTimingAspect::WakeupHook hook, Barrier *barrier=0)
Definition: thread_manager.cpp:562
fawkes::ThreadManager::add
virtual void add(ThreadList &tl)
Definition: thread_manager.h:56
fawkes::Barrier
Definition: barrier.h:35
fawkes::ThreadInitializer
Definition: thread_initializer.h:45
fawkes::ThreadManager::interrupt_timed_thread_wait
virtual void interrupt_timed_thread_wait()
Definition: thread_manager.cpp:606
fawkes::ThreadList
Definition: thread_list.h:59
fawkes::ThreadManager::remove
virtual void remove(ThreadList &tl)
Definition: thread_manager.h:68