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
36namespace fawkes {
37class Mutex;
38class WaitCondition;
39class ThreadInitializer;
40class ThreadFinalizer;
41
43{
44public:
46 ThreadManager(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
47 virtual ~ThreadManager();
48
49 void set_inifin(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
50
51 virtual void
53 {
54 add_maybelocked(tl, /* lock */ true);
55 }
56
57 virtual void
59 {
60 add_maybelocked(t, /* lock */ true);
61 }
62
63 virtual void
65 {
66 remove_maybelocked(tl, /* lock */ true);
67 }
68
69 virtual void
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
88private:
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
114private:
115 ThreadInitializer *initializer_;
116 ThreadFinalizer * finalizer_;
117
118 LockMap<BlockedTimingAspect::WakeupHook, ThreadList> threads_;
119 LockMap<BlockedTimingAspect::WakeupHook, ThreadList>::iterator tit_;
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
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
WakeupHook
Type to define at which hook the thread is woken up.
Blocked timing executor.
Definition: executor.h:37
Thread finalizer interface.
Thread initializer interface.
List of threads.
Definition: thread_list.h:56
Base application thread manager.
virtual void interrupt_timed_thread_wait()
Interrupt any currently running wait_for_timed_threads() and cause it to throw an InterruptedExceptio...
virtual ~ThreadManager()
Destructor.
virtual void add(ThreadList &tl)
Add multiple threads.
virtual void try_recover(std::list< std::string > &recovered_threads)
Try to recover threads.
virtual void wait_for_timed_threads()
Wait for timed threads.
virtual void remove(ThreadList &tl)
Remove multiple threads.
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)
Wakeup thread for given hook and wait for completion.
virtual void wakeup(BlockedTimingAspect::WakeupHook hook, Barrier *barrier=0)
Wakeup thread for given hook.
ThreadCollector * aspect_collector() const
Get a thread collector to be used for an aspect initializer.
virtual bool timed_threads_exist()
Check if any timed threads exist.
virtual void add(Thread *t)
Add single thread.
ThreadManager()
Constructor.
virtual void force_remove(ThreadList &tl)
Force removal of the given threads.
virtual void remove(Thread *t)
Remove single thread.
void set_inifin(ThreadInitializer *initializer, ThreadFinalizer *finalizer)
Set initializer/finalizer.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.