Fawkes API  Fawkes Development Version
mainloop.cpp
1 
2 /***************************************************************************
3  * mainloop.cpp - Fawkes MainLoopAspect initializer/finalizer
4  *
5  * Created: Wed Nov 24 00:44:55 2010
6  * Copyright 2006-2010 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 #include <aspect/blocked_timing/executor.h>
25 #include <aspect/inifins/mainloop.h>
26 #include <aspect/mainloop.h>
27 #include <aspect/mainloop/employer.h>
28 #include <core/threading/thread_finalizer.h>
29 
30 namespace fawkes {
31 
32 /** @class MainLoopAspectIniFin <aspect/inifins/mainloop.h>
33  * Initializer/finalizer for the MainLoopAspect.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param employer main loop employer to register main loop to
39  * @param btexec blocked timing executor to pass to thread
40  */
41 MainLoopAspectIniFin::MainLoopAspectIniFin(MainLoopEmployer * employer,
42  BlockedTimingExecutor *btexec)
43 : AspectIniFin("MainLoopAspect")
44 {
45  employer_ = employer;
46  btexec_ = btexec;
47 }
48 
49 void
51 {
52  MainLoopAspect *mainloop_thread;
53  mainloop_thread = dynamic_cast<MainLoopAspect *>(thread);
54  if (mainloop_thread == NULL) {
55  throw CannotInitializeThreadException("Thread '%s' claims to have the "
56  "MainLoopAspect, but RTTI says it "
57  "has not. ",
58  thread->name());
59  }
60 
61  if (thread->opmode() != Thread::OPMODE_WAITFORWAKEUP) {
62  throw CannotInitializeThreadException("MainLoopAspect thread must operate "
63  "in wait-for-wakeup mode.");
64  }
65 
66  try {
67  mainloop_uc_.add(mainloop_thread);
68  mainloop_thread->init_MainLoopAspect(btexec_);
69  thread->add_notification_listener(this);
70  } catch (Exception &e) {
71  CannotInitializeThreadException ce("Main loop thread failed to initialize");
72  ce.append(e);
73  throw ce;
74  }
75 }
76 
77 void
78 MainLoopAspectIniFin::finalize(Thread *thread)
79 {
80  MainLoopAspect *mainloop_thread;
81  mainloop_thread = dynamic_cast<MainLoopAspect *>(thread);
82  if (mainloop_thread == NULL) {
83  throw CannotInitializeThreadException("Thread '%s' claims to have the "
84  "MainLoopAspect, but RTTI says it "
85  "has not. ",
86  thread->name());
87  }
88 
89  try {
90  employer_->set_mainloop_thread(NULL);
91  mainloop_uc_.remove(mainloop_thread);
92  } catch (Exception &e) {
93  CannotFinalizeThreadException ce("Failed to remove time source");
94  ce.append(e);
95  throw;
96  }
97 }
98 
99 bool
100 MainLoopAspectIniFin::thread_started(Thread *thread) throw()
101 {
102  MainLoopAspect *mainloop_thread;
103  if ((mainloop_thread = dynamic_cast<MainLoopAspect *>(thread)) != NULL) {
104  try {
105  employer_->set_mainloop_thread(thread);
106  } catch (Exception &e) {
107  //logger_->log_error("AspectIniFin", "Main loop thread started successfully "
108  // "but could not add main loop thread's main loop");
109  }
110  }
111 
112  return false;
113 }
114 
115 bool
116 MainLoopAspectIniFin::thread_init_failed(Thread *thread) throw()
117 {
118  MainLoopAspect *mainloop_thread;
119  if ((mainloop_thread = dynamic_cast<MainLoopAspect *>(thread)) != NULL) {
120  try {
121  mainloop_uc_.remove(mainloop_thread);
122  } catch (Exception &e) {
123  //logger_->log_error("AspectIniFin", "Failed to remove main loop from "
124  // "uniqueness constraint on thread init fail of %s",
125  // thread->name());
126  }
127  }
128 
129  try {
130  finalize(thread);
131  } catch (Exception &e) {
132  /*
133  logger_->log_error("AspectIniFin", "Initialization of thread '%s' failed, but "
134  "the thread thread could not be internally finalized",
135  thread->name());
136  logger_->log_error("AspectIniFin", e);
137  */
138  }
139 
140  return false;
141 }
142 
143 } // end namespace fawkes
fawkes::MainLoopAspect
Definition: mainloop.h:38
fawkes::MainLoopAspectIniFin::finalize
virtual void finalize(Thread *thread)
Definition: mainloop.cpp:82
fawkes::CannotInitializeThreadException
Definition: thread_initializer.h:37
fawkes::MainLoopEmployer::set_mainloop_thread
virtual void set_mainloop_thread(Thread *mainloop_thread)=0
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::MainLoopAspectIniFin::thread_started
virtual bool thread_started(Thread *thread)
Definition: mainloop.cpp:104
fawkes
fawkes::Thread::OPMODE_WAITFORWAKEUP
operate in wait-for-wakeup mode
Definition: thread.h:57
fawkes::Thread::add_notification_listener
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Definition: thread.cpp:1166
fawkes::MainLoopAspect::init_MainLoopAspect
void init_MainLoopAspect(BlockedTimingExecutor *btexec)
Initialize main loop aspect.
Definition: mainloop.cpp:68
fawkes::MainLoopAspectIniFin::MainLoopAspectIniFin
MainLoopAspectIniFin(MainLoopEmployer *employer, BlockedTimingExecutor *btexec)
Constructor.
Definition: mainloop.cpp:45
fawkes::MainLoopAspectIniFin::thread_init_failed
virtual bool thread_init_failed(Thread *thread)
Definition: mainloop.cpp:120
fawkes::Thread
Definition: thread.h:44
fawkes::Thread::opmode
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:675
fawkes::MainLoopAspectIniFin::init
virtual void init(Thread *thread)
Definition: mainloop.cpp:54
fawkes::Exception
Definition: exception.h:39