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 <baseapp/thread_manager.h>
28 #include <core/threading/thread.h>
29 #include <aspect/mainloop/employer.h>
30 #include <aspect/blocked_timing.h>
31 #include <utils/system/signal.h>
32 #include <logging/multi.h>
33 
34 #include <list>
35 #include <string>
36 #include <getopt.h>
37 
38 namespace fawkes {
39 #if 0 /* just to make Emacs auto-indent happy */
40 }
41 #endif
42 class Configuration;
43 class Configuration;
44 class ConfigNetworkHandler;
45 class NetworkLogger;
46 class Clock;
47 class TimeWait;
48 class AspectManager;
49 class PluginManager;
50 class Time;
51 class PluginNetworkHandler;
52 class InterruptibleBarrier;
53 class Barrier;
54 class Mutex;
55 class ThreadManager;
56 class FawkesNetworkManager;
57 
59 : public Thread,
60  public MainLoopEmployer
61 {
62  public:
64  MultiLogger *multi_logger,
65  ThreadManager *thread_manager,
66  PluginManager *plugin_manager,
67  const char *load_plugins,
68  const char *default_plugin = 0);
69  virtual ~FawkesMainThread();
70 
71  virtual void once();
72  virtual void loop();
73 
74  virtual void set_mainloop_thread(Thread *mainloop_thread);
75 
76  void full_start();
77 
78  MultiLogger * logger() const;
79 
80  class Runner : public SignalHandler {
81  public:
82  Runner(FawkesMainThread *fmt, bool register_signals = true);
83  ~Runner();
84  void run();
85  void handle_signal(int signum);
86  private:
87  FawkesMainThread *__fmt;
88  Mutex *__init_mutex;
89  bool __init_running;
90  bool __init_quit;
91  bool __sigint_running;
92  bool __register_signals;
93  };
94 
95  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
96  protected: virtual void run() { Thread::run(); }
97 
98  private:
99  void destruct();
100 
101  inline void safe_wake(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec)
102  {
103  try {
104  __thread_manager->wakeup_and_wait(hook, timeout_usec);
105  } catch (Exception &e) {
106  if (__enable_looptime_warnings) {
107  //__multi_logger->log_error("FawkesMainThread",
108  // "Error while processing hook %s, exception follows",
109  // BlockedTimingAspect::blocked_timing_hook_to_string(hook));
110  __multi_logger->log_error("FawkesMainThread", e);
111  }
112 
113  }
114  }
115 
116 
117  Configuration *__config;
118  MultiLogger *__multi_logger;
119  Clock *__clock;
120  TimeWait *__time_wait;
121 
122  Barrier *__init_barrier;
123  Thread *__mainloop_thread;
124  Mutex *__mainloop_mutex;
125  InterruptibleBarrier *__mainloop_barrier;
126 
127  char *__default_plugin;
128  char *__load_plugins;
129 
130  ThreadManager *__thread_manager;
131  PluginManager *__plugin_manager;
132 
133  std::list<std::string> __recovered_threads;
134  unsigned int __desired_loop_time_usec;
135  float __desired_loop_time_sec;
136  unsigned int __max_thread_time_usec;
137  unsigned int __max_thread_time_nanosec;
138  Time *__loop_start;
139  Time *__loop_end;
140  bool __enable_looptime_warnings;
141 
142 };
143 
144 } // end namespace fawkes
145 
146 #endif
void full_start()
Start the thread and wait until once() completes.
Fawkes library namespace.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
Interface for signal handling.
Definition: signal.h:35
A class for handling time.
Definition: time.h:91
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: main_thread.h:96
virtual void loop()
Code to execute in the thread.
Fawkes Plugin Manager.
Definition: manager.h:51
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Log through multiple loggers.
Definition: multi.h:35
virtual ~FawkesMainThread()
Destructor.
Base application thread manager.
WakeupHook
Type to define at which hook the thread is woken up.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Runner(FawkesMainThread *fmt, bool register_signals=true)
Constructor.
Main loop employer The MainLoopEmployer calls the main loop for execution.
Definition: employer.h:31
void run()
Run main thread.
virtual void set_mainloop_thread(Thread *mainloop_thread)
Set a new main loop.
void handle_signal(int signum)
Handle signals.
MultiLogger * logger() const
Get logger.
Mutex mutual exclusion lock.
Definition: mutex.h:32
Fawkes default main thread.
Definition: main_thread.h:58
Interface for configuration handling.
Definition: config.h:67
FawkesMainThread(Configuration *config, MultiLogger *multi_logger, ThreadManager *thread_manager, PluginManager *plugin_manager, const char *load_plugins, const char *default_plugin=0)
Constructor.
Definition: main_thread.cpp:73
Time wait utility.
Definition: wait.h:32
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
Utility class to run the main thread.
Definition: main_thread.h:80
virtual void once()
Execute an action exactly once.