Fawkes API Fawkes Development Version
main_thread.h
1
2/***************************************************************************
3 * main_thread.h - Fawkes main thread
4 *
5 * Created: Thu Nov 2 16:46:37 2006
6 * Copyright 2006-2008 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_MAIN_THREAD_H_
25#define _LIBS_BASEAPP_MAIN_THREAD_H_
26
27#include <aspect/blocked_timing.h>
28#include <aspect/mainloop/employer.h>
29#include <baseapp/thread_manager.h>
30#include <core/threading/thread.h>
31#include <logging/multi.h>
32#include <syncpoint/syncpoint_manager.h>
33#include <utils/system/signal.h>
34
35#include <getopt.h>
36#include <list>
37#include <string>
38#include <vector>
39
40namespace fawkes {
41class Configuration;
42class Configuration;
43class ConfigNetworkHandler;
44class NetworkLogger;
45class Clock;
46class TimeWait;
47class AspectManager;
48class PluginManager;
49class Time;
50class PluginNetworkHandler;
51class InterruptibleBarrier;
52class Barrier;
53class Mutex;
54class ThreadManager;
55class SyncPointManager;
56class FawkesNetworkManager;
57
59{
60public:
62 MultiLogger * multi_logger,
63 ThreadManager * thread_manager,
64 SyncPointManager *syncpoint_manager,
65 PluginManager * plugin_manager,
66 const char * load_plugins,
67 const char * default_plugin = 0);
68 virtual ~FawkesMainThread();
69
70 virtual void once();
71 virtual void loop();
72
73 virtual void set_mainloop_thread(Thread *mainloop_thread);
74
75 void full_start();
76
77 MultiLogger *logger() const;
78
79 class Runner : public SignalHandler
80 {
81 public:
82 Runner(FawkesMainThread *fmt, bool register_signals = true);
83 ~Runner();
84 void run();
85 void handle_signal(int signum);
86
87 private:
88 FawkesMainThread *fmt_;
89 Mutex * init_mutex_;
90 bool init_running_;
91 bool init_quit_;
92 bool sigint_running_;
93 bool register_signals_;
94 };
95
96 /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
97protected:
98 virtual void
100 {
101 Thread::run();
102 }
103
104private:
105 void destruct();
106
107 inline void
108 safe_wake(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec)
109 {
110 try {
111 thread_manager_->wakeup_and_wait(hook, timeout_usec);
112 } catch (Exception &e) {
113 if (enable_looptime_warnings_) {
114 //multi_logger_->log_error("FawkesMainThread",
115 // "Error while processing hook %s, exception follows",
116 // BlockedTimingAspect::blocked_timing_hook_to_string(hook));
117 multi_logger_->log_error("FawkesMainThread", e);
118 }
119 }
120 }
121
122 Configuration *config_;
123 MultiLogger * multi_logger_;
124 Clock * clock_;
125 TimeWait * time_wait_;
126
127 Barrier * init_barrier_;
128 Thread * mainloop_thread_;
129 Mutex * mainloop_mutex_;
130 InterruptibleBarrier *mainloop_barrier_;
131
132 char *default_plugin_;
133 char *load_plugins_;
134
135 ThreadManager * thread_manager_;
136 SyncPointManager *syncpoint_manager_;
137 PluginManager * plugin_manager_;
138
139 std::list<std::string> recovered_threads_;
140 unsigned int desired_loop_time_usec_;
141 float desired_loop_time_sec_;
142 unsigned int max_thread_time_usec_;
143 unsigned int max_thread_time_nanosec_;
144 Time * loop_start_;
145 Time * loop_end_;
146 bool enable_looptime_warnings_;
147
148 std::vector<RefPtr<SyncPoint>> syncpoints_start_hook_;
149 std::vector<RefPtr<SyncPoint>> syncpoints_end_hook_;
150};
151
152} // end namespace fawkes
153
154#endif
WakeupHook
Type to define at which hook the thread is woken up.
Interface for configuration handling.
Definition: config.h:68
Utility class to run the main thread.
Definition: main_thread.h:80
Runner(FawkesMainThread *fmt, bool register_signals=true)
Constructor.
void run()
Run main thread.
void handle_signal(int signum)
Handle signals.
Fawkes default main thread.
Definition: main_thread.h:59
virtual void set_mainloop_thread(Thread *mainloop_thread)
Set a new main loop.
virtual void once()
Execute an action exactly once.
void full_start()
Start the thread and wait until once() completes.
MultiLogger * logger() const
Get logger.
FawkesMainThread(Configuration *config, MultiLogger *multi_logger, ThreadManager *thread_manager, SyncPointManager *syncpoint_manager, PluginManager *plugin_manager, const char *load_plugins, const char *default_plugin=0)
Constructor.
Definition: main_thread.cpp:64
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: main_thread.h:99
virtual void loop()
Code to execute in the thread.
virtual ~FawkesMainThread()
Destructor.
Main loop employer The MainLoopEmployer calls the main loop for execution.
Definition: employer.h:32
Log through multiple loggers.
Definition: multi.h:35
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: multi.cpp:237
Mutex mutual exclusion lock.
Definition: mutex.h:33
Fawkes Plugin Manager.
Definition: manager.h:48
Interface for signal handling.
Definition: signal.h:36
This class gives access to SyncPoints.
Base application thread manager.
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)
Wakeup thread for given hook and wait for completion.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Thread(const char *name)
Constructor.
Definition: thread.cpp:204
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:918
Fawkes library namespace.