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
30namespace 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 */
43: AspectIniFin("MainLoopAspect")
44{
45 employer_ = employer;
46 btexec_ = btexec;
47}
48
49void
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
77void
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
99bool
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
115bool
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
Aspect initializer/finalizer base class.
Definition: inifin.h:34
Blocked timing executor.
Definition: executor.h:37
Thread cannot be finalized.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
virtual bool thread_init_failed(Thread *thread) noexcept
Thread initialization failed.
Definition: mainloop.cpp:116
MainLoopAspectIniFin(MainLoopEmployer *employer, BlockedTimingExecutor *btexec)
Constructor.
Definition: mainloop.cpp:41
virtual bool thread_started(Thread *thread) noexcept
Thread started successfully.
Definition: mainloop.cpp:100
virtual void init(Thread *thread)
Initialize thread.
Definition: mainloop.cpp:50
virtual void finalize(Thread *thread)
Finalize thread.
Definition: mainloop.cpp:78
Thread aspect that allows to replace the main loop of the main application of Fawkes.
Definition: mainloop.h:35
void init_MainLoopAspect(BlockedTimingExecutor *btexec)
Initialize main loop aspect.
Definition: mainloop.cpp:64
Main loop employer The MainLoopEmployer calls the main loop for execution.
Definition: employer.h:32
virtual void set_mainloop_thread(Thread *mainloop_thread)=0
Set a new main loop.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:671
void add_notification_listener(ThreadNotificationListener *notification_listener)
Add notification listener.
Definition: thread.cpp:1162
@ OPMODE_WAITFORWAKEUP
operate in wait-for-wakeup mode
Definition: thread.h:58
Fawkes library namespace.