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 
40 namespace fawkes {
41 class Configuration;
42 class Configuration;
43 class ConfigNetworkHandler;
44 class NetworkLogger;
45 class Clock;
46 class TimeWait;
47 class AspectManager;
48 class PluginManager;
49 class Time;
50 class PluginNetworkHandler;
51 class InterruptibleBarrier;
52 class Barrier;
53 class Mutex;
54 class ThreadManager;
55 class SyncPointManager;
56 class FawkesNetworkManager;
57 
58 class FawkesMainThread : public Thread, public MainLoopEmployer
59 {
60 public:
61  FawkesMainThread(Configuration * config,
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);
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() */
97 protected:
98  virtual void
99  run()
100  {
101  Thread::run();
102  }
103 
104 private:
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
fawkes::MultiLogger::log_error
virtual void log_error(const char *component, const char *format,...)
Definition: multi.cpp:241
fawkes::PluginManager
Definition: manager.h:51
fawkes::SyncPointManager
Definition: syncpoint_manager.h:41
fawkes::FawkesMainThread::Runner::Runner
Runner(FawkesMainThread *fmt, bool register_signals=true)
Constructor.
Definition: main_thread.cpp:449
fawkes::Mutex
Definition: mutex.h:36
fawkes::FawkesMainThread::Runner
Definition: main_thread.h:83
fawkes::MultiLogger
Definition: multi.h:38
fawkes::SignalHandler
Definition: signal.h:43
fawkes::FawkesMainThread::Runner::handle_signal
void handle_signal(int signum)
Handle signals.
Definition: main_thread.cpp:500
fawkes::FawkesMainThread::Runner::run
void run()
Run main thread.
Definition: main_thread.cpp:480
fawkes::FawkesMainThread::Runner::~Runner
~Runner()
Destructor.
Definition: main_thread.cpp:468
fawkes::FawkesMainThread::logger
MultiLogger * logger() const
Get logger.
Definition: main_thread.cpp:433
fawkes::FawkesMainThread
Definition: main_thread.h:62
fawkes::FawkesMainThread::FawkesMainThread
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:68
fawkes::ThreadManager::wakeup_and_wait
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)
Definition: thread_manager.cpp:545
fawkes::FawkesMainThread::full_start
void full_start()
Start the thread and wait until once() completes.
Definition: main_thread.cpp:198
fawkes::Thread::Thread
Thread(const char *name)
Constructor.
Definition: thread.cpp:208
fawkes
fawkes::FawkesMainThread::loop
virtual void loop()
Code to execute in the thread.
Definition: main_thread.cpp:306
fawkes::FawkesMainThread::once
virtual void once()
Execute an action exactly once.
Definition: main_thread.cpp:210
fawkes::ThreadManager
Definition: thread_manager.h:46
fawkes::FawkesMainThread::~FawkesMainThread
virtual ~FawkesMainThread()
Destructor.
Definition: main_thread.cpp:134
fawkes::BlockedTimingAspect::WakeupHook
WakeupHook
Type to define at which hook the thread is woken up.
Definition: blocked_timing.h:62
fawkes::Thread::run
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:922
fawkes::Thread
Definition: thread.h:44
fawkes::FawkesMainThread::set_mainloop_thread
virtual void set_mainloop_thread(Thread *mainloop_thread)
Definition: main_thread.cpp:295
fawkes::FawkesMainThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: main_thread.h:103
fawkes::Exception
Definition: exception.h:39