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 <core/threading/thread_list.h>
28 #include <core/threading/thread_collector.h>
29 #include <core/exception.h>
30 #include <aspect/blocked_timing.h>
31 #include <aspect/blocked_timing/executor.h>
32 
33 #include <core/utils/lock_map.h>
34 #include <list>
35 
36 namespace fawkes {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 class Mutex;
41 class WaitCondition;
42 class ThreadInitializer;
43 class ThreadFinalizer;
44 
46 : public ThreadCollector,
48 {
49  public:
50  ThreadManager();
51  ThreadManager(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
52  virtual ~ThreadManager();
53 
54  void set_inifin(ThreadInitializer *initializer,
55  ThreadFinalizer *finalizer);
56 
57  virtual void add(ThreadList &tl)
58  {
59  add_maybelocked(tl, /* lock */ true);
60  }
61 
62  virtual void add(Thread *t)
63  {
64  add_maybelocked(t, /* lock */ true);
65  }
66 
67  virtual void remove(ThreadList &tl)
68  {
69  remove_maybelocked(tl, /* lock */ true);
70  }
71 
72  virtual void remove(Thread *t)
73  {
74  remove_maybelocked(t, /* lock */ true);
75  }
76 
77  virtual void force_remove(ThreadList &tl);
78  virtual void force_remove(Thread *t);
79 
81  unsigned int timeout_usec = 0);
82  virtual void wakeup(BlockedTimingAspect::WakeupHook hook,
83  Barrier *barrier = 0);
84  virtual void try_recover(std::list<std::string> &recovered_threads);
85 
86  virtual bool timed_threads_exist();
87  virtual void wait_for_timed_threads();
88  virtual void interrupt_timed_thread_wait();
89 
91 
92  private:
93  void internal_add_thread(Thread *t);
94  void internal_remove_thread(Thread *t);
95  void add_maybelocked(ThreadList &tl, bool lock);
96  void add_maybelocked(Thread *t, bool lock);
97  void remove_maybelocked(ThreadList &tl, bool lock);
98  void remove_maybelocked(Thread *t, bool lock);
99 
100  class ThreadManagerAspectCollector : public ThreadCollector
101  {
102  public:
103  ThreadManagerAspectCollector(ThreadManager *parent_manager);
104 
105  virtual void add(ThreadList &tl);
106  virtual void add(Thread *t);
107 
108  virtual void remove(ThreadList &tl);
109  virtual void remove(Thread *t);
110 
111  virtual void force_remove(ThreadList &tl);
112  virtual void force_remove(Thread *t);
113 
114  private:
115  ThreadManager *__parent_manager;
116  };
117 
118  private:
119  ThreadInitializer *__initializer;
120  ThreadFinalizer *__finalizer;
121 
124 
125  ThreadList __untimed_threads;
126  WaitCondition *__waitcond_timedthreads;
127 
128  ThreadManagerAspectCollector *__aspect_collector;
129  bool __interrupt_timed_thread_wait;
130 
131 };
132 
133 } // end namespace fawkes
134 
135 #endif
void set_inifin(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Set initializer/finalizer.
Wait until a given condition holds.
virtual void add(ThreadList &tl)
Add multiple threads.
Fawkes library namespace.
virtual ~ThreadManager()
Destructor.
Thread collector.
virtual bool timed_threads_exist()
Check if any timed threads exist.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void force_remove(ThreadList &tl)
Force removal of the given threads.
ThreadManager()
Constructor.
Map with a lock.
Definition: lock_map.h:37
virtual void wait_for_timed_threads()
Wait for timed threads.
List of threads.
Definition: thread_list.h:57
Base application thread manager.
WakeupHook
Type to define at which hook the thread is woken up.
Thread initializer interface.
Blocked timing executor.
Definition: executor.h:35
virtual void add(Thread *t)
Add single thread.
virtual void wakeup(BlockedTimingAspect::WakeupHook hook, Barrier *barrier=0)
Wakeup thread for given hook.
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)
Wakeup thread for given hook and wait for completion.
virtual void interrupt_timed_thread_wait()
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedExceptio...
virtual void try_recover(std::list< std::string > &recovered_threads)
Try to recover threads.
ThreadCollector * aspect_collector() const
Get a thread collector to be used for an aspect initializer.
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
Thread finalizer interface.